Update base for latest Safe Haskell.
[packages/base.git] / GHC / Int.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash,
3 StandaloneDeriving #-}
4 {-# OPTIONS_HADDOCK hide #-}
5
6 -----------------------------------------------------------------------------
7 -- |
8 -- Module : GHC.Int
9 -- Copyright : (c) The University of Glasgow 1997-2002
10 -- License : see libraries/base/LICENSE
11 --
12 -- Maintainer : cvs-ghc@haskell.org
13 -- Stability : internal
14 -- Portability : non-portable (GHC Extensions)
15 --
16 -- The sized integral datatypes, 'Int8', 'Int16', 'Int32', and 'Int64'.
17 --
18 -----------------------------------------------------------------------------
19
20 #include "MachDeps.h"
21
22 -- #hide
23 module GHC.Int (
24 Int8(..), Int16(..), Int32(..), Int64(..),
25 uncheckedIShiftL64#, uncheckedIShiftRA64#
26 ) where
27
28 import Data.Bits
29
30 #if WORD_SIZE_IN_BITS < 64
31 import GHC.IntWord64
32 #endif
33
34 import GHC.Base
35 import GHC.Enum
36 import GHC.Num
37 import GHC.Real
38 import GHC.Read
39 import GHC.Arr
40 import GHC.Err
41 import GHC.Word hiding (uncheckedShiftL64#, uncheckedShiftRL64#)
42 import GHC.Show
43 import GHC.Float () -- for RealFrac methods
44
45
46 ------------------------------------------------------------------------
47 -- type Int8
48 ------------------------------------------------------------------------
49
50 -- Int8 is represented in the same way as Int. Operations may assume
51 -- and must ensure that it holds only values from its logical range.
52
53 data Int8 = I8# Int# deriving (Eq, Ord)
54 -- ^ 8-bit signed integer type
55
56 instance Show Int8 where
57 showsPrec p x = showsPrec p (fromIntegral x :: Int)
58
59 instance Num Int8 where
60 (I8# x#) + (I8# y#) = I8# (narrow8Int# (x# +# y#))
61 (I8# x#) - (I8# y#) = I8# (narrow8Int# (x# -# y#))
62 (I8# x#) * (I8# y#) = I8# (narrow8Int# (x# *# y#))
63 negate (I8# x#) = I8# (narrow8Int# (negateInt# x#))
64 abs x | x >= 0 = x
65 | otherwise = negate x
66 signum x | x > 0 = 1
67 signum 0 = 0
68 signum _ = -1
69 fromInteger i = I8# (narrow8Int# (integerToInt i))
70
71 instance Real Int8 where
72 toRational x = toInteger x % 1
73
74 instance Enum Int8 where
75 succ x
76 | x /= maxBound = x + 1
77 | otherwise = succError "Int8"
78 pred x
79 | x /= minBound = x - 1
80 | otherwise = predError "Int8"
81 toEnum i@(I# i#)
82 | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
83 = I8# i#
84 | otherwise = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
85 fromEnum (I8# x#) = I# x#
86 enumFrom = boundedEnumFrom
87 enumFromThen = boundedEnumFromThen
88
89 instance Integral Int8 where
90 quot x@(I8# x#) y@(I8# y#)
91 | y == 0 = divZeroError
92 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
93 | otherwise = I8# (narrow8Int# (x# `quotInt#` y#))
94 rem (I8# x#) y@(I8# y#)
95 | y == 0 = divZeroError
96 | otherwise = I8# (narrow8Int# (x# `remInt#` y#))
97 div x@(I8# x#) y@(I8# y#)
98 | y == 0 = divZeroError
99 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
100 | otherwise = I8# (narrow8Int# (x# `divInt#` y#))
101 mod (I8# x#) y@(I8# y#)
102 | y == 0 = divZeroError
103 | otherwise = I8# (narrow8Int# (x# `modInt#` y#))
104 quotRem x@(I8# x#) y@(I8# y#)
105 | y == 0 = divZeroError
106 -- Note [Order of tests]
107 | y == (-1) && x == minBound = (overflowError, 0)
108 | otherwise = (I8# (narrow8Int# (x# `quotInt#` y#)),
109 I8# (narrow8Int# (x# `remInt#` y#)))
110 divMod x@(I8# x#) y@(I8# y#)
111 | y == 0 = divZeroError
112 -- Note [Order of tests]
113 | y == (-1) && x == minBound = (overflowError, 0)
114 | otherwise = (I8# (narrow8Int# (x# `divInt#` y#)),
115 I8# (narrow8Int# (x# `modInt#` y#)))
116 toInteger (I8# x#) = smallInteger x#
117
118 instance Bounded Int8 where
119 minBound = -0x80
120 maxBound = 0x7F
121
122 instance Ix Int8 where
123 range (m,n) = [m..n]
124 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
125 inRange (m,n) i = m <= i && i <= n
126
127 instance Read Int8 where
128 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
129
130 instance Bits Int8 where
131 {-# INLINE shift #-}
132
133 (I8# x#) .&. (I8# y#) = I8# (word2Int# (int2Word# x# `and#` int2Word# y#))
134 (I8# x#) .|. (I8# y#) = I8# (word2Int# (int2Word# x# `or#` int2Word# y#))
135 (I8# x#) `xor` (I8# y#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#))
136 complement (I8# x#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
137 (I8# x#) `shift` (I# i#)
138 | i# >=# 0# = I8# (narrow8Int# (x# `iShiftL#` i#))
139 | otherwise = I8# (x# `iShiftRA#` negateInt# i#)
140 (I8# x#) `shiftL` (I# i#) = I8# (narrow8Int# (x# `iShiftL#` i#))
141 (I8# x#) `shiftR` (I# i#) = I8# (x# `iShiftRA#` i#)
142 (I8# x#) `rotate` (I# i#)
143 | i'# ==# 0#
144 = I8# x#
145 | otherwise
146 = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
147 (x'# `uncheckedShiftRL#` (8# -# i'#)))))
148 where
149 !x'# = narrow8Word# (int2Word# x#)
150 !i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
151 bitSize _ = 8
152 isSigned _ = True
153 popCount (I8# x#) = I# (word2Int# (popCnt8# (int2Word# x#)))
154
155 {-# RULES
156 "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
157 "fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#)
158 "fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#)
159 #-}
160
161 {-# RULES
162 "properFraction/Float->(Int8,Float)"
163 forall x. properFraction (x :: Float) =
164 case properFraction x of {
165 (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
166 "truncate/Float->Int8"
167 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int8) (truncate x)
168 "floor/Float->Int8"
169 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int8) (floor x)
170 "ceiling/Float->Int8"
171 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int8) (ceiling x)
172 "round/Float->Int8"
173 forall x. round (x :: Float) = (fromIntegral :: Int -> Int8) (round x)
174 #-}
175
176 {-# RULES
177 "properFraction/Double->(Int8,Double)"
178 forall x. properFraction (x :: Double) =
179 case properFraction x of {
180 (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
181 "truncate/Double->Int8"
182 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int8) (truncate x)
183 "floor/Double->Int8"
184 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int8) (floor x)
185 "ceiling/Double->Int8"
186 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int8) (ceiling x)
187 "round/Double->Int8"
188 forall x. round (x :: Double) = (fromIntegral :: Int -> Int8) (round x)
189 #-}
190
191 ------------------------------------------------------------------------
192 -- type Int16
193 ------------------------------------------------------------------------
194
195 -- Int16 is represented in the same way as Int. Operations may assume
196 -- and must ensure that it holds only values from its logical range.
197
198 data Int16 = I16# Int# deriving (Eq, Ord)
199 -- ^ 16-bit signed integer type
200
201 instance Show Int16 where
202 showsPrec p x = showsPrec p (fromIntegral x :: Int)
203
204 instance Num Int16 where
205 (I16# x#) + (I16# y#) = I16# (narrow16Int# (x# +# y#))
206 (I16# x#) - (I16# y#) = I16# (narrow16Int# (x# -# y#))
207 (I16# x#) * (I16# y#) = I16# (narrow16Int# (x# *# y#))
208 negate (I16# x#) = I16# (narrow16Int# (negateInt# x#))
209 abs x | x >= 0 = x
210 | otherwise = negate x
211 signum x | x > 0 = 1
212 signum 0 = 0
213 signum _ = -1
214 fromInteger i = I16# (narrow16Int# (integerToInt i))
215
216 instance Real Int16 where
217 toRational x = toInteger x % 1
218
219 instance Enum Int16 where
220 succ x
221 | x /= maxBound = x + 1
222 | otherwise = succError "Int16"
223 pred x
224 | x /= minBound = x - 1
225 | otherwise = predError "Int16"
226 toEnum i@(I# i#)
227 | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
228 = I16# i#
229 | otherwise = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
230 fromEnum (I16# x#) = I# x#
231 enumFrom = boundedEnumFrom
232 enumFromThen = boundedEnumFromThen
233
234 instance Integral Int16 where
235 quot x@(I16# x#) y@(I16# y#)
236 | y == 0 = divZeroError
237 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
238 | otherwise = I16# (narrow16Int# (x# `quotInt#` y#))
239 rem (I16# x#) y@(I16# y#)
240 | y == 0 = divZeroError
241 | otherwise = I16# (narrow16Int# (x# `remInt#` y#))
242 div x@(I16# x#) y@(I16# y#)
243 | y == 0 = divZeroError
244 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
245 | otherwise = I16# (narrow16Int# (x# `divInt#` y#))
246 mod (I16# x#) y@(I16# y#)
247 | y == 0 = divZeroError
248 | otherwise = I16# (narrow16Int# (x# `modInt#` y#))
249 quotRem x@(I16# x#) y@(I16# y#)
250 | y == 0 = divZeroError
251 -- Note [Order of tests]
252 | y == (-1) && x == minBound = (overflowError, 0)
253 | otherwise = (I16# (narrow16Int# (x# `quotInt#` y#)),
254 I16# (narrow16Int# (x# `remInt#` y#)))
255 divMod x@(I16# x#) y@(I16# y#)
256 | y == 0 = divZeroError
257 -- Note [Order of tests]
258 | y == (-1) && x == minBound = (overflowError, 0)
259 | otherwise = (I16# (narrow16Int# (x# `divInt#` y#)),
260 I16# (narrow16Int# (x# `modInt#` y#)))
261 toInteger (I16# x#) = smallInteger x#
262
263 instance Bounded Int16 where
264 minBound = -0x8000
265 maxBound = 0x7FFF
266
267 instance Ix Int16 where
268 range (m,n) = [m..n]
269 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
270 inRange (m,n) i = m <= i && i <= n
271
272 instance Read Int16 where
273 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
274
275 instance Bits Int16 where
276 {-# INLINE shift #-}
277
278 (I16# x#) .&. (I16# y#) = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
279 (I16# x#) .|. (I16# y#) = I16# (word2Int# (int2Word# x# `or#` int2Word# y#))
280 (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
281 complement (I16# x#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
282 (I16# x#) `shift` (I# i#)
283 | i# >=# 0# = I16# (narrow16Int# (x# `iShiftL#` i#))
284 | otherwise = I16# (x# `iShiftRA#` negateInt# i#)
285 (I16# x#) `shiftL` (I# i#) = I16# (narrow16Int# (x# `iShiftL#` i#))
286 (I16# x#) `shiftR` (I# i#) = I16# (x# `iShiftRA#` i#)
287 (I16# x#) `rotate` (I# i#)
288 | i'# ==# 0#
289 = I16# x#
290 | otherwise
291 = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
292 (x'# `uncheckedShiftRL#` (16# -# i'#)))))
293 where
294 !x'# = narrow16Word# (int2Word# x#)
295 !i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
296 bitSize _ = 16
297 isSigned _ = True
298 popCount (I16# x#) = I# (word2Int# (popCnt16# (int2Word# x#)))
299
300
301 {-# RULES
302 "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
303 "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x#
304 "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16
305 "fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
306 "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
307 #-}
308
309 {-# RULES
310 "properFraction/Float->(Int16,Float)"
311 forall x. properFraction (x :: Float) =
312 case properFraction x of {
313 (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
314 "truncate/Float->Int16"
315 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int16) (truncate x)
316 "floor/Float->Int16"
317 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int16) (floor x)
318 "ceiling/Float->Int16"
319 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int16) (ceiling x)
320 "round/Float->Int16"
321 forall x. round (x :: Float) = (fromIntegral :: Int -> Int16) (round x)
322 #-}
323
324 {-# RULES
325 "properFraction/Double->(Int16,Double)"
326 forall x. properFraction (x :: Double) =
327 case properFraction x of {
328 (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
329 "truncate/Double->Int16"
330 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int16) (truncate x)
331 "floor/Double->Int16"
332 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int16) (floor x)
333 "ceiling/Double->Int16"
334 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int16) (ceiling x)
335 "round/Double->Int16"
336 forall x. round (x :: Double) = (fromIntegral :: Int -> Int16) (round x)
337 #-}
338
339 ------------------------------------------------------------------------
340 -- type Int32
341 ------------------------------------------------------------------------
342
343 -- Int32 is represented in the same way as Int.
344 #if WORD_SIZE_IN_BITS > 32
345 -- Operations may assume and must ensure that it holds only values
346 -- from its logical range.
347 #endif
348
349 data Int32 = I32# Int# deriving (Eq, Ord)
350 -- ^ 32-bit signed integer type
351
352 instance Show Int32 where
353 showsPrec p x = showsPrec p (fromIntegral x :: Int)
354
355 instance Num Int32 where
356 (I32# x#) + (I32# y#) = I32# (narrow32Int# (x# +# y#))
357 (I32# x#) - (I32# y#) = I32# (narrow32Int# (x# -# y#))
358 (I32# x#) * (I32# y#) = I32# (narrow32Int# (x# *# y#))
359 negate (I32# x#) = I32# (narrow32Int# (negateInt# x#))
360 abs x | x >= 0 = x
361 | otherwise = negate x
362 signum x | x > 0 = 1
363 signum 0 = 0
364 signum _ = -1
365 fromInteger i = I32# (narrow32Int# (integerToInt i))
366
367 instance Enum Int32 where
368 succ x
369 | x /= maxBound = x + 1
370 | otherwise = succError "Int32"
371 pred x
372 | x /= minBound = x - 1
373 | otherwise = predError "Int32"
374 #if WORD_SIZE_IN_BITS == 32
375 toEnum (I# i#) = I32# i#
376 #else
377 toEnum i@(I# i#)
378 | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
379 = I32# i#
380 | otherwise = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
381 #endif
382 fromEnum (I32# x#) = I# x#
383 enumFrom = boundedEnumFrom
384 enumFromThen = boundedEnumFromThen
385
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# (narrow32Int# (x# `quotInt#` y#))
391 rem (I32# x#) y@(I32# y#)
392 | y == 0 = divZeroError
393 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
394 -- but minBound `rem` -1 is well-defined (0). We therefore
395 -- special-case it.
396 | y == (-1) = 0
397 | otherwise = I32# (narrow32Int# (x# `remInt#` y#))
398 div x@(I32# x#) y@(I32# y#)
399 | y == 0 = divZeroError
400 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
401 | otherwise = I32# (narrow32Int# (x# `divInt#` y#))
402 mod (I32# x#) y@(I32# y#)
403 | y == 0 = divZeroError
404 -- The divMod CPU instruction fails for minBound `divMod` -1,
405 -- but minBound `mod` -1 is well-defined (0). We therefore
406 -- special-case it.
407 | y == (-1) = 0
408 | otherwise = I32# (narrow32Int# (x# `modInt#` y#))
409 quotRem x@(I32# x#) y@(I32# y#)
410 | y == 0 = divZeroError
411 -- Note [Order of tests]
412 | y == (-1) && x == minBound = (overflowError, 0)
413 | otherwise = (I32# (narrow32Int# (x# `quotInt#` y#)),
414 I32# (narrow32Int# (x# `remInt#` y#)))
415 divMod x@(I32# x#) y@(I32# y#)
416 | y == 0 = divZeroError
417 -- Note [Order of tests]
418 | y == (-1) && x == minBound = (overflowError, 0)
419 | otherwise = (I32# (narrow32Int# (x# `divInt#` y#)),
420 I32# (narrow32Int# (x# `modInt#` y#)))
421 toInteger (I32# x#) = smallInteger x#
422
423 instance Read Int32 where
424 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
425
426 instance Bits Int32 where
427 {-# INLINE shift #-}
428
429 (I32# x#) .&. (I32# y#) = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
430 (I32# x#) .|. (I32# y#) = I32# (word2Int# (int2Word# x# `or#` int2Word# y#))
431 (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
432 complement (I32# x#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
433 (I32# x#) `shift` (I# i#)
434 | i# >=# 0# = I32# (narrow32Int# (x# `iShiftL#` i#))
435 | otherwise = I32# (x# `iShiftRA#` negateInt# i#)
436 (I32# x#) `shiftL` (I# i#) = I32# (narrow32Int# (x# `iShiftL#` i#))
437 (I32# x#) `shiftR` (I# i#) = I32# (x# `iShiftRA#` i#)
438 (I32# x#) `rotate` (I# i#)
439 | i'# ==# 0#
440 = I32# x#
441 | otherwise
442 = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
443 (x'# `uncheckedShiftRL#` (32# -# i'#)))))
444 where
445 !x'# = narrow32Word# (int2Word# x#)
446 !i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
447 bitSize _ = 32
448 isSigned _ = True
449 popCount (I32# x#) = I# (word2Int# (popCnt32# (int2Word# x#)))
450
451 {-# RULES
452 "fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
453 "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
454 "fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x#
455 "fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x#
456 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
457 "fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
458 "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
459 #-}
460
461 {-# RULES
462 "properFraction/Float->(Int32,Float)"
463 forall x. properFraction (x :: Float) =
464 case properFraction x of {
465 (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
466 "truncate/Float->Int32"
467 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int32) (truncate x)
468 "floor/Float->Int32"
469 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int32) (floor x)
470 "ceiling/Float->Int32"
471 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int32) (ceiling x)
472 "round/Float->Int32"
473 forall x. round (x :: Float) = (fromIntegral :: Int -> Int32) (round x)
474 #-}
475
476 {-# RULES
477 "properFraction/Double->(Int32,Double)"
478 forall x. properFraction (x :: Double) =
479 case properFraction x of {
480 (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
481 "truncate/Double->Int32"
482 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int32) (truncate x)
483 "floor/Double->Int32"
484 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int32) (floor x)
485 "ceiling/Double->Int32"
486 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int32) (ceiling x)
487 "round/Double->Int32"
488 forall x. round (x :: Double) = (fromIntegral :: Int -> Int32) (round x)
489 #-}
490
491 instance Real Int32 where
492 toRational x = toInteger x % 1
493
494 instance Bounded Int32 where
495 minBound = -0x80000000
496 maxBound = 0x7FFFFFFF
497
498 instance Ix Int32 where
499 range (m,n) = [m..n]
500 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
501 inRange (m,n) i = m <= i && i <= n
502
503 ------------------------------------------------------------------------
504 -- type Int64
505 ------------------------------------------------------------------------
506
507 #if WORD_SIZE_IN_BITS < 64
508
509 data Int64 = I64# Int64#
510 -- ^ 64-bit signed integer type
511
512 instance Eq Int64 where
513 (I64# x#) == (I64# y#) = x# `eqInt64#` y#
514 (I64# x#) /= (I64# y#) = x# `neInt64#` y#
515
516 instance Ord Int64 where
517 (I64# x#) < (I64# y#) = x# `ltInt64#` y#
518 (I64# x#) <= (I64# y#) = x# `leInt64#` y#
519 (I64# x#) > (I64# y#) = x# `gtInt64#` y#
520 (I64# x#) >= (I64# y#) = x# `geInt64#` y#
521
522 instance Show Int64 where
523 showsPrec p x = showsPrec p (toInteger x)
524
525 instance Num Int64 where
526 (I64# x#) + (I64# y#) = I64# (x# `plusInt64#` y#)
527 (I64# x#) - (I64# y#) = I64# (x# `minusInt64#` y#)
528 (I64# x#) * (I64# y#) = I64# (x# `timesInt64#` y#)
529 negate (I64# x#) = I64# (negateInt64# x#)
530 abs x | x >= 0 = x
531 | otherwise = negate x
532 signum x | x > 0 = 1
533 signum 0 = 0
534 signum _ = -1
535 fromInteger i = I64# (integerToInt64 i)
536
537 instance Enum Int64 where
538 succ x
539 | x /= maxBound = x + 1
540 | otherwise = succError "Int64"
541 pred x
542 | x /= minBound = x - 1
543 | otherwise = predError "Int64"
544 toEnum (I# i#) = I64# (intToInt64# i#)
545 fromEnum x@(I64# x#)
546 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
547 = I# (int64ToInt# x#)
548 | otherwise = fromEnumError "Int64" x
549 enumFrom = integralEnumFrom
550 enumFromThen = integralEnumFromThen
551 enumFromTo = integralEnumFromTo
552 enumFromThenTo = integralEnumFromThenTo
553
554 instance Integral Int64 where
555 quot x@(I64# x#) y@(I64# y#)
556 | y == 0 = divZeroError
557 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
558 | otherwise = I64# (x# `quotInt64#` y#)
559 rem (I64# x#) y@(I64# y#)
560 | y == 0 = divZeroError
561 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
562 -- but minBound `rem` -1 is well-defined (0). We therefore
563 -- special-case it.
564 | y == (-1) = 0
565 | otherwise = I64# (x# `remInt64#` y#)
566 div x@(I64# x#) y@(I64# y#)
567 | y == 0 = divZeroError
568 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
569 | otherwise = I64# (x# `divInt64#` y#)
570 mod (I64# x#) y@(I64# y#)
571 | y == 0 = divZeroError
572 -- The divMod CPU instruction fails for minBound `divMod` -1,
573 -- but minBound `mod` -1 is well-defined (0). We therefore
574 -- special-case it.
575 | y == (-1) = 0
576 | otherwise = I64# (x# `modInt64#` y#)
577 quotRem x@(I64# x#) y@(I64# y#)
578 | y == 0 = divZeroError
579 -- Note [Order of tests]
580 | y == (-1) && x == minBound = (overflowError, 0)
581 | otherwise = (I64# (x# `quotInt64#` y#),
582 I64# (x# `remInt64#` y#))
583 divMod x@(I64# x#) y@(I64# y#)
584 | y == 0 = divZeroError
585 -- Note [Order of tests]
586 | y == (-1) && x == minBound = (overflowError, 0)
587 | otherwise = (I64# (x# `divInt64#` y#),
588 I64# (x# `modInt64#` y#))
589 toInteger (I64# x) = int64ToInteger x
590
591
592 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
593 x# `divInt64#` y#
594 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#)
595 = ((x# `minusInt64#` y#) `minusInt64#` intToInt64# 1#) `quotInt64#` y#
596 | (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
597 = ((x# `minusInt64#` y#) `plusInt64#` intToInt64# 1#) `quotInt64#` y#
598 | otherwise = x# `quotInt64#` y#
599 x# `modInt64#` y#
600 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) ||
601 (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
602 = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0#
603 | otherwise = r#
604 where
605 !r# = x# `remInt64#` y#
606
607 instance Read Int64 where
608 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
609
610 instance Bits Int64 where
611 {-# INLINE shift #-}
612
613 (I64# x#) .&. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#))
614 (I64# x#) .|. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `or64#` int64ToWord64# y#))
615 (I64# x#) `xor` (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#))
616 complement (I64# x#) = I64# (word64ToInt64# (not64# (int64ToWord64# x#)))
617 (I64# x#) `shift` (I# i#)
618 | i# >=# 0# = I64# (x# `iShiftL64#` i#)
619 | otherwise = I64# (x# `iShiftRA64#` negateInt# i#)
620 (I64# x#) `shiftL` (I# i#) = I64# (x# `iShiftL64#` i#)
621 (I64# x#) `shiftR` (I# i#) = I64# (x# `iShiftRA64#` i#)
622 (I64# x#) `rotate` (I# i#)
623 | i'# ==# 0#
624 = I64# x#
625 | otherwise
626 = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
627 (x'# `uncheckedShiftRL64#` (64# -# i'#))))
628 where
629 !x'# = int64ToWord64# x#
630 !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
631 bitSize _ = 64
632 isSigned _ = True
633 popCount (I64# x#) =
634 I# (word2Int# (popCnt64# (int64ToWord64# x#)))
635
636 -- give the 64-bit shift operations the same treatment as the 32-bit
637 -- ones (see GHC.Base), namely we wrap them in tests to catch the
638 -- cases when we're shifting more than 64 bits to avoid unspecified
639 -- behaviour in the C shift operations.
640
641 iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#
642
643 a `iShiftL64#` b | b >=# 64# = intToInt64# 0#
644 | otherwise = a `uncheckedIShiftL64#` b
645
646 a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#)
647 then intToInt64# (-1#)
648 else intToInt64# 0#
649 | otherwise = a `uncheckedIShiftRA64#` b
650
651 {-# RULES
652 "fromIntegral/Int->Int64" fromIntegral = \(I# x#) -> I64# (intToInt64# x#)
653 "fromIntegral/Word->Int64" fromIntegral = \(W# x#) -> I64# (word64ToInt64# (wordToWord64# x#))
654 "fromIntegral/Word64->Int64" fromIntegral = \(W64# x#) -> I64# (word64ToInt64# x#)
655 "fromIntegral/Int64->Int" fromIntegral = \(I64# x#) -> I# (int64ToInt# x#)
656 "fromIntegral/Int64->Word" fromIntegral = \(I64# x#) -> W# (int2Word# (int64ToInt# x#))
657 "fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#)
658 "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64
659 #-}
660
661 -- No RULES for RealFrac methods if Int is smaller than Int64, we can't
662 -- go through Int and whether going through Integer is faster is uncertain.
663 #else
664
665 -- Int64 is represented in the same way as Int.
666 -- Operations may assume and must ensure that it holds only values
667 -- from its logical range.
668
669 data Int64 = I64# Int# deriving (Eq, Ord)
670 -- ^ 64-bit signed integer type
671
672 instance Show Int64 where
673 showsPrec p x = showsPrec p (fromIntegral x :: Int)
674
675 instance Num Int64 where
676 (I64# x#) + (I64# y#) = I64# (x# +# y#)
677 (I64# x#) - (I64# y#) = I64# (x# -# y#)
678 (I64# x#) * (I64# y#) = I64# (x# *# y#)
679 negate (I64# x#) = I64# (negateInt# x#)
680 abs x | x >= 0 = x
681 | otherwise = negate x
682 signum x | x > 0 = 1
683 signum 0 = 0
684 signum _ = -1
685 fromInteger i = I64# (integerToInt i)
686
687 instance Enum Int64 where
688 succ x
689 | x /= maxBound = x + 1
690 | otherwise = succError "Int64"
691 pred x
692 | x /= minBound = x - 1
693 | otherwise = predError "Int64"
694 toEnum (I# i#) = I64# i#
695 fromEnum (I64# x#) = I# x#
696 enumFrom = boundedEnumFrom
697 enumFromThen = boundedEnumFromThen
698
699 instance Integral Int64 where
700 quot x@(I64# x#) y@(I64# y#)
701 | y == 0 = divZeroError
702 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
703 | otherwise = I64# (x# `quotInt#` y#)
704 rem (I64# x#) y@(I64# y#)
705 | y == 0 = divZeroError
706 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
707 -- but minBound `rem` -1 is well-defined (0). We therefore
708 -- special-case it.
709 | y == (-1) = 0
710 | otherwise = I64# (x# `remInt#` y#)
711 div x@(I64# x#) y@(I64# y#)
712 | y == 0 = divZeroError
713 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
714 | otherwise = I64# (x# `divInt#` y#)
715 mod (I64# x#) y@(I64# y#)
716 | y == 0 = divZeroError
717 -- The divMod CPU instruction fails for minBound `divMod` -1,
718 -- but minBound `mod` -1 is well-defined (0). We therefore
719 -- special-case it.
720 | y == (-1) = 0
721 | otherwise = I64# (x# `modInt#` y#)
722 quotRem x@(I64# x#) y@(I64# y#)
723 | y == 0 = divZeroError
724 -- Note [Order of tests]
725 | y == (-1) && x == minBound = (overflowError, 0)
726 | otherwise = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#))
727 divMod x@(I64# x#) y@(I64# y#)
728 | y == 0 = divZeroError
729 -- Note [Order of tests]
730 | y == (-1) && x == minBound = (overflowError, 0)
731 | otherwise = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#))
732 toInteger (I64# x#) = smallInteger x#
733
734 instance Read Int64 where
735 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
736
737 instance Bits Int64 where
738 {-# INLINE shift #-}
739
740 (I64# x#) .&. (I64# y#) = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
741 (I64# x#) .|. (I64# y#) = I64# (word2Int# (int2Word# x# `or#` int2Word# y#))
742 (I64# x#) `xor` (I64# y#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
743 complement (I64# x#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
744 (I64# x#) `shift` (I# i#)
745 | i# >=# 0# = I64# (x# `iShiftL#` i#)
746 | otherwise = I64# (x# `iShiftRA#` negateInt# i#)
747 (I64# x#) `shiftL` (I# i#) = I64# (x# `iShiftL#` i#)
748 (I64# x#) `shiftR` (I# i#) = I64# (x# `iShiftRA#` i#)
749 (I64# x#) `rotate` (I# i#)
750 | i'# ==# 0#
751 = I64# x#
752 | otherwise
753 = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
754 (x'# `uncheckedShiftRL#` (64# -# i'#))))
755 where
756 !x'# = int2Word# x#
757 !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
758 bitSize _ = 64
759 isSigned _ = True
760 popCount (I64# x#) = I# (word2Int# (popCnt64# (int2Word# x#)))
761
762 {-# RULES
763 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
764 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
765 #-}
766
767 {-# RULES
768 "properFraction/Float->(Int64,Float)"
769 forall x. properFraction (x :: Float) =
770 case properFraction x of {
771 (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
772 "truncate/Float->Int64"
773 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int64) (truncate x)
774 "floor/Float->Int64"
775 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int64) (floor x)
776 "ceiling/Float->Int64"
777 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int64) (ceiling x)
778 "round/Float->Int64"
779 forall x. round (x :: Float) = (fromIntegral :: Int -> Int64) (round x)
780 #-}
781
782 {-# RULES
783 "properFraction/Double->(Int64,Double)"
784 forall x. properFraction (x :: Double) =
785 case properFraction x of {
786 (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
787 "truncate/Double->Int64"
788 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int64) (truncate x)
789 "floor/Double->Int64"
790 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int64) (floor x)
791 "ceiling/Double->Int64"
792 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int64) (ceiling x)
793 "round/Double->Int64"
794 forall x. round (x :: Double) = (fromIntegral :: Int -> Int64) (round x)
795 #-}
796
797 uncheckedIShiftL64# :: Int# -> Int# -> Int#
798 uncheckedIShiftL64# = uncheckedIShiftL#
799
800 uncheckedIShiftRA64# :: Int# -> Int# -> Int#
801 uncheckedIShiftRA64# = uncheckedIShiftRA#
802 #endif
803
804 instance Real Int64 where
805 toRational x = toInteger x % 1
806
807 instance Bounded Int64 where
808 minBound = -0x8000000000000000
809 maxBound = 0x7FFFFFFFFFFFFFFF
810
811 instance Ix Int64 where
812 range (m,n) = [m..n]
813 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
814 inRange (m,n) i = m <= i && i <= n
815
816
817 {-
818 Note [Order of tests]
819
820 Suppose we had a definition like:
821
822 quot x y
823 | y == 0 = divZeroError
824 | x == minBound && y == (-1) = overflowError
825 | otherwise = x `primQuot` y
826
827 Note in particular that the
828 x == minBound
829 test comes before the
830 y == (-1)
831 test.
832
833 this expands to something like:
834
835 case y of
836 0 -> divZeroError
837 _ -> case x of
838 -9223372036854775808 ->
839 case y of
840 -1 -> overflowError
841 _ -> x `primQuot` y
842 _ -> x `primQuot` y
843
844 Now if we have the call (x `quot` 2), and quot gets inlined, then we get:
845
846 case 2 of
847 0 -> divZeroError
848 _ -> case x of
849 -9223372036854775808 ->
850 case 2 of
851 -1 -> overflowError
852 _ -> x `primQuot` 2
853 _ -> x `primQuot` 2
854
855 which simplifies to:
856
857 case x of
858 -9223372036854775808 -> x `primQuot` 2
859 _ -> x `primQuot` 2
860
861 Now we have a case with two identical branches, which would be
862 eliminated (assuming it doesn't affect strictness, which it doesn't in
863 this case), leaving the desired:
864
865 x `primQuot` 2
866
867 except in the minBound branch we know what x is, and GHC cleverly does
868 the division at compile time, giving:
869
870 case x of
871 -9223372036854775808 -> -4611686018427387904
872 _ -> x `primQuot` 2
873
874 So instead we use a definition like:
875
876 quot x y
877 | y == 0 = divZeroError
878 | y == (-1) && x == minBound = overflowError
879 | otherwise = x `primQuot` y
880
881 which gives us:
882
883 case y of
884 0 -> divZeroError
885 -1 ->
886 case x of
887 -9223372036854775808 -> overflowError
888 _ -> x `primQuot` y
889 _ -> x `primQuot` y
890
891 for which our call (x `quot` 2) expands to:
892
893 case 2 of
894 0 -> divZeroError
895 -1 ->
896 case x of
897 -9223372036854775808 -> overflowError
898 _ -> x `primQuot` 2
899 _ -> x `primQuot` 2
900
901 which simplifies to:
902
903 x `primQuot` 2
904
905 as required.
906
907
908
909 But we now have the same problem with a constant numerator: the call
910 (2 `quot` y) expands to
911
912 case y of
913 0 -> divZeroError
914 -1 ->
915 case 2 of
916 -9223372036854775808 -> overflowError
917 _ -> 2 `primQuot` y
918 _ -> 2 `primQuot` y
919
920 which simplifies to:
921
922 case y of
923 0 -> divZeroError
924 -1 -> 2 `primQuot` y
925 _ -> 2 `primQuot` y
926
927 which simplifies to:
928
929 case y of
930 0 -> divZeroError
931 -1 -> -2
932 _ -> 2 `primQuot` y
933
934
935 However, constant denominators are more common than constant numerators,
936 so the
937 y == (-1) && x == minBound
938 order gives us better code in the common case.
939 -}