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