Built-in Natural literals in Core
[ghc.git] / libraries / base / GHC / Int.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash, UnboxedTuples,
3 StandaloneDeriving, NegativeLiterals #-}
4 {-# OPTIONS_HADDOCK hide #-}
5
6 -----------------------------------------------------------------------------
7 -- |
8 -- Module : GHC.Int
9 -- Copyright : (c) The University of Glasgow 1997-2002
10 -- License : see libraries/base/LICENSE
11 --
12 -- Maintainer : cvs-ghc@haskell.org
13 -- Stability : internal
14 -- Portability : non-portable (GHC Extensions)
15 --
16 -- The sized integral datatypes, 'Int8', 'Int16', 'Int32', and 'Int64'.
17 --
18 -----------------------------------------------------------------------------
19
20 #include "MachDeps.h"
21
22 module GHC.Int (
23 Int(..), Int8(..), Int16(..), Int32(..), Int64(..),
24 uncheckedIShiftL64#, uncheckedIShiftRA64#,
25 -- * Equality operators
26 -- | See GHC.Classes#matching_overloaded_methods_in_rules
27 eqInt, neInt, gtInt, geInt, ltInt, leInt,
28 eqInt8, neInt8, gtInt8, geInt8, ltInt8, leInt8,
29 eqInt16, neInt16, gtInt16, geInt16, ltInt16, leInt16,
30 eqInt32, neInt32, gtInt32, geInt32, ltInt32, leInt32,
31 eqInt64, neInt64, gtInt64, geInt64, ltInt64, leInt64
32 ) where
33
34 import Data.Bits
35 import Data.Maybe
36
37 #if WORD_SIZE_IN_BITS < 64
38 import GHC.IntWord64
39 #endif
40
41 import GHC.Base
42 import GHC.Enum
43 import GHC.Num
44 import GHC.Real
45 import GHC.Read
46 import GHC.Arr
47 import GHC.Word hiding (uncheckedShiftL64#, uncheckedShiftRL64#)
48 import GHC.Show
49
50 ------------------------------------------------------------------------
51 -- type Int8
52 ------------------------------------------------------------------------
53
54 -- Int8 is represented in the same way as Int. Operations may assume
55 -- and must ensure that it holds only values from its logical range.
56
57 data {-# CTYPE "HsInt8" #-} Int8 = I8# Int#
58 -- ^ 8-bit signed integer type
59
60 -- See GHC.Classes#matching_overloaded_methods_in_rules
61 -- | @since 2.01
62 instance Eq Int8 where
63 (==) = eqInt8
64 (/=) = neInt8
65
66 eqInt8, neInt8 :: Int8 -> Int8 -> Bool
67 eqInt8 (I8# x) (I8# y) = isTrue# (x ==# y)
68 neInt8 (I8# x) (I8# y) = isTrue# (x /=# y)
69 {-# INLINE [1] eqInt8 #-}
70 {-# INLINE [1] neInt8 #-}
71
72 -- | @since 2.01
73 instance Ord Int8 where
74 (<) = ltInt8
75 (<=) = leInt8
76 (>=) = geInt8
77 (>) = gtInt8
78
79 {-# INLINE [1] gtInt8 #-}
80 {-# INLINE [1] geInt8 #-}
81 {-# INLINE [1] ltInt8 #-}
82 {-# INLINE [1] leInt8 #-}
83 gtInt8, geInt8, ltInt8, leInt8 :: Int8 -> Int8 -> Bool
84 (I8# x) `gtInt8` (I8# y) = isTrue# (x ># y)
85 (I8# x) `geInt8` (I8# y) = isTrue# (x >=# y)
86 (I8# x) `ltInt8` (I8# y) = isTrue# (x <# y)
87 (I8# x) `leInt8` (I8# y) = isTrue# (x <=# y)
88
89 -- | @since 2.01
90 instance Show Int8 where
91 showsPrec p x = showsPrec p (fromIntegral x :: Int)
92
93 -- | @since 2.01
94 instance Num Int8 where
95 (I8# x#) + (I8# y#) = I8# (narrow8Int# (x# +# y#))
96 (I8# x#) - (I8# y#) = I8# (narrow8Int# (x# -# y#))
97 (I8# x#) * (I8# y#) = I8# (narrow8Int# (x# *# y#))
98 negate (I8# x#) = I8# (narrow8Int# (negateInt# x#))
99 abs x | x >= 0 = x
100 | otherwise = negate x
101 signum x | x > 0 = 1
102 signum 0 = 0
103 signum _ = -1
104 fromInteger i = I8# (narrow8Int# (integerToInt i))
105
106 -- | @since 2.01
107 instance Real Int8 where
108 toRational x = toInteger x % 1
109
110 -- | @since 2.01
111 instance Enum Int8 where
112 succ x
113 | x /= maxBound = x + 1
114 | otherwise = succError "Int8"
115 pred x
116 | x /= minBound = x - 1
117 | otherwise = predError "Int8"
118 toEnum i@(I# i#)
119 | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
120 = I8# i#
121 | otherwise = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
122 fromEnum (I8# x#) = I# x#
123 enumFrom = boundedEnumFrom
124 enumFromThen = boundedEnumFromThen
125
126 -- | @since 2.01
127 instance Integral Int8 where
128 quot x@(I8# x#) y@(I8# y#)
129 | y == 0 = divZeroError
130 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
131 | otherwise = I8# (narrow8Int# (x# `quotInt#` y#))
132 rem (I8# x#) y@(I8# y#)
133 | y == 0 = divZeroError
134 | otherwise = I8# (narrow8Int# (x# `remInt#` y#))
135 div x@(I8# x#) y@(I8# y#)
136 | y == 0 = divZeroError
137 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
138 | otherwise = I8# (narrow8Int# (x# `divInt#` y#))
139 mod (I8# x#) y@(I8# y#)
140 | y == 0 = divZeroError
141 | otherwise = I8# (narrow8Int# (x# `modInt#` y#))
142 quotRem x@(I8# x#) y@(I8# y#)
143 | y == 0 = divZeroError
144 -- Note [Order of tests]
145 | y == (-1) && x == minBound = (overflowError, 0)
146 | otherwise = case x# `quotRemInt#` y# of
147 (# q, r #) ->
148 (I8# (narrow8Int# q),
149 I8# (narrow8Int# r))
150 divMod x@(I8# x#) y@(I8# y#)
151 | y == 0 = divZeroError
152 -- Note [Order of tests]
153 | y == (-1) && x == minBound = (overflowError, 0)
154 | otherwise = case x# `divModInt#` y# of
155 (# d, m #) ->
156 (I8# (narrow8Int# d),
157 I8# (narrow8Int# m))
158 toInteger (I8# x#) = smallInteger x#
159
160 -- | @since 2.01
161 instance Bounded Int8 where
162 minBound = -0x80
163 maxBound = 0x7F
164
165 -- | @since 2.01
166 instance Ix Int8 where
167 range (m,n) = [m..n]
168 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
169 inRange (m,n) i = m <= i && i <= n
170
171 -- | @since 2.01
172 instance Read Int8 where
173 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
174
175 -- | @since 2.01
176 instance Bits Int8 where
177 {-# INLINE shift #-}
178 {-# INLINE bit #-}
179 {-# INLINE testBit #-}
180
181 (I8# x#) .&. (I8# y#) = I8# (word2Int# (int2Word# x# `and#` int2Word# y#))
182 (I8# x#) .|. (I8# y#) = I8# (word2Int# (int2Word# x# `or#` int2Word# y#))
183 (I8# x#) `xor` (I8# y#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#))
184 complement (I8# x#) = I8# (word2Int# (not# (int2Word# x#)))
185 (I8# x#) `shift` (I# i#)
186 | isTrue# (i# >=# 0#) = I8# (narrow8Int# (x# `iShiftL#` i#))
187 | otherwise = I8# (x# `iShiftRA#` negateInt# i#)
188 (I8# x#) `shiftL` (I# i#) = I8# (narrow8Int# (x# `iShiftL#` i#))
189 (I8# x#) `unsafeShiftL` (I# i#) = I8# (narrow8Int# (x# `uncheckedIShiftL#` i#))
190 (I8# x#) `shiftR` (I# i#) = I8# (x# `iShiftRA#` i#)
191 (I8# x#) `unsafeShiftR` (I# i#) = I8# (x# `uncheckedIShiftRA#` i#)
192 (I8# x#) `rotate` (I# i#)
193 | isTrue# (i'# ==# 0#)
194 = I8# x#
195 | otherwise
196 = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
197 (x'# `uncheckedShiftRL#` (8# -# i'#)))))
198 where
199 !x'# = narrow8Word# (int2Word# x#)
200 !i'# = word2Int# (int2Word# i# `and#` 7##)
201 bitSizeMaybe i = Just (finiteBitSize i)
202 bitSize i = finiteBitSize i
203 isSigned _ = True
204 popCount (I8# x#) = I# (word2Int# (popCnt8# (int2Word# x#)))
205 bit = bitDefault
206 testBit = testBitDefault
207
208 -- | @since 4.6.0.0
209 instance FiniteBits Int8 where
210 finiteBitSize _ = 8
211 countLeadingZeros (I8# x#) = I# (word2Int# (clz8# (int2Word# x#)))
212 countTrailingZeros (I8# x#) = I# (word2Int# (ctz8# (int2Word# x#)))
213
214 {-# RULES
215 "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
216 "fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#)
217 "fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#)
218 #-}
219
220 {-# RULES
221 "properFraction/Float->(Int8,Float)"
222 properFraction = \x ->
223 case properFraction x of {
224 (n, y) -> ((fromIntegral :: Int -> Int8) n, y :: Float) }
225 "truncate/Float->Int8"
226 truncate = (fromIntegral :: Int -> Int8) . (truncate :: Float -> Int)
227 "floor/Float->Int8"
228 floor = (fromIntegral :: Int -> Int8) . (floor :: Float -> Int)
229 "ceiling/Float->Int8"
230 ceiling = (fromIntegral :: Int -> Int8) . (ceiling :: Float -> Int)
231 "round/Float->Int8"
232 round = (fromIntegral :: Int -> Int8) . (round :: Float -> Int)
233 #-}
234
235 {-# RULES
236 "properFraction/Double->(Int8,Double)"
237 properFraction = \x ->
238 case properFraction x of {
239 (n, y) -> ((fromIntegral :: Int -> Int8) n, y :: Double) }
240 "truncate/Double->Int8"
241 truncate = (fromIntegral :: Int -> Int8) . (truncate :: Double -> Int)
242 "floor/Double->Int8"
243 floor = (fromIntegral :: Int -> Int8) . (floor :: Double -> Int)
244 "ceiling/Double->Int8"
245 ceiling = (fromIntegral :: Int -> Int8) . (ceiling :: Double -> Int)
246 "round/Double->Int8"
247 round = (fromIntegral :: Int -> Int8) . (round :: Double -> Int)
248 #-}
249
250 ------------------------------------------------------------------------
251 -- type Int16
252 ------------------------------------------------------------------------
253
254 -- Int16 is represented in the same way as Int. Operations may assume
255 -- and must ensure that it holds only values from its logical range.
256
257 data {-# CTYPE "HsInt16" #-} Int16 = I16# Int#
258 -- ^ 16-bit signed integer type
259
260 -- See GHC.Classes#matching_overloaded_methods_in_rules
261 -- | @since 2.01
262 instance Eq Int16 where
263 (==) = eqInt16
264 (/=) = neInt16
265
266 eqInt16, neInt16 :: Int16 -> Int16 -> Bool
267 eqInt16 (I16# x) (I16# y) = isTrue# (x ==# y)
268 neInt16 (I16# x) (I16# y) = isTrue# (x /=# y)
269 {-# INLINE [1] eqInt16 #-}
270 {-# INLINE [1] neInt16 #-}
271
272 -- | @since 2.01
273 instance Ord Int16 where
274 (<) = ltInt16
275 (<=) = leInt16
276 (>=) = geInt16
277 (>) = gtInt16
278
279 {-# INLINE [1] gtInt16 #-}
280 {-# INLINE [1] geInt16 #-}
281 {-# INLINE [1] ltInt16 #-}
282 {-# INLINE [1] leInt16 #-}
283 gtInt16, geInt16, ltInt16, leInt16 :: Int16 -> Int16 -> Bool
284 (I16# x) `gtInt16` (I16# y) = isTrue# (x ># y)
285 (I16# x) `geInt16` (I16# y) = isTrue# (x >=# y)
286 (I16# x) `ltInt16` (I16# y) = isTrue# (x <# y)
287 (I16# x) `leInt16` (I16# y) = isTrue# (x <=# y)
288
289 -- | @since 2.01
290 instance Show Int16 where
291 showsPrec p x = showsPrec p (fromIntegral x :: Int)
292
293 -- | @since 2.01
294 instance Num Int16 where
295 (I16# x#) + (I16# y#) = I16# (narrow16Int# (x# +# y#))
296 (I16# x#) - (I16# y#) = I16# (narrow16Int# (x# -# y#))
297 (I16# x#) * (I16# y#) = I16# (narrow16Int# (x# *# y#))
298 negate (I16# x#) = I16# (narrow16Int# (negateInt# x#))
299 abs x | x >= 0 = x
300 | otherwise = negate x
301 signum x | x > 0 = 1
302 signum 0 = 0
303 signum _ = -1
304 fromInteger i = I16# (narrow16Int# (integerToInt i))
305
306 -- | @since 2.01
307 instance Real Int16 where
308 toRational x = toInteger x % 1
309
310 -- | @since 2.01
311 instance Enum Int16 where
312 succ x
313 | x /= maxBound = x + 1
314 | otherwise = succError "Int16"
315 pred x
316 | x /= minBound = x - 1
317 | otherwise = predError "Int16"
318 toEnum i@(I# i#)
319 | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
320 = I16# i#
321 | otherwise = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
322 fromEnum (I16# x#) = I# x#
323 enumFrom = boundedEnumFrom
324 enumFromThen = boundedEnumFromThen
325
326 -- | @since 2.01
327 instance Integral Int16 where
328 quot x@(I16# x#) y@(I16# y#)
329 | y == 0 = divZeroError
330 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
331 | otherwise = I16# (narrow16Int# (x# `quotInt#` y#))
332 rem (I16# x#) y@(I16# y#)
333 | y == 0 = divZeroError
334 | otherwise = I16# (narrow16Int# (x# `remInt#` y#))
335 div x@(I16# x#) y@(I16# y#)
336 | y == 0 = divZeroError
337 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
338 | otherwise = I16# (narrow16Int# (x# `divInt#` y#))
339 mod (I16# x#) y@(I16# y#)
340 | y == 0 = divZeroError
341 | otherwise = I16# (narrow16Int# (x# `modInt#` y#))
342 quotRem x@(I16# x#) y@(I16# y#)
343 | y == 0 = divZeroError
344 -- Note [Order of tests]
345 | y == (-1) && x == minBound = (overflowError, 0)
346 | otherwise = case x# `quotRemInt#` y# of
347 (# q, r #) ->
348 (I16# (narrow16Int# q),
349 I16# (narrow16Int# r))
350 divMod x@(I16# x#) y@(I16# y#)
351 | y == 0 = divZeroError
352 -- Note [Order of tests]
353 | y == (-1) && x == minBound = (overflowError, 0)
354 | otherwise = case x# `divModInt#` y# of
355 (# d, m #) ->
356 (I16# (narrow16Int# d),
357 I16# (narrow16Int# m))
358 toInteger (I16# x#) = smallInteger x#
359
360 -- | @since 2.01
361 instance Bounded Int16 where
362 minBound = -0x8000
363 maxBound = 0x7FFF
364
365 -- | @since 2.01
366 instance Ix Int16 where
367 range (m,n) = [m..n]
368 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
369 inRange (m,n) i = m <= i && i <= n
370
371 -- | @since 2.01
372 instance Read Int16 where
373 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
374
375 -- | @since 2.01
376 instance Bits Int16 where
377 {-# INLINE shift #-}
378 {-# INLINE bit #-}
379 {-# INLINE testBit #-}
380
381 (I16# x#) .&. (I16# y#) = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
382 (I16# x#) .|. (I16# y#) = I16# (word2Int# (int2Word# x# `or#` int2Word# y#))
383 (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
384 complement (I16# x#) = I16# (word2Int# (not# (int2Word# x#)))
385 (I16# x#) `shift` (I# i#)
386 | isTrue# (i# >=# 0#) = I16# (narrow16Int# (x# `iShiftL#` i#))
387 | otherwise = I16# (x# `iShiftRA#` negateInt# i#)
388 (I16# x#) `shiftL` (I# i#) = I16# (narrow16Int# (x# `iShiftL#` i#))
389 (I16# x#) `unsafeShiftL` (I# i#) = I16# (narrow16Int# (x# `uncheckedIShiftL#` i#))
390 (I16# x#) `shiftR` (I# i#) = I16# (x# `iShiftRA#` i#)
391 (I16# x#) `unsafeShiftR` (I# i#) = I16# (x# `uncheckedIShiftRA#` i#)
392 (I16# x#) `rotate` (I# i#)
393 | isTrue# (i'# ==# 0#)
394 = I16# x#
395 | otherwise
396 = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
397 (x'# `uncheckedShiftRL#` (16# -# i'#)))))
398 where
399 !x'# = narrow16Word# (int2Word# x#)
400 !i'# = word2Int# (int2Word# i# `and#` 15##)
401 bitSizeMaybe i = Just (finiteBitSize i)
402 bitSize i = finiteBitSize i
403 isSigned _ = True
404 popCount (I16# x#) = I# (word2Int# (popCnt16# (int2Word# x#)))
405 bit = bitDefault
406 testBit = testBitDefault
407
408 -- | @since 4.6.0.0
409 instance FiniteBits Int16 where
410 finiteBitSize _ = 16
411 countLeadingZeros (I16# x#) = I# (word2Int# (clz16# (int2Word# x#)))
412 countTrailingZeros (I16# x#) = I# (word2Int# (ctz16# (int2Word# x#)))
413
414 {-# RULES
415 "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
416 "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x#
417 "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16
418 "fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
419 "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
420 #-}
421
422 {-# RULES
423 "properFraction/Float->(Int16,Float)"
424 properFraction = \x ->
425 case properFraction x of {
426 (n, y) -> ((fromIntegral :: Int -> Int16) n, y :: Float) }
427 "truncate/Float->Int16"
428 truncate = (fromIntegral :: Int -> Int16) . (truncate :: Float -> Int)
429 "floor/Float->Int16"
430 floor = (fromIntegral :: Int -> Int16) . (floor :: Float -> Int)
431 "ceiling/Float->Int16"
432 ceiling = (fromIntegral :: Int -> Int16) . (ceiling :: Float -> Int)
433 "round/Float->Int16"
434 round = (fromIntegral :: Int -> Int16) . (round :: Float -> Int)
435 #-}
436
437 {-# RULES
438 "properFraction/Double->(Int16,Double)"
439 properFraction = \x ->
440 case properFraction x of {
441 (n, y) -> ((fromIntegral :: Int -> Int16) n, y :: Double) }
442 "truncate/Double->Int16"
443 truncate = (fromIntegral :: Int -> Int16) . (truncate :: Double -> Int)
444 "floor/Double->Int16"
445 floor = (fromIntegral :: Int -> Int16) . (floor :: Double -> Int)
446 "ceiling/Double->Int16"
447 ceiling = (fromIntegral :: Int -> Int16) . (ceiling :: Double -> Int)
448 "round/Double->Int16"
449 round = (fromIntegral :: Int -> Int16) . (round :: Double -> Int)
450 #-}
451
452 ------------------------------------------------------------------------
453 -- type Int32
454 ------------------------------------------------------------------------
455
456 -- Int32 is represented in the same way as Int.
457 #if WORD_SIZE_IN_BITS > 32
458 -- Operations may assume and must ensure that it holds only values
459 -- from its logical range.
460 #endif
461
462 data {-# CTYPE "HsInt32" #-} Int32 = I32# Int#
463 -- ^ 32-bit signed integer type
464
465 -- See GHC.Classes#matching_overloaded_methods_in_rules
466 -- | @since 2.01
467 instance Eq Int32 where
468 (==) = eqInt32
469 (/=) = neInt32
470
471 eqInt32, neInt32 :: Int32 -> Int32 -> Bool
472 eqInt32 (I32# x) (I32# y) = isTrue# (x ==# y)
473 neInt32 (I32# x) (I32# y) = isTrue# (x /=# y)
474 {-# INLINE [1] eqInt32 #-}
475 {-# INLINE [1] neInt32 #-}
476
477 -- | @since 2.01
478 instance Ord Int32 where
479 (<) = ltInt32
480 (<=) = leInt32
481 (>=) = geInt32
482 (>) = gtInt32
483
484 {-# INLINE [1] gtInt32 #-}
485 {-# INLINE [1] geInt32 #-}
486 {-# INLINE [1] ltInt32 #-}
487 {-# INLINE [1] leInt32 #-}
488 gtInt32, geInt32, ltInt32, leInt32 :: Int32 -> Int32 -> Bool
489 (I32# x) `gtInt32` (I32# y) = isTrue# (x ># y)
490 (I32# x) `geInt32` (I32# y) = isTrue# (x >=# y)
491 (I32# x) `ltInt32` (I32# y) = isTrue# (x <# y)
492 (I32# x) `leInt32` (I32# y) = isTrue# (x <=# y)
493
494 -- | @since 2.01
495 instance Show Int32 where
496 showsPrec p x = showsPrec p (fromIntegral x :: Int)
497
498 -- | @since 2.01
499 instance Num Int32 where
500 (I32# x#) + (I32# y#) = I32# (narrow32Int# (x# +# y#))
501 (I32# x#) - (I32# y#) = I32# (narrow32Int# (x# -# y#))
502 (I32# x#) * (I32# y#) = I32# (narrow32Int# (x# *# y#))
503 negate (I32# x#) = I32# (narrow32Int# (negateInt# x#))
504 abs x | x >= 0 = x
505 | otherwise = negate x
506 signum x | x > 0 = 1
507 signum 0 = 0
508 signum _ = -1
509 fromInteger i = I32# (narrow32Int# (integerToInt i))
510
511 -- | @since 2.01
512 instance Enum Int32 where
513 succ x
514 | x /= maxBound = x + 1
515 | otherwise = succError "Int32"
516 pred x
517 | x /= minBound = x - 1
518 | otherwise = predError "Int32"
519 #if WORD_SIZE_IN_BITS == 32
520 toEnum (I# i#) = I32# i#
521 #else
522 toEnum i@(I# i#)
523 | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
524 = I32# i#
525 | otherwise = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
526 #endif
527 fromEnum (I32# x#) = I# x#
528 enumFrom = boundedEnumFrom
529 enumFromThen = boundedEnumFromThen
530
531 -- | @since 2.01
532 instance Integral Int32 where
533 quot x@(I32# x#) y@(I32# y#)
534 | y == 0 = divZeroError
535 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
536 | otherwise = I32# (narrow32Int# (x# `quotInt#` y#))
537 rem (I32# x#) y@(I32# y#)
538 | y == 0 = divZeroError
539 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
540 -- but minBound `rem` -1 is well-defined (0). We therefore
541 -- special-case it.
542 | y == (-1) = 0
543 | otherwise = I32# (narrow32Int# (x# `remInt#` y#))
544 div x@(I32# x#) y@(I32# y#)
545 | y == 0 = divZeroError
546 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
547 | otherwise = I32# (narrow32Int# (x# `divInt#` y#))
548 mod (I32# x#) y@(I32# y#)
549 | y == 0 = divZeroError
550 -- The divMod CPU instruction fails for minBound `divMod` -1,
551 -- but minBound `mod` -1 is well-defined (0). We therefore
552 -- special-case it.
553 | y == (-1) = 0
554 | otherwise = I32# (narrow32Int# (x# `modInt#` y#))
555 quotRem x@(I32# x#) y@(I32# y#)
556 | y == 0 = divZeroError
557 -- Note [Order of tests]
558 | y == (-1) && x == minBound = (overflowError, 0)
559 | otherwise = case x# `quotRemInt#` y# of
560 (# q, r #) ->
561 (I32# (narrow32Int# q),
562 I32# (narrow32Int# r))
563 divMod x@(I32# x#) y@(I32# y#)
564 | y == 0 = divZeroError
565 -- Note [Order of tests]
566 | y == (-1) && x == minBound = (overflowError, 0)
567 | otherwise = case x# `divModInt#` y# of
568 (# d, m #) ->
569 (I32# (narrow32Int# d),
570 I32# (narrow32Int# m))
571 toInteger (I32# x#) = smallInteger x#
572
573 -- | @since 2.01
574 instance Read Int32 where
575 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
576
577 -- | @since 2.01
578 instance Bits Int32 where
579 {-# INLINE shift #-}
580 {-# INLINE bit #-}
581 {-# INLINE testBit #-}
582
583 (I32# x#) .&. (I32# y#) = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
584 (I32# x#) .|. (I32# y#) = I32# (word2Int# (int2Word# x# `or#` int2Word# y#))
585 (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
586 complement (I32# x#) = I32# (word2Int# (not# (int2Word# x#)))
587 (I32# x#) `shift` (I# i#)
588 | isTrue# (i# >=# 0#) = I32# (narrow32Int# (x# `iShiftL#` i#))
589 | otherwise = I32# (x# `iShiftRA#` negateInt# i#)
590 (I32# x#) `shiftL` (I# i#) = I32# (narrow32Int# (x# `iShiftL#` i#))
591 (I32# x#) `unsafeShiftL` (I# i#) =
592 I32# (narrow32Int# (x# `uncheckedIShiftL#` i#))
593 (I32# x#) `shiftR` (I# i#) = I32# (x# `iShiftRA#` i#)
594 (I32# x#) `unsafeShiftR` (I# i#) = I32# (x# `uncheckedIShiftRA#` i#)
595 (I32# x#) `rotate` (I# i#)
596 | isTrue# (i'# ==# 0#)
597 = I32# x#
598 | otherwise
599 = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
600 (x'# `uncheckedShiftRL#` (32# -# i'#)))))
601 where
602 !x'# = narrow32Word# (int2Word# x#)
603 !i'# = word2Int# (int2Word# i# `and#` 31##)
604 bitSizeMaybe i = Just (finiteBitSize i)
605 bitSize i = finiteBitSize i
606 isSigned _ = True
607 popCount (I32# x#) = I# (word2Int# (popCnt32# (int2Word# x#)))
608 bit = bitDefault
609 testBit = testBitDefault
610
611 -- | @since 4.6.0.0
612 instance FiniteBits Int32 where
613 finiteBitSize _ = 32
614 countLeadingZeros (I32# x#) = I# (word2Int# (clz32# (int2Word# x#)))
615 countTrailingZeros (I32# x#) = I# (word2Int# (ctz32# (int2Word# x#)))
616
617 {-# RULES
618 "fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
619 "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
620 "fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x#
621 "fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x#
622 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
623 "fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
624 "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
625 #-}
626
627 {-# RULES
628 "properFraction/Float->(Int32,Float)"
629 properFraction = \x ->
630 case properFraction x of {
631 (n, y) -> ((fromIntegral :: Int -> Int32) n, y :: Float) }
632 "truncate/Float->Int32"
633 truncate = (fromIntegral :: Int -> Int32) . (truncate :: Float -> Int)
634 "floor/Float->Int32"
635 floor = (fromIntegral :: Int -> Int32) . (floor :: Float -> Int)
636 "ceiling/Float->Int32"
637 ceiling = (fromIntegral :: Int -> Int32) . (ceiling :: Float -> Int)
638 "round/Float->Int32"
639 round = (fromIntegral :: Int -> Int32) . (round :: Float -> Int)
640 #-}
641
642 {-# RULES
643 "properFraction/Double->(Int32,Double)"
644 properFraction = \x ->
645 case properFraction x of {
646 (n, y) -> ((fromIntegral :: Int -> Int32) n, y :: Double) }
647 "truncate/Double->Int32"
648 truncate = (fromIntegral :: Int -> Int32) . (truncate :: Double -> Int)
649 "floor/Double->Int32"
650 floor = (fromIntegral :: Int -> Int32) . (floor :: Double -> Int)
651 "ceiling/Double->Int32"
652 ceiling = (fromIntegral :: Int -> Int32) . (ceiling :: Double -> Int)
653 "round/Double->Int32"
654 round = (fromIntegral :: Int -> Int32) . (round :: Double -> Int)
655 #-}
656
657 -- | @since 2.01
658 instance Real Int32 where
659 toRational x = toInteger x % 1
660
661 -- | @since 2.01
662 instance Bounded Int32 where
663 minBound = -0x80000000
664 maxBound = 0x7FFFFFFF
665
666 -- | @since 2.01
667 instance Ix Int32 where
668 range (m,n) = [m..n]
669 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
670 inRange (m,n) i = m <= i && i <= n
671
672 ------------------------------------------------------------------------
673 -- type Int64
674 ------------------------------------------------------------------------
675
676 #if WORD_SIZE_IN_BITS < 64
677
678 data {-# CTYPE "HsInt64" #-} Int64 = I64# Int64#
679 -- ^ 64-bit signed integer type
680
681 -- See GHC.Classes#matching_overloaded_methods_in_rules
682 -- | @since 2.01
683 instance Eq Int64 where
684 (==) = eqInt64
685 (/=) = neInt64
686
687 eqInt64, neInt64 :: Int64 -> Int64 -> Bool
688 eqInt64 (I64# x) (I64# y) = isTrue# (x `eqInt64#` y)
689 neInt64 (I64# x) (I64# y) = isTrue# (x `neInt64#` y)
690 {-# INLINE [1] eqInt64 #-}
691 {-# INLINE [1] neInt64 #-}
692
693 -- | @since 2.01
694 instance Ord Int64 where
695 (<) = ltInt64
696 (<=) = leInt64
697 (>=) = geInt64
698 (>) = gtInt64
699
700 {-# INLINE [1] gtInt64 #-}
701 {-# INLINE [1] geInt64 #-}
702 {-# INLINE [1] ltInt64 #-}
703 {-# INLINE [1] leInt64 #-}
704 gtInt64, geInt64, ltInt64, leInt64 :: Int64 -> Int64 -> Bool
705 (I64# x) `gtInt64` (I64# y) = isTrue# (x `gtInt64#` y)
706 (I64# x) `geInt64` (I64# y) = isTrue# (x `geInt64#` y)
707 (I64# x) `ltInt64` (I64# y) = isTrue# (x `ltInt64#` y)
708 (I64# x) `leInt64` (I64# y) = isTrue# (x `leInt64#` y)
709
710 -- | @since 2.01
711 instance Show Int64 where
712 showsPrec p x = showsPrec p (toInteger x)
713
714 -- | @since 2.01
715 instance Num Int64 where
716 (I64# x#) + (I64# y#) = I64# (x# `plusInt64#` y#)
717 (I64# x#) - (I64# y#) = I64# (x# `minusInt64#` y#)
718 (I64# x#) * (I64# y#) = I64# (x# `timesInt64#` y#)
719 negate (I64# x#) = I64# (negateInt64# x#)
720 abs x | x >= 0 = x
721 | otherwise = negate x
722 signum x | x > 0 = 1
723 signum 0 = 0
724 signum _ = -1
725 fromInteger i = I64# (integerToInt64 i)
726
727 -- | @since 2.01
728 instance Enum Int64 where
729 succ x
730 | x /= maxBound = x + 1
731 | otherwise = succError "Int64"
732 pred x
733 | x /= minBound = x - 1
734 | otherwise = predError "Int64"
735 toEnum (I# i#) = I64# (intToInt64# i#)
736 fromEnum x@(I64# x#)
737 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
738 = I# (int64ToInt# x#)
739 | otherwise = fromEnumError "Int64" x
740 enumFrom = integralEnumFrom
741 enumFromThen = integralEnumFromThen
742 enumFromTo = integralEnumFromTo
743 enumFromThenTo = integralEnumFromThenTo
744
745 -- | @since 2.01
746 instance Integral Int64 where
747 quot x@(I64# x#) y@(I64# y#)
748 | y == 0 = divZeroError
749 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
750 | otherwise = I64# (x# `quotInt64#` y#)
751 rem (I64# x#) y@(I64# y#)
752 | y == 0 = divZeroError
753 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
754 -- but minBound `rem` -1 is well-defined (0). We therefore
755 -- special-case it.
756 | y == (-1) = 0
757 | otherwise = I64# (x# `remInt64#` y#)
758 div x@(I64# x#) y@(I64# y#)
759 | y == 0 = divZeroError
760 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
761 | otherwise = I64# (x# `divInt64#` y#)
762 mod (I64# x#) y@(I64# y#)
763 | y == 0 = divZeroError
764 -- The divMod CPU instruction fails for minBound `divMod` -1,
765 -- but minBound `mod` -1 is well-defined (0). We therefore
766 -- special-case it.
767 | y == (-1) = 0
768 | otherwise = I64# (x# `modInt64#` y#)
769 quotRem x@(I64# x#) y@(I64# y#)
770 | y == 0 = divZeroError
771 -- Note [Order of tests]
772 | y == (-1) && x == minBound = (overflowError, 0)
773 | otherwise = (I64# (x# `quotInt64#` y#),
774 I64# (x# `remInt64#` y#))
775 divMod x@(I64# x#) y@(I64# y#)
776 | y == 0 = divZeroError
777 -- Note [Order of tests]
778 | y == (-1) && x == minBound = (overflowError, 0)
779 | otherwise = (I64# (x# `divInt64#` y#),
780 I64# (x# `modInt64#` y#))
781 toInteger (I64# x) = int64ToInteger x
782
783
784 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
785
786 -- Define div in terms of quot, being careful to avoid overflow (#7233)
787 x# `divInt64#` y#
788 | isTrue# (x# `gtInt64#` zero) && isTrue# (y# `ltInt64#` zero)
789 = ((x# `minusInt64#` one) `quotInt64#` y#) `minusInt64#` one
790 | isTrue# (x# `ltInt64#` zero) && isTrue# (y# `gtInt64#` zero)
791 = ((x# `plusInt64#` one) `quotInt64#` y#) `minusInt64#` one
792 | otherwise
793 = x# `quotInt64#` y#
794 where
795 !zero = intToInt64# 0#
796 !one = intToInt64# 1#
797
798 x# `modInt64#` y#
799 | isTrue# (x# `gtInt64#` zero) && isTrue# (y# `ltInt64#` zero) ||
800 isTrue# (x# `ltInt64#` zero) && isTrue# (y# `gtInt64#` zero)
801 = if isTrue# (r# `neInt64#` zero) then r# `plusInt64#` y# else zero
802 | otherwise = r#
803 where
804 !zero = intToInt64# 0#
805 !r# = x# `remInt64#` y#
806
807 -- | @since 2.01
808 instance Read Int64 where
809 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
810
811 -- | @since 2.01
812 instance Bits Int64 where
813 {-# INLINE shift #-}
814 {-# INLINE bit #-}
815 {-# INLINE testBit #-}
816
817 (I64# x#) .&. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#))
818 (I64# x#) .|. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `or64#` int64ToWord64# y#))
819 (I64# x#) `xor` (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#))
820 complement (I64# x#) = I64# (word64ToInt64# (not64# (int64ToWord64# x#)))
821 (I64# x#) `shift` (I# i#)
822 | isTrue# (i# >=# 0#) = I64# (x# `iShiftL64#` i#)
823 | otherwise = I64# (x# `iShiftRA64#` negateInt# i#)
824 (I64# x#) `shiftL` (I# i#) = I64# (x# `iShiftL64#` i#)
825 (I64# x#) `unsafeShiftL` (I# i#) = I64# (x# `uncheckedIShiftL64#` i#)
826 (I64# x#) `shiftR` (I# i#) = I64# (x# `iShiftRA64#` i#)
827 (I64# x#) `unsafeShiftR` (I# i#) = I64# (x# `uncheckedIShiftRA64#` i#)
828 (I64# x#) `rotate` (I# i#)
829 | isTrue# (i'# ==# 0#)
830 = I64# x#
831 | otherwise
832 = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
833 (x'# `uncheckedShiftRL64#` (64# -# i'#))))
834 where
835 !x'# = int64ToWord64# x#
836 !i'# = word2Int# (int2Word# i# `and#` 63##)
837 bitSizeMaybe i = Just (finiteBitSize i)
838 bitSize i = finiteBitSize i
839 isSigned _ = True
840 popCount (I64# x#) =
841 I# (word2Int# (popCnt64# (int64ToWord64# x#)))
842 bit = bitDefault
843 testBit = testBitDefault
844
845 -- give the 64-bit shift operations the same treatment as the 32-bit
846 -- ones (see GHC.Base), namely we wrap them in tests to catch the
847 -- cases when we're shifting more than 64 bits to avoid unspecified
848 -- behaviour in the C shift operations.
849
850 iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#
851
852 a `iShiftL64#` b | isTrue# (b >=# 64#) = intToInt64# 0#
853 | otherwise = a `uncheckedIShiftL64#` b
854
855 a `iShiftRA64#` b | isTrue# (b >=# 64#) = if isTrue# (a `ltInt64#` (intToInt64# 0#))
856 then intToInt64# (-1#)
857 else intToInt64# 0#
858 | otherwise = a `uncheckedIShiftRA64#` b
859
860 {-# RULES
861 "fromIntegral/Int->Int64" fromIntegral = \(I# x#) -> I64# (intToInt64# x#)
862 "fromIntegral/Word->Int64" fromIntegral = \(W# x#) -> I64# (word64ToInt64# (wordToWord64# x#))
863 "fromIntegral/Word64->Int64" fromIntegral = \(W64# x#) -> I64# (word64ToInt64# x#)
864 "fromIntegral/Int64->Int" fromIntegral = \(I64# x#) -> I# (int64ToInt# x#)
865 "fromIntegral/Int64->Word" fromIntegral = \(I64# x#) -> W# (int2Word# (int64ToInt# x#))
866 "fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#)
867 "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64
868 #-}
869
870 -- No RULES for RealFrac methods if Int is smaller than Int64, we can't
871 -- go through Int and whether going through Integer is faster is uncertain.
872 #else
873
874 -- Int64 is represented in the same way as Int.
875 -- Operations may assume and must ensure that it holds only values
876 -- from its logical range.
877
878 data {-# CTYPE "HsInt64" #-} Int64 = I64# Int#
879 -- ^ 64-bit signed integer type
880
881 -- See GHC.Classes#matching_overloaded_methods_in_rules
882 -- | @since 2.01
883 instance Eq Int64 where
884 (==) = eqInt64
885 (/=) = neInt64
886
887 eqInt64, neInt64 :: Int64 -> Int64 -> Bool
888 eqInt64 (I64# x) (I64# y) = isTrue# (x ==# y)
889 neInt64 (I64# x) (I64# y) = isTrue# (x /=# y)
890 {-# INLINE [1] eqInt64 #-}
891 {-# INLINE [1] neInt64 #-}
892
893 -- | @since 2.01
894 instance Ord Int64 where
895 (<) = ltInt64
896 (<=) = leInt64
897 (>=) = geInt64
898 (>) = gtInt64
899
900 {-# INLINE [1] gtInt64 #-}
901 {-# INLINE [1] geInt64 #-}
902 {-# INLINE [1] ltInt64 #-}
903 {-# INLINE [1] leInt64 #-}
904 gtInt64, geInt64, ltInt64, leInt64 :: Int64 -> Int64 -> Bool
905 (I64# x) `gtInt64` (I64# y) = isTrue# (x ># y)
906 (I64# x) `geInt64` (I64# y) = isTrue# (x >=# y)
907 (I64# x) `ltInt64` (I64# y) = isTrue# (x <# y)
908 (I64# x) `leInt64` (I64# y) = isTrue# (x <=# y)
909
910 -- | @since 2.01
911 instance Show Int64 where
912 showsPrec p x = showsPrec p (fromIntegral x :: Int)
913
914 -- | @since 2.01
915 instance Num Int64 where
916 (I64# x#) + (I64# y#) = I64# (x# +# y#)
917 (I64# x#) - (I64# y#) = I64# (x# -# y#)
918 (I64# x#) * (I64# y#) = I64# (x# *# y#)
919 negate (I64# x#) = I64# (negateInt# x#)
920 abs x | x >= 0 = x
921 | otherwise = negate x
922 signum x | x > 0 = 1
923 signum 0 = 0
924 signum _ = -1
925 fromInteger i = I64# (integerToInt i)
926
927 -- | @since 2.01
928 instance Enum Int64 where
929 succ x
930 | x /= maxBound = x + 1
931 | otherwise = succError "Int64"
932 pred x
933 | x /= minBound = x - 1
934 | otherwise = predError "Int64"
935 toEnum (I# i#) = I64# i#
936 fromEnum (I64# x#) = I# x#
937 enumFrom = boundedEnumFrom
938 enumFromThen = boundedEnumFromThen
939
940 -- | @since 2.01
941 instance Integral Int64 where
942 quot x@(I64# x#) y@(I64# y#)
943 | y == 0 = divZeroError
944 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
945 | otherwise = I64# (x# `quotInt#` y#)
946 rem (I64# x#) y@(I64# y#)
947 | y == 0 = divZeroError
948 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
949 -- but minBound `rem` -1 is well-defined (0). We therefore
950 -- special-case it.
951 | y == (-1) = 0
952 | otherwise = I64# (x# `remInt#` y#)
953 div x@(I64# x#) y@(I64# y#)
954 | y == 0 = divZeroError
955 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
956 | otherwise = I64# (x# `divInt#` y#)
957 mod (I64# x#) y@(I64# y#)
958 | y == 0 = divZeroError
959 -- The divMod CPU instruction fails for minBound `divMod` -1,
960 -- but minBound `mod` -1 is well-defined (0). We therefore
961 -- special-case it.
962 | y == (-1) = 0
963 | otherwise = I64# (x# `modInt#` y#)
964 quotRem x@(I64# x#) y@(I64# y#)
965 | y == 0 = divZeroError
966 -- Note [Order of tests]
967 | y == (-1) && x == minBound = (overflowError, 0)
968 | otherwise = case x# `quotRemInt#` y# of
969 (# q, r #) ->
970 (I64# q, I64# r)
971 divMod x@(I64# x#) y@(I64# y#)
972 | y == 0 = divZeroError
973 -- Note [Order of tests]
974 | y == (-1) && x == minBound = (overflowError, 0)
975 | otherwise = case x# `divModInt#` y# of
976 (# d, m #) ->
977 (I64# d, I64# m)
978 toInteger (I64# x#) = smallInteger x#
979
980 -- | @since 2.01
981 instance Read Int64 where
982 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
983
984 -- | @since 2.01
985 instance Bits Int64 where
986 {-# INLINE shift #-}
987 {-# INLINE bit #-}
988 {-# INLINE testBit #-}
989
990 (I64# x#) .&. (I64# y#) = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
991 (I64# x#) .|. (I64# y#) = I64# (word2Int# (int2Word# x# `or#` int2Word# y#))
992 (I64# x#) `xor` (I64# y#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
993 complement (I64# x#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
994 (I64# x#) `shift` (I# i#)
995 | isTrue# (i# >=# 0#) = I64# (x# `iShiftL#` i#)
996 | otherwise = I64# (x# `iShiftRA#` negateInt# i#)
997 (I64# x#) `shiftL` (I# i#) = I64# (x# `iShiftL#` i#)
998 (I64# x#) `unsafeShiftL` (I# i#) = I64# (x# `uncheckedIShiftL#` i#)
999 (I64# x#) `shiftR` (I# i#) = I64# (x# `iShiftRA#` i#)
1000 (I64# x#) `unsafeShiftR` (I# i#) = I64# (x# `uncheckedIShiftRA#` i#)
1001 (I64# x#) `rotate` (I# i#)
1002 | isTrue# (i'# ==# 0#)
1003 = I64# x#
1004 | otherwise
1005 = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
1006 (x'# `uncheckedShiftRL#` (64# -# i'#))))
1007 where
1008 !x'# = int2Word# x#
1009 !i'# = word2Int# (int2Word# i# `and#` 63##)
1010 bitSizeMaybe i = Just (finiteBitSize i)
1011 bitSize i = finiteBitSize i
1012 isSigned _ = True
1013 popCount (I64# x#) = I# (word2Int# (popCnt64# (int2Word# x#)))
1014 bit = bitDefault
1015 testBit = testBitDefault
1016
1017 {-# RULES
1018 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
1019 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
1020 #-}
1021
1022 {-# RULES
1023 "properFraction/Float->(Int64,Float)"
1024 properFraction = \x ->
1025 case properFraction x of {
1026 (n, y) -> ((fromIntegral :: Int -> Int64) n, y :: Float) }
1027 "truncate/Float->Int64"
1028 truncate = (fromIntegral :: Int -> Int64) . (truncate :: Float -> Int)
1029 "floor/Float->Int64"
1030 floor = (fromIntegral :: Int -> Int64) . (floor :: Float -> Int)
1031 "ceiling/Float->Int64"
1032 ceiling = (fromIntegral :: Int -> Int64) . (ceiling :: Float -> Int)
1033 "round/Float->Int64"
1034 round = (fromIntegral :: Int -> Int64) . (round :: Float -> Int)
1035 #-}
1036
1037 {-# RULES
1038 "properFraction/Double->(Int64,Double)"
1039 properFraction = \x ->
1040 case properFraction x of {
1041 (n, y) -> ((fromIntegral :: Int -> Int64) n, y :: Double) }
1042 "truncate/Double->Int64"
1043 truncate = (fromIntegral :: Int -> Int64) . (truncate :: Double -> Int)
1044 "floor/Double->Int64"
1045 floor = (fromIntegral :: Int -> Int64) . (floor :: Double -> Int)
1046 "ceiling/Double->Int64"
1047 ceiling = (fromIntegral :: Int -> Int64) . (ceiling :: Double -> Int)
1048 "round/Double->Int64"
1049 round = (fromIntegral :: Int -> Int64) . (round :: Double -> Int)
1050 #-}
1051
1052 uncheckedIShiftL64# :: Int# -> Int# -> Int#
1053 uncheckedIShiftL64# = uncheckedIShiftL#
1054
1055 uncheckedIShiftRA64# :: Int# -> Int# -> Int#
1056 uncheckedIShiftRA64# = uncheckedIShiftRA#
1057 #endif
1058
1059 -- | @since 4.6.0.0
1060 instance FiniteBits Int64 where
1061 finiteBitSize _ = 64
1062 #if WORD_SIZE_IN_BITS < 64
1063 countLeadingZeros (I64# x#) = I# (word2Int# (clz64# (int64ToWord64# x#)))
1064 countTrailingZeros (I64# x#) = I# (word2Int# (ctz64# (int64ToWord64# x#)))
1065 #else
1066 countLeadingZeros (I64# x#) = I# (word2Int# (clz64# (int2Word# x#)))
1067 countTrailingZeros (I64# x#) = I# (word2Int# (ctz64# (int2Word# x#)))
1068 #endif
1069
1070 -- | @since 2.01
1071 instance Real Int64 where
1072 toRational x = toInteger x % 1
1073
1074 -- | @since 2.01
1075 instance Bounded Int64 where
1076 minBound = -0x8000000000000000
1077 maxBound = 0x7FFFFFFFFFFFFFFF
1078
1079 -- | @since 2.01
1080 instance Ix Int64 where
1081 range (m,n) = [m..n]
1082 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
1083 inRange (m,n) i = m <= i && i <= n
1084
1085 -------------------------------------------------------------------------------
1086
1087 {-# RULES
1088 "fromIntegral/Natural->Int8"
1089 fromIntegral = (fromIntegral :: Int -> Int8) . naturalToInt
1090 "fromIntegral/Natural->Int16"
1091 fromIntegral = (fromIntegral :: Int -> Int16) . naturalToInt
1092 "fromIntegral/Natural->Int32"
1093 fromIntegral = (fromIntegral :: Int -> Int32) . naturalToInt
1094 #-}
1095
1096 {-# RULES
1097 "fromIntegral/Int8->Natural"
1098 fromIntegral = intToNatural . (fromIntegral :: Int8 -> Int)
1099 "fromIntegral/Int16->Natural"
1100 fromIntegral = intToNatural . (fromIntegral :: Int16 -> Int)
1101 "fromIntegral/Int32->Natural"
1102 fromIntegral = intToNatural . (fromIntegral :: Int32 -> Int)
1103 #-}
1104
1105 #if WORD_SIZE_IN_BITS == 64
1106 -- these RULES are valid for Word==Word64 & Int==Int64
1107 {-# RULES
1108 "fromIntegral/Natural->Int64"
1109 fromIntegral = (fromIntegral :: Int -> Int64) . naturalToInt
1110 "fromIntegral/Int64->Natural"
1111 fromIntegral = intToNatural . (fromIntegral :: Int64 -> Int)
1112 #-}
1113 #endif
1114
1115
1116 {- Note [Order of tests]
1117 ~~~~~~~~~~~~~~~~~~~~~~~~~
1118 (See Trac #3065, #5161.) Suppose we had a definition like:
1119
1120 quot x y
1121 | y == 0 = divZeroError
1122 | x == minBound && y == (-1) = overflowError
1123 | otherwise = x `primQuot` y
1124
1125 Note in particular that the
1126 x == minBound
1127 test comes before the
1128 y == (-1)
1129 test.
1130
1131 this expands to something like:
1132
1133 case y of
1134 0 -> divZeroError
1135 _ -> case x of
1136 -9223372036854775808 ->
1137 case y of
1138 -1 -> overflowError
1139 _ -> x `primQuot` y
1140 _ -> x `primQuot` y
1141
1142 Now if we have the call (x `quot` 2), and quot gets inlined, then we get:
1143
1144 case 2 of
1145 0 -> divZeroError
1146 _ -> case x of
1147 -9223372036854775808 ->
1148 case 2 of
1149 -1 -> overflowError
1150 _ -> x `primQuot` 2
1151 _ -> x `primQuot` 2
1152
1153 which simplifies to:
1154
1155 case x of
1156 -9223372036854775808 -> x `primQuot` 2
1157 _ -> x `primQuot` 2
1158
1159 Now we have a case with two identical branches, which would be
1160 eliminated (assuming it doesn't affect strictness, which it doesn't in
1161 this case), leaving the desired:
1162
1163 x `primQuot` 2
1164
1165 except in the minBound branch we know what x is, and GHC cleverly does
1166 the division at compile time, giving:
1167
1168 case x of
1169 -9223372036854775808 -> -4611686018427387904
1170 _ -> x `primQuot` 2
1171
1172 So instead we use a definition like:
1173
1174 quot x y
1175 | y == 0 = divZeroError
1176 | y == (-1) && x == minBound = overflowError
1177 | otherwise = x `primQuot` y
1178
1179 which gives us:
1180
1181 case y of
1182 0 -> divZeroError
1183 -1 ->
1184 case x of
1185 -9223372036854775808 -> overflowError
1186 _ -> x `primQuot` y
1187 _ -> x `primQuot` y
1188
1189 for which our call (x `quot` 2) expands to:
1190
1191 case 2 of
1192 0 -> divZeroError
1193 -1 ->
1194 case x of
1195 -9223372036854775808 -> overflowError
1196 _ -> x `primQuot` 2
1197 _ -> x `primQuot` 2
1198
1199 which simplifies to:
1200
1201 x `primQuot` 2
1202
1203 as required.
1204
1205
1206
1207 But we now have the same problem with a constant numerator: the call
1208 (2 `quot` y) expands to
1209
1210 case y of
1211 0 -> divZeroError
1212 -1 ->
1213 case 2 of
1214 -9223372036854775808 -> overflowError
1215 _ -> 2 `primQuot` y
1216 _ -> 2 `primQuot` y
1217
1218 which simplifies to:
1219
1220 case y of
1221 0 -> divZeroError
1222 -1 -> 2 `primQuot` y
1223 _ -> 2 `primQuot` y
1224
1225 which simplifies to:
1226
1227 case y of
1228 0 -> divZeroError
1229 -1 -> -2
1230 _ -> 2 `primQuot` y
1231
1232
1233 However, constant denominators are more common than constant numerators,
1234 so the
1235 y == (-1) && x == minBound
1236 order gives us better code in the common case.
1237 -}