2 % (c) The AQUA Project, Glasgow University, 1997-1999
5 \section[Int]{Module @Int@}
7 This code is largely copied from the Hugs library of the same name,
8 suitably hammered to use unboxed types.
19 , int8ToInt16 -- :: Int8 -> Int16
20 , int8ToInt32 -- :: Int8 -> Int32
21 , int8ToInt64 -- :: Int8 -> Int64
23 , int16ToInt8 -- :: Int16 -> Int8
24 , int16ToInt32 -- :: Int16 -> Int32
25 , int16ToInt64 -- :: Int16 -> Int64
27 , int32ToInt8 -- :: Int32 -> Int8
28 , int32ToInt16 -- :: Int32 -> Int16
29 , int32ToInt64 -- :: Int32 -> Int64
31 , int64ToInt8 -- :: Int64 -> Int8
32 , int64ToInt16 -- :: Int64 -> Int16
33 , int64ToInt32 -- :: Int64 -> Int32
35 , int8ToInt -- :: Int8 -> Int
36 , int16ToInt -- :: Int16 -> Int
37 , int32ToInt -- :: Int32 -> Int
38 , int64ToInt -- :: Int32 -> Int
40 , intToInt8 -- :: Int -> Int8
41 , intToInt16 -- :: Int -> Int16
42 , intToInt32 -- :: Int -> Int32
43 , intToInt64 -- :: Int -> Int32
45 , integerToInt8 -- :: Integer -> Int8
46 , integerToInt16 -- :: Integer -> Int16
47 , integerToInt32 -- :: Integer -> Int32
48 , integerToInt64 -- :: Integer -> Int64
50 , int8ToInteger -- :: Int8 -> Integer
51 , int16ToInteger -- :: Int16 -> Integer
52 , int32ToInteger -- :: Int32 -> Integer
53 , int64ToInteger -- :: Int64 -> Integer
55 -- plus Eq, Ord, Num, Bounded, Real, Integral, Ix, Enum, Read,
56 -- Show and Bits instances for each of Int8, Int16, Int32 and Int64
58 -- The "official" place to get these from is Addr, importing
59 -- them from Int is a non-standard thing to do.
80 -- The "official" place to get these from is Foreign
81 #ifndef __PARALLEL_HASKELL__
82 , indexInt8OffForeignObj
83 , indexInt16OffForeignObj
84 , indexInt32OffForeignObj
85 , indexInt64OffForeignObj
87 , readInt8OffForeignObj
88 , readInt16OffForeignObj
89 , readInt32OffForeignObj
90 , readInt64OffForeignObj
92 , writeInt8OffForeignObj
93 , writeInt16OffForeignObj
94 , writeInt32OffForeignObj
95 , writeInt64OffForeignObj
98 -- non-standard, GHC specific
104 import PreludeBuiltin
110 import PrelAddr ( Int64(..), Word64(..) )
115 import Numeric ( readDec )
116 import Word ( Word32 )
118 -----------------------------------------------------------------------------
119 -- The "official" coercion functions
120 -----------------------------------------------------------------------------
122 int8ToInt :: Int8 -> Int
123 int16ToInt :: Int16 -> Int
124 int32ToInt :: Int32 -> Int
126 intToInt8 :: Int -> Int8
127 intToInt16 :: Int -> Int16
128 intToInt32 :: Int -> Int32
130 int8ToInt16 :: Int8 -> Int16
131 int8ToInt32 :: Int8 -> Int32
133 int16ToInt8 :: Int16 -> Int8
134 int16ToInt32 :: Int16 -> Int32
136 int32ToInt8 :: Int32 -> Int8
137 int32ToInt16 :: Int32 -> Int16
139 int8ToInt16 (I8# x) = I16# x
140 int8ToInt32 (I8# x) = I32# x
141 int8ToInt64 = int32ToInt64 . int8ToInt32
143 int16ToInt8 (I16# x) = I8# x
144 int16ToInt32 (I16# x) = I32# x
145 int16ToInt64 = int32ToInt64 . int16ToInt32
147 int32ToInt8 (I32# x) = I8# x
148 int32ToInt16 (I32# x) = I16# x
151 intToWord :: Int -> Word
152 intToWord (I# i#) = W# (int2Word# i#)
155 \subsection[Int8]{The @Int8@ interface}
159 instance CCallable Int8
160 instance CReturnable Int8
162 int8ToInt (I8# x) = I# (int8ToInt# x)
164 int8ToInt# :: Int# -> Int#
165 int8ToInt# x = if x' <=# 0x7f# then x' else x' -# 0x100#
166 where x' = word2Int# (int2Word# x `and#` int2Word# 0xff#)
169 -- This doesn't perform any bounds checking
170 -- on the value it is passed, nor its sign.
171 -- i.e., show (intToInt8 511) => "-1"
173 intToInt8 (I# x) = I8# (intToInt8# x)
175 intToInt8# :: Int# -> Int#
176 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
178 instance Eq Int8 where
179 (I8# x#) == (I8# y#) = x# ==# y#
180 (I8# x#) /= (I8# y#) = x# /=# y#
182 instance Ord Int8 where
183 compare (I8# x#) (I8# y#) = compareInt# (int8ToInt# x#) (int8ToInt# y#)
185 compareInt# :: Int# -> Int# -> Ordering
191 instance Num Int8 where
192 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
193 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
194 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
198 else I8# (0x100# -# x#)
202 fromInteger (J# a# s# d#)
203 = case (integer2Int# a# s# d#) of { i# -> I8# (intToInt8# i#) }
206 instance Bounded Int8 where
210 instance Real Int8 where
211 toRational x = toInteger x % 1
213 instance Integral Int8 where
215 | x > 0 && y < 0 = quotInt8 (x-y-1) y
216 | x < 0 && y > 0 = quotInt8 (x-y+1) y
217 | otherwise = quotInt8 x y
219 quot x@(I8# _) y@(I8# y#)
220 | y# /=# 0# = x `quotInt8` y
221 | otherwise = divZeroError "quot{Int8}" x
222 rem x@(I8# _) y@(I8# y#)
223 | y# /=# 0# = x `remInt8` y
224 | otherwise = divZeroError "rem{Int8}" x
226 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
228 where r = remInt8 x y
230 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
231 toInteger i8 = toInteger (int8ToInt i8)
232 toInt i8 = int8ToInt i8
234 remInt8, quotInt8 :: Int8 -> Int8 -> Int8
235 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `remInt#` (int8ToInt# y)))
236 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `quotInt#` (int8ToInt# y)))
238 instance Ix Int8 where
241 | inRange b i = int8ToInt (i - m)
242 | otherwise = indexError i b "Int8"
243 inRange (m,n) i = m <= i && i <= n
245 instance Enum Int8 where
247 | i == maxBound = succError "Int8"
250 | i == minBound = predError "Int8"
254 | x >= toInt (minBound::Int8) && x <= toInt (maxBound::Int8)
257 = toEnumError "Int8" x (minBound::Int8,maxBound::Int8)
260 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int8)]
262 map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int8)]
266 | otherwise = maxBound
268 instance Read Int8 where
269 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
271 instance Show Int8 where
272 showsPrec p i8 = showsPrec p (int8ToInt i8)
274 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
275 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
277 instance Bits Int8 where
278 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
279 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
280 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
281 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
282 shift (I8# x) i@(I# i#)
283 | i > 0 = I8# (intToInt8# (iShiftL# (int8ToInt# x) i#))
284 | otherwise = I8# (intToInt8# (iShiftRA# (int8ToInt# x) (negateInt# i#)))
285 i8@(I8# x) `rotate` (I# i)
288 I8# (intToInt8# ( word2Int# (
289 (int2Word# (iShiftL# (int8ToInt# x) i'))
291 (int2Word# (iShiftRA# (word2Int# (
293 (int2Word# (0x100# -# pow2# i2))))
295 | otherwise = rotate i8 (I# (8# +# i))
297 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
300 setBit x i = x .|. bit i
301 clearBit x i = x .&. complement (bit i)
302 complementBit x i = x `xor` bit i
303 testBit x i = (x .&. bit i) /= 0
307 pow2# :: Int# -> Int#
308 pow2# x# = iShiftL# 1# x#
310 pow2_64# :: Int# -> Int64#
311 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
317 \subsection[Int16]{The @Int16@ interface}
320 data Int16 = I16# Int#
321 instance CCallable Int16
322 instance CReturnable Int16
324 int16ToInt (I16# x) = I# (int16ToInt# x)
326 int16ToInt# :: Int# -> Int#
327 int16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
328 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
330 intToInt16 (I# x) = I16# (intToInt16# x)
332 intToInt16# :: Int# -> Int#
333 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
335 instance Eq Int16 where
336 (I16# x#) == (I16# y#) = x# ==# y#
337 (I16# x#) /= (I16# y#) = x# /=# y#
339 instance Ord Int16 where
340 compare (I16# x#) (I16# y#) = compareInt# (int16ToInt# x#) (int16ToInt# y#)
342 instance Num Int16 where
343 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
344 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
345 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
349 else I16# (0x10000# -# x#)
352 fromInteger (J# a# s# d#)
353 = case (integer2Int# a# s# d#) of { i# -> I16# (intToInt16# i#) }
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
369 quot x@(I16# _) y@(I16# y#)
370 | y# /=# 0# = x `quotInt16` y
371 | otherwise = divZeroError "quot{Int16}" x
372 rem x@(I16# _) y@(I16# y#)
373 | y# /=# 0# = x `remInt16` y
374 | otherwise = divZeroError "rem{Int16}" x
376 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
378 where r = remInt16 x y
380 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
381 toInteger i16 = toInteger (int16ToInt i16)
382 toInt i16 = int16ToInt i16
384 remInt16, quotInt16 :: Int16 -> Int16 -> Int16
385 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `remInt#` (int16ToInt# y)))
386 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `quotInt#` (int16ToInt# y)))
388 instance Ix Int16 where
391 | inRange b i = int16ToInt (i - m)
392 | otherwise = indexError i b "Int16"
393 inRange (m,n) i = m <= i && i <= n
395 instance Enum Int16 where
397 | i == maxBound = succError "Int16"
401 | i == minBound = predError "Int16"
405 | x >= toInt (minBound::Int16) && x <= toInt (maxBound::Int16)
408 = toEnumError "Int16" x (minBound::Int16, maxBound::Int16)
410 fromEnum = int16ToInt
412 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int16)]
413 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int16)]
416 | otherwise = maxBound
418 instance Read Int16 where
419 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
421 instance Show Int16 where
422 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# (int16ToInt# x) i#))
434 | otherwise = I16# (intToInt16# (iShiftRA# (int16ToInt# x) (negateInt# i#)))
435 i16@(I16# x) `rotate` (I# i)
438 I16# (intToInt16# (word2Int# (
439 (int2Word# (iShiftL# (int16ToInt# 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#)
449 setBit x i = x .|. bit i
450 clearBit x i = x .&. complement (bit i)
451 complementBit x i = x `xor` bit i
452 testBit x i = (x .&. bit i) /= 0
456 sizeofInt16 :: Word32
462 \subsection[Int32]{The @Int32@ interface}
467 data Int32 = I32# Int#
468 instance CCallable Int32
469 instance CReturnable Int32
471 int32ToInt (I32# x) = I# (int32ToInt# x)
473 int32ToInt# :: Int# -> Int#
474 #if WORD_SIZE_IN_BYTES > 4
475 int32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
476 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
481 intToInt32 (I# x) = I32# (intToInt32# x)
482 intToInt32# :: Int# -> Int#
483 #if WORD_SIZE_IN_BYTES > 4
484 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
489 instance Eq Int32 where
490 (I32# x#) == (I32# y#) = x# ==# y#
491 (I32# x#) /= (I32# y#) = x# /=# y#
493 instance Ord Int32 where
494 compare (I32# x#) (I32# y#) = compareInt# (int32ToInt# x#) (int32ToInt# y#)
496 instance Num Int32 where
497 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
498 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
499 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
500 #if WORD_SIZE_IN_BYTES > 4
504 else I32# (intToInt32# (0x100000000# -# x'))
506 negate (I32# x) = I32# (negateInt# x)
510 fromInteger (J# a# s# d#)
511 = case (integer2Int# a# s# d#) of { i# -> I32# (intToInt32# i#) }
514 instance Bounded Int32 where
515 minBound = fromInt minBound
516 maxBound = fromInt maxBound
518 instance Real Int32 where
519 toRational x = toInteger x % 1
521 instance Integral Int32 where
523 | x > 0 && y < 0 = quotInt32 (x-y-1) y
524 | x < 0 && y > 0 = quotInt32 (x-y+1) y
525 | otherwise = quotInt32 x y
526 quot x@(I32# _) y@(I32# y#)
527 | y# /=# 0# = x `quotInt32` y
528 | otherwise = divZeroError "quot{Int32}" x
529 rem x@(I32# _) y@(I32# y#)
530 | y# /=# 0# = x `remInt32` y
531 | otherwise = divZeroError "rem{Int32}" x
533 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
535 where r = remInt32 x y
537 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
538 toInteger i32 = toInteger (int32ToInt i32)
539 toInt i32 = int32ToInt i32
541 remInt32, quotInt32 :: Int32 -> Int32 -> Int32
542 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `remInt#` (int32ToInt# y)))
543 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `quotInt#` (int32ToInt# y)))
545 instance Ix Int32 where
548 | inRange b i = int32ToInt (i - m)
549 | otherwise = indexError i b "Int32"
550 inRange (m,n) i = m <= i && i <= n
552 instance Enum Int32 where
554 | i == maxBound = succError "Int32"
558 | i == minBound = predError "Int32"
562 -- with Int having the same range as Int32, the following test
563 -- shouldn't fail. However, having it here
564 | x >= toInt (minBound::Int32) && x <= toInt (maxBound::Int32)
567 = toEnumError "Int32" x (minBound::Int32, maxBound::Int32)
569 fromEnum = int32ToInt
571 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int32)]
572 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int32)]
576 | otherwise = maxBound
578 instance Read Int32 where
579 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
581 instance Show Int32 where
582 showsPrec p i32 = showsPrec p (int32ToInt i32)
584 instance Bits Int32 where
585 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
586 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
587 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
588 #if WORD_SIZE_IN_BYTES > 4
589 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
591 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
593 shift (I32# x) i@(I# i#)
594 | i > 0 = I32# (intToInt32# (iShiftL# (int32ToInt# x) i#))
595 | otherwise = I32# (intToInt32# (iShiftRA# (int32ToInt# x) (negateInt# i#)))
596 i32@(I32# x) `rotate` (I# i)
599 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
600 I32# (intToInt32# ( word2Int# (
601 (int2Word# (iShiftL# (int32ToInt# x) i'))
603 (int2Word# (iShiftRA# (word2Int# (
606 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
608 | otherwise = rotate i32 (I# (32# +# i))
610 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
612 (I32# maxBound#) = maxBound
614 setBit x i = x .|. bit i
615 clearBit x i = x .&. complement (bit i)
616 complementBit x i = x `xor` bit i
617 testBit x i = (x .&. bit i) /= 0
621 sizeofInt32 :: Word32
625 \subsection[Int64]{The @Int64@ interface}
629 #if WORD_SIZE_IN_BYTES == 8
630 --data Int64 = I64# Int#
632 int32ToInt64 :: Int32 -> Int64
633 int32ToInt64 (I32# i#) = I64# i#
635 intToInt32# :: Int# -> Int#
636 intToInt32# i# = word2Int# ((int2Word# i#) `and#` (case (maxBound::Word32) of W# x# -> x#))
638 int64ToInt32 :: Int64 -> Int32
639 int64ToInt32 (I64# i#) = I32# (intToInt32# w#)
641 instance Eq Int64 where
642 (I64# x) == (I64# y) = x `eqInt#` y
643 (I64# x) /= (I64# y) = x `neInt#` y
645 instance Ord Int32 where
646 compare (I64# x#) (I64# y#) = compareInt# x# y#
648 instance Num Int64 where
649 (I64# x) + (I64# y) = I64# (x +# y)
650 (I64# x) - (I64# y) = I64# (x -# y)
651 (I64# x) * (I64# y) = I64# (x *# y)
652 negate w@(I64# x) = I64# (negateInt# x)
655 fromInteger (J# a# s# d#) = case (integer2Int# a# s# d#) of { i# -> I64# i# }
658 instance Bounded Int64 where
659 minBound = integerToInt64 (-0x8000000000000000)
660 maxBound = integerToInt64 0x7fffffffffffffff
662 instance Integral Int64 where
664 | x > 0 && y < 0 = quotInt64 (x-y-1) y
665 | x < 0 && y > 0 = quotInt64 (x-y+1) y
666 | otherwise = quotInt64 x y
668 quot x@(I64# _) y@(I64# y#)
669 | y# /=# 0# = x `quotInt64` y
670 | otherwise = divZeroError "quot{Int64}" x
672 rem x@(I64# _) y@(I64# y#)
673 | y# /=# 0# = x `remInt64` y
674 | otherwise = divZeroError "rem{Int64}" x
677 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
679 where r = remInt64 x y
681 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
682 toInteger (I64# i#) = toInteger (I# i#)
683 toInt (I64# i#) = I# i#
685 instance Bits Int64 where
686 (I64# x) .&. (I64# y) = I64# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
687 (I64# x) .|. (I64# y) = I64# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
688 (I64# x) `xor` (I64# y) = I64# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
689 complement (I64# x) = I64# (negateInt# x)
690 shift (I64# x) i@(I# i#)
691 | i > 0 = I64# (iShiftL# x i#)
692 | otherwise = I64# (iShiftRA# x (negateInt# i#))
693 i64@(I64# x) `rotate` (I# i)
696 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
698 (int2Word# (iShiftL# x i'))
700 (int2Word# (iShiftRA# (word2Int# (
703 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
705 | otherwise = rotate i64 (I# (64# +# i))
707 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
709 (I64# maxBound#) = maxBound
711 setBit x i = x .|. bit i
712 clearBit x i = x .&. complement (bit i)
713 complementBit x i = x `xor` bit i
714 testBit x i = (x .&. bit i) /= 0
720 remInt64 (I64# x) (I64# y) = I64# (x `remInt#` y)
721 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt#` y)
723 int64ToInteger :: Int64 -> Integer
724 int64ToInteger (I64# i#) = toInteger (I# i#)
726 integerToInt64 :: Integer -> Int64
727 integerToInt64 i = case fromInteger i of { I# i# -> I64# i# }
729 intToInt64 :: Int -> Int64
730 intToInt64 (I# i#) = I64# i#
732 int64ToInt :: Int64 -> Int
733 int64ToInt (I64# i#) = I# i#
736 --assume: support for long-longs
737 --data Int64 = I64 Int64# deriving (Eq, Ord, Bounded)
739 int32ToInt64 :: Int32 -> Int64
740 int32ToInt64 (I32# i#) = I64# (intToInt64# i#)
742 int64ToInt32 :: Int64 -> Int32
743 int64ToInt32 (I64# i#) = I32# (int64ToInt# i#)
745 int64ToInteger :: Int64 -> Integer
746 int64ToInteger (I64# x#) =
747 case int64ToInteger# x# of
748 (# a#, s#, p# #) -> J# a# s# p#
750 integerToInt64 :: Integer -> Int64
751 integerToInt64 (J# a# s# d#) = I64# (integerToInt64# a# s# d#)
753 instance Eq Int64 where
754 (I64# x) == (I64# y) = x `eqInt64#` y
755 (I64# x) /= (I64# y) = x `neInt64#` y
757 instance Ord Int64 where
758 compare (I64# x) (I64# y) = compareInt64# x y
759 (<) (I64# x) (I64# y) = x `ltInt64#` y
760 (<=) (I64# x) (I64# y) = x `leInt64#` y
761 (>=) (I64# x) (I64# y) = x `geInt64#` y
762 (>) (I64# x) (I64# y) = x `gtInt64#` y
763 max x@(I64# x#) y@(I64# y#) =
764 case (compareInt64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
765 min x@(I64# x#) y@(I64# y#) =
766 case (compareInt64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
768 instance Num Int64 where
769 (I64# x) + (I64# y) = I64# (x `plusInt64#` y)
770 (I64# x) - (I64# y) = I64# (x `minusInt64#` y)
771 (I64# x) * (I64# y) = I64# (x `timesInt64#` y)
772 negate (I64# x) = I64# (negateInt64# x)
775 fromInteger i = integerToInt64 i
776 fromInt i = intToInt64 i
778 compareInt64# :: Int64# -> Int64# -> Ordering
780 | i# `ltInt64#` j# = LT
781 | i# `eqInt64#` j# = EQ
784 instance Bounded Int64 where
785 minBound = integerToInt64 (-0x8000000000000000)
786 maxBound = integerToInt64 0x7fffffffffffffff
788 instance Integral Int64 where
790 | x > 0 && y < 0 = quotInt64 (x-y-1) y
791 | x < 0 && y > 0 = quotInt64 (x-y+1) y
792 | otherwise = quotInt64 x y
794 quot x@(I64# _) y@(I64# y#)
795 | y# `neInt64#` (intToInt64# 0#) = x `quotInt64` y
796 | otherwise = divZeroError "quot{Int64}" x
798 rem x@(I64# _) y@(I64# y#)
799 | y# `neInt64#` (intToInt64# 0#) = x `remInt64` y
800 | otherwise = divZeroError "rem{Int64}" x
803 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
805 where r = remInt64 x y
807 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
808 toInteger i = int64ToInteger i
809 toInt i = int64ToInt i
811 instance Bits Int64 where
812 (I64# x) .&. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `and64#` (int64ToWord64# y)))
813 (I64# x) .|. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `or64#` (int64ToWord64# y)))
814 (I64# x) `xor` (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `xor64#` (int64ToWord64# y)))
815 complement (I64# x) = I64# (negateInt64# x)
816 shift (I64# x) i@(I# i#)
817 | i > 0 = I64# (iShiftL64# x i#)
818 | otherwise = I64# (iShiftRA64# x (negateInt# i#))
819 i64@(I64# x) `rotate` (I# i)
822 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
823 I64# (word64ToInt64# (
824 (int64ToWord64# (iShiftL64# x i')) `or64#`
825 (int64ToWord64# (iShiftRA64# (word64ToInt64# ((int64ToWord64# x) `and64#`
826 (int64ToWord64# (maxBound# `minusInt64#` (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
828 | otherwise = rotate i64 (I# (64# +# i))
830 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
832 (I64# maxBound#) = maxBound
834 setBit x i = x .|. bit i
835 clearBit x i = x .&. complement (bit i)
836 complementBit x i = x `xor` bit i
837 testBit x i = (x .&. bit i) /= 0
841 remInt64, quotInt64 :: Int64 -> Int64 -> Int64
842 remInt64 (I64# x) (I64# y) = I64# (x `remInt64#` y)
843 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt64#` y)
845 intToInt64 :: Int -> Int64
846 intToInt64 (I# i#) = I64# (intToInt64# i#)
848 int64ToInt :: Int64 -> Int
849 int64ToInt (I64# i#) = I# (int64ToInt# i#)
851 -- Word64# primop wrappers:
853 ltInt64# :: Int64# -> Int64# -> Bool
854 ltInt64# x# y# = unsafePerformIO $ do
855 v <- _ccall_ stg_ltInt64 x# y#
860 leInt64# :: Int64# -> Int64# -> Bool
861 leInt64# x# y# = unsafePerformIO $ do
862 v <- _ccall_ stg_leInt64 x# y#
867 eqInt64# :: Int64# -> Int64# -> Bool
868 eqInt64# x# y# = unsafePerformIO $ do
869 v <- _ccall_ stg_eqInt64 x# y#
874 neInt64# :: Int64# -> Int64# -> Bool
875 neInt64# x# y# = unsafePerformIO $ do
876 v <- _ccall_ stg_neInt64 x# y#
881 geInt64# :: Int64# -> Int64# -> Bool
882 geInt64# x# y# = unsafePerformIO $ do
883 v <- _ccall_ stg_geInt64 x# y#
888 gtInt64# :: Int64# -> Int64# -> Bool
889 gtInt64# x# y# = unsafePerformIO $ do
890 v <- _ccall_ stg_gtInt64 x# y#
895 plusInt64# :: Int64# -> Int64# -> Int64#
897 case (unsafePerformIO (_ccall_ stg_plusInt64 a# b#)) of
900 minusInt64# :: Int64# -> Int64# -> Int64#
902 case (unsafePerformIO (_ccall_ stg_minusInt64 a# b#)) of
905 timesInt64# :: Int64# -> Int64# -> Int64#
907 case (unsafePerformIO (_ccall_ stg_timesInt64 a# b#)) of
910 quotInt64# :: Int64# -> Int64# -> Int64#
912 case (unsafePerformIO (_ccall_ stg_quotInt64 a# b#)) of
915 remInt64# :: Int64# -> Int64# -> Int64#
917 case (unsafePerformIO (_ccall_ stg_remInt64 a# b#)) of
920 negateInt64# :: Int64# -> Int64#
922 case (unsafePerformIO (_ccall_ stg_negateInt64 a#)) of
925 and64# :: Word64# -> Word64# -> Word64#
927 case (unsafePerformIO (_ccall_ stg_and64 a# b#)) of
930 or64# :: Word64# -> Word64# -> Word64#
932 case (unsafePerformIO (_ccall_ stg_or64 a# b#)) of
935 xor64# :: Word64# -> Word64# -> Word64#
937 case (unsafePerformIO (_ccall_ stg_xor64 a# b#)) of
940 not64# :: Word64# -> Word64#
942 case (unsafePerformIO (_ccall_ stg_not64 a#)) of
945 shiftL64# :: Word64# -> Int# -> Word64#
947 case (unsafePerformIO (_ccall_ stg_shiftL64 a# b#)) of
950 iShiftL64# :: Int64# -> Int# -> Int64#
952 case (unsafePerformIO (_ccall_ stg_iShiftL64 a# b#)) of
955 iShiftRL64# :: Int64# -> Int# -> Int64#
957 case (unsafePerformIO (_ccall_ stg_iShiftRL64 a# b#)) of
960 iShiftRA64# :: Int64# -> Int# -> Int64#
962 case (unsafePerformIO (_ccall_ stg_iShiftRA64 a# b#)) of
965 shiftRL64# :: Word64# -> Int# -> Word64#
967 case (unsafePerformIO (_ccall_ stg_shifRtL64 a# b#)) of
970 int64ToInt# :: Int64# -> Int#
972 case (unsafePerformIO (_ccall_ stg_int64ToInt i64#)) of
975 wordToWord64# :: Word# -> Word64#
977 case (unsafePerformIO (_ccall_ stg_wordToWord64 w#)) of
980 word64ToInt64# :: Word64# -> Int64#
982 case (unsafePerformIO (_ccall_ stg_word64ToInt64 w#)) of
985 int64ToWord64# :: Int64# -> Word64#
987 case (unsafePerformIO (_ccall_ stg_int64ToWord64 i#)) of
990 intToInt64# :: Int# -> Int64#
992 case (unsafePerformIO (_ccall_ stg_intToInt64 i#)) of
998 -- Code that's independent of Int64 rep.
1000 instance Enum Int64 where
1002 | i == maxBound = succError "Int64"
1006 | i == minBound = predError "Int64"
1009 toEnum i = intToInt64 i
1011 | x >= intToInt64 (minBound::Int) && x <= intToInt64 (maxBound::Int)
1014 = fromEnumError "Int64" x
1016 enumFrom e1 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger (maxBound::Int64)]
1017 enumFromTo e1 e2 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger e2]
1018 enumFromThen e1 e2 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger last]
1022 | e2 < e1 = minBound
1023 | otherwise = maxBound
1025 enumFromThenTo e1 e2 e3 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger e3]
1028 instance Show Int64 where
1029 showsPrec p i64 = showsPrec p (int64ToInteger i64)
1031 instance Read Int64 where
1032 readsPrec _ s = [ (integerToInt64 x,r) | (x,r) <- readDec s ]
1035 instance Ix Int64 where
1036 range (m,n) = [m..n]
1038 | inRange b i = int64ToInt (i-m)
1039 | otherwise = indexError i b "Int64"
1040 inRange (m,n) i = m <= i && i <= n
1042 instance Real Int64 where
1043 toRational x = toInteger x % 1
1046 sizeofInt64 :: Word32
1049 int8ToInteger :: Int8 -> Integer
1050 int8ToInteger i = toInteger i
1052 int16ToInteger :: Int16 -> Integer
1053 int16ToInteger i = toInteger i
1055 int32ToInteger :: Int32 -> Integer
1056 int32ToInteger i = toInteger i
1058 int64ToInt8 :: Int64 -> Int8
1059 int64ToInt8 = int32ToInt8 . int64ToInt32
1061 int64ToInt16 :: Int64 -> Int16
1062 int64ToInt16 = int32ToInt16 . int64ToInt32
1064 integerToInt8 :: Integer -> Int8
1065 integerToInt8 = fromInteger
1067 integerToInt16 :: Integer -> Int16
1068 integerToInt16 = fromInteger
1070 integerToInt32 :: Integer -> Int32
1071 integerToInt32 = fromInteger
1077 \subsection[Int Utils]{Miscellaneous utilities}
1081 Code copied from the Prelude
1084 absReal :: (Ord a, Num a) => a -> a
1085 absReal x | x >= 0 = x
1088 signumReal :: (Ord a, Num a) => a -> a
1089 signumReal x | x == 0 = 0
1095 indexInt8OffAddr :: Addr -> Int -> Int8
1096 indexInt8OffAddr (A# a#) (I# i#) = intToInt8 (I# (ord# (indexCharOffAddr# a# i#)))
1098 indexInt16OffAddr :: Addr -> Int -> Int16
1099 indexInt16OffAddr a i =
1100 #ifdef WORDS_BIGENDIAN
1101 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1103 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1107 l = indexInt8OffAddr a byte_idx
1108 h = indexInt8OffAddr a (byte_idx+1)
1110 indexInt32OffAddr :: Addr -> Int -> Int32
1111 indexInt32OffAddr (A# a#) i = intToInt32 (I# (indexIntOffAddr# a# i'#))
1113 -- adjust index to be in Int units, not Int32 ones.
1115 #if WORD_SIZE_IN_BYTES==8
1121 indexInt64OffAddr :: Addr -> Int -> Int64
1122 indexInt64OffAddr (A# a#) (I# i#)
1123 #if WORD_SIZE_IN_BYTES==8
1124 = I64# (indexIntOffAddr# a# i#)
1126 = I64# (indexInt64OffAddr# a# i#)
1129 #ifndef __PARALLEL_HASKELL__
1131 indexInt8OffForeignObj :: ForeignObj -> Int -> Int8
1132 indexInt8OffForeignObj (ForeignObj fo#) (I# i#) = intToInt8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
1134 indexInt16OffForeignObj :: ForeignObj -> Int -> Int16
1135 indexInt16OffForeignObj fo i =
1136 # ifdef WORDS_BIGENDIAN
1137 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1139 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1143 l = indexInt8OffForeignObj fo byte_idx
1144 h = indexInt8OffForeignObj fo (byte_idx+1)
1146 indexInt32OffForeignObj :: ForeignObj -> Int -> Int32
1147 indexInt32OffForeignObj (ForeignObj fo#) i = intToInt32 (I# (indexIntOffForeignObj# fo# i'#))
1149 -- adjust index to be in Int units, not Int32 ones.
1151 # if WORD_SIZE_IN_BYTES==8
1157 indexInt64OffForeignObj :: ForeignObj -> Int -> Int64
1158 indexInt64OffForeignObj (ForeignObj fo#) (I# i#)
1159 # if WORD_SIZE_IN_BYTES==8
1160 = I64# (indexIntOffForeignObj# fo# i#)
1162 = I64# (indexInt64OffForeignObj# fo# i#)
1165 #endif /* __PARALLEL_HASKELL__ */
1168 Read words out of mutable memory:
1171 readInt8OffAddr :: Addr -> Int -> IO Int8
1172 readInt8OffAddr a i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' a i
1174 readInt16OffAddr :: Addr -> Int -> IO Int16
1175 readInt16OffAddr a i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' a i
1177 readInt32OffAddr :: Addr -> Int -> IO Int32
1178 readInt32OffAddr a i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' a i
1180 readInt64OffAddr :: Addr -> Int -> IO Int64
1181 #if WORD_SIZE_IN_BYTES==8
1182 readInt64OffAddr a i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' a i
1184 readInt64OffAddr a i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' a i
1187 #ifndef __PARALLEL_HASKELL__
1189 readInt8OffForeignObj :: ForeignObj -> Int -> IO Int8
1190 readInt8OffForeignObj fo i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' fo i
1192 readInt16OffForeignObj :: ForeignObj -> Int -> IO Int16
1193 readInt16OffForeignObj fo i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' fo i
1195 readInt32OffForeignObj :: ForeignObj -> Int -> IO Int32
1196 readInt32OffForeignObj fo i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' fo i
1198 readInt64OffForeignObj :: ForeignObj -> Int -> IO Int64
1199 # if WORD_SIZE_IN_BYTES==8
1200 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
1202 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' fo i
1205 #endif /* __PARALLEL_HASKELL__ */
1209 writeInt8OffAddr :: Addr -> Int -> Int8 -> IO ()
1210 writeInt8OffAddr a i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' a i e
1212 writeInt16OffAddr :: Addr -> Int -> Int16 -> IO ()
1213 writeInt16OffAddr a i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' a i e
1215 writeInt32OffAddr :: Addr -> Int -> Int32 -> IO ()
1216 writeInt32OffAddr a i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' a i e
1218 writeInt64OffAddr :: Addr -> Int -> Int64 -> IO ()
1219 #if WORD_SIZE_IN_BYTES==8
1220 writeInt64OffAddr a i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' a i e
1222 writeInt64OffAddr a i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' a i e
1225 #ifndef __PARALLEL_HASKELL__
1227 writeInt8OffForeignObj :: ForeignObj -> Int -> Int8 -> IO ()
1228 writeInt8OffForeignObj fo i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' fo i e
1230 writeInt16OffForeignObj :: ForeignObj -> Int -> Int16 -> IO ()
1231 writeInt16OffForeignObj fo i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' fo i e
1233 writeInt32OffForeignObj :: ForeignObj -> Int -> Int32 -> IO ()
1234 writeInt32OffForeignObj fo i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' fo i e
1236 writeInt64OffForeignObj :: ForeignObj -> Int -> Int64 -> IO ()
1237 # if WORD_SIZE_IN_BYTES==8
1238 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
1240 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' fo i e
1243 #endif /* __PARALLEL_HASKELL__ */
1251 {-# NOINLINE indexError #-}
1252 indexError :: Show a => a -> (a,a) -> String -> b
1254 = error (showString "Ix{" . showString tp . showString "}.index: Index " .
1255 showParen True (showsPrec 0 i) .
1256 showString " out of range " $
1257 showParen True (showsPrec 0 rng) "")
1260 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1261 toEnumError inst_ty tag bnds
1262 = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1263 (showParen True (showsPrec 0 tag) $
1264 " is outside of bounds " ++
1267 fromEnumError :: (Show a,Show b) => String -> a -> b
1268 fromEnumError inst_ty tag
1269 = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1270 (showParen True (showsPrec 0 tag) $
1271 " is outside of Int's bounds " ++
1272 show (minBound::Int,maxBound::Int)))
1274 succError :: String -> a
1276 = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1278 predError :: String -> a
1280 = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1282 divZeroError :: (Show a) => String -> a -> b
1284 = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")