1 {-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash,
3 {-# OPTIONS_HADDOCK hide #-}
4 -----------------------------------------------------------------------------
7 -- Copyright : (c) The University of Glasgow 1997-2002
8 -- License : see libraries/base/LICENSE
10 -- Maintainer : cvs-ghc@haskell.org
11 -- Stability : internal
12 -- Portability : non-portable (GHC Extensions)
14 -- The sized integral datatypes, 'Int8', 'Int16', 'Int32', and 'Int64'.
16 -----------------------------------------------------------------------------
22 Int8(..), Int16(..), Int32(..), Int64(..),
23 uncheckedIShiftL64#, uncheckedIShiftRA64#
28 #if WORD_SIZE_IN_BITS < 32
31 #if WORD_SIZE_IN_BITS < 64
42 import GHC.Word hiding (uncheckedShiftL64#, uncheckedShiftRL64#)
44 import GHC.Float () -- for RealFrac methods
47 ------------------------------------------------------------------------
49 ------------------------------------------------------------------------
51 -- Int8 is represented in the same way as Int. Operations may assume
52 -- and must ensure that it holds only values from its logical range.
54 data Int8 = I8# Int# deriving (Eq, Ord)
55 -- ^ 8-bit signed integer type
57 instance Show Int8 where
58 showsPrec p x = showsPrec p (fromIntegral x :: Int)
60 instance Num Int8 where
61 (I8# x#) + (I8# y#) = I8# (narrow8Int# (x# +# y#))
62 (I8# x#) - (I8# y#) = I8# (narrow8Int# (x# -# y#))
63 (I8# x#) * (I8# y#) = I8# (narrow8Int# (x# *# y#))
64 negate (I8# x#) = I8# (narrow8Int# (negateInt# x#))
66 | otherwise = negate x
70 fromInteger i = I8# (narrow8Int# (toInt# i))
72 instance Real Int8 where
73 toRational x = toInteger x % 1
75 instance Enum Int8 where
77 | x /= maxBound = x + 1
78 | otherwise = succError "Int8"
80 | x /= minBound = x - 1
81 | otherwise = predError "Int8"
83 | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
85 | otherwise = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
86 fromEnum (I8# x#) = I# x#
87 enumFrom = boundedEnumFrom
88 enumFromThen = boundedEnumFromThen
90 instance Integral Int8 where
91 quot x@(I8# x#) y@(I8# y#)
92 | y == 0 = divZeroError
93 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
94 | otherwise = I8# (narrow8Int# (x# `quotInt#` y#))
95 rem x@(I8# x#) y@(I8# y#)
96 | y == 0 = divZeroError
97 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
98 | otherwise = I8# (narrow8Int# (x# `remInt#` y#))
99 div x@(I8# x#) y@(I8# y#)
100 | y == 0 = divZeroError
101 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
102 | otherwise = I8# (narrow8Int# (x# `divInt#` y#))
103 mod x@(I8# x#) y@(I8# y#)
104 | y == 0 = divZeroError
105 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
106 | otherwise = I8# (narrow8Int# (x# `modInt#` y#))
107 quotRem x@(I8# x#) y@(I8# y#)
108 | y == 0 = divZeroError
109 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
110 | otherwise = (I8# (narrow8Int# (x# `quotInt#` y#)),
111 I8# (narrow8Int# (x# `remInt#` y#)))
112 divMod x@(I8# x#) y@(I8# y#)
113 | y == 0 = divZeroError
114 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
115 | otherwise = (I8# (narrow8Int# (x# `divInt#` y#)),
116 I8# (narrow8Int# (x# `modInt#` y#)))
117 toInteger (I8# x#) = smallInteger x#
119 instance Bounded Int8 where
123 instance Ix Int8 where
125 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
126 inRange (m,n) i = m <= i && i <= n
128 instance Read Int8 where
129 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
131 instance Bits Int8 where
134 (I8# x#) .&. (I8# y#) = I8# (word2Int# (int2Word# x# `and#` int2Word# y#))
135 (I8# x#) .|. (I8# y#) = I8# (word2Int# (int2Word# x# `or#` int2Word# y#))
136 (I8# x#) `xor` (I8# y#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#))
137 complement (I8# x#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
138 (I8# x#) `shift` (I# i#)
139 | i# >=# 0# = I8# (narrow8Int# (x# `iShiftL#` i#))
140 | otherwise = I8# (x# `iShiftRA#` negateInt# i#)
141 (I8# x#) `rotate` (I# i#)
145 = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
146 (x'# `uncheckedShiftRL#` (8# -# i'#)))))
148 !x'# = narrow8Word# (int2Word# x#)
149 !i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
154 "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
155 "fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#)
156 "fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#)
160 "properFraction/Float->(Int8,Float)"
161 forall x. properFraction (x :: Float) =
162 case properFraction x of {
163 (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
164 "truncate/Float->Int8"
165 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int8) (truncate x)
167 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int8) (floor x)
168 "ceiling/Float->Int8"
169 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int8) (ceiling x)
171 forall x. round (x :: Float) = (fromIntegral :: Int -> Int8) (round x)
175 "properFraction/Double->(Int8,Double)"
176 forall x. properFraction (x :: Double) =
177 case properFraction x of {
178 (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
179 "truncate/Double->Int8"
180 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int8) (truncate x)
182 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int8) (floor x)
183 "ceiling/Double->Int8"
184 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int8) (ceiling x)
186 forall x. round (x :: Double) = (fromIntegral :: Int -> Int8) (round x)
189 ------------------------------------------------------------------------
191 ------------------------------------------------------------------------
193 -- Int16 is represented in the same way as Int. Operations may assume
194 -- and must ensure that it holds only values from its logical range.
196 data Int16 = I16# Int# deriving (Eq, Ord)
197 -- ^ 16-bit signed integer type
199 instance Show Int16 where
200 showsPrec p x = showsPrec p (fromIntegral x :: Int)
202 instance Num Int16 where
203 (I16# x#) + (I16# y#) = I16# (narrow16Int# (x# +# y#))
204 (I16# x#) - (I16# y#) = I16# (narrow16Int# (x# -# y#))
205 (I16# x#) * (I16# y#) = I16# (narrow16Int# (x# *# y#))
206 negate (I16# x#) = I16# (narrow16Int# (negateInt# x#))
208 | otherwise = negate x
212 fromInteger i = I16# (narrow16Int# (toInt# i))
214 instance Real Int16 where
215 toRational x = toInteger x % 1
217 instance Enum Int16 where
219 | x /= maxBound = x + 1
220 | otherwise = succError "Int16"
222 | x /= minBound = x - 1
223 | otherwise = predError "Int16"
225 | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
227 | otherwise = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
228 fromEnum (I16# x#) = I# x#
229 enumFrom = boundedEnumFrom
230 enumFromThen = boundedEnumFromThen
232 instance Integral Int16 where
233 quot x@(I16# x#) y@(I16# y#)
234 | y == 0 = divZeroError
235 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
236 | otherwise = I16# (narrow16Int# (x# `quotInt#` y#))
237 rem x@(I16# x#) y@(I16# y#)
238 | y == 0 = divZeroError
239 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
240 | otherwise = I16# (narrow16Int# (x# `remInt#` y#))
241 div x@(I16# x#) y@(I16# y#)
242 | y == 0 = divZeroError
243 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
244 | otherwise = I16# (narrow16Int# (x# `divInt#` y#))
245 mod x@(I16# x#) y@(I16# y#)
246 | y == 0 = divZeroError
247 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
248 | otherwise = I16# (narrow16Int# (x# `modInt#` y#))
249 quotRem x@(I16# x#) y@(I16# y#)
250 | y == 0 = divZeroError
251 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
252 | otherwise = (I16# (narrow16Int# (x# `quotInt#` y#)),
253 I16# (narrow16Int# (x# `remInt#` y#)))
254 divMod x@(I16# x#) y@(I16# y#)
255 | y == 0 = divZeroError
256 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
257 | otherwise = (I16# (narrow16Int# (x# `divInt#` y#)),
258 I16# (narrow16Int# (x# `modInt#` y#)))
259 toInteger (I16# x#) = smallInteger x#
261 instance Bounded Int16 where
265 instance Ix Int16 where
267 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
268 inRange (m,n) i = m <= i && i <= n
270 instance Read Int16 where
271 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
273 instance Bits Int16 where
276 (I16# x#) .&. (I16# y#) = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
277 (I16# x#) .|. (I16# y#) = I16# (word2Int# (int2Word# x# `or#` int2Word# y#))
278 (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
279 complement (I16# x#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
280 (I16# x#) `shift` (I# i#)
281 | i# >=# 0# = I16# (narrow16Int# (x# `iShiftL#` i#))
282 | otherwise = I16# (x# `iShiftRA#` negateInt# i#)
283 (I16# x#) `rotate` (I# i#)
287 = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
288 (x'# `uncheckedShiftRL#` (16# -# i'#)))))
290 !x'# = narrow16Word# (int2Word# x#)
291 !i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
297 "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
298 "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x#
299 "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16
300 "fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
301 "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
305 "properFraction/Float->(Int16,Float)"
306 forall x. properFraction (x :: Float) =
307 case properFraction x of {
308 (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
309 "truncate/Float->Int16"
310 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int16) (truncate x)
312 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int16) (floor x)
313 "ceiling/Float->Int16"
314 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int16) (ceiling x)
316 forall x. round (x :: Float) = (fromIntegral :: Int -> Int16) (round x)
320 "properFraction/Double->(Int16,Double)"
321 forall x. properFraction (x :: Double) =
322 case properFraction x of {
323 (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
324 "truncate/Double->Int16"
325 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int16) (truncate x)
326 "floor/Double->Int16"
327 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int16) (floor x)
328 "ceiling/Double->Int16"
329 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int16) (ceiling x)
330 "round/Double->Int16"
331 forall x. round (x :: Double) = (fromIntegral :: Int -> Int16) (round x)
334 ------------------------------------------------------------------------
336 ------------------------------------------------------------------------
338 #if WORD_SIZE_IN_BITS < 32
340 data Int32 = I32# Int32#
341 -- ^ 32-bit signed integer type
343 instance Eq Int32 where
344 (I32# x#) == (I32# y#) = x# `eqInt32#` y#
345 (I32# x#) /= (I32# y#) = x# `neInt32#` y#
347 instance Ord Int32 where
348 (I32# x#) < (I32# y#) = x# `ltInt32#` y#
349 (I32# x#) <= (I32# y#) = x# `leInt32#` y#
350 (I32# x#) > (I32# y#) = x# `gtInt32#` y#
351 (I32# x#) >= (I32# y#) = x# `geInt32#` y#
353 instance Show Int32 where
354 showsPrec p x = showsPrec p (toInteger x)
356 instance Num Int32 where
357 (I32# x#) + (I32# y#) = I32# (x# `plusInt32#` y#)
358 (I32# x#) - (I32# y#) = I32# (x# `minusInt32#` y#)
359 (I32# x#) * (I32# y#) = I32# (x# `timesInt32#` y#)
360 negate (I32# x#) = I32# (negateInt32# x#)
362 | otherwise = negate x
366 fromInteger (S# i#) = I32# (intToInt32# i#)
367 fromInteger (J# s# d#) = I32# (integerToInt32# s# d#)
369 instance Enum Int32 where
371 | x /= maxBound = x + 1
372 | otherwise = succError "Int32"
374 | x /= minBound = x - 1
375 | otherwise = predError "Int32"
376 toEnum (I# i#) = I32# (intToInt32# i#)
378 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
379 = I# (int32ToInt# x#)
380 | otherwise = fromEnumError "Int32" x
381 enumFrom = integralEnumFrom
382 enumFromThen = integralEnumFromThen
383 enumFromTo = integralEnumFromTo
384 enumFromThenTo = integralEnumFromThenTo
386 instance Integral Int32 where
387 quot x@(I32# x#) y@(I32# y#)
388 | y == 0 = divZeroError
389 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
390 | otherwise = I32# (x# `quotInt32#` y#)
391 rem x@(I32# x#) y@(I32# y#)
392 | y == 0 = divZeroError
393 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
394 | otherwise = I32# (x# `remInt32#` y#)
395 div x@(I32# x#) y@(I32# y#)
396 | y == 0 = divZeroError
397 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
398 | otherwise = I32# (x# `divInt32#` y#)
399 mod x@(I32# x#) y@(I32# y#)
400 | y == 0 = divZeroError
401 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
402 | otherwise = I32# (x# `modInt32#` y#)
403 quotRem x@(I32# x#) y@(I32# y#)
404 | y == 0 = divZeroError
405 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
406 | otherwise = (I32# (x# `quotInt32#` y#),
407 I32# (x# `remInt32#` y#))
408 divMod x@(I32# x#) y@(I32# y#)
409 | y == 0 = divZeroError
410 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
411 | otherwise = (I32# (x# `divInt32#` y#),
412 I32# (x# `modInt32#` y#))
413 toInteger x@(I32# x#)
414 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
415 = smallInteger (int32ToInt# x#)
416 | otherwise = case int32ToInteger# x# of (# s, d #) -> J# s d
418 divInt32#, modInt32# :: Int32# -> Int32# -> Int32#
420 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#)
421 = ((x# `minusInt32#` y#) `minusInt32#` intToInt32# 1#) `quotInt32#` y#
422 | (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
423 = ((x# `minusInt32#` y#) `plusInt32#` intToInt32# 1#) `quotInt32#` y#
424 | otherwise = x# `quotInt32#` y#
426 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#) ||
427 (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
428 = if r# `neInt32#` intToInt32# 0# then r# `plusInt32#` y# else intToInt32# 0#
431 r# = x# `remInt32#` y#
433 instance Read Int32 where
434 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
436 instance Bits Int32 where
439 (I32# x#) .&. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `and32#` int32ToWord32# y#))
440 (I32# x#) .|. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `or32#` int32ToWord32# y#))
441 (I32# x#) `xor` (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `xor32#` int32ToWord32# y#))
442 complement (I32# x#) = I32# (word32ToInt32# (not32# (int32ToWord32# x#)))
443 (I32# x#) `shift` (I# i#)
444 | i# >=# 0# = I32# (x# `iShiftL32#` i#)
445 | otherwise = I32# (x# `iShiftRA32#` negateInt# i#)
446 (I32# x#) `rotate` (I# i#)
450 = I32# (word32ToInt32# ((x'# `shiftL32#` i'#) `or32#`
451 (x'# `shiftRL32#` (32# -# i'#))))
453 x'# = int32ToWord32# x#
454 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
460 "fromIntegral/Int->Int32" fromIntegral = \(I# x#) -> I32# (intToInt32# x#)
461 "fromIntegral/Word->Int32" fromIntegral = \(W# x#) -> I32# (word32ToInt32# (wordToWord32# x#))
462 "fromIntegral/Word32->Int32" fromIntegral = \(W32# x#) -> I32# (word32ToInt32# x#)
463 "fromIntegral/Int32->Int" fromIntegral = \(I32# x#) -> I# (int32ToInt# x#)
464 "fromIntegral/Int32->Word" fromIntegral = \(I32# x#) -> W# (int2Word# (int32ToInt# x#))
465 "fromIntegral/Int32->Word32" fromIntegral = \(I32# x#) -> W32# (int32ToWord32# x#)
466 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
469 -- No rules for RealFrac methods if Int32 is larger than Int
472 -- Int32 is represented in the same way as Int.
473 #if WORD_SIZE_IN_BITS > 32
474 -- Operations may assume and must ensure that it holds only values
475 -- from its logical range.
478 data Int32 = I32# Int# deriving (Eq, Ord)
479 -- ^ 32-bit signed integer type
481 instance Show Int32 where
482 showsPrec p x = showsPrec p (fromIntegral x :: Int)
484 instance Num Int32 where
485 (I32# x#) + (I32# y#) = I32# (narrow32Int# (x# +# y#))
486 (I32# x#) - (I32# y#) = I32# (narrow32Int# (x# -# y#))
487 (I32# x#) * (I32# y#) = I32# (narrow32Int# (x# *# y#))
488 negate (I32# x#) = I32# (narrow32Int# (negateInt# x#))
490 | otherwise = negate x
494 fromInteger i = I32# (narrow32Int# (toInt# i))
496 instance Enum Int32 where
498 | x /= maxBound = x + 1
499 | otherwise = succError "Int32"
501 | x /= minBound = x - 1
502 | otherwise = predError "Int32"
503 #if WORD_SIZE_IN_BITS == 32
504 toEnum (I# i#) = I32# i#
507 | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
509 | otherwise = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
511 fromEnum (I32# x#) = I# x#
512 enumFrom = boundedEnumFrom
513 enumFromThen = boundedEnumFromThen
515 instance Integral Int32 where
516 quot x@(I32# x#) y@(I32# y#)
517 | y == 0 = divZeroError
518 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
519 | otherwise = I32# (narrow32Int# (x# `quotInt#` y#))
520 rem x@(I32# x#) y@(I32# y#)
521 | y == 0 = divZeroError
522 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
523 | otherwise = I32# (narrow32Int# (x# `remInt#` y#))
524 div x@(I32# x#) y@(I32# y#)
525 | y == 0 = divZeroError
526 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
527 | otherwise = I32# (narrow32Int# (x# `divInt#` y#))
528 mod x@(I32# x#) y@(I32# y#)
529 | y == 0 = divZeroError
530 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
531 | otherwise = I32# (narrow32Int# (x# `modInt#` y#))
532 quotRem x@(I32# x#) y@(I32# y#)
533 | y == 0 = divZeroError
534 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
535 | otherwise = (I32# (narrow32Int# (x# `quotInt#` y#)),
536 I32# (narrow32Int# (x# `remInt#` y#)))
537 divMod x@(I32# x#) y@(I32# y#)
538 | y == 0 = divZeroError
539 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
540 | otherwise = (I32# (narrow32Int# (x# `divInt#` y#)),
541 I32# (narrow32Int# (x# `modInt#` y#)))
542 toInteger (I32# x#) = smallInteger x#
544 instance Read Int32 where
545 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
547 instance Bits Int32 where
550 (I32# x#) .&. (I32# y#) = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
551 (I32# x#) .|. (I32# y#) = I32# (word2Int# (int2Word# x# `or#` int2Word# y#))
552 (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
553 complement (I32# x#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
554 (I32# x#) `shift` (I# i#)
555 | i# >=# 0# = I32# (narrow32Int# (x# `iShiftL#` i#))
556 | otherwise = I32# (x# `iShiftRA#` negateInt# i#)
557 (I32# x#) `rotate` (I# i#)
561 = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
562 (x'# `uncheckedShiftRL#` (32# -# i'#)))))
564 !x'# = narrow32Word# (int2Word# x#)
565 !i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
570 "fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
571 "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
572 "fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x#
573 "fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x#
574 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
575 "fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
576 "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
580 "properFraction/Float->(Int32,Float)"
581 forall x. properFraction (x :: Float) =
582 case properFraction x of {
583 (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
584 "truncate/Float->Int32"
585 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int32) (truncate x)
587 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int32) (floor x)
588 "ceiling/Float->Int32"
589 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int32) (ceiling x)
591 forall x. round (x :: Float) = (fromIntegral :: Int -> Int32) (round x)
595 "properFraction/Double->(Int32,Double)"
596 forall x. properFraction (x :: Double) =
597 case properFraction x of {
598 (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
599 "truncate/Double->Int32"
600 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int32) (truncate x)
601 "floor/Double->Int32"
602 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int32) (floor x)
603 "ceiling/Double->Int32"
604 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int32) (ceiling x)
605 "round/Double->Int32"
606 forall x. round (x :: Double) = (fromIntegral :: Int -> Int32) (round x)
611 instance Real Int32 where
612 toRational x = toInteger x % 1
614 instance Bounded Int32 where
615 minBound = -0x80000000
616 maxBound = 0x7FFFFFFF
618 instance Ix Int32 where
620 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
621 inRange (m,n) i = m <= i && i <= n
623 ------------------------------------------------------------------------
625 ------------------------------------------------------------------------
627 #if WORD_SIZE_IN_BITS < 64
629 data Int64 = I64# Int64#
630 -- ^ 64-bit signed integer type
632 instance Eq Int64 where
633 (I64# x#) == (I64# y#) = x# `eqInt64#` y#
634 (I64# x#) /= (I64# y#) = x# `neInt64#` y#
636 instance Ord Int64 where
637 (I64# x#) < (I64# y#) = x# `ltInt64#` y#
638 (I64# x#) <= (I64# y#) = x# `leInt64#` y#
639 (I64# x#) > (I64# y#) = x# `gtInt64#` y#
640 (I64# x#) >= (I64# y#) = x# `geInt64#` y#
642 instance Show Int64 where
643 showsPrec p x = showsPrec p (toInteger x)
645 instance Num Int64 where
646 (I64# x#) + (I64# y#) = I64# (x# `plusInt64#` y#)
647 (I64# x#) - (I64# y#) = I64# (x# `minusInt64#` y#)
648 (I64# x#) * (I64# y#) = I64# (x# `timesInt64#` y#)
649 negate (I64# x#) = I64# (negateInt64# x#)
651 | otherwise = negate x
655 fromInteger i = I64# (integerToInt64 i)
657 instance Enum Int64 where
659 | x /= maxBound = x + 1
660 | otherwise = succError "Int64"
662 | x /= minBound = x - 1
663 | otherwise = predError "Int64"
664 toEnum (I# i#) = I64# (intToInt64# i#)
666 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
667 = I# (int64ToInt# x#)
668 | otherwise = fromEnumError "Int64" x
669 enumFrom = integralEnumFrom
670 enumFromThen = integralEnumFromThen
671 enumFromTo = integralEnumFromTo
672 enumFromThenTo = integralEnumFromThenTo
674 instance Integral Int64 where
675 quot x@(I64# x#) y@(I64# y#)
676 | y == 0 = divZeroError
677 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
678 | otherwise = I64# (x# `quotInt64#` y#)
679 rem x@(I64# x#) y@(I64# y#)
680 | y == 0 = divZeroError
681 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
682 | otherwise = I64# (x# `remInt64#` y#)
683 div x@(I64# x#) y@(I64# y#)
684 | y == 0 = divZeroError
685 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
686 | otherwise = I64# (x# `divInt64#` y#)
687 mod x@(I64# x#) y@(I64# y#)
688 | y == 0 = divZeroError
689 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
690 | otherwise = I64# (x# `modInt64#` y#)
691 quotRem x@(I64# x#) y@(I64# y#)
692 | y == 0 = divZeroError
693 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
694 | otherwise = (I64# (x# `quotInt64#` y#),
695 I64# (x# `remInt64#` y#))
696 divMod x@(I64# x#) y@(I64# y#)
697 | y == 0 = divZeroError
698 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
699 | otherwise = (I64# (x# `divInt64#` y#),
700 I64# (x# `modInt64#` y#))
701 toInteger (I64# x) = int64ToInteger x
704 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
706 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#)
707 = ((x# `minusInt64#` y#) `minusInt64#` intToInt64# 1#) `quotInt64#` y#
708 | (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
709 = ((x# `minusInt64#` y#) `plusInt64#` intToInt64# 1#) `quotInt64#` y#
710 | otherwise = x# `quotInt64#` y#
712 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) ||
713 (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
714 = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0#
717 !r# = x# `remInt64#` y#
719 instance Read Int64 where
720 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
722 instance Bits Int64 where
725 (I64# x#) .&. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#))
726 (I64# x#) .|. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `or64#` int64ToWord64# y#))
727 (I64# x#) `xor` (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#))
728 complement (I64# x#) = I64# (word64ToInt64# (not64# (int64ToWord64# x#)))
729 (I64# x#) `shift` (I# i#)
730 | i# >=# 0# = I64# (x# `iShiftL64#` i#)
731 | otherwise = I64# (x# `iShiftRA64#` negateInt# i#)
732 (I64# x#) `rotate` (I# i#)
736 = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
737 (x'# `uncheckedShiftRL64#` (64# -# i'#))))
739 !x'# = int64ToWord64# x#
740 !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
744 -- give the 64-bit shift operations the same treatment as the 32-bit
745 -- ones (see GHC.Base), namely we wrap them in tests to catch the
746 -- cases when we're shifting more than 64 bits to avoid unspecified
747 -- behaviour in the C shift operations.
749 iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#
751 a `iShiftL64#` b | b >=# 64# = intToInt64# 0#
752 | otherwise = a `uncheckedIShiftL64#` b
754 a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#)
755 then intToInt64# (-1#)
757 | otherwise = a `uncheckedIShiftRA64#` b
760 "fromIntegral/Int->Int64" fromIntegral = \(I# x#) -> I64# (intToInt64# x#)
761 "fromIntegral/Word->Int64" fromIntegral = \(W# x#) -> I64# (word64ToInt64# (wordToWord64# x#))
762 "fromIntegral/Word64->Int64" fromIntegral = \(W64# x#) -> I64# (word64ToInt64# x#)
763 "fromIntegral/Int64->Int" fromIntegral = \(I64# x#) -> I# (int64ToInt# x#)
764 "fromIntegral/Int64->Word" fromIntegral = \(I64# x#) -> W# (int2Word# (int64ToInt# x#))
765 "fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#)
766 "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64
769 -- No RULES for RealFrac methods if Int is smaller than Int64, we can't
770 -- go through Int and whether going through Integer is faster is uncertain.
773 -- Int64 is represented in the same way as Int.
774 -- Operations may assume and must ensure that it holds only values
775 -- from its logical range.
777 data Int64 = I64# Int# deriving (Eq, Ord)
778 -- ^ 64-bit signed integer type
780 instance Show Int64 where
781 showsPrec p x = showsPrec p (fromIntegral x :: Int)
783 instance Num Int64 where
784 (I64# x#) + (I64# y#) = I64# (x# +# y#)
785 (I64# x#) - (I64# y#) = I64# (x# -# y#)
786 (I64# x#) * (I64# y#) = I64# (x# *# y#)
787 negate (I64# x#) = I64# (negateInt# x#)
789 | otherwise = negate x
793 fromInteger i = I64# (toInt# i)
795 instance Enum Int64 where
797 | x /= maxBound = x + 1
798 | otherwise = succError "Int64"
800 | x /= minBound = x - 1
801 | otherwise = predError "Int64"
802 toEnum (I# i#) = I64# i#
803 fromEnum (I64# x#) = I# x#
804 enumFrom = boundedEnumFrom
805 enumFromThen = boundedEnumFromThen
807 instance Integral Int64 where
808 quot x@(I64# x#) y@(I64# y#)
809 | y == 0 = divZeroError
810 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
811 | otherwise = I64# (x# `quotInt#` y#)
812 rem x@(I64# x#) y@(I64# y#)
813 | y == 0 = divZeroError
814 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
815 | otherwise = I64# (x# `remInt#` y#)
816 div x@(I64# x#) y@(I64# y#)
817 | y == 0 = divZeroError
818 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
819 | otherwise = I64# (x# `divInt#` y#)
820 mod x@(I64# x#) y@(I64# y#)
821 | y == 0 = divZeroError
822 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
823 | otherwise = I64# (x# `modInt#` y#)
824 quotRem x@(I64# x#) y@(I64# y#)
825 | y == 0 = divZeroError
826 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
827 | otherwise = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#))
828 divMod x@(I64# x#) y@(I64# y#)
829 | y == 0 = divZeroError
830 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
831 | otherwise = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#))
832 toInteger (I64# x#) = smallInteger x#
834 instance Read Int64 where
835 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
837 instance Bits Int64 where
840 (I64# x#) .&. (I64# y#) = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
841 (I64# x#) .|. (I64# y#) = I64# (word2Int# (int2Word# x# `or#` int2Word# y#))
842 (I64# x#) `xor` (I64# y#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
843 complement (I64# x#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
844 (I64# x#) `shift` (I# i#)
845 | i# >=# 0# = I64# (x# `iShiftL#` i#)
846 | otherwise = I64# (x# `iShiftRA#` negateInt# i#)
847 (I64# x#) `rotate` (I# i#)
851 = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
852 (x'# `uncheckedShiftRL#` (64# -# i'#))))
855 !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
860 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
861 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
865 "properFraction/Float->(Int64,Float)"
866 forall x. properFraction (x :: Float) =
867 case properFraction x of {
868 (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
869 "truncate/Float->Int64"
870 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int64) (truncate x)
872 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int64) (floor x)
873 "ceiling/Float->Int64"
874 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int64) (ceiling x)
876 forall x. round (x :: Float) = (fromIntegral :: Int -> Int64) (round x)
880 "properFraction/Double->(Int64,Double)"
881 forall x. properFraction (x :: Double) =
882 case properFraction x of {
883 (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
884 "truncate/Double->Int64"
885 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int64) (truncate x)
886 "floor/Double->Int64"
887 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int64) (floor x)
888 "ceiling/Double->Int64"
889 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int64) (ceiling x)
890 "round/Double->Int64"
891 forall x. round (x :: Double) = (fromIntegral :: Int -> Int64) (round x)
894 uncheckedIShiftL64# :: Int# -> Int# -> Int#
895 uncheckedIShiftL64# = uncheckedIShiftL#
897 uncheckedIShiftRA64# :: Int# -> Int# -> Int#
898 uncheckedIShiftRA64# = uncheckedIShiftRA#
901 instance Real Int64 where
902 toRational x = toInteger x % 1
904 instance Bounded Int64 where
905 minBound = -0x8000000000000000
906 maxBound = 0x7FFFFFFFFFFFFFFF
908 instance Ix Int64 where
910 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
911 inRange (m,n) i = m <= i && i <= n
915 Note [Order of tests]
917 Suppose we had a definition like:
920 | y == 0 = divZeroError
921 | x == minBound && y == (-1) = overflowError
922 | otherwise = x `primQuot` y
924 Note in particular that the
926 test comes before the
930 this expands to something like:
935 -9223372036854775808 ->
941 Now if we have the call (x `quot` 2), and quot gets inlined, then we get:
946 -9223372036854775808 ->
955 -9223372036854775808 -> x `primQuot` 2
958 Now we have a case with two identical branches, which would be
959 eliminated (assuming it doesn't affect strictness, which it doesn't in
960 this case), leaving the desired:
964 except in the minBound branch we know what x is, and GHC cleverly does
965 the division at compile time, giving:
968 -9223372036854775808 -> -4611686018427387904
971 So instead we use a definition like:
974 | y == 0 = divZeroError
975 | y == (-1) && x == minBound = overflowError
976 | otherwise = x `primQuot` y
984 -9223372036854775808 -> overflowError
988 for which our call (x `quot` 2) expands to:
994 -9223372036854775808 -> overflowError
1006 But we now have the same problem with a constant numerator: the call
1007 (2 `quot` y) expands to
1013 -9223372036854775808 -> overflowError
1017 which simplifies to:
1021 -1 -> 2 `primQuot` y
1024 which simplifies to:
1032 However, constant denominators are more common than constant numerators,
1034 y == (-1) && x == minBound
1035 order gives us better code in the common case.