2 % (c) The AQUA Project, Glasgow University, 1997-1998
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.
18 , int8ToInt -- :: Int8 -> Int
19 , intToInt8 -- :: Int -> Int8
20 , int16ToInt -- :: Int16 -> Int
21 , intToInt16 -- :: Int -> Int16
22 , int32ToInt -- :: Int32 -> Int
23 , intToInt32 -- :: Int -> Int32
25 , intToInt64 -- :: Int -> Int64
26 , int64ToInt -- :: Int64 -> Int
28 , integerToInt64 -- :: Integer -> Int64
29 , int64ToInteger -- :: Int64 -> Integer
31 -- plus Eq, Ord, Num, Bounded, Real, Integral, Ix, Enum, Read,
32 -- Show and Bits instances for each of Int8, Int16, Int32 and Int64
34 -- The "official" place to get these from is Addr, importing
35 -- them from Int is a non-standard thing to do.
56 -- The "official" place to get these from is Foreign
57 #ifndef __PARALLEL_HASKELL__
58 , indexInt8OffForeignObj
59 , indexInt16OffForeignObj
60 , indexInt32OffForeignObj
61 , indexInt64OffForeignObj
63 , readInt8OffForeignObj
64 , readInt16OffForeignObj
65 , readInt32OffForeignObj
66 , readInt64OffForeignObj
68 , writeInt8OffForeignObj
69 , writeInt16OffForeignObj
70 , writeInt32OffForeignObj
71 , writeInt64OffForeignObj
74 -- non-standard, GHC specific
86 import PrelAddr ( Int64(..), Word64(..) )
90 import Numeric ( readDec )
91 import Word ( Word32 )
93 -----------------------------------------------------------------------------
94 -- The "official" coercion functions
95 -----------------------------------------------------------------------------
97 int8ToInt :: Int8 -> Int
98 intToInt8 :: Int -> Int8
99 int16ToInt :: Int16 -> Int
100 intToInt16 :: Int -> Int16
101 int32ToInt :: Int32 -> Int
102 intToInt32 :: Int -> Int32
104 -- And some non-exported ones
106 int8ToInt16 :: Int8 -> Int16
107 int8ToInt32 :: Int8 -> Int32
108 int16ToInt8 :: Int16 -> Int8
109 int16ToInt32 :: Int16 -> Int32
110 int32ToInt8 :: Int32 -> Int8
111 int32ToInt16 :: Int32 -> Int16
113 int8ToInt16 (I8# x) = I16# x
114 int8ToInt32 (I8# x) = I32# x
115 int16ToInt8 (I16# x) = I8# x
116 int16ToInt32 (I16# x) = I32# x
117 int32ToInt8 (I32# x) = I8# x
118 int32ToInt16 (I32# x) = I16# x
121 intToWord :: Int -> Word
122 intToWord (I# i#) = W# (int2Word# i#)
125 \subsection[Int8]{The @Int8@ interface}
129 instance CCallable Int8
130 instance CReturnable Int8
132 int8ToInt (I8# x) = I# (int8ToInt# x)
133 int8ToInt# x = if x' <=# 0x7f# then x' else x' -# 0x100#
134 where x' = word2Int# (int2Word# x `and#` int2Word# 0xff#)
137 -- This doesn't perform any bounds checking
138 -- on the value it is passed, nor its sign.
139 -- i.e., show (intToInt8 511) => "-1"
141 intToInt8 (I# x) = I8# (intToInt8# x)
142 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
144 instance Eq Int8 where
145 (I8# x#) == (I8# y#) = x# ==# y#
146 (I8# x#) /= (I8# y#) = x# /=# y#
148 instance Ord Int8 where
149 compare (I8# x#) (I8# y#) = compareInt# (int8ToInt# x#) (int8ToInt# y#)
151 compareInt# :: Int# -> Int# -> Ordering
157 instance Num Int8 where
158 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
159 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
160 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
164 else I8# (0x100# -# x#)
168 fromInteger (J# a# s# d#)
169 = case (integer2Int# a# s# d#) of { i# -> I8# (intToInt8# i#) }
172 instance Bounded Int8 where
176 instance Real Int8 where
177 toRational x = toInteger x % 1
179 instance Integral Int8 where
180 div x@(I8# x#) y@(I8# y#) =
181 if x > 0 && y < 0 then quotInt8 (x-y-1) y
182 else if x < 0 && y > 0 then quotInt8 (x-y+1) y
184 quot x@(I8# _) y@(I8# y#)
185 | y# /=# 0# = x `quotInt8` y
186 | otherwise = error "Integral.Int8.quot: divide by 0\n"
187 rem x@(I8# _) y@(I8# y#)
188 | y# /=# 0# = x `remInt8` y
189 | otherwise = error "Integral.Int8.rem: divide by 0\n"
190 mod x@(I8# x#) y@(I8# y#) =
191 if x > 0 && y < 0 || x < 0 && y > 0 then
192 if r/=0 then r+y else 0
195 where r = remInt8 x y
196 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
197 toInteger i8 = toInteger (int8ToInt i8)
198 toInt i8 = int8ToInt i8
200 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `remInt#` (int8ToInt# y)))
201 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `quotInt#` (int8ToInt# y)))
203 instance Ix Int8 where
206 | inRange b i = int8ToInt (i - m)
207 | otherwise = error (showString "Ix{Int8}.index: Index " .
208 showParen True (showsPrec 0 i) .
209 showString " out of range " $
210 showParen True (showsPrec 0 b) "")
211 inRange (m,n) i = m <= i && i <= n
213 instance Enum Int8 where
216 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int8)]
217 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int8)]
218 where last = if d < c then minBound else maxBound
220 instance Read Int8 where
221 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
223 instance Show Int8 where
224 showsPrec p i8 = showsPrec p (int8ToInt i8)
226 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
227 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
229 instance Bits Int8 where
230 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
231 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
232 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
233 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
234 shift (I8# x) i@(I# i#)
235 | i > 0 = I8# (intToInt8# (iShiftL# (int8ToInt# x) i#))
236 | otherwise = I8# (intToInt8# (iShiftRA# (int8ToInt# x) (negateInt# i#)))
237 i8@(I8# x) `rotate` (I# i)
240 I8# (intToInt8# ( word2Int# (
241 (int2Word# (iShiftL# (int8ToInt# x) i'))
243 (int2Word# (iShiftRA# (word2Int# (
245 (int2Word# (0x100# -# pow2# i2))))
247 | otherwise = rotate i8 (I# (8# +# i))
249 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
252 setBit x i = x .|. bit i
253 clearBit x i = x .&. complement (bit i)
254 complementBit x i = x `xor` bit i
255 testBit x i = (x .&. bit i) /= 0
259 pow2# :: Int# -> Int#
260 pow2# x# = iShiftL# 1# x#
262 pow2_64# :: Int# -> Int64#
263 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
269 \subsection[Int16]{The @Int16@ interface}
272 data Int16 = I16# Int#
273 instance CCallable Int16
274 instance CReturnable Int16
276 int16ToInt (I16# x) = I# (int16ToInt# x)
278 int16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
279 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
281 intToInt16 (I# x) = I16# (intToInt16# x)
282 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
284 instance Eq Int16 where
285 (I16# x#) == (I16# y#) = x# ==# y#
286 (I16# x#) /= (I16# y#) = x# /=# y#
288 instance Ord Int16 where
289 compare (I16# x#) (I16# y#) = compareInt# (int16ToInt# x#) (int16ToInt# y#)
291 instance Num Int16 where
292 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
293 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
294 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
298 else I16# (0x10000# -# x#)
301 fromInteger (J# a# s# d#)
302 = case (integer2Int# a# s# d#) of { i# -> I16# (intToInt16# i#) }
305 instance Bounded Int16 where
309 instance Real Int16 where
310 toRational x = toInteger x % 1
312 instance Integral Int16 where
313 div x@(I16# x#) y@(I16# y#) =
314 if x > 0 && y < 0 then quotInt16 (x-y-1) y
315 else if x < 0 && y > 0 then quotInt16 (x-y+1) y
317 quot x@(I16# _) y@(I16# y#)
318 | y# /=# 0# = x `quotInt16` y
319 | otherwise = error "Integral.Int16.quot: divide by 0\n"
320 rem x@(I16# _) y@(I16# y#)
321 | y# /=# 0# = x `remInt16` y
322 | otherwise = error "Integral.Int16.rem: divide by 0\n"
323 mod x@(I16# x#) y@(I16# y#) =
324 if x > 0 && y < 0 || x < 0 && y > 0 then
325 if r/=0 then r+y else 0
328 where r = remInt16 x y
329 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
330 toInteger i16 = toInteger (int16ToInt i16)
331 toInt i16 = int16ToInt i16
333 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `remInt#` (int16ToInt# y)))
334 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `quotInt#` (int16ToInt# y)))
336 instance Ix Int16 where
339 | inRange b i = int16ToInt (i - m)
340 | otherwise = error (showString "Ix{Int16}.index: Index " .
341 showParen True (showsPrec 0 i) .
342 showString " out of range " $
343 showParen True (showsPrec 0 b) "")
344 inRange (m,n) i = m <= i && i <= n
346 instance Enum Int16 where
348 fromEnum = int16ToInt
349 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int16)]
350 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int16)]
351 where last = if d < c then minBound else maxBound
353 instance Read Int16 where
354 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
356 instance Show Int16 where
357 showsPrec p i16 = showsPrec p (int16ToInt i16)
359 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
360 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
362 instance Bits Int16 where
363 (I16# x) .&. (I16# y) = I16# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
364 (I16# x) .|. (I16# y) = I16# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
365 (I16# x) `xor` (I16# y) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
366 complement (I16# x) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffff#)))
367 shift (I16# x) i@(I# i#)
368 | i > 0 = I16# (intToInt16# (iShiftL# (int16ToInt# x) i#))
369 | otherwise = I16# (intToInt16# (iShiftRA# (int16ToInt# x) (negateInt# i#)))
370 i16@(I16# x) `rotate` (I# i)
373 I16# (intToInt16# (word2Int# (
374 (int2Word# (iShiftL# (int16ToInt# x) i'))
376 (int2Word# (iShiftRA# ( word2Int# (
377 (int2Word# x) `and#` (int2Word# (0x100# -# pow2# i2))))
379 | otherwise = rotate i16 (I# (16# +# i))
381 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
384 setBit x i = x .|. bit i
385 clearBit x i = x .&. complement (bit i)
386 complementBit x i = x `xor` bit i
387 testBit x i = (x .&. bit i) /= 0
391 sizeofInt16 :: Word32
397 \subsection[Int32]{The @Int32@ interface}
402 data Int32 = I32# Int#
403 instance CCallable Int32
404 instance CReturnable Int32
406 int32ToInt (I32# x) = I# (int32ToInt# x)
408 int32ToInt# :: Int# -> Int#
409 #if WORD_SIZE_IN_BYTES > 4
410 int32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
411 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
416 intToInt32 (I# x) = I32# (intToInt32# x)
417 intToInt32# :: Int# -> Int#
418 #if WORD_SIZE_IN_BYTES > 4
419 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
424 instance Eq Int32 where
425 (I32# x#) == (I32# y#) = x# ==# y#
426 (I32# x#) /= (I32# y#) = x# /=# y#
428 instance Ord Int32 where
429 compare (I32# x#) (I32# y#) = compareInt# (int32ToInt# x#) (int32ToInt# y#)
431 instance Num Int32 where
432 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
433 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
434 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
435 #if WORD_SIZE_IN_BYTES > 4
439 else I32# (intToInt32# (0x100000000# -# x'))
441 negate (I32# x) = I32# (negateInt# x)
445 fromInteger (J# a# s# d#)
446 = case (integer2Int# a# s# d#) of { i# -> I32# (intToInt32# i#) }
449 -- ToDo: remove LitLit when minBound::Int is fixed (currently it's one
450 -- too high, and doesn't allow the correct minBound to be defined here).
451 instance Bounded Int32 where
452 minBound = case ``0x80000000'' of { I# x -> I32# x }
453 maxBound = I32# 0x7fffffff#
455 instance Real Int32 where
456 toRational x = toInteger x % 1
458 instance Integral Int32 where
459 div x@(I32# x#) y@(I32# y#) =
460 if x > 0 && y < 0 then quotInt32 (x-y-1) y
461 else if x < 0 && y > 0 then quotInt32 (x-y+1) y
463 quot x@(I32# _) y@(I32# y#)
464 | y# /=# 0# = x `quotInt32` y
465 | otherwise = error "Integral.Int32.quot: divide by 0\n"
466 rem x@(I32# _) y@(I32# y#)
467 | y# /=# 0# = x `remInt32` y
468 | otherwise = error "Integral.Int32.rem: divide by 0\n"
469 mod x@(I32# x#) y@(I32# y#) =
470 if x > 0 && y < 0 || x < 0 && y > 0 then
471 if r/=0 then r+y else 0
474 where r = remInt32 x y
475 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
476 toInteger i32 = toInteger (int32ToInt i32)
477 toInt i32 = int32ToInt i32
479 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `remInt#` (int32ToInt# y)))
480 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `quotInt#` (int32ToInt# y)))
482 instance Ix Int32 where
485 | inRange b i = int32ToInt (i - m)
486 | otherwise = error (showString "Ix{Int32}.index: Index " .
487 showParen True (showsPrec 0 i) .
488 showString " out of range " $
489 showParen True (showsPrec 0 b) "")
490 inRange (m,n) i = m <= i && i <= n
492 instance Enum Int32 where
494 fromEnum = int32ToInt
495 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int32)]
496 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int32)]
497 where last = if d < c then minBound else maxBound
499 instance Read Int32 where
500 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
502 instance Show Int32 where
503 showsPrec p i32 = showsPrec p (int32ToInt i32)
505 instance Bits Int32 where
506 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
507 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
508 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
509 #if WORD_SIZE_IN_BYTES > 4
510 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
512 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
514 shift (I32# x) i@(I# i#)
515 | i > 0 = I32# (intToInt32# (iShiftL# (int32ToInt# x) i#))
516 | otherwise = I32# (intToInt32# (iShiftRA# (int32ToInt# x) (negateInt# i#)))
517 i32@(I32# x) `rotate` (I# i)
520 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
521 I32# (intToInt32# ( word2Int# (
522 (int2Word# (iShiftL# (int32ToInt# x) i'))
524 (int2Word# (iShiftRA# (word2Int# (
527 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
529 | otherwise = rotate i32 (I# (32# +# i))
531 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
533 (I32# maxBound#) = maxBound
535 setBit x i = x .|. bit i
536 clearBit x i = x .&. complement (bit i)
537 complementBit x i = x `xor` bit i
538 testBit x i = (x .&. bit i) /= 0
542 sizeofInt32 :: Word32
546 \subsection[Int64]{The @Int64@ interface}
550 #if WORD_SIZE_IN_BYTES == 8
551 --data Int64 = I64# Int#
553 int32ToInt64 :: Int32 -> Int64
554 int32ToInt64 (I32# i#) = I64# i#
556 intToInt32# :: Int# -> Int#
557 intToInt32# i# = word2Int# ((int2Word# i#) `and#` (case (maxBound::Word32) of W# x# -> x#))
559 int64ToInt32 :: Int64 -> Int32
560 int64ToInt32 (I64# i#) = I32# (intToInt32# w#)
562 instance Eq Int64 where
563 (I64# x) == (I64# y) = x `eqInt#` y
564 (I64# x) /= (I64# y) = x `neInt#` y
566 instance Ord Int32 where
567 compare (I64# x#) (I64# y#) = compareInt# x# y#
569 instance Num Int64 where
570 (I64# x) + (I64# y) = I64# (x +# y)
571 (I64# x) - (I64# y) = I64# (x -# y)
572 (I64# x) * (I64# y) = I64# (x *# y)
573 negate w@(I64# x) = I64# (negateInt# x)
576 fromInteger (J# a# s# d#) = case (integer2Int# a# s# d#) of { i# -> I64# i# }
579 instance Bounded Int64 where
580 minBound = integerToInt64 (-0x8000000000000000)
581 maxBound = integerToInt64 0x7fffffffffffffff
583 instance Real Int64 where
584 toRational x = toInteger x % 1
586 instance Integral Int64 where
587 div x@(I64# x#) y@(I64# y#)
588 | x > 0 && y < 0 = quotInt64 (x-y-1) y
589 | x < 0 && y > 0 = quotInt64 (x-y+1) y
590 | otherwise = quotInt64 x y
592 quot x@(I64# _) y@(I64# y#)
593 | y# /=# 0# = x `quotInt64` y
594 | otherwise = error "Integral.Int64.quot: divide by 0\n"
596 rem x@(I64# _) y@(I64# y#)
597 | y# /=# 0# = x `remInt64` y
598 | otherwise = error "Integral.Int32.rem: divide by 0\n"
600 mod x@(I64# x#) y@(I64# y#)
601 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
603 where r = remInt64 x y
605 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
606 toInteger (I64# i#) = toInteger (I# i#)
607 toInt (I64# i#) = I# i#
609 instance Enum Int64 where
610 toEnum (I# i) = I64# i#
611 fromEnum (I64# i) = I64# i#
612 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int64)] -- a long list!
613 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int64)]
614 where last = if d < c then minBound else maxBound
617 instance Read Int64 where
618 readsPrec p s = [ (intToInt64 x,r) | (x,r) <- readsPrec p s ]
620 instance Show Int64 where
621 showsPrec p i64 = showsPrec p (int64ToInt i64)
623 instance Bits Int64 where
624 (I64# x) .&. (I64# y) = I64# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
625 (I64# x) .|. (I64# y) = I64# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
626 (I64# x) `xor` (I64# y) = I64# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
627 complement (I64# x) = I64# (negateInt# x)
628 shift (I64# x) i@(I# i#)
629 | i > 0 = I64# (iShiftL# x i#)
630 | otherwise = I64# (iShiftRA# x (negateInt# i#))
631 i64@(I64# x) `rotate` (I# i)
634 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
636 (int2Word# (iShiftL# x i'))
638 (int2Word# (iShiftRA# (word2Int# (
641 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
643 | otherwise = rotate i64 (I# (64# +# i))
645 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
647 (I64# maxBound#) = maxBound
649 setBit x i = x .|. bit i
650 clearBit x i = x .&. complement (bit i)
651 complementBit x i = x `xor` bit i
652 testBit x i = (x .&. bit i) /= 0
658 remInt64 (I64# x) (I64# y) = I64# (x `remInt#` y)
659 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt#` y)
661 int64ToInteger :: Int64 -> Integer
662 int64ToInteger (I64# i#) = toInteger (I# i#)
664 integerToInt64 :: Integer -> Int64
665 integerToInt64 i = case fromInteger i of { I# i# -> I64# i# }
667 intToInt64 :: Int -> Int64
668 intToInt64 (I# i#) = I64# i#
670 int64ToInt :: Int64 -> Int
671 int64ToInt (I64# i#) = I# i#
674 --assume: support for long-longs
675 --data Int64 = I64 Int64# deriving (Eq, Ord, Bounded)
677 int32ToInt64 :: Int32 -> Int64
678 int32ToInt64 (I32# i#) = I64# (intToInt64# i#)
680 int64ToInt32 :: Int64 -> Int32
681 int64ToInt32 (I64# i#) = I32# (int64ToInt# i#)
683 int64ToInteger :: Int64 -> Integer
684 int64ToInteger (I64# x#) =
685 case int64ToInteger# x# of
686 (# a#, s#, p# #) -> J# a# s# p#
688 integerToInt64 :: Integer -> Int64
689 integerToInt64 (J# a# s# d#) = I64# (integerToInt64# a# s# d#)
691 instance Show Int64 where
692 showsPrec p x = showsPrec p (int64ToInteger x)
694 instance Read Int64 where
695 readsPrec p s = [ (integerToInt64 x,r) | (x,r) <- readDec s ]
697 instance Eq Int64 where
698 (I64# x) == (I64# y) = x `eqInt64#` y
699 (I64# x) /= (I64# y) = x `neInt64#` y
701 instance Ord Int64 where
702 compare (I64# x) (I64# y) = compareInt64# x y
703 (<) (I64# x) (I64# y) = x `ltInt64#` y
704 (<=) (I64# x) (I64# y) = x `leInt64#` y
705 (>=) (I64# x) (I64# y) = x `geInt64#` y
706 (>) (I64# x) (I64# y) = x `gtInt64#` y
707 max x@(I64# x#) y@(I64# y#) =
708 case (compareInt64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
709 min x@(I64# x#) y@(I64# y#) =
710 case (compareInt64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
712 instance Num Int64 where
713 (I64# x) + (I64# y) = I64# (x `plusInt64#` y)
714 (I64# x) - (I64# y) = I64# (x `minusInt64#` y)
715 (I64# x) * (I64# y) = I64# (x `timesInt64#` y)
716 negate (I64# x) = I64# (negateInt64# x)
719 fromInteger i = integerToInt64 i
720 fromInt i = intToInt64 i
723 | i# `ltInt64#` j# = LT
724 | i# `eqInt64#` j# = EQ
727 instance Bounded Int64 where
728 minBound = integerToInt64 (-0x8000000000000000)
729 maxBound = integerToInt64 0x7fffffffffffffff
731 instance Real Int64 where
732 toRational x = toInteger x % 1
734 instance Integral Int64 where
735 div x@(I64# x#) y@(I64# y#)
736 | x > 0 && y < 0 = quotInt64 (x-y-1) y
737 | x < 0 && y > 0 = quotInt64 (x-y+1) y
738 | otherwise = quotInt64 x y
740 quot x@(I64# _) y@(I64# y#)
741 | y# `neInt64#` (intToInt64# 0#) = x `quotInt64` y
742 | otherwise = error "Integral.Int64.quot: divide by 0\n"
744 rem x@(I64# _) y@(I64# y#)
745 | y# `neInt64#` (intToInt64# 0#) = x `remInt64` y
746 | otherwise = error "Integral.Int32.rem: divide by 0\n"
748 mod x@(I64# x#) y@(I64# y#)
749 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
751 where r = remInt64 x y
753 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
754 toInteger i = int64ToInteger i
755 toInt i = int64ToInt i
757 instance Enum Int64 where
758 toEnum (I# i) = I64# (intToInt64# i)
759 fromEnum (I64# w) = I# (int64ToInt# w)
760 enumFrom i = eft64 i 1
761 enumFromTo i1 i2 = eftt64 i1 1 (> i2)
762 enumFromThen i1 i2 = eftt64 i1 (i2 - i1) (>last)
765 | i1 < i2 = maxBound::Int64
766 | otherwise = minBound
769 instance Bits Int64 where
770 (I64# x) .&. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `and64#` (int64ToWord64# y)))
771 (I64# x) .|. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `or64#` (int64ToWord64# y)))
772 (I64# x) `xor` (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `xor64#` (int64ToWord64# y)))
773 complement (I64# x) = I64# (negateInt64# x)
774 shift (I64# x) i@(I# i#)
775 | i > 0 = I64# (iShiftL64# x i#)
776 | otherwise = I64# (iShiftRA64# x (negateInt# i#))
777 i64@(I64# x) `rotate` (I# i)
780 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
781 I64# (word64ToInt64# (
782 (int64ToWord64# (iShiftL64# x i')) `or64#`
783 (int64ToWord64# (iShiftRA64# (word64ToInt64# ((int64ToWord64# x) `and64#`
784 (int64ToWord64# (maxBound# `minusInt64#` (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
786 | otherwise = rotate i64 (I# (64# +# i))
788 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
790 (I64# maxBound#) = maxBound
792 setBit x i = x .|. bit i
793 clearBit x i = x .&. complement (bit i)
794 complementBit x i = x `xor` bit i
795 testBit x i = (x .&. bit i) /= 0
799 remInt64 (I64# x) (I64# y) = I64# (x `remInt64#` y)
800 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt64#` y)
802 intToInt64 :: Int -> Int64
803 intToInt64 (I# i#) = I64# (intToInt64# i#)
805 int64ToInt :: Int64 -> Int
806 int64ToInt (I64# i#) = I# (int64ToInt# i#)
808 -- Enum Int64 helper funs:
810 eftt64 :: Int64 -> Int64 -> (Int64->Bool) -> [Int64]
811 eftt64 now step done = go now
815 | otherwise = now : go (now+step)
817 eft64 :: Int64 -> Int64 -> [Int64]
818 eft64 now step = go now
821 | x == maxBound = [x]
822 | otherwise = x:go (x+step)
825 -- Word64# primop wrappers:
827 ltInt64# :: Int64# -> Int64# -> Bool
828 ltInt64# x# y# = unsafePerformIO $ do
829 v <- _ccall_ stg_ltInt64 x# y#
834 leInt64# :: Int64# -> Int64# -> Bool
835 leInt64# x# y# = unsafePerformIO $ do
836 v <- _ccall_ stg_leInt64 x# y#
841 eqInt64# :: Int64# -> Int64# -> Bool
842 eqInt64# x# y# = unsafePerformIO $ do
843 v <- _ccall_ stg_eqInt64 x# y#
848 neInt64# :: Int64# -> Int64# -> Bool
849 neInt64# x# y# = unsafePerformIO $ do
850 v <- _ccall_ stg_neInt64 x# y#
855 geInt64# :: Int64# -> Int64# -> Bool
856 geInt64# x# y# = unsafePerformIO $ do
857 v <- _ccall_ stg_geInt64 x# y#
862 gtInt64# :: Int64# -> Int64# -> Bool
863 gtInt64# x# y# = unsafePerformIO $ do
864 v <- _ccall_ stg_gtInt64 x# y#
869 plusInt64# :: Int64# -> Int64# -> Int64#
871 case (unsafePerformIO (_ccall_ stg_plusInt64 a# b#)) of
874 minusInt64# :: Int64# -> Int64# -> Int64#
876 case (unsafePerformIO (_ccall_ stg_minusInt64 a# b#)) of
879 timesInt64# :: Int64# -> Int64# -> Int64#
881 case (unsafePerformIO (_ccall_ stg_timesInt64 a# b#)) of
884 quotInt64# :: Int64# -> Int64# -> Int64#
886 case (unsafePerformIO (_ccall_ stg_quotInt64 a# b#)) of
889 remInt64# :: Int64# -> Int64# -> Int64#
891 case (unsafePerformIO (_ccall_ stg_remInt64 a# b#)) of
894 negateInt64# :: Int64# -> Int64#
896 case (unsafePerformIO (_ccall_ stg_negateInt64 a#)) of
899 and64# :: Word64# -> Word64# -> Word64#
901 case (unsafePerformIO (_ccall_ stg_and64 a# b#)) of
904 or64# :: Word64# -> Word64# -> Word64#
906 case (unsafePerformIO (_ccall_ stg_or64 a# b#)) of
909 xor64# :: Word64# -> Word64# -> Word64#
911 case (unsafePerformIO (_ccall_ stg_xor64 a# b#)) of
914 not64# :: Word64# -> Word64#
916 case (unsafePerformIO (_ccall_ stg_not64 a#)) of
919 shiftL64# :: Word64# -> Int# -> Word64#
921 case (unsafePerformIO (_ccall_ stg_shiftL64 a# b#)) of
924 iShiftL64# :: Int64# -> Int# -> Int64#
926 case (unsafePerformIO (_ccall_ stg_iShiftL64 a# b#)) of
929 iShiftRL64# :: Int64# -> Int# -> Int64#
931 case (unsafePerformIO (_ccall_ stg_iShiftRL64 a# b#)) of
934 iShiftRA64# :: Int64# -> Int# -> Int64#
936 case (unsafePerformIO (_ccall_ stg_iShiftRA64 a# b#)) of
939 shiftRL64# :: Word64# -> Int# -> Word64#
941 case (unsafePerformIO (_ccall_ stg_shifRtL64 a# b#)) of
944 int64ToInt# :: Int64# -> Int#
946 case (unsafePerformIO (_ccall_ stg_int64ToInt i#)) of
949 wordToWord64# :: Word# -> Word64#
951 case (unsafePerformIO (_ccall_ stg_wordToWord64 w#)) of
954 word64ToInt64# :: Word64# -> Int64#
956 case (unsafePerformIO (_ccall_ stg_word64ToInt64 w#)) of
959 int64ToWord64# :: Int64# -> Word64#
961 case (unsafePerformIO (_ccall_ stg_int64ToWord64 w#)) of
964 intToInt64# :: Int# -> Int64#
966 case (unsafePerformIO (_ccall_ stg_intToInt64 i#)) of
972 sizeofInt64 :: Word32
978 \subsection[Int Utils]{Miscellaneous utilities}
982 Code copied from the Prelude
985 absReal x | x >= 0 = x
988 signumReal x | x == 0 = 0
994 indexInt8OffAddr :: Addr -> Int -> Int8
995 indexInt8OffAddr (A# a#) (I# i#) = intToInt8 (I# (ord# (indexCharOffAddr# a# i#)))
997 indexInt16OffAddr :: Addr -> Int -> Int16
998 indexInt16OffAddr a i =
999 #ifdef WORDS_BIGENDIAN
1000 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1002 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1006 l = indexInt8OffAddr a byte_idx
1007 h = indexInt8OffAddr a (byte_idx+1)
1009 indexInt32OffAddr :: Addr -> Int -> Int32
1010 indexInt32OffAddr (A# a#) i = intToInt32 (I# (indexIntOffAddr# a# i'#))
1012 -- adjust index to be in Int units, not Int32 ones.
1014 #if WORD_SIZE_IN_BYTES==8
1020 indexInt64OffAddr :: Addr -> Int -> Int64
1021 indexInt64OffAddr (A# a#) (I# i#)
1022 #if WORD_SIZE_IN_BYTES==8
1023 = I64# (indexIntOffAddr# a# i#)
1025 = I64# (indexInt64OffAddr# a# i#)
1028 #ifndef __PARALLEL_HASKELL__
1030 indexInt8OffForeignObj :: ForeignObj -> Int -> Int8
1031 indexInt8OffForeignObj (ForeignObj fo#) (I# i#) = intToInt8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
1033 indexInt16OffForeignObj :: ForeignObj -> Int -> Int16
1034 indexInt16OffForeignObj fo i =
1035 # ifdef WORDS_BIGENDIAN
1036 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1038 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1042 l = indexInt8OffForeignObj fo byte_idx
1043 h = indexInt8OffForeignObj fo (byte_idx+1)
1045 indexInt32OffForeignObj :: ForeignObj -> Int -> Int32
1046 indexInt32OffForeignObj (ForeignObj fo#) i = intToInt32 (I# (indexIntOffForeignObj# fo# i'#))
1048 -- adjust index to be in Int units, not Int32 ones.
1050 # if WORD_SIZE_IN_BYTES==8
1056 indexInt64OffForeignObj :: ForeignObj -> Int -> Int64
1057 indexInt64OffForeignObj (ForeignObj fo#) (I# i#)
1058 # if WORD_SIZE_IN_BYTES==8
1059 = I64# (indexIntOffForeignObj# fo# i#)
1061 = I64# (indexInt64OffForeignObj# fo# i#)
1064 #endif /* __PARALLEL_HASKELL__ */
1067 Read words out of mutable memory:
1070 readInt8OffAddr :: Addr -> Int -> IO Int8
1071 readInt8OffAddr a i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' a i
1073 readInt16OffAddr :: Addr -> Int -> IO Int16
1074 readInt16OffAddr a i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' a i
1076 readInt32OffAddr :: Addr -> Int -> IO Int32
1077 readInt32OffAddr a i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' a i
1079 readInt64OffAddr :: Addr -> Int -> IO Int64
1080 #if WORD_SIZE_IN_BYTES==8
1081 readInt64OffAddr a i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' a i
1083 readInt64OffAddr a i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' a i
1086 #ifndef __PARALLEL_HASKELL__
1088 readInt8OffForeignObj :: ForeignObj -> Int -> IO Int8
1089 readInt8OffForeignObj fo i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' fo i
1091 readInt16OffForeignObj :: ForeignObj -> Int -> IO Int16
1092 readInt16OffForeignObj fo i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' fo i
1094 readInt32OffForeignObj :: ForeignObj -> Int -> IO Int32
1095 readInt32OffForeignObj fo i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' fo i
1097 readInt64OffForeignObj :: ForeignObj -> Int -> IO Int64
1098 # if WORD_SIZE_IN_BYTES==8
1099 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
1101 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' fo i
1104 #endif /* __PARALLEL_HASKELL__ */
1108 writeInt8OffAddr :: Addr -> Int -> Int8 -> IO ()
1109 writeInt8OffAddr a i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' a i e
1111 writeInt16OffAddr :: Addr -> Int -> Int16 -> IO ()
1112 writeInt16OffAddr a i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' a i e
1114 writeInt32OffAddr :: Addr -> Int -> Int32 -> IO ()
1115 writeInt32OffAddr a i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' a i e
1117 writeInt64OffAddr :: Addr -> Int -> Int64 -> IO ()
1118 #if WORD_SIZE_IN_BYTES==8
1119 writeInt64OffAddr a i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' a i e
1121 writeInt64OffAddr a i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' a i e
1124 #ifndef __PARALLEL_HASKELL__
1126 writeInt8OffForeignObj :: ForeignObj -> Int -> Int8 -> IO ()
1127 writeInt8OffForeignObj fo i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' fo i e
1129 writeInt16OffForeignObj :: ForeignObj -> Int -> Int16 -> IO ()
1130 writeInt16OffForeignObj fo i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' fo i e
1132 writeInt32OffForeignObj :: ForeignObj -> Int -> Int32 -> IO ()
1133 writeInt32OffForeignObj fo i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' fo i e
1135 writeInt64OffForeignObj :: ForeignObj -> Int -> Int64 -> IO ()
1136 # if WORD_SIZE_IN_BYTES==8
1137 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
1139 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' fo i e
1142 #endif /* __PARALLEL_HASKELL__ */