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 , indexInt8OffForeignObj
58 , indexInt16OffForeignObj
59 , indexInt32OffForeignObj
60 , indexInt64OffForeignObj
61 , readInt8OffForeignObj
62 , readInt16OffForeignObj
63 , readInt32OffForeignObj
64 , readInt64OffForeignObj
65 , writeInt8OffForeignObj
66 , writeInt16OffForeignObj
67 , writeInt32OffForeignObj
68 , writeInt64OffForeignObj
70 -- non-standard, GHC specific
80 import Numeric ( readDec )
81 import Word ( Word32 )
84 -----------------------------------------------------------------------------
85 -- The "official" coercion functions
86 -----------------------------------------------------------------------------
88 int8ToInt :: Int8 -> Int
89 intToInt8 :: Int -> Int8
90 int16ToInt :: Int16 -> Int
91 intToInt16 :: Int -> Int16
92 int32ToInt :: Int32 -> Int
93 intToInt32 :: Int -> Int32
95 -- And some non-exported ones
97 int8ToInt16 :: Int8 -> Int16
98 int8ToInt32 :: Int8 -> Int32
99 int16ToInt8 :: Int16 -> Int8
100 int16ToInt32 :: Int16 -> Int32
101 int32ToInt8 :: Int32 -> Int8
102 int32ToInt16 :: Int32 -> Int16
104 int8ToInt16 (I8# x) = I16# x
105 int8ToInt32 (I8# x) = I32# x
106 int16ToInt8 (I16# x) = I8# x
107 int16ToInt32 (I16# x) = I32# x
108 int32ToInt8 (I32# x) = I8# x
109 int32ToInt16 (I32# x) = I16# x
112 intToWord :: Int -> Word
113 intToWord (I# i#) = W# (int2Word# i#)
116 \subsection[Int8]{The @Int8@ interface}
120 instance CCallable Int8
121 instance CReturnable Int8
123 int8ToInt (I8# x) = I# (int8ToInt# x)
124 int8ToInt# x = if x' <=# 0x7f# then x' else x' -# 0x100#
125 where x' = word2Int# (int2Word# x `and#` int2Word# 0xff#)
128 -- This doesn't perform any bounds checking
129 -- on the value it is passed, nor its sign.
130 -- i.e., show (intToInt8 511) => "-1"
132 intToInt8 (I# x) = I8# (intToInt8# x)
133 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
135 instance Eq Int8 where
136 (I8# x#) == (I8# y#) = x# ==# y#
137 (I8# x#) /= (I8# y#) = x# /=# y#
139 instance Ord Int8 where
140 compare (I8# x#) (I8# y#) = compareInt# (int8ToInt# x#) (int8ToInt# y#)
142 compareInt# :: Int# -> Int# -> Ordering
148 instance Num Int8 where
149 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
150 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
151 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
155 else I8# (0x100# -# x#)
159 fromInteger (J# a# s# d#)
160 = case (integer2Int# a# s# d#) of { i# -> I8# (intToInt8# i#) }
163 instance Bounded Int8 where
167 instance Real Int8 where
168 toRational x = toInteger x % 1
170 instance Integral Int8 where
171 div x@(I8# x#) y@(I8# y#) =
172 if x > 0 && y < 0 then quotInt8 (x-y-1) y
173 else if x < 0 && y > 0 then quotInt8 (x-y+1) y
175 quot x@(I8# _) y@(I8# y#)
176 | y# /=# 0# = x `quotInt8` y
177 | otherwise = error "Integral.Int8.quot: divide by 0\n"
178 rem x@(I8# _) y@(I8# y#)
179 | y# /=# 0# = x `remInt8` y
180 | otherwise = error "Integral.Int8.rem: divide by 0\n"
181 mod x@(I8# x#) y@(I8# y#) =
182 if x > 0 && y < 0 || x < 0 && y > 0 then
183 if r/=0 then r+y else 0
186 where r = remInt8 x y
187 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
188 toInteger i8 = toInteger (int8ToInt i8)
189 toInt i8 = int8ToInt i8
191 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `remInt#` (int8ToInt# y)))
192 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `quotInt#` (int8ToInt# y)))
194 instance Ix Int8 where
197 | inRange b i = int8ToInt (i - m)
198 | otherwise = error (showString "Ix{Int8}.index: Index " .
199 showParen True (showsPrec 0 i) .
200 showString " out of range " $
201 showParen True (showsPrec 0 b) "")
202 inRange (m,n) i = m <= i && i <= n
204 instance Enum Int8 where
207 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int8)]
208 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int8)]
209 where last = if d < c then minBound else maxBound
211 instance Read Int8 where
212 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
214 instance Show Int8 where
215 showsPrec p i8 = showsPrec p (int8ToInt i8)
217 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
218 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
220 instance Bits Int8 where
221 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
222 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
223 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
224 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
225 shift (I8# x) i@(I# i#)
226 | i > 0 = I8# (intToInt8# (iShiftL# (int8ToInt# x) i#))
227 | otherwise = I8# (intToInt8# (iShiftRA# (int8ToInt# x) (negateInt# i#)))
228 i8@(I8# x) `rotate` (I# i)
231 I8# (intToInt8# ( word2Int# (
232 (int2Word# (iShiftL# (int8ToInt# x) i'))
234 (int2Word# (iShiftRA# (word2Int# (
236 (int2Word# (0x100# -# pow2# i2))))
238 | otherwise = rotate i8 (I# (8# +# i))
240 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
243 setBit x i = x .|. bit i
244 clearBit x i = x .&. complement (bit i)
245 complementBit x i = x `xor` bit i
246 testBit x i = (x .&. bit i) /= 0
250 pow2# :: Int# -> Int#
251 pow2# x# = iShiftL# 1# x#
253 pow2_64# :: Int# -> Int64#
254 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
260 \subsection[Int16]{The @Int16@ interface}
263 data Int16 = I16# Int#
264 instance CCallable Int16
265 instance CReturnable Int16
267 int16ToInt (I16# x) = I# (int16ToInt# x)
269 int16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
270 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
272 intToInt16 (I# x) = I16# (intToInt16# x)
273 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
275 instance Eq Int16 where
276 (I16# x#) == (I16# y#) = x# ==# y#
277 (I16# x#) /= (I16# y#) = x# /=# y#
279 instance Ord Int16 where
280 compare (I16# x#) (I16# y#) = compareInt# (int16ToInt# x#) (int16ToInt# y#)
282 instance Num Int16 where
283 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
284 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
285 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
289 else I16# (0x10000# -# x#)
292 fromInteger (J# a# s# d#)
293 = case (integer2Int# a# s# d#) of { i# -> I16# (intToInt16# i#) }
296 instance Bounded Int16 where
300 instance Real Int16 where
301 toRational x = toInteger x % 1
303 instance Integral Int16 where
304 div x@(I16# x#) y@(I16# y#) =
305 if x > 0 && y < 0 then quotInt16 (x-y-1) y
306 else if x < 0 && y > 0 then quotInt16 (x-y+1) y
308 quot x@(I16# _) y@(I16# y#)
309 | y# /=# 0# = x `quotInt16` y
310 | otherwise = error "Integral.Int16.quot: divide by 0\n"
311 rem x@(I16# _) y@(I16# y#)
312 | y# /=# 0# = x `remInt16` y
313 | otherwise = error "Integral.Int16.rem: divide by 0\n"
314 mod x@(I16# x#) y@(I16# y#) =
315 if x > 0 && y < 0 || x < 0 && y > 0 then
316 if r/=0 then r+y else 0
319 where r = remInt16 x y
320 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
321 toInteger i16 = toInteger (int16ToInt i16)
322 toInt i16 = int16ToInt i16
324 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `remInt#` (int16ToInt# y)))
325 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `quotInt#` (int16ToInt# y)))
327 instance Ix Int16 where
330 | inRange b i = int16ToInt (i - m)
331 | otherwise = error (showString "Ix{Int16}.index: Index " .
332 showParen True (showsPrec 0 i) .
333 showString " out of range " $
334 showParen True (showsPrec 0 b) "")
335 inRange (m,n) i = m <= i && i <= n
337 instance Enum Int16 where
339 fromEnum = int16ToInt
340 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int16)]
341 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int16)]
342 where last = if d < c then minBound else maxBound
344 instance Read Int16 where
345 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
347 instance Show Int16 where
348 showsPrec p i16 = showsPrec p (int16ToInt i16)
350 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
351 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
353 instance Bits Int16 where
354 (I16# x) .&. (I16# y) = I16# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
355 (I16# x) .|. (I16# y) = I16# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
356 (I16# x) `xor` (I16# y) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
357 complement (I16# x) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffff#)))
358 shift (I16# x) i@(I# i#)
359 | i > 0 = I16# (intToInt16# (iShiftL# (int16ToInt# x) i#))
360 | otherwise = I16# (intToInt16# (iShiftRA# (int16ToInt# x) (negateInt# i#)))
361 i16@(I16# x) `rotate` (I# i)
364 I16# (intToInt16# (word2Int# (
365 (int2Word# (iShiftL# (int16ToInt# x) i'))
367 (int2Word# (iShiftRA# ( word2Int# (
368 (int2Word# x) `and#` (int2Word# (0x100# -# pow2# i2))))
370 | otherwise = rotate i16 (I# (16# +# i))
372 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
375 setBit x i = x .|. bit i
376 clearBit x i = x .&. complement (bit i)
377 complementBit x i = x `xor` bit i
378 testBit x i = (x .&. bit i) /= 0
382 sizeofInt16 :: Word32
388 \subsection[Int32]{The @Int32@ interface}
393 data Int32 = I32# Int#
394 instance CCallable Int32
395 instance CReturnable Int32
397 int32ToInt (I32# x) = I# (int32ToInt# x)
399 int32ToInt# :: Int# -> Int#
400 #if WORD_SIZE_IN_BYTES > 4
401 int32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
402 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
407 intToInt32 (I# x) = I32# (intToInt32# x)
408 intToInt32# :: Int# -> Int#
409 #if WORD_SIZE_IN_BYTES > 4
410 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
415 instance Eq Int32 where
416 (I32# x#) == (I32# y#) = x# ==# y#
417 (I32# x#) /= (I32# y#) = x# /=# y#
419 instance Ord Int32 where
420 compare (I32# x#) (I32# y#) = compareInt# (int32ToInt# x#) (int32ToInt# y#)
422 instance Num Int32 where
423 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
424 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
425 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
426 #if WORD_SIZE_IN_BYTES > 4
430 else I32# (intToInt32# (0x100000000# -# x'))
432 negate (I32# x) = I32# (negateInt# x)
436 fromInteger (J# a# s# d#)
437 = case (integer2Int# a# s# d#) of { i# -> I32# (intToInt32# i#) }
440 -- ToDo: remove LitLit when minBound::Int is fixed (currently it's one
441 -- too high, and doesn't allow the correct minBound to be defined here).
442 instance Bounded Int32 where
443 minBound = case ``0x80000000'' of { I# x -> I32# x }
444 maxBound = I32# 0x7fffffff#
446 instance Real Int32 where
447 toRational x = toInteger x % 1
449 instance Integral Int32 where
450 div x@(I32# x#) y@(I32# y#) =
451 if x > 0 && y < 0 then quotInt32 (x-y-1) y
452 else if x < 0 && y > 0 then quotInt32 (x-y+1) y
454 quot x@(I32# _) y@(I32# y#)
455 | y# /=# 0# = x `quotInt32` y
456 | otherwise = error "Integral.Int32.quot: divide by 0\n"
457 rem x@(I32# _) y@(I32# y#)
458 | y# /=# 0# = x `remInt32` y
459 | otherwise = error "Integral.Int32.rem: divide by 0\n"
460 mod x@(I32# x#) y@(I32# y#) =
461 if x > 0 && y < 0 || x < 0 && y > 0 then
462 if r/=0 then r+y else 0
465 where r = remInt32 x y
466 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
467 toInteger i32 = toInteger (int32ToInt i32)
468 toInt i32 = int32ToInt i32
470 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `remInt#` (int32ToInt# y)))
471 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `quotInt#` (int32ToInt# y)))
473 instance Ix Int32 where
476 | inRange b i = int32ToInt (i - m)
477 | otherwise = error (showString "Ix{Int32}.index: Index " .
478 showParen True (showsPrec 0 i) .
479 showString " out of range " $
480 showParen True (showsPrec 0 b) "")
481 inRange (m,n) i = m <= i && i <= n
483 instance Enum Int32 where
485 fromEnum = int32ToInt
486 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int32)]
487 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int32)]
488 where last = if d < c then minBound else maxBound
490 instance Read Int32 where
491 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
493 instance Show Int32 where
494 showsPrec p i32 = showsPrec p (int32ToInt i32)
496 instance Bits Int32 where
497 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
498 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
499 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
500 #if WORD_SIZE_IN_BYTES > 4
501 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
503 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
505 shift (I32# x) i@(I# i#)
506 | i > 0 = I32# (intToInt32# (iShiftL# (int32ToInt# x) i#))
507 | otherwise = I32# (intToInt32# (iShiftRA# (int32ToInt# x) (negateInt# i#)))
508 i32@(I32# x) `rotate` (I# i)
511 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
512 I32# (intToInt32# ( word2Int# (
513 (int2Word# (iShiftL# (int32ToInt# x) i'))
515 (int2Word# (iShiftRA# (word2Int# (
518 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
520 | otherwise = rotate i32 (I# (32# +# i))
522 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
524 (I32# maxBound#) = maxBound
526 setBit x i = x .|. bit i
527 clearBit x i = x .&. complement (bit i)
528 complementBit x i = x `xor` bit i
529 testBit x i = (x .&. bit i) /= 0
533 sizeofInt32 :: Word32
537 \subsection[Int64]{The @Int64@ interface}
541 #if WORD_SIZE_IN_BYTES == 8
542 data Int64 = I64# Int#
544 int32ToInt64 :: Int32 -> Int64
545 int32ToInt64 (I32# i#) = I64# i#
547 intToInt32# :: Int# -> Int#
548 intToInt32# i# = word2Int# ((int2Word# i#) `and#` (case (maxBound::Word32) of W# x# -> x#))
550 int64ToInt32 :: Int64 -> Int32
551 int64ToInt32 (I64# i#) = I32# (intToInt32# w#)
553 instance Eq Int64 where
554 (I64# x) == (I64# y) = x `eqInt#` y
555 (I64# x) /= (I64# y) = x `neInt#` y
557 instance Ord Int32 where
558 compare (I64# x#) (I64# y#) = compareInt# x# y#
560 instance Num Int64 where
561 (I64# x) + (I64# y) = I64# (x +# y)
562 (I64# x) - (I64# y) = I64# (x -# y)
563 (I64# x) * (I64# y) = I64# (x *# y)
564 negate w@(I64# x) = I64# (negateInt# x)
567 fromInteger (J# a# s# d#) = case (integer2Int# a# s# d#) of { i# -> I64# i# }
570 instance Bounded Int64 where
571 minBound = integerToInt64 (-0x8000000000000000)
572 maxBound = integerToInt64 0x7fffffffffffffff
574 instance Real Int64 where
575 toRational x = toInteger x % 1
577 instance Integral Int64 where
578 div x@(I64# x#) y@(I64# y#)
579 | x > 0 && y < 0 = quotInt64 (x-y-1) y
580 | x < 0 && y > 0 = quotInt64 (x-y+1) y
581 | otherwise = quotInt64 x y
583 quot x@(I64# _) y@(I64# y#)
584 | y# /=# 0# = x `quotInt64` y
585 | otherwise = error "Integral.Int64.quot: divide by 0\n"
587 rem x@(I64# _) y@(I64# y#)
588 | y# /=# 0# = x `remInt64` y
589 | otherwise = error "Integral.Int32.rem: divide by 0\n"
591 mod x@(I64# x#) y@(I64# y#)
592 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
594 where r = remInt64 x y
596 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
597 toInteger (I64# i#) = toInteger (I# i#)
598 toInt (I64# i#) = I# i#
600 instance Enum Int64 where
601 toEnum (I# i) = I64# i#
602 fromEnum (I64# i) = I64# i#
603 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int64)] -- a long list!
604 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int64)]
605 where last = if d < c then minBound else maxBound
608 instance Read Int64 where
609 readsPrec p s = [ (intToInt64 x,r) | (x,r) <- readsPrec p s ]
611 instance Show Int64 where
612 showsPrec p i64 = showsPrec p (int64ToInt i64)
614 instance Bits Int64 where
615 (I64# x) .&. (I64# y) = I64# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
616 (I64# x) .|. (I64# y) = I64# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
617 (I64# x) `xor` (I64# y) = I64# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
618 complement (I64# x) = I64# (negateInt# x)
619 shift (I64# x) i@(I# i#)
620 | i > 0 = I64# (iShiftL# x i#)
621 | otherwise = I64# (iShiftRA# x (negateInt# i#))
622 i64@(I64# x) `rotate` (I# i)
625 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
627 (int2Word# (iShiftL# x i'))
629 (int2Word# (iShiftRA# (word2Int# (
632 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
634 | otherwise = rotate i64 (I# (64# +# i))
636 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
638 (I64# maxBound#) = maxBound
640 setBit x i = x .|. bit i
641 clearBit x i = x .&. complement (bit i)
642 complementBit x i = x `xor` bit i
643 testBit x i = (x .&. bit i) /= 0
649 remInt64 (I64# x) (I64# y) = I64# (x `remInt#` y)
650 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt#` y)
652 int64ToInteger :: Int64 -> Integer
653 int64ToInteger (I64# i#) = toInteger (I# i#)
655 integerToInt64 :: Integer -> Int64
656 integerToInt64 i = case fromInteger i of { I# i# -> I64# i# }
658 intToInt64 :: Int -> Int64
659 intToInt64 (I# i#) = I64# i#
661 int64ToInt :: Int64 -> Int
662 int64ToInt (I64# i#) = I# i#
665 --assume: support for long-longs
666 --data Int64 = I64 Int64# deriving (Eq, Ord, Bounded)
668 int32ToInt64 :: Int32 -> Int64
669 int32ToInt64 (I32# i#) = I64# (intToInt64# i#)
671 int64ToInt32 :: Int64 -> Int32
672 int64ToInt32 (I64# i#) = I32# (int64ToInt# i#)
674 int64ToInteger :: Int64 -> Integer
675 int64ToInteger (I64# x#) = int64ToInteger# x#
677 integerToInt64 :: Integer -> Int64
678 integerToInt64 (J# a# s# d#) = I64# (integerToInt64# a# s# d#)
680 instance Show Int64 where
681 showsPrec p x = showsPrec p (int64ToInteger x)
683 instance Read Int64 where
684 readsPrec p s = [ (integerToInt64 x,r) | (x,r) <- readDec s ]
686 instance Eq Int64 where
687 (I64# x) == (I64# y) = x `eqInt64#` y
688 (I64# x) /= (I64# y) = x `neInt64#` y
690 instance Ord Int64 where
691 compare (I64# x) (I64# y) = compareInt64# x y
692 (<) (I64# x) (I64# y) = x `ltInt64#` y
693 (<=) (I64# x) (I64# y) = x `leInt64#` y
694 (>=) (I64# x) (I64# y) = x `geInt64#` y
695 (>) (I64# x) (I64# y) = x `gtInt64#` y
696 max x@(I64# x#) y@(I64# y#) =
697 case (compareInt64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
698 min x@(I64# x#) y@(I64# y#) =
699 case (compareInt64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
701 instance Num Int64 where
702 (I64# x) + (I64# y) = I64# (x `plusInt64#` y)
703 (I64# x) - (I64# y) = I64# (x `minusInt64#` y)
704 (I64# x) * (I64# y) = I64# (x `timesInt64#` y)
705 negate (I64# x) = I64# (negateInt64# x)
708 fromInteger i = integerToInt64 i
709 fromInt i = intToInt64 i
712 | i# `ltInt64#` j# = LT
713 | i# `eqInt64#` j# = EQ
716 instance Bounded Int64 where
717 minBound = integerToInt64 (-0x8000000000000000)
718 maxBound = integerToInt64 0x7fffffffffffffff
720 instance Real Int64 where
721 toRational x = toInteger x % 1
723 instance Integral Int64 where
724 div x@(I64# x#) y@(I64# y#)
725 | x > 0 && y < 0 = quotInt64 (x-y-1) y
726 | x < 0 && y > 0 = quotInt64 (x-y+1) y
727 | otherwise = quotInt64 x y
729 quot x@(I64# _) y@(I64# y#)
730 | y# `neInt64#` (intToInt64# 0#) = x `quotInt64` y
731 | otherwise = error "Integral.Int64.quot: divide by 0\n"
733 rem x@(I64# _) y@(I64# y#)
734 | y# `neInt64#` (intToInt64# 0#) = x `remInt64` y
735 | otherwise = error "Integral.Int32.rem: divide by 0\n"
737 mod x@(I64# x#) y@(I64# y#)
738 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
740 where r = remInt64 x y
742 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
743 toInteger i = int64ToInteger i
744 toInt i = int64ToInt i
746 instance Enum Int64 where
747 toEnum (I# i) = I64# (intToInt64# i)
748 fromEnum (I64# w) = I# (int64ToInt# w)
749 enumFrom i = eft64 i 1
750 enumFromTo i1 i2 = eftt64 i1 1 (> i2)
751 enumFromThen i1 i2 = eftt64 i1 (i2 - i1) (>last)
754 | i1 < i2 = maxBound::Int64
755 | otherwise = minBound
758 instance Bits Int64 where
759 (I64# x) .&. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `and64#` (int64ToWord64# y)))
760 (I64# x) .|. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `or64#` (int64ToWord64# y)))
761 (I64# x) `xor` (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `xor64#` (int64ToWord64# y)))
762 complement (I64# x) = I64# (negateInt64# x)
763 shift (I64# x) i@(I# i#)
764 | i > 0 = I64# (iShiftL64# x i#)
765 | otherwise = I64# (iShiftRA64# x (negateInt# i#))
766 i64@(I64# x) `rotate` (I# i)
769 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
770 I64# (word64ToInt64# (
771 (int64ToWord64# (iShiftL64# x i')) `or64#`
772 (int64ToWord64# (iShiftRA64# (word64ToInt64# ((int64ToWord64# x) `and64#`
773 (int64ToWord64# (maxBound# `minusInt64#` (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
775 | otherwise = rotate i64 (I# (64# +# i))
777 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
779 (I64# maxBound#) = maxBound
781 setBit x i = x .|. bit i
782 clearBit x i = x .&. complement (bit i)
783 complementBit x i = x `xor` bit i
784 testBit x i = (x .&. bit i) /= 0
788 remInt64 (I64# x) (I64# y) = I64# (x `remInt64#` y)
789 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt64#` y)
791 intToInt64 :: Int -> Int64
792 intToInt64 (I# i#) = I64# (intToInt64# i#)
794 int64ToInt :: Int64 -> Int
795 int64ToInt (I64# i#) = I# (int64ToInt# i#)
797 -- Enum Int64 helper funs:
799 eftt64 :: Int64 -> Int64 -> (Int64->Bool) -> [Int64]
800 eftt64 now step done = go now
804 | otherwise = now : go (now+step)
806 eft64 :: Int64 -> Int64 -> [Int64]
807 eft64 now step = go now
810 | x == maxBound = [x]
811 | otherwise = x:go (x+step)
814 -- Word64# primop wrappers:
816 ltInt64# :: Int64# -> Int64# -> Bool
817 ltInt64# x# y# = unsafePerformIO $ do
818 v <- _ccall_ stg_ltInt64 x# y#
823 leInt64# :: Int64# -> Int64# -> Bool
824 leInt64# x# y# = unsafePerformIO $ do
825 v <- _ccall_ stg_leInt64 x# y#
830 eqInt64# :: Int64# -> Int64# -> Bool
831 eqInt64# x# y# = unsafePerformIO $ do
832 v <- _ccall_ stg_eqInt64 x# y#
837 neInt64# :: Int64# -> Int64# -> Bool
838 neInt64# x# y# = unsafePerformIO $ do
839 v <- _ccall_ stg_neInt64 x# y#
844 geInt64# :: Int64# -> Int64# -> Bool
845 geInt64# x# y# = unsafePerformIO $ do
846 v <- _ccall_ stg_geInt64 x# y#
851 gtInt64# :: Int64# -> Int64# -> Bool
852 gtInt64# x# y# = unsafePerformIO $ do
853 v <- _ccall_ stg_gtInt64 x# y#
858 plusInt64# :: Int64# -> Int64# -> Int64#
860 case (unsafePerformIO (_ccall_ stg_plusInt64 a# b#)) of
863 minusInt64# :: Int64# -> Int64# -> Int64#
865 case (unsafePerformIO (_ccall_ stg_minusInt64 a# b#)) of
868 timesInt64# :: Int64# -> Int64# -> Int64#
870 case (unsafePerformIO (_ccall_ stg_timesInt64 a# b#)) of
873 quotInt64# :: Int64# -> Int64# -> Int64#
875 case (unsafePerformIO (_ccall_ stg_quotInt64 a# b#)) of
878 remInt64# :: Int64# -> Int64# -> Int64#
880 case (unsafePerformIO (_ccall_ stg_remInt64 a# b#)) of
883 negateInt64# :: Int64# -> Int64#
885 case (unsafePerformIO (_ccall_ stg_negateInt64 a#)) of
888 and64# :: Word64# -> Word64# -> Word64#
890 case (unsafePerformIO (_ccall_ stg_and64 a# b#)) of
893 or64# :: Word64# -> Word64# -> Word64#
895 case (unsafePerformIO (_ccall_ stg_or64 a# b#)) of
898 xor64# :: Word64# -> Word64# -> Word64#
900 case (unsafePerformIO (_ccall_ stg_xor64 a# b#)) of
903 not64# :: Word64# -> Word64#
905 case (unsafePerformIO (_ccall_ stg_not64 a#)) of
908 shiftL64# :: Word64# -> Int# -> Word64#
910 case (unsafePerformIO (_ccall_ stg_shiftL64 a# b#)) of
913 iShiftL64# :: Int64# -> Int# -> Int64#
915 case (unsafePerformIO (_ccall_ stg_iShiftL64 a# b#)) of
918 iShiftRL64# :: Int64# -> Int# -> Int64#
920 case (unsafePerformIO (_ccall_ stg_iShiftRL64 a# b#)) of
923 iShiftRA64# :: Int64# -> Int# -> Int64#
925 case (unsafePerformIO (_ccall_ stg_iShiftRA64 a# b#)) of
928 shiftRL64# :: Word64# -> Int# -> Word64#
930 case (unsafePerformIO (_ccall_ stg_shifRtL64 a# b#)) of
933 int64ToInt# :: Int64# -> Int#
935 case (unsafePerformIO (_ccall_ stg_int64ToInt i#)) of
938 wordToWord64# :: Word# -> Word64#
940 case (unsafePerformIO (_ccall_ stg_wordToWord64 w#)) of
943 word64ToInt64# :: Word64# -> Int64#
945 case (unsafePerformIO (_ccall_ stg_word64ToInt64 w#)) of
948 int64ToWord64# :: Int64# -> Word64#
950 case (unsafePerformIO (_ccall_ stg_int64ToWord64 w#)) of
953 intToInt64# :: Int# -> Int64#
955 case (unsafePerformIO (_ccall_ stg_intToInt64 i#)) of
961 sizeofInt64 :: Word32
967 \subsection[Int Utils]{Miscellaneous utilities}
971 Code copied from the Prelude
974 absReal x | x >= 0 = x
977 signumReal x | x == 0 = 0
983 indexInt8OffAddr :: Addr -> Int -> Int8
984 indexInt8OffAddr (A# a#) (I# i#) = intToInt8 (I# (ord# (indexCharOffAddr# a# i#)))
986 indexInt8OffForeignObj :: ForeignObj -> Int -> Int8
987 indexInt8OffForeignObj (ForeignObj fo#) (I# i#) = intToInt8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
989 indexInt16OffAddr :: Addr -> Int -> Int16
990 indexInt16OffAddr a i =
991 #ifdef WORDS_BIGENDIAN
992 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
994 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
998 l = indexInt8OffAddr a byte_idx
999 h = indexInt8OffAddr a (byte_idx+1)
1001 indexInt16OffForeignObj :: ForeignObj -> Int -> Int16
1002 indexInt16OffForeignObj fo i =
1003 #ifdef WORDS_BIGENDIAN
1004 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1006 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1010 l = indexInt8OffForeignObj fo byte_idx
1011 h = indexInt8OffForeignObj fo (byte_idx+1)
1013 indexInt32OffAddr :: Addr -> Int -> Int32
1014 indexInt32OffAddr (A# a#) i = intToInt32 (I# (indexIntOffAddr# a# i'#))
1016 -- adjust index to be in Int units, not Int32 ones.
1018 #if WORD_SIZE_IN_BYTES==8
1024 indexInt32OffForeignObj :: ForeignObj -> Int -> Int32
1025 indexInt32OffForeignObj (ForeignObj fo#) i = intToInt32 (I# (indexIntOffForeignObj# fo# i'#))
1027 -- adjust index to be in Int units, not Int32 ones.
1029 #if WORD_SIZE_IN_BYTES==8
1035 indexInt64OffAddr :: Addr -> Int -> Int64
1036 indexInt64OffAddr (A# a#) (I# i#)
1037 #if WORD_SIZE_IN_BYTES==8
1038 = I64# (indexIntOffAddr# a# i#)
1040 = I64# (indexInt64OffAddr# a# i#)
1043 indexInt64OffForeignObj :: ForeignObj -> Int -> Int64
1044 indexInt64OffForeignObj (ForeignObj fo#) (I# i#)
1045 #if WORD_SIZE_IN_BYTES==8
1046 = I64# (indexIntOffForeignObj# fo# i#)
1048 = I64# (indexInt64OffForeignObj# fo# i#)
1053 Read words out of mutable memory:
1056 readInt8OffAddr :: Addr -> Int -> IO Int8
1057 readInt8OffAddr a i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' a i
1059 readInt8OffForeignObj :: ForeignObj -> Int -> IO Int8
1060 readInt8OffForeignObj fo i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' fo i
1062 readInt16OffAddr :: Addr -> Int -> IO Int16
1063 readInt16OffAddr a i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' a i
1065 readInt16OffForeignObj :: ForeignObj -> Int -> IO Int16
1066 readInt16OffForeignObj fo i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' fo i
1068 readInt32OffAddr :: Addr -> Int -> IO Int32
1069 readInt32OffAddr a i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' a i
1071 readInt32OffForeignObj :: ForeignObj -> Int -> IO Int32
1072 readInt32OffForeignObj fo i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' fo i
1074 readInt64OffAddr :: Addr -> Int -> IO Int64
1075 #if WORD_SIZE_IN_BYTES==8
1076 readInt64OffAddr a i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' a i
1078 readInt64OffAddr a i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' a i
1081 readInt64OffForeignObj :: ForeignObj -> Int -> IO Int64
1082 #if WORD_SIZE_IN_BYTES==8
1083 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
1085 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' fo i
1090 writeInt8OffAddr :: Addr -> Int -> Int8 -> IO ()
1091 writeInt8OffAddr a i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' a i e
1093 writeInt8OffForeignObj :: ForeignObj -> Int -> Int8 -> IO ()
1094 writeInt8OffForeignObj fo i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' fo i e
1096 writeInt16OffAddr :: Addr -> Int -> Int16 -> IO ()
1097 writeInt16OffAddr a i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' a i e
1099 writeInt16OffForeignObj :: ForeignObj -> Int -> Int16 -> IO ()
1100 writeInt16OffForeignObj fo i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' fo i e
1102 writeInt32OffAddr :: Addr -> Int -> Int32 -> IO ()
1103 writeInt32OffAddr a i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' a i e
1105 writeInt32OffForeignObj :: ForeignObj -> Int -> Int32 -> IO ()
1106 writeInt32OffForeignObj fo i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' fo i e
1108 writeInt64OffAddr :: Addr -> Int -> Int64 -> IO ()
1109 #if WORD_SIZE_IN_BYTES==8
1110 writeInt64OffAddr a i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' a i e
1112 writeInt64OffAddr a i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' a i e
1115 writeInt64OffForeignObj :: ForeignObj -> Int -> Int64 -> IO ()
1116 #if WORD_SIZE_IN_BYTES==8
1117 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
1119 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' fo i e