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