2 % (c) The University of Glasgow, 2000
4 \section[PrelInt]{Module @PrelInt@}
7 {-# OPTIONS -monly-3-regs #-}
11 Int8(..), Int16(..), Int32(..), Int64(..)
13 , intToInt8 -- :: Int -> Int8
14 , intToInt16 -- :: Int -> Int16
15 , intToInt32 -- :: Int -> Int32
16 , intToInt64 -- :: Int -> Int64
18 , integerToInt8 -- :: Integer -> Int8
19 , integerToInt16 -- :: Integer -> Int16
20 , integerToInt32 -- :: Integer -> Int32
21 , integerToInt64 -- :: Integer -> Int64
23 , int8ToInt -- :: Int8 -> Int
24 , int8ToInteger -- :: Int8 -> Integer
25 , int8ToInt16 -- :: Int8 -> Int16
26 , int8ToInt32 -- :: Int8 -> Int32
27 , int8ToInt64 -- :: Int8 -> Int64
29 , int16ToInt -- :: Int16 -> Int
30 , int16ToInteger -- :: Int16 -> Integer
31 , int16ToInt8 -- :: Int16 -> Int8
32 , int16ToInt32 -- :: Int16 -> Int32
33 , int16ToInt64 -- :: Int16 -> Int64
35 , int32ToInt -- :: Int32 -> Int
36 , int32ToInteger -- :: Int32 -> Integer
37 , int32ToInt8 -- :: Int32 -> Int8
38 , int32ToInt16 -- :: Int32 -> Int16
39 , int32ToInt64 -- :: Int32 -> Int64
41 , int64ToInt -- :: Int64 -> Int
42 , int64ToInteger -- :: Int64 -> Integer
43 , int64ToInt8 -- :: Int64 -> Int8
44 , int64ToInt16 -- :: Int64 -> Int16
45 , int64ToInt32 -- :: Int64 -> Int32
48 , intToInt8#, i8ToInt#, intToInt16#, i16ToInt#, intToInt32#, i32ToInt#,
49 , intToInt64#, plusInt64#, minusInt64#, negateInt64#
61 -- ---------------------------------------------------------------------------
62 -- Coercion functions (DEPRECATED)
63 -- ---------------------------------------------------------------------------
65 intToInt8 :: Int -> Int8
66 intToInt16 :: Int -> Int16
67 intToInt32 :: Int -> Int32
68 intToInt64 :: Int -> Int64
70 integerToInt8 :: Integer -> Int8
71 integerToInt16 :: Integer -> Int16
72 integerToInt32 :: Integer -> Int32
73 integerToInt64 :: Integer -> Int64
75 int8ToInt :: Int8 -> Int
76 int8ToInteger :: Int8 -> Integer
77 int8ToInt16 :: Int8 -> Int16
78 int8ToInt32 :: Int8 -> Int32
79 int8ToInt64 :: Int8 -> Int64
81 int16ToInt :: Int16 -> Int
82 int16ToInteger :: Int16 -> Integer
83 int16ToInt8 :: Int16 -> Int8
84 int16ToInt32 :: Int16 -> Int32
85 int16ToInt64 :: Int16 -> Int64
87 int32ToInt :: Int32 -> Int
88 int32ToInteger :: Int32 -> Integer
89 int32ToInt8 :: Int32 -> Int8
90 int32ToInt16 :: Int32 -> Int16
91 int32ToInt64 :: Int32 -> Int64
93 int64ToInt :: Int64 -> Int
94 int64ToInteger :: Int64 -> Integer
95 int64ToInt8 :: Int64 -> Int8
96 int64ToInt16 :: Int64 -> Int16
97 int64ToInt32 :: Int64 -> Int32
99 integerToInt8 = fromInteger
100 integerToInt16 = fromInteger
101 integerToInt32 = fromInteger
103 int8ToInt16 = intToInt16 . int8ToInt
104 int8ToInt32 = intToInt32 . int8ToInt
105 int16ToInt32 = intToInt32 . int16ToInt
107 int16ToInt8 (I16# x) = I8# (intToInt8# x)
108 int32ToInt8 (I32# x) = I8# (intToInt8# x)
109 int32ToInt16 (I32# x) = I16# (intToInt16# x)
111 int8ToInteger = toInteger
112 int8ToInt64 = int32ToInt64 . int8ToInt32
114 int16ToInteger = toInteger
115 int16ToInt64 = int32ToInt64 . int16ToInt32
117 int32ToInteger = toInteger
119 int64ToInt8 = int32ToInt8 . int64ToInt32
120 int64ToInt16 = int32ToInt16 . int64ToInt32
122 -----------------------------------------------------------------------------
123 -- The following rules for fromIntegral remove the need to export specialized
124 -- conversion functions.
125 -----------------------------------------------------------------------------
128 "fromIntegral/Int->Int8" fromIntegral = intToInt8;
129 "fromIntegral/Int->Int16" fromIntegral = intToInt16;
130 "fromIntegral/Int->Int32" fromIntegral = intToInt32;
131 "fromIntegral/Int->Int64" fromIntegral = intToInt64;
133 "fromIntegral/Integer->Int8" fromIntegral = integerToInt8;
134 "fromIntegral/Integer->Int16" fromIntegral = integerToInt16;
135 "fromIntegral/Integer->Int32" fromIntegral = integerToInt32;
136 "fromIntegral/Integer->Int64" fromIntegral = integerToInt64;
138 "fromIntegral/Int8->Int" fromIntegral = int8ToInt;
139 "fromIntegral/Int8->Integer" fromIntegral = int8ToInteger;
140 "fromIntegral/Int8->Int16" fromIntegral = int8ToInt16;
141 "fromIntegral/Int8->Int32" fromIntegral = int8ToInt32;
142 "fromIntegral/Int8->Int64" fromIntegral = int8ToInt64;
144 "fromIntegral/Int16->Int" fromIntegral = int16ToInt;
145 "fromIntegral/Int16->Integer" fromIntegral = int16ToInteger;
146 "fromIntegral/Int16->Int8" fromIntegral = int16ToInt8;
147 "fromIntegral/Int16->Int32" fromIntegral = int16ToInt32;
148 "fromIntegral/Int16->Int64" fromIntegral = int16ToInt64;
150 "fromIntegral/Int32->Int" fromIntegral = int32ToInt;
151 "fromIntegral/Int32->Integer" fromIntegral = int32ToInteger;
152 "fromIntegral/Int32->Int8" fromIntegral = int32ToInt8;
153 "fromIntegral/Int32->Int16" fromIntegral = int32ToInt16;
154 "fromIntegral/Int32->Int64" fromIntegral = int32ToInt64;
156 "fromIntegral/Int64->Int" fromIntegral = int64ToInt;
157 "fromIntegral/Int64->Integer" fromIntegral = int64ToInteger;
158 "fromIntegral/Int64->Int8" fromIntegral = int64ToInt8;
159 "fromIntegral/Int64->Int16" fromIntegral = int64ToInt16;
160 "fromIntegral/Int64->Int32" fromIntegral = int64ToInt32
163 -- -----------------------------------------------------------------------------
165 -- -----------------------------------------------------------------------------
169 instance CCallable Int8
170 instance CReturnable Int8
172 int8ToInt (I8# x) = I# (i8ToInt# x)
174 i8ToInt# :: Int# -> Int#
175 i8ToInt# x = if x' <=# 0x7f# then x' else x' -# 0x100#
176 where x' = word2Int# (int2Word# x `and#` int2Word# 0xff#)
178 -- This doesn't perform any bounds checking on the value it is passed,
179 -- nor its sign, i.e., show (intToInt8 511) => "-1"
180 intToInt8 (I# x) = I8# (intToInt8# x)
182 intToInt8# :: Int# -> Int#
183 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
185 instance Eq Int8 where
186 (I8# x#) == (I8# y#) = x# ==# y#
187 (I8# x#) /= (I8# y#) = x# /=# y#
189 instance Ord Int8 where
190 compare (I8# x#) (I8# y#) = compareInt# (i8ToInt# x#) (i8ToInt# y#)
192 compareInt# :: Int# -> Int# -> Ordering
198 instance Num Int8 where
199 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
200 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
201 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
205 else I8# (0x100# -# x#)
209 fromInteger (S# i#) = I8# (intToInt8# i#)
210 fromInteger (J# s# d#) = I8# (intToInt8# (integer2Int# s# d#))
212 instance Bounded Int8 where
216 instance Real Int8 where
217 toRational x = toInteger x % 1
219 instance Integral Int8 where
221 | x > 0 && y < 0 = quotInt8 (x-y-1) y
222 | x < 0 && y > 0 = quotInt8 (x-y+1) y
223 | otherwise = quotInt8 x y
224 quot x@(I8# _) y@(I8# y#)
225 | y# /=# 0# = x `quotInt8` y
226 | otherwise = divZeroError "quot{Int8}" x
227 rem x@(I8# _) y@(I8# y#)
228 | y# /=# 0# = x `remInt8` y
229 | otherwise = divZeroError "rem{Int8}" x
231 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
233 where r = remInt8 x y
235 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
236 toInteger i8 = toInteger (int8ToInt i8)
237 toInt i8 = int8ToInt i8
240 remInt8, quotInt8 :: Int8 -> Int8 -> Int8
241 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((i8ToInt# x) `remInt#` (i8ToInt# y)))
242 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((i8ToInt# x) `quotInt#` (i8ToInt# y)))
244 instance Ix Int8 where
247 | inRange b i = int8ToInt (i - m)
248 | otherwise = indexError b i "Int8"
249 inRange (m,n) i = m <= i && i <= n
251 instance Enum Int8 where
253 | i == maxBound = succError "Int8"
256 | i == minBound = predError "Int8"
260 | x >= toInt (minBound::Int8) && x <= toInt (maxBound::Int8)
263 = toEnumError "Int8" x (minBound::Int8,maxBound::Int8)
266 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int8)]
268 map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int8)]
272 | otherwise = maxBound
274 instance Read Int8 where
275 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
277 instance Show Int8 where
278 showsPrec p i8 = showsPrec p (int8ToInt i8)
280 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
281 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
283 instance Bits Int8 where
284 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
285 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
286 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
287 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
288 shift (I8# x) i@(I# i#)
289 | i > 0 = I8# (intToInt8# (iShiftL# (i8ToInt# x) i#))
290 | otherwise = I8# (intToInt8# (iShiftRA# (i8ToInt# x) (negateInt# i#)))
291 i8@(I8# x) `rotate` (I# i)
294 I8# (intToInt8# ( word2Int# (
295 (int2Word# (iShiftL# (i8ToInt# x) i'))
297 (int2Word# (iShiftRA# (word2Int# (
299 (int2Word# (0x100# -# pow2# i2))))
301 | otherwise = rotate i8 (I# (8# +# i))
303 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
308 pow2# :: Int# -> Int#
309 pow2# x# = iShiftL# 1# x#
311 pow2_64# :: Int# -> Int64#
312 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
314 -- -----------------------------------------------------------------------------
316 -- -----------------------------------------------------------------------------
318 data Int16 = I16# Int#
320 instance CCallable Int16
321 instance CReturnable Int16
323 int16ToInt (I16# x) = I# (i16ToInt# x)
325 i16ToInt# :: Int# -> Int#
326 i16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
327 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
329 -- This doesn't perform any bounds checking on the value it is passed,
330 -- nor its sign, i.e., show (intToInt8 131071) => "-1"
331 intToInt16 (I# x) = I16# (intToInt16# x)
333 intToInt16# :: Int# -> Int#
334 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
336 instance Eq Int16 where
337 (I16# x#) == (I16# y#) = x# ==# y#
338 (I16# x#) /= (I16# y#) = x# /=# y#
340 instance Ord Int16 where
341 compare (I16# x#) (I16# y#) = compareInt# (i16ToInt# x#) (i16ToInt# y#)
343 instance Num Int16 where
344 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
345 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
346 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
350 else I16# (0x10000# -# x#)
353 fromInteger (S# i#) = I16# (intToInt16# i#)
354 fromInteger (J# s# d#) = I16# (intToInt16# (integer2Int# s# d#))
356 instance Bounded Int16 where
360 instance Real Int16 where
361 toRational x = toInteger x % 1
363 instance Integral Int16 where
365 | x > 0 && y < 0 = quotInt16 (x-y-1) y
366 | x < 0 && y > 0 = quotInt16 (x-y+1) y
367 | otherwise = quotInt16 x y
368 quot x@(I16# _) y@(I16# y#)
369 | y# /=# 0# = x `quotInt16` y
370 | otherwise = divZeroError "quot{Int16}" x
371 rem x@(I16# _) y@(I16# y#)
372 | y# /=# 0# = x `remInt16` y
373 | otherwise = divZeroError "rem{Int16}" x
375 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
377 where r = remInt16 x y
379 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
380 toInteger i16 = toInteger (int16ToInt i16)
381 toInt i16 = int16ToInt i16
383 remInt16, quotInt16 :: Int16 -> Int16 -> Int16
384 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((i16ToInt# x) `remInt#` (i16ToInt# y)))
385 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((i16ToInt# x) `quotInt#` (i16ToInt# y)))
387 instance Ix Int16 where
390 | inRange b i = int16ToInt (i - m)
391 | otherwise = indexError b i "Int16"
392 inRange (m,n) i = m <= i && i <= n
394 instance Enum Int16 where
396 | i == maxBound = succError "Int16"
400 | i == minBound = predError "Int16"
404 | x >= toInt (minBound::Int16) && x <= toInt (maxBound::Int16)
407 = toEnumError "Int16" x (minBound::Int16, maxBound::Int16)
409 fromEnum = int16ToInt
411 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int16)]
412 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int16)]
415 | otherwise = maxBound
417 instance Read Int16 where
418 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
420 instance Show Int16 where
421 showsPrec p i16 = showsPrec p (int16ToInt i16)
424 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
425 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
427 instance Bits Int16 where
428 (I16# x) .&. (I16# y) = I16# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
429 (I16# x) .|. (I16# y) = I16# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
430 (I16# x) `xor` (I16# y) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
431 complement (I16# x) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffff#)))
432 shift (I16# x) i@(I# i#)
433 | i > 0 = I16# (intToInt16# (iShiftL# (i16ToInt# x) i#))
434 | otherwise = I16# (intToInt16# (iShiftRA# (i16ToInt# x) (negateInt# i#)))
435 i16@(I16# x) `rotate` (I# i)
438 I16# (intToInt16# (word2Int# (
439 (int2Word# (iShiftL# (i16ToInt# x) i'))
441 (int2Word# (iShiftRA# ( word2Int# (
442 (int2Word# x) `and#` (int2Word# (0x100# -# pow2# i2))))
444 | otherwise = rotate i16 (I# (16# +# i))
446 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
451 -- -----------------------------------------------------------------------------
453 -- -----------------------------------------------------------------------------
455 data Int32 = I32# Int#
457 instance CCallable Int32
458 instance CReturnable Int32
460 int32ToInt (I32# x) = I# (i32ToInt# x)
462 i32ToInt# :: Int# -> Int#
463 #if WORD_SIZE_IN_BYTES > 4
464 i32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
465 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
470 intToInt32 (I# x) = I32# (intToInt32# x)
472 intToInt32# :: Int# -> Int#
473 #if WORD_SIZE_IN_BYTES > 4
474 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
479 instance Eq Int32 where
480 (I32# x#) == (I32# y#) = x# ==# y#
481 (I32# x#) /= (I32# y#) = x# /=# y#
483 instance Ord Int32 where
484 compare (I32# x#) (I32# y#) = compareInt# (i32ToInt# x#) (i32ToInt# y#)
486 instance Num Int32 where
487 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
488 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
489 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
490 #if WORD_SIZE_IN_BYTES > 4
494 else I32# (intToInt32# (0x100000000# -# x'))
496 negate (I32# x) = I32# (negateInt# x)
500 fromInteger (S# i#) = I32# (intToInt32# i#)
501 fromInteger (J# s# d#) = I32# (intToInt32# (integer2Int# s# d#))
504 instance Bounded Int32 where
505 minBound = int2Int32 minBound
506 maxBound = int2Int32 maxBound
508 int2Int32 :: Int -> Int32
509 int2Int32 (I# i#) = I32# (intToInt32# i#)
511 instance Real Int32 where
512 toRational x = toInteger x % 1
514 instance Integral Int32 where
516 | x > 0 && y < 0 = quotInt32 (x-y-1) y
517 | x < 0 && y > 0 = quotInt32 (x-y+1) y
518 | otherwise = quotInt32 x y
519 quot x@(I32# _) y@(I32# y#)
520 | y# /=# 0# = x `quotInt32` y
521 | otherwise = divZeroError "quot{Int32}" x
522 rem x@(I32# _) y@(I32# y#)
523 | y# /=# 0# = x `remInt32` y
524 | otherwise = divZeroError "rem{Int32}" x
526 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
528 where r = remInt32 x y
530 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
531 toInteger i32 = toInteger (int32ToInt i32)
532 toInt i32 = int32ToInt i32
534 remInt32, quotInt32 :: Int32 -> Int32 -> Int32
535 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((i32ToInt# x) `remInt#` (i32ToInt# y)))
536 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((i32ToInt# x) `quotInt#` (i32ToInt# y)))
538 instance Ix Int32 where
541 | inRange b i = int32ToInt (i - m)
542 | otherwise = indexError b i "Int32"
543 inRange (m,n) i = m <= i && i <= n
545 instance Enum Int32 where
547 | i == maxBound = succError "Int32"
551 | i == minBound = predError "Int32"
555 -- with Int having the same range as Int32, the following test
556 -- shouldn't fail. However, having it here
557 | x >= toInt (minBound::Int32) && x <= toInt (maxBound::Int32)
560 = toEnumError "Int32" x (minBound::Int32, maxBound::Int32)
562 fromEnum = int32ToInt
564 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int32)]
565 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int32)]
569 | otherwise = maxBound
572 instance Read Int32 where
573 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
575 instance Show Int32 where
576 showsPrec p i32 = showsPrec p (int32ToInt i32)
578 instance Bits Int32 where
579 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
580 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
581 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
582 #if WORD_SIZE_IN_BYTES > 4
583 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
585 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
587 shift (I32# x) i@(I# i#)
588 | i > 0 = I32# (intToInt32# (iShiftL# (i32ToInt# x) i#))
589 | otherwise = I32# (intToInt32# (iShiftRA# (i32ToInt# x) (negateInt# i#)))
590 i32@(I32# x) `rotate` (I# i)
593 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
594 I32# (intToInt32# ( word2Int# (
595 (int2Word# (iShiftL# (i32ToInt# x) i'))
597 (int2Word# (iShiftRA# (word2Int# (
600 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
602 | otherwise = rotate i32 (I# (32# +# i))
604 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
606 (I32# maxBound#) = maxBound
610 -- -----------------------------------------------------------------------------
612 -- -----------------------------------------------------------------------------
614 #if WORD_SIZE_IN_BYTES == 8
615 data Int64 = I64# Int#
617 int32ToInt64 (I32# i#) = I64# i#
619 intToInt32# :: Int# -> Int#
620 intToInt32# i# = word2Int# ((int2Word# i#) `and#` (case (maxBound::Word32) of W# x# -> x#))
622 int64ToInt32 (I64# i#) = I32# (intToInt32# w#)
624 instance Eq Int64 where
625 (I64# x) == (I64# y) = x `eqInt#` y
626 (I64# x) /= (I64# y) = x `neInt#` y
628 instance Ord Int32 where
629 compare (I64# x#) (I64# y#) = compareInt# x# y#
631 instance Num Int64 where
632 (I64# x) + (I64# y) = I64# (x +# y)
633 (I64# x) - (I64# y) = I64# (x -# y)
634 (I64# x) * (I64# y) = I64# (x *# y)
635 negate w@(I64# x) = I64# (negateInt# x)
638 fromInteger (S# i#) = I64# i#
639 fromInteger (J# s# d#) = I64# (integer2Int# s# d#)
641 instance Bounded Int64 where
642 minBound = integerToInt64 (-0x8000000000000000)
643 maxBound = integerToInt64 0x7fffffffffffffff
645 instance Integral Int64 where
647 | x > 0 && y < 0 = quotInt64 (x-y-1) y
648 | x < 0 && y > 0 = quotInt64 (x-y+1) y
649 | otherwise = quotInt64 x y
651 quot x@(I64# _) y@(I64# y#)
652 | y# /=# 0# = x `quotInt64` y
653 | otherwise = divZeroError "quot{Int64}" x
655 rem x@(I64# _) y@(I64# y#)
656 | y# /=# 0# = x `remInt64` y
657 | otherwise = divZeroError "rem{Int64}" x
660 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
662 where r = remInt64 x y
664 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
665 toInteger (I64# i#) = toInteger (I# i#)
666 toInt (I64# i#) = I# i#
668 remInt64 (I64# x) (I64# y) = I64# (x `remInt#` y)
669 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt#` y)
671 int64ToInteger (I64# i#) = toInteger (I# i#)
672 integerToInt64 i = case fromInteger i of { I# i# -> I64# i# }
674 intToInt64 (I# i#) = I64# i#
675 int64ToInt (I64# i#) = I# i#
678 --assume: support for long-longs
679 data Int64 = I64# Int64#
681 int32ToInt64 (I32# i#) = I64# (intToInt64# i#)
682 int64ToInt32 (I64# i#) = I32# (int64ToInt# i#)
684 int64ToInteger (I64# x#) =
685 case int64ToInteger# x# of
686 (# s#, p# #) -> J# s# p#
688 integerToInt64 (S# i#) = I64# (intToInt64# i#)
689 integerToInt64 (J# s# d#) = I64# (integerToInt64# s# d#)
691 instance Eq Int64 where
692 (I64# x) == (I64# y) = x `eqInt64#` y
693 (I64# x) /= (I64# y) = x `neInt64#` y
695 instance Ord Int64 where
696 compare (I64# x) (I64# y) = compareInt64# x y
697 (<) (I64# x) (I64# y) = x `ltInt64#` y
698 (<=) (I64# x) (I64# y) = x `leInt64#` y
699 (>=) (I64# x) (I64# y) = x `geInt64#` y
700 (>) (I64# x) (I64# y) = x `gtInt64#` y
701 max x@(I64# x#) y@(I64# y#) =
702 case (compareInt64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
703 min x@(I64# x#) y@(I64# y#) =
704 case (compareInt64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
706 instance Num Int64 where
707 (I64# x) + (I64# y) = I64# (x `plusInt64#` y)
708 (I64# x) - (I64# y) = I64# (x `minusInt64#` y)
709 (I64# x) * (I64# y) = I64# (x `timesInt64#` y)
710 negate (I64# x) = I64# (negateInt64# x)
713 fromInteger i = integerToInt64 i
715 compareInt64# :: Int64# -> Int64# -> Ordering
717 | i# `ltInt64#` j# = LT
718 | i# `eqInt64#` j# = EQ
721 instance Bounded Int64 where
722 minBound = integerToInt64 (-0x8000000000000000)
723 maxBound = integerToInt64 0x7fffffffffffffff
725 instance Integral Int64 where
727 | x > 0 && y < 0 = quotInt64 (x-y-1) y
728 | x < 0 && y > 0 = quotInt64 (x-y+1) y
729 | otherwise = quotInt64 x y
731 quot x@(I64# _) y@(I64# y#)
732 | y# `neInt64#` (intToInt64# 0#) = x `quotInt64` y
733 | otherwise = divZeroError "quot{Int64}" x
735 rem x@(I64# _) y@(I64# y#)
736 | y# `neInt64#` (intToInt64# 0#) = x `remInt64` y
737 | otherwise = divZeroError "rem{Int64}" x
740 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
742 where r = remInt64 x y
744 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
745 toInteger i = int64ToInteger i
746 toInt i = int64ToInt i
748 remInt64, quotInt64 :: Int64 -> Int64 -> Int64
749 remInt64 (I64# x) (I64# y) = I64# (x `remInt64#` y)
750 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt64#` y)
752 intToInt64 (I# i#) = I64# (intToInt64# i#)
753 int64ToInt (I64# i#) = I# (int64ToInt# i#)
755 -- Int64# primop wrappers:
757 ltInt64# :: Int64# -> Int64# -> Bool
758 ltInt64# x# y# = stg_ltInt64 x# y# /=# 0#
760 leInt64# :: Int64# -> Int64# -> Bool
761 leInt64# x# y# = stg_leInt64 x# y# /=# 0#
763 eqInt64# :: Int64# -> Int64# -> Bool
764 eqInt64# x# y# = stg_eqInt64 x# y# /=# 0#
766 neInt64# :: Int64# -> Int64# -> Bool
767 neInt64# x# y# = stg_neInt64 x# y# /=# 0#
769 geInt64# :: Int64# -> Int64# -> Bool
770 geInt64# x# y# = stg_geInt64 x# y# /=# 0#
772 gtInt64# :: Int64# -> Int64# -> Bool
773 gtInt64# x# y# = stg_gtInt64 x# y# /=# 0#
775 foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64#
776 foreign import "stg_int64ToInt" unsafe int64ToInt# :: Int64# -> Int#
777 foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64#
778 foreign import "stg_remInt64" unsafe remInt64# :: Int64# -> Int64# -> Int64#
779 foreign import "stg_quotInt64" unsafe quotInt64# :: Int64# -> Int64# -> Int64#
780 foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64#
781 foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64#
782 foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64#
783 foreign import "stg_gtInt64" unsafe stg_gtInt64 :: Int64# -> Int64# -> Int#
784 foreign import "stg_geInt64" unsafe stg_geInt64 :: Int64# -> Int64# -> Int#
785 foreign import "stg_neInt64" unsafe stg_neInt64 :: Int64# -> Int64# -> Int#
786 foreign import "stg_eqInt64" unsafe stg_eqInt64 :: Int64# -> Int64# -> Int#
787 foreign import "stg_leInt64" unsafe stg_leInt64 :: Int64# -> Int64# -> Int#
788 foreign import "stg_ltInt64" unsafe stg_ltInt64 :: Int64# -> Int64# -> Int#
793 -- Code that's independent of Int64 rep.
795 instance CCallable Int64
796 instance CReturnable Int64
798 instance Enum Int64 where
800 | i == maxBound = succError "Int64"
804 | i == minBound = predError "Int64"
807 toEnum i = intToInt64 i
809 | x >= intToInt64 (minBound::Int) && x <= intToInt64 (maxBound::Int)
812 = fromEnumError "Int64" x
814 enumFrom e1 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger (maxBound::Int64)]
815 enumFromTo e1 e2 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger e2]
816 enumFromThen e1 e2 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger last]
821 | otherwise = maxBound
823 enumFromThenTo e1 e2 e3 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger e3]
825 instance Show Int64 where
826 showsPrec p i64 = showsPrec p (int64ToInteger i64)
828 instance Read Int64 where
829 readsPrec _ s = [ (integerToInt64 x,r) | (x,r) <- readDec s ]
831 instance Ix Int64 where
834 | inRange b i = int64ToInt (i-m)
835 | otherwise = indexError b i "Int64"
836 inRange (m,n) i = m <= i && i <= n
838 instance Real Int64 where
839 toRational x = toInteger x % 1
841 #if WORD_SIZE_IN_BYTES == 8
842 instance Bits Int64 where
843 (I64# x) .&. (I64# y) = I64# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
844 (I64# x) .|. (I64# y) = I64# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
845 (I64# x) `xor` (I64# y) = I64# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
846 complement (I64# x) = I64# (negateInt# x)
847 shift (I64# x) i@(I# i#)
848 | i > 0 = I64# (iShiftL# x i#)
849 | otherwise = I64# (iShiftRA# x (negateInt# i#))
850 i64@(I64# x) `rotate` (I# i)
853 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
855 (int2Word# (iShiftL# x i'))
857 (int2Word# (iShiftRA# (word2Int# (
860 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
862 | otherwise = rotate i64 (I# (64# +# i))
864 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
866 (I64# maxBound#) = maxBound
870 #else /* WORD_SIZE_IN_BYTES != 8 */
872 instance Bits Int64 where
873 (I64# x) .&. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `and64#` (int64ToWord64# y)))
874 (I64# x) .|. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `or64#` (int64ToWord64# y)))
875 (I64# x) `xor` (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `xor64#` (int64ToWord64# y)))
876 complement (I64# x) = I64# (negateInt64# x)
877 shift (I64# x) i@(I# i#)
878 | i > 0 = I64# (iShiftL64# x i#)
879 | otherwise = I64# (iShiftRA64# x (negateInt# i#))
880 i64@(I64# x) `rotate` (I# i)
883 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
884 I64# (word64ToInt64# (
885 (int64ToWord64# (iShiftL64# x i')) `or64#`
886 (int64ToWord64# (iShiftRA64# (word64ToInt64# ((int64ToWord64# x) `and64#`
887 (int64ToWord64# (maxBound# `minusInt64#` (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
889 | otherwise = rotate i64 (I# (64# +# i))
891 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
893 (I64# maxBound#) = maxBound
897 foreign import "stg_not64" unsafe not64# :: Word64# -> Word64#
898 foreign import "stg_xor64" unsafe xor64# :: Word64# -> Word64# -> Word64#
899 foreign import "stg_or64" unsafe or64# :: Word64# -> Word64# -> Word64#
900 foreign import "stg_and64" unsafe and64# :: Word64# -> Word64# -> Word64#
901 foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> Word64#
902 foreign import "stg_iShiftRA64" unsafe iShiftRA64# :: Int64# -> Int# -> Int64#
903 foreign import "stg_iShiftRL64" unsafe iShiftRL64# :: Int64# -> Int# -> Int64#
904 foreign import "stg_iShiftL64" unsafe iShiftL64# :: Int64# -> Int# -> Int64#
905 foreign import "stg_shiftL64" unsafe shiftL64# :: Word64# -> Int# -> Word64#
909 -- ---------------------------------------------------------------------------
910 -- Miscellaneous Utilities
911 -- ---------------------------------------------------------------------------
913 absReal :: (Ord a, Num a) => a -> a
914 absReal x | x >= 0 = x
917 signumReal :: (Ord a, Num a) => a -> a
918 signumReal x | x == 0 = 0