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