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
101 -- Internal, do not use.
109 import PreludeBuiltin
115 import PrelAddr ( Int64(..), Word64(..), Addr(..), Word(..) )
120 import Numeric ( readDec )
121 import Word ( Word32 )
123 -----------------------------------------------------------------------------
124 -- The "official" coercion functions
125 -----------------------------------------------------------------------------
127 int8ToInt :: Int8 -> Int
128 int16ToInt :: Int16 -> Int
129 int32ToInt :: Int32 -> Int
131 int8ToInt# :: Int8 -> Int#
132 int16ToInt# :: Int16 -> Int#
133 int32ToInt# :: Int32 -> Int#
135 intToInt8 :: Int -> Int8
136 intToInt16 :: Int -> Int16
137 intToInt32 :: Int -> Int32
139 int8ToInt16 :: Int8 -> Int16
140 int8ToInt32 :: Int8 -> Int32
142 int16ToInt8 :: Int16 -> Int8
143 int16ToInt32 :: Int16 -> Int32
145 int32ToInt8 :: Int32 -> Int8
146 int32ToInt16 :: Int32 -> Int16
148 int8ToInt16 (I8# x) = I16# x
149 int8ToInt32 (I8# x) = I32# x
150 int8ToInt64 = int32ToInt64 . int8ToInt32
152 int16ToInt8 (I16# x) = I8# x
153 int16ToInt32 (I16# x) = I32# x
154 int16ToInt64 = int32ToInt64 . int16ToInt32
156 int32ToInt8 (I32# x) = I8# x
157 int32ToInt16 (I32# x) = I16# x
160 intToWord :: Int -> Word
161 intToWord (I# i#) = W# (int2Word# i#)
164 \subsection[Int8]{The @Int8@ interface}
168 instance CCallable Int8
169 instance CReturnable Int8
171 int8ToInt (I8# x) = I# (i8ToInt# x)
172 int8ToInt# (I8# x) = 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#)
179 -- This doesn't perform any bounds checking
180 -- on the value it is passed, nor its sign.
181 -- i.e., show (intToInt8 511) => "-1"
183 intToInt8 (I# x) = I8# (intToInt8# x)
185 intToInt8# :: Int# -> Int#
186 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
188 instance Eq Int8 where
189 (I8# x#) == (I8# y#) = x# ==# y#
190 (I8# x#) /= (I8# y#) = x# /=# y#
192 instance Ord Int8 where
193 compare (I8# x#) (I8# y#) = compareInt# (i8ToInt# x#) (i8ToInt# y#)
195 compareInt# :: Int# -> Int# -> Ordering
201 instance Num Int8 where
202 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
203 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
204 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
208 else I8# (0x100# -# x#)
212 fromInteger (S# i#) = I8# (intToInt8# i#)
213 fromInteger (J# s# d#) = I8# (intToInt8# (integer2Int# s# d#))
216 instance Bounded Int8 where
220 instance Real Int8 where
221 toRational x = toInteger x % 1
223 instance Integral Int8 where
225 | x > 0 && y < 0 = quotInt8 (x-y-1) y
226 | x < 0 && y > 0 = quotInt8 (x-y+1) y
227 | otherwise = quotInt8 x y
229 quot x@(I8# _) y@(I8# y#)
230 | y# /=# 0# = x `quotInt8` y
231 | otherwise = divZeroError "quot{Int8}" x
232 rem x@(I8# _) y@(I8# y#)
233 | y# /=# 0# = x `remInt8` y
234 | otherwise = divZeroError "rem{Int8}" x
236 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
238 where r = remInt8 x y
240 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
241 toInteger i8 = toInteger (int8ToInt i8)
242 toInt i8 = int8ToInt i8
244 remInt8, quotInt8 :: Int8 -> Int8 -> Int8
245 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((i8ToInt# x) `remInt#` (i8ToInt# y)))
246 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((i8ToInt# x) `quotInt#` (i8ToInt# y)))
248 instance Ix Int8 where
251 | inRange b i = int8ToInt (i - m)
252 | otherwise = indexError i b "Int8"
253 inRange (m,n) i = m <= i && i <= n
255 instance Enum Int8 where
257 | i == maxBound = succError "Int8"
260 | i == minBound = predError "Int8"
264 | x >= toInt (minBound::Int8) && x <= toInt (maxBound::Int8)
267 = toEnumError "Int8" x (minBound::Int8,maxBound::Int8)
270 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int8)]
272 map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int8)]
276 | otherwise = maxBound
278 instance Read Int8 where
279 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
281 instance Show Int8 where
282 showsPrec p i8 = showsPrec p (int8ToInt i8)
284 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
285 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
287 instance Bits Int8 where
288 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
289 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
290 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
291 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
292 shift (I8# x) i@(I# i#)
293 | i > 0 = I8# (intToInt8# (iShiftL# (i8ToInt# x) i#))
294 | otherwise = I8# (intToInt8# (iShiftRA# (i8ToInt# x) (negateInt# i#)))
295 i8@(I8# x) `rotate` (I# i)
298 I8# (intToInt8# ( word2Int# (
299 (int2Word# (iShiftL# (i8ToInt# x) i'))
301 (int2Word# (iShiftRA# (word2Int# (
303 (int2Word# (0x100# -# pow2# i2))))
305 | otherwise = rotate i8 (I# (8# +# i))
307 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
310 setBit x i = x .|. bit i
311 clearBit x i = x .&. complement (bit i)
312 complementBit x i = x `xor` bit i
313 testBit x i = (x .&. bit i) /= 0
317 pow2# :: Int# -> Int#
318 pow2# x# = iShiftL# 1# x#
320 pow2_64# :: Int# -> Int64#
321 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
327 \subsection[Int16]{The @Int16@ interface}
330 data Int16 = I16# Int#
331 instance CCallable Int16
332 instance CReturnable Int16
334 int16ToInt (I16# x) = I# (i16ToInt# x)
335 int16ToInt# (I16# x) = i16ToInt# x
337 i16ToInt# :: Int# -> Int#
338 i16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
339 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
341 intToInt16 (I# x) = I16# (intToInt16# x)
343 intToInt16# :: Int# -> Int#
344 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
346 instance Eq Int16 where
347 (I16# x#) == (I16# y#) = x# ==# y#
348 (I16# x#) /= (I16# y#) = x# /=# y#
350 instance Ord Int16 where
351 compare (I16# x#) (I16# y#) = compareInt# (i16ToInt# x#) (i16ToInt# y#)
353 instance Num Int16 where
354 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
355 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
356 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
360 else I16# (0x10000# -# x#)
363 fromInteger (S# i#) = I16# (intToInt16# i#)
364 fromInteger (J# s# d#) = I16# (intToInt16# (integer2Int# s# d#))
367 instance Bounded Int16 where
371 instance Real Int16 where
372 toRational x = toInteger x % 1
374 instance Integral Int16 where
376 | x > 0 && y < 0 = quotInt16 (x-y-1) y
377 | x < 0 && y > 0 = quotInt16 (x-y+1) y
378 | otherwise = quotInt16 x y
380 quot x@(I16# _) y@(I16# y#)
381 | y# /=# 0# = x `quotInt16` y
382 | otherwise = divZeroError "quot{Int16}" x
383 rem x@(I16# _) y@(I16# y#)
384 | y# /=# 0# = x `remInt16` y
385 | otherwise = divZeroError "rem{Int16}" x
387 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
389 where r = remInt16 x y
391 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
392 toInteger i16 = toInteger (int16ToInt i16)
393 toInt i16 = int16ToInt i16
395 remInt16, quotInt16 :: Int16 -> Int16 -> Int16
396 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((i16ToInt# x) `remInt#` (i16ToInt# y)))
397 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((i16ToInt# x) `quotInt#` (i16ToInt# y)))
399 instance Ix Int16 where
402 | inRange b i = int16ToInt (i - m)
403 | otherwise = indexError i b "Int16"
404 inRange (m,n) i = m <= i && i <= n
406 instance Enum Int16 where
408 | i == maxBound = succError "Int16"
412 | i == minBound = predError "Int16"
416 | x >= toInt (minBound::Int16) && x <= toInt (maxBound::Int16)
419 = toEnumError "Int16" x (minBound::Int16, maxBound::Int16)
421 fromEnum = int16ToInt
423 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int16)]
424 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int16)]
427 | otherwise = maxBound
429 instance Read Int16 where
430 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
432 instance Show Int16 where
433 showsPrec p i16 = showsPrec p (int16ToInt i16)
435 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
436 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
438 instance Bits Int16 where
439 (I16# x) .&. (I16# y) = I16# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
440 (I16# x) .|. (I16# y) = I16# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
441 (I16# x) `xor` (I16# y) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
442 complement (I16# x) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffff#)))
443 shift (I16# x) i@(I# i#)
444 | i > 0 = I16# (intToInt16# (iShiftL# (i16ToInt# x) i#))
445 | otherwise = I16# (intToInt16# (iShiftRA# (i16ToInt# x) (negateInt# i#)))
446 i16@(I16# x) `rotate` (I# i)
449 I16# (intToInt16# (word2Int# (
450 (int2Word# (iShiftL# (i16ToInt# x) i'))
452 (int2Word# (iShiftRA# ( word2Int# (
453 (int2Word# x) `and#` (int2Word# (0x100# -# pow2# i2))))
455 | otherwise = rotate i16 (I# (16# +# i))
457 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
460 setBit x i = x .|. bit i
461 clearBit x i = x .&. complement (bit i)
462 complementBit x i = x `xor` bit i
463 testBit x i = (x .&. bit i) /= 0
467 sizeofInt16 :: Word32
473 \subsection[Int32]{The @Int32@ interface}
478 data Int32 = I32# Int#
479 instance CCallable Int32
480 instance CReturnable Int32
482 int32ToInt (I32# x) = I# (i32ToInt# x)
483 int32ToInt# (I32# x) = i32ToInt# x
485 i32ToInt# :: Int# -> Int#
486 #if WORD_SIZE_IN_BYTES > 4
487 i32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
488 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
493 intToInt32 (I# x) = I32# (intToInt32# x)
494 intToInt32# :: Int# -> Int#
495 #if WORD_SIZE_IN_BYTES > 4
496 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
501 instance Eq Int32 where
502 (I32# x#) == (I32# y#) = x# ==# y#
503 (I32# x#) /= (I32# y#) = x# /=# y#
505 instance Ord Int32 where
506 compare (I32# x#) (I32# y#) = compareInt# (i32ToInt# x#) (i32ToInt# y#)
508 instance Num Int32 where
509 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
510 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
511 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
512 #if WORD_SIZE_IN_BYTES > 4
516 else I32# (intToInt32# (0x100000000# -# x'))
518 negate (I32# x) = I32# (negateInt# x)
522 fromInteger (S# i#) = I32# (intToInt32# i#)
523 fromInteger (J# s# d#) = I32# (intToInt32# (integer2Int# s# d#))
526 instance Bounded Int32 where
527 minBound = fromInt minBound
528 maxBound = fromInt maxBound
530 instance Real Int32 where
531 toRational x = toInteger x % 1
533 instance Integral Int32 where
535 | x > 0 && y < 0 = quotInt32 (x-y-1) y
536 | x < 0 && y > 0 = quotInt32 (x-y+1) y
537 | otherwise = quotInt32 x y
538 quot x@(I32# _) y@(I32# y#)
539 | y# /=# 0# = x `quotInt32` y
540 | otherwise = divZeroError "quot{Int32}" x
541 rem x@(I32# _) y@(I32# y#)
542 | y# /=# 0# = x `remInt32` y
543 | otherwise = divZeroError "rem{Int32}" x
545 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
547 where r = remInt32 x y
549 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
550 toInteger i32 = toInteger (int32ToInt i32)
551 toInt i32 = int32ToInt i32
553 remInt32, quotInt32 :: Int32 -> Int32 -> Int32
554 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((i32ToInt# x) `remInt#` (i32ToInt# y)))
555 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((i32ToInt# x) `quotInt#` (i32ToInt# y)))
557 instance Ix Int32 where
560 | inRange b i = int32ToInt (i - m)
561 | otherwise = indexError i b "Int32"
562 inRange (m,n) i = m <= i && i <= n
564 instance Enum Int32 where
566 | i == maxBound = succError "Int32"
570 | i == minBound = predError "Int32"
574 -- with Int having the same range as Int32, the following test
575 -- shouldn't fail. However, having it here
576 | x >= toInt (minBound::Int32) && x <= toInt (maxBound::Int32)
579 = toEnumError "Int32" x (minBound::Int32, maxBound::Int32)
581 fromEnum = int32ToInt
583 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int32)]
584 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int32)]
588 | otherwise = maxBound
590 instance Read Int32 where
591 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
593 instance Show Int32 where
594 showsPrec p i32 = showsPrec p (int32ToInt i32)
596 instance Bits Int32 where
597 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
598 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
599 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
600 #if WORD_SIZE_IN_BYTES > 4
601 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
603 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
605 shift (I32# x) i@(I# i#)
606 | i > 0 = I32# (intToInt32# (iShiftL# (i32ToInt# x) i#))
607 | otherwise = I32# (intToInt32# (iShiftRA# (i32ToInt# x) (negateInt# i#)))
608 i32@(I32# x) `rotate` (I# i)
611 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
612 I32# (intToInt32# ( word2Int# (
613 (int2Word# (iShiftL# (i32ToInt# x) i'))
615 (int2Word# (iShiftRA# (word2Int# (
618 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
620 | otherwise = rotate i32 (I# (32# +# i))
622 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
624 (I32# maxBound#) = maxBound
626 setBit x i = x .|. bit i
627 clearBit x i = x .&. complement (bit i)
628 complementBit x i = x `xor` bit i
629 testBit x i = (x .&. bit i) /= 0
633 sizeofInt32 :: Word32
637 \subsection[Int64]{The @Int64@ interface}
641 #if WORD_SIZE_IN_BYTES == 8
642 --data Int64 = I64# Int#
644 int32ToInt64 :: Int32 -> Int64
645 int32ToInt64 (I32# i#) = I64# i#
647 intToInt32# :: Int# -> Int#
648 intToInt32# i# = word2Int# ((int2Word# i#) `and#` (case (maxBound::Word32) of W# x# -> x#))
650 int64ToInt32 :: Int64 -> Int32
651 int64ToInt32 (I64# i#) = I32# (intToInt32# w#)
653 instance Eq Int64 where
654 (I64# x) == (I64# y) = x `eqInt#` y
655 (I64# x) /= (I64# y) = x `neInt#` y
657 instance Ord Int32 where
658 compare (I64# x#) (I64# y#) = compareInt# x# y#
660 instance Num Int64 where
661 (I64# x) + (I64# y) = I64# (x +# y)
662 (I64# x) - (I64# y) = I64# (x -# y)
663 (I64# x) * (I64# y) = I64# (x *# y)
664 negate w@(I64# x) = I64# (negateInt# x)
667 fromInteger (S# i#) = I64# i#
668 fromInteger (J# s# d#) = I64# (integer2Int# s# d#)
671 instance Bounded Int64 where
672 minBound = integerToInt64 (-0x8000000000000000)
673 maxBound = integerToInt64 0x7fffffffffffffff
675 instance Integral Int64 where
677 | x > 0 && y < 0 = quotInt64 (x-y-1) y
678 | x < 0 && y > 0 = quotInt64 (x-y+1) y
679 | otherwise = quotInt64 x y
681 quot x@(I64# _) y@(I64# y#)
682 | y# /=# 0# = x `quotInt64` y
683 | otherwise = divZeroError "quot{Int64}" x
685 rem x@(I64# _) y@(I64# y#)
686 | y# /=# 0# = x `remInt64` y
687 | otherwise = divZeroError "rem{Int64}" x
690 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
692 where r = remInt64 x y
694 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
695 toInteger (I64# i#) = toInteger (I# i#)
696 toInt (I64# i#) = I# i#
698 instance Bits Int64 where
699 (I64# x) .&. (I64# y) = I64# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
700 (I64# x) .|. (I64# y) = I64# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
701 (I64# x) `xor` (I64# y) = I64# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
702 complement (I64# x) = I64# (negateInt# x)
703 shift (I64# x) i@(I# i#)
704 | i > 0 = I64# (iShiftL# x i#)
705 | otherwise = I64# (iShiftRA# x (negateInt# i#))
706 i64@(I64# x) `rotate` (I# i)
709 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
711 (int2Word# (iShiftL# x i'))
713 (int2Word# (iShiftRA# (word2Int# (
716 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
718 | otherwise = rotate i64 (I# (64# +# i))
720 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
722 (I64# maxBound#) = maxBound
724 setBit x i = x .|. bit i
725 clearBit x i = x .&. complement (bit i)
726 complementBit x i = x `xor` bit i
727 testBit x i = (x .&. bit i) /= 0
733 remInt64 (I64# x) (I64# y) = I64# (x `remInt#` y)
734 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt#` y)
736 int64ToInteger :: Int64 -> Integer
737 int64ToInteger (I64# i#) = toInteger (I# i#)
739 integerToInt64 :: Integer -> Int64
740 integerToInt64 i = case fromInteger i of { I# i# -> I64# i# }
742 intToInt64 :: Int -> Int64
743 intToInt64 (I# i#) = I64# i#
745 int64ToInt :: Int64 -> Int
746 int64ToInt (I64# i#) = I# i#
749 --assume: support for long-longs
750 --data Int64 = I64 Int64# deriving (Eq, Ord, Bounded)
752 int32ToInt64 :: Int32 -> Int64
753 int32ToInt64 (I32# i#) = I64# (intToInt64# i#)
755 int64ToInt32 :: Int64 -> Int32
756 int64ToInt32 (I64# i#) = I32# (int64ToInt# i#)
758 int64ToInteger :: Int64 -> Integer
759 int64ToInteger (I64# x#) =
760 case int64ToInteger# x# of
761 (# s#, p# #) -> J# s# p#
763 integerToInt64 :: Integer -> Int64
764 integerToInt64 (S# i#) = I64# (intToInt64# i#)
765 integerToInt64 (J# s# d#) = I64# (integerToInt64# s# d#)
767 instance Eq Int64 where
768 (I64# x) == (I64# y) = x `eqInt64#` y
769 (I64# x) /= (I64# y) = x `neInt64#` y
771 instance Ord Int64 where
772 compare (I64# x) (I64# y) = compareInt64# x y
773 (<) (I64# x) (I64# y) = x `ltInt64#` y
774 (<=) (I64# x) (I64# y) = x `leInt64#` y
775 (>=) (I64# x) (I64# y) = x `geInt64#` y
776 (>) (I64# x) (I64# y) = x `gtInt64#` y
777 max x@(I64# x#) y@(I64# y#) =
778 case (compareInt64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
779 min x@(I64# x#) y@(I64# y#) =
780 case (compareInt64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
782 instance Num Int64 where
783 (I64# x) + (I64# y) = I64# (x `plusInt64#` y)
784 (I64# x) - (I64# y) = I64# (x `minusInt64#` y)
785 (I64# x) * (I64# y) = I64# (x `timesInt64#` y)
786 negate (I64# x) = I64# (negateInt64# x)
789 fromInteger i = integerToInt64 i
790 fromInt i = intToInt64 i
792 compareInt64# :: Int64# -> Int64# -> Ordering
794 | i# `ltInt64#` j# = LT
795 | i# `eqInt64#` j# = EQ
798 instance Bounded Int64 where
799 minBound = integerToInt64 (-0x8000000000000000)
800 maxBound = integerToInt64 0x7fffffffffffffff
802 instance Integral Int64 where
804 | x > 0 && y < 0 = quotInt64 (x-y-1) y
805 | x < 0 && y > 0 = quotInt64 (x-y+1) y
806 | otherwise = quotInt64 x y
808 quot x@(I64# _) y@(I64# y#)
809 | y# `neInt64#` (intToInt64# 0#) = x `quotInt64` y
810 | otherwise = divZeroError "quot{Int64}" x
812 rem x@(I64# _) y@(I64# y#)
813 | y# `neInt64#` (intToInt64# 0#) = x `remInt64` y
814 | otherwise = divZeroError "rem{Int64}" x
817 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
819 where r = remInt64 x y
821 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
822 toInteger i = int64ToInteger i
823 toInt i = int64ToInt i
825 instance Bits Int64 where
826 (I64# x) .&. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `and64#` (int64ToWord64# y)))
827 (I64# x) .|. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `or64#` (int64ToWord64# y)))
828 (I64# x) `xor` (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `xor64#` (int64ToWord64# y)))
829 complement (I64# x) = I64# (negateInt64# x)
830 shift (I64# x) i@(I# i#)
831 | i > 0 = I64# (iShiftL64# x i#)
832 | otherwise = I64# (iShiftRA64# x (negateInt# i#))
833 i64@(I64# x) `rotate` (I# i)
836 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
837 I64# (word64ToInt64# (
838 (int64ToWord64# (iShiftL64# x i')) `or64#`
839 (int64ToWord64# (iShiftRA64# (word64ToInt64# ((int64ToWord64# x) `and64#`
840 (int64ToWord64# (maxBound# `minusInt64#` (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
842 | otherwise = rotate i64 (I# (64# +# i))
844 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
846 (I64# maxBound#) = maxBound
848 setBit x i = x .|. bit i
849 clearBit x i = x .&. complement (bit i)
850 complementBit x i = x `xor` bit i
851 testBit x i = (x .&. bit i) /= 0
855 remInt64, quotInt64 :: Int64 -> Int64 -> Int64
856 remInt64 (I64# x) (I64# y) = I64# (x `remInt64#` y)
857 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt64#` y)
859 intToInt64 :: Int -> Int64
860 intToInt64 (I# i#) = I64# (intToInt64# i#)
862 int64ToInt :: Int64 -> Int
863 int64ToInt (I64# i#) = I# (int64ToInt# i#)
865 -- Word64# primop wrappers:
867 ltInt64# :: Int64# -> Int64# -> Bool
868 ltInt64# x# y# = unsafePerformIO $ do
869 v <- _ccall_ stg_ltInt64 x# y#
874 leInt64# :: Int64# -> Int64# -> Bool
875 leInt64# x# y# = unsafePerformIO $ do
876 v <- _ccall_ stg_leInt64 x# y#
881 eqInt64# :: Int64# -> Int64# -> Bool
882 eqInt64# x# y# = unsafePerformIO $ do
883 v <- _ccall_ stg_eqInt64 x# y#
888 neInt64# :: Int64# -> Int64# -> Bool
889 neInt64# x# y# = unsafePerformIO $ do
890 v <- _ccall_ stg_neInt64 x# y#
895 geInt64# :: Int64# -> Int64# -> Bool
896 geInt64# x# y# = unsafePerformIO $ do
897 v <- _ccall_ stg_geInt64 x# y#
902 gtInt64# :: Int64# -> Int64# -> Bool
903 gtInt64# x# y# = unsafePerformIO $ do
904 v <- _ccall_ stg_gtInt64 x# y#
909 plusInt64# :: Int64# -> Int64# -> Int64#
911 case (unsafePerformIO (_ccall_ stg_plusInt64 a# b#)) of
914 minusInt64# :: Int64# -> Int64# -> Int64#
916 case (unsafePerformIO (_ccall_ stg_minusInt64 a# b#)) of
919 timesInt64# :: Int64# -> Int64# -> Int64#
921 case (unsafePerformIO (_ccall_ stg_timesInt64 a# b#)) of
924 quotInt64# :: Int64# -> Int64# -> Int64#
926 case (unsafePerformIO (_ccall_ stg_quotInt64 a# b#)) of
929 remInt64# :: Int64# -> Int64# -> Int64#
931 case (unsafePerformIO (_ccall_ stg_remInt64 a# b#)) of
934 negateInt64# :: Int64# -> Int64#
936 case (unsafePerformIO (_ccall_ stg_negateInt64 a#)) of
939 and64# :: Word64# -> Word64# -> Word64#
941 case (unsafePerformIO (_ccall_ stg_and64 a# b#)) of
944 or64# :: Word64# -> Word64# -> Word64#
946 case (unsafePerformIO (_ccall_ stg_or64 a# b#)) of
949 xor64# :: Word64# -> Word64# -> Word64#
951 case (unsafePerformIO (_ccall_ stg_xor64 a# b#)) of
954 not64# :: Word64# -> Word64#
956 case (unsafePerformIO (_ccall_ stg_not64 a#)) of
959 shiftL64# :: Word64# -> Int# -> Word64#
961 case (unsafePerformIO (_ccall_ stg_shiftL64 a# b#)) of
964 iShiftL64# :: Int64# -> Int# -> Int64#
966 case (unsafePerformIO (_ccall_ stg_iShiftL64 a# b#)) of
969 iShiftRL64# :: Int64# -> Int# -> Int64#
971 case (unsafePerformIO (_ccall_ stg_iShiftRL64 a# b#)) of
974 iShiftRA64# :: Int64# -> Int# -> Int64#
976 case (unsafePerformIO (_ccall_ stg_iShiftRA64 a# b#)) of
979 shiftRL64# :: Word64# -> Int# -> Word64#
981 case (unsafePerformIO (_ccall_ stg_shifRtL64 a# b#)) of
984 int64ToInt# :: Int64# -> Int#
986 case (unsafePerformIO (_ccall_ stg_int64ToInt i64#)) of
989 wordToWord64# :: Word# -> Word64#
991 case (unsafePerformIO (_ccall_ stg_wordToWord64 w#)) of
994 word64ToInt64# :: Word64# -> Int64#
996 case (unsafePerformIO (_ccall_ stg_word64ToInt64 w#)) of
999 int64ToWord64# :: Int64# -> Word64#
1001 case (unsafePerformIO (_ccall_ stg_int64ToWord64 i#)) of
1004 intToInt64# :: Int# -> Int64#
1006 case (unsafePerformIO (_ccall_ stg_intToInt64 i#)) of
1012 -- Code that's independent of Int64 rep.
1014 instance Enum Int64 where
1016 | i == maxBound = succError "Int64"
1020 | i == minBound = predError "Int64"
1023 toEnum i = intToInt64 i
1025 | x >= intToInt64 (minBound::Int) && x <= intToInt64 (maxBound::Int)
1028 = fromEnumError "Int64" x
1030 enumFrom e1 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger (maxBound::Int64)]
1031 enumFromTo e1 e2 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger e2]
1032 enumFromThen e1 e2 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger last]
1036 | e2 < e1 = minBound
1037 | otherwise = maxBound
1039 enumFromThenTo e1 e2 e3 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger e3]
1042 instance Show Int64 where
1043 showsPrec p i64 = showsPrec p (int64ToInteger i64)
1045 instance Read Int64 where
1046 readsPrec _ s = [ (integerToInt64 x,r) | (x,r) <- readDec s ]
1049 instance Ix Int64 where
1050 range (m,n) = [m..n]
1052 | inRange b i = int64ToInt (i-m)
1053 | otherwise = indexError i b "Int64"
1054 inRange (m,n) i = m <= i && i <= n
1056 instance Real Int64 where
1057 toRational x = toInteger x % 1
1060 sizeofInt64 :: Word32
1063 int8ToInteger :: Int8 -> Integer
1064 int8ToInteger i = toInteger i
1066 int16ToInteger :: Int16 -> Integer
1067 int16ToInteger i = toInteger i
1069 int32ToInteger :: Int32 -> Integer
1070 int32ToInteger i = toInteger i
1072 int64ToInt8 :: Int64 -> Int8
1073 int64ToInt8 = int32ToInt8 . int64ToInt32
1075 int64ToInt16 :: Int64 -> Int16
1076 int64ToInt16 = int32ToInt16 . int64ToInt32
1078 integerToInt8 :: Integer -> Int8
1079 integerToInt8 = fromInteger
1081 integerToInt16 :: Integer -> Int16
1082 integerToInt16 = fromInteger
1084 integerToInt32 :: Integer -> Int32
1085 integerToInt32 = fromInteger
1091 \subsection[Int Utils]{Miscellaneous utilities}
1095 Code copied from the Prelude
1098 absReal :: (Ord a, Num a) => a -> a
1099 absReal x | x >= 0 = x
1102 signumReal :: (Ord a, Num a) => a -> a
1103 signumReal x | x == 0 = 0
1109 indexInt8OffAddr :: Addr -> Int -> Int8
1110 indexInt8OffAddr (A# a#) (I# i#) = intToInt8 (I# (ord# (indexCharOffAddr# a# i#)))
1112 indexInt16OffAddr :: Addr -> Int -> Int16
1113 indexInt16OffAddr a i =
1114 #ifdef WORDS_BIGENDIAN
1115 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1117 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1121 l = indexInt8OffAddr a byte_idx
1122 h = indexInt8OffAddr a (byte_idx+1)
1124 indexInt32OffAddr :: Addr -> Int -> Int32
1125 indexInt32OffAddr (A# a#) i = intToInt32 (I# (indexIntOffAddr# a# i'#))
1127 -- adjust index to be in Int units, not Int32 ones.
1129 #if WORD_SIZE_IN_BYTES==8
1135 indexInt64OffAddr :: Addr -> Int -> Int64
1136 indexInt64OffAddr (A# a#) (I# i#)
1137 #if WORD_SIZE_IN_BYTES==8
1138 = I64# (indexIntOffAddr# a# i#)
1140 = I64# (indexInt64OffAddr# a# i#)
1143 #ifndef __PARALLEL_HASKELL__
1145 indexInt8OffForeignObj :: ForeignObj -> Int -> Int8
1146 indexInt8OffForeignObj (ForeignObj fo#) (I# i#) = intToInt8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
1148 indexInt16OffForeignObj :: ForeignObj -> Int -> Int16
1149 indexInt16OffForeignObj fo i =
1150 # ifdef WORDS_BIGENDIAN
1151 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1153 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1157 l = indexInt8OffForeignObj fo byte_idx
1158 h = indexInt8OffForeignObj fo (byte_idx+1)
1160 indexInt32OffForeignObj :: ForeignObj -> Int -> Int32
1161 indexInt32OffForeignObj (ForeignObj fo#) i = intToInt32 (I# (indexIntOffForeignObj# fo# i'#))
1163 -- adjust index to be in Int units, not Int32 ones.
1165 # if WORD_SIZE_IN_BYTES==8
1171 indexInt64OffForeignObj :: ForeignObj -> Int -> Int64
1172 indexInt64OffForeignObj (ForeignObj fo#) (I# i#)
1173 # if WORD_SIZE_IN_BYTES==8
1174 = I64# (indexIntOffForeignObj# fo# i#)
1176 = I64# (indexInt64OffForeignObj# fo# i#)
1179 #endif /* __PARALLEL_HASKELL__ */
1182 Read words out of mutable memory:
1185 readInt8OffAddr :: Addr -> Int -> IO Int8
1186 readInt8OffAddr a i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' a i
1188 readInt16OffAddr :: Addr -> Int -> IO Int16
1189 readInt16OffAddr a i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' a i
1191 readInt32OffAddr :: Addr -> Int -> IO Int32
1192 readInt32OffAddr a i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' a i
1194 readInt64OffAddr :: Addr -> Int -> IO Int64
1195 #if WORD_SIZE_IN_BYTES==8
1196 readInt64OffAddr a i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' a i
1198 readInt64OffAddr a i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' a i
1201 #ifndef __PARALLEL_HASKELL__
1203 readInt8OffForeignObj :: ForeignObj -> Int -> IO Int8
1204 readInt8OffForeignObj fo i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' fo i
1206 readInt16OffForeignObj :: ForeignObj -> Int -> IO Int16
1207 readInt16OffForeignObj fo i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' fo i
1209 readInt32OffForeignObj :: ForeignObj -> Int -> IO Int32
1210 readInt32OffForeignObj fo i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' fo i
1212 readInt64OffForeignObj :: ForeignObj -> Int -> IO Int64
1213 # if WORD_SIZE_IN_BYTES==8
1214 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
1216 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' fo i
1219 #endif /* __PARALLEL_HASKELL__ */
1223 writeInt8OffAddr :: Addr -> Int -> Int8 -> IO ()
1224 writeInt8OffAddr a i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' a i e
1226 writeInt16OffAddr :: Addr -> Int -> Int16 -> IO ()
1227 writeInt16OffAddr a i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' a i e
1229 writeInt32OffAddr :: Addr -> Int -> Int32 -> IO ()
1230 writeInt32OffAddr a i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' a i e
1232 writeInt64OffAddr :: Addr -> Int -> Int64 -> IO ()
1233 #if WORD_SIZE_IN_BYTES==8
1234 writeInt64OffAddr a i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' a i e
1236 writeInt64OffAddr a i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' a i e
1239 #ifndef __PARALLEL_HASKELL__
1241 writeInt8OffForeignObj :: ForeignObj -> Int -> Int8 -> IO ()
1242 writeInt8OffForeignObj fo i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' fo i e
1244 writeInt16OffForeignObj :: ForeignObj -> Int -> Int16 -> IO ()
1245 writeInt16OffForeignObj fo i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' fo i e
1247 writeInt32OffForeignObj :: ForeignObj -> Int -> Int32 -> IO ()
1248 writeInt32OffForeignObj fo i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' fo i e
1250 writeInt64OffForeignObj :: ForeignObj -> Int -> Int64 -> IO ()
1251 # if WORD_SIZE_IN_BYTES==8
1252 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
1254 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' fo i e
1257 #endif /* __PARALLEL_HASKELL__ */
1265 {-# NOINLINE indexError #-}
1266 indexError :: Show a => a -> (a,a) -> String -> b
1268 = error (showString "Ix{" . showString tp . showString "}.index: Index " .
1269 showParen True (showsPrec 0 i) .
1270 showString " out of range " $
1271 showParen True (showsPrec 0 rng) "")
1274 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1275 toEnumError inst_ty tag bnds
1276 = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1277 (showParen True (showsPrec 0 tag) $
1278 " is outside of bounds " ++
1281 fromEnumError :: (Show a,Show b) => String -> a -> b
1282 fromEnumError inst_ty tag
1283 = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1284 (showParen True (showsPrec 0 tag) $
1285 " is outside of Int's bounds " ++
1286 show (minBound::Int,maxBound::Int)))
1288 succError :: String -> a
1290 = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1292 predError :: String -> a
1294 = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1296 divZeroError :: (Show a) => String -> a -> b
1298 = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")