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