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
59 -- The "official" place to get these from is Addr, importing
60 -- them from Int is a non-standard thing to do.
83 -- The "official" place to get these from is Foreign
84 #ifndef __PARALLEL_HASKELL__
86 , indexInt8OffForeignObj
87 , indexInt16OffForeignObj
88 , indexInt32OffForeignObj
89 , indexInt64OffForeignObj
91 , readInt8OffForeignObj
92 , readInt16OffForeignObj
93 , readInt32OffForeignObj
94 , readInt64OffForeignObj
96 , writeInt8OffForeignObj
97 , writeInt16OffForeignObj
98 , writeInt32OffForeignObj
99 , writeInt64OffForeignObj
103 -- The non-standard fromInt and toInt methods
104 , Num( fromInt ), Integral( toInt )
106 -- non-standard, GHC specific
110 -- Internal, do not use.
123 import PrelAddr ( Int64(..), Word64(..), Addr(..), Word(..) )
124 import PrelNum ( Num(..), Integral(..) ) -- To get fromInt/toInt
131 import Numeric ( readDec )
132 import Word ( Word32 )
138 -----------------------------------------------------------------------------
139 -- The "official" coercion functions
140 -----------------------------------------------------------------------------
142 int8ToInt :: Int8 -> Int
143 int16ToInt :: Int16 -> Int
144 int32ToInt :: Int32 -> Int
146 int8ToInt# :: Int8 -> Int#
147 int16ToInt# :: Int16 -> Int#
148 int32ToInt# :: Int32 -> Int#
150 intToInt8 :: Int -> Int8
151 intToInt16 :: Int -> Int16
152 intToInt32 :: Int -> Int32
154 int8ToInt16 :: Int8 -> Int16
155 int8ToInt32 :: Int8 -> Int32
157 int16ToInt8 :: Int16 -> Int8
158 int16ToInt32 :: Int16 -> Int32
160 int32ToInt8 :: Int32 -> Int8
161 int32ToInt16 :: Int32 -> Int16
163 int8ToInt16 (I8# x) = I16# x
164 int8ToInt32 (I8# x) = I32# x
165 int8ToInt64 = int32ToInt64 . int8ToInt32
167 int16ToInt8 (I16# x) = I8# x
168 int16ToInt32 (I16# x) = I32# x
169 int16ToInt64 = int32ToInt64 . int16ToInt32
171 int32ToInt8 (I32# x) = I8# x
172 int32ToInt16 (I32# x) = I16# x
175 intToWord :: Int -> Word
176 intToWord (I# i#) = W# (int2Word# i#)
179 \subsection[Int8]{The @Int8@ interface}
183 instance CCallable Int8
184 instance CReturnable Int8
186 int8ToInt (I8# x) = I# (i8ToInt# x)
187 int8ToInt# (I8# x) = i8ToInt# x
189 i8ToInt# :: Int# -> Int#
190 i8ToInt# x = if x' <=# 0x7f# then x' else x' -# 0x100#
191 where x' = word2Int# (int2Word# x `and#` int2Word# 0xff#)
194 -- This doesn't perform any bounds checking
195 -- on the value it is passed, nor its sign.
196 -- i.e., show (intToInt8 511) => "-1"
198 intToInt8 (I# x) = I8# (intToInt8# x)
200 intToInt8# :: Int# -> Int#
201 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
203 instance Eq Int8 where
204 (I8# x#) == (I8# y#) = x# ==# y#
205 (I8# x#) /= (I8# y#) = x# /=# y#
207 instance Ord Int8 where
208 compare (I8# x#) (I8# y#) = compareInt# (i8ToInt# x#) (i8ToInt# y#)
210 compareInt# :: Int# -> Int# -> Ordering
216 instance Num Int8 where
217 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
218 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
219 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
223 else I8# (0x100# -# x#)
227 fromInteger (S# i#) = I8# (intToInt8# i#)
228 fromInteger (J# s# d#) = I8# (intToInt8# (integer2Int# s# d#))
231 instance Bounded Int8 where
235 instance Real Int8 where
236 toRational x = toInteger x % 1
238 instance Integral Int8 where
240 | x > 0 && y < 0 = quotInt8 (x-y-1) y
241 | x < 0 && y > 0 = quotInt8 (x-y+1) y
242 | otherwise = quotInt8 x y
244 quot x@(I8# _) y@(I8# y#)
245 | y# /=# 0# = x `quotInt8` y
246 | otherwise = divZeroError "quot{Int8}" x
247 rem x@(I8# _) y@(I8# y#)
248 | y# /=# 0# = x `remInt8` y
249 | otherwise = divZeroError "rem{Int8}" x
251 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
253 where r = remInt8 x y
255 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
256 toInteger i8 = toInteger (int8ToInt i8)
257 toInt i8 = int8ToInt i8
259 remInt8, quotInt8 :: Int8 -> Int8 -> Int8
260 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((i8ToInt# x) `remInt#` (i8ToInt# y)))
261 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((i8ToInt# x) `quotInt#` (i8ToInt# y)))
263 instance Ix Int8 where
266 | inRange b i = int8ToInt (i - m)
267 | otherwise = indexError i b "Int8"
268 inRange (m,n) i = m <= i && i <= n
270 instance Enum Int8 where
272 | i == maxBound = succError "Int8"
275 | i == minBound = predError "Int8"
279 | x >= toInt (minBound::Int8) && x <= toInt (maxBound::Int8)
282 = toEnumError "Int8" x (minBound::Int8,maxBound::Int8)
285 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int8)]
287 map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int8)]
291 | otherwise = maxBound
293 instance Read Int8 where
294 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
296 instance Show Int8 where
297 showsPrec p i8 = showsPrec p (int8ToInt i8)
299 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
300 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
302 instance Bits Int8 where
303 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
304 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
305 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
306 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
307 shift (I8# x) i@(I# i#)
308 | i > 0 = I8# (intToInt8# (iShiftL# (i8ToInt# x) i#))
309 | otherwise = I8# (intToInt8# (iShiftRA# (i8ToInt# x) (negateInt# i#)))
310 i8@(I8# x) `rotate` (I# i)
313 I8# (intToInt8# ( word2Int# (
314 (int2Word# (iShiftL# (i8ToInt# x) i'))
316 (int2Word# (iShiftRA# (word2Int# (
318 (int2Word# (0x100# -# pow2# i2))))
320 | otherwise = rotate i8 (I# (8# +# i))
322 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
325 setBit x i = x .|. bit i
326 clearBit x i = x .&. complement (bit i)
327 complementBit x i = x `xor` bit i
328 testBit x i = (x .&. bit i) /= 0
332 pow2# :: Int# -> Int#
333 pow2# x# = iShiftL# 1# x#
335 pow2_64# :: Int# -> Int64#
336 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
342 \subsection[Int16]{The @Int16@ interface}
345 data Int16 = I16# Int#
346 instance CCallable Int16
347 instance CReturnable Int16
349 int16ToInt (I16# x) = I# (i16ToInt# x)
350 int16ToInt# (I16# x) = i16ToInt# x
352 i16ToInt# :: Int# -> Int#
353 i16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
354 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
356 intToInt16 (I# x) = I16# (intToInt16# x)
358 intToInt16# :: Int# -> Int#
359 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
361 instance Eq Int16 where
362 (I16# x#) == (I16# y#) = x# ==# y#
363 (I16# x#) /= (I16# y#) = x# /=# y#
365 instance Ord Int16 where
366 compare (I16# x#) (I16# y#) = compareInt# (i16ToInt# x#) (i16ToInt# y#)
368 instance Num Int16 where
369 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
370 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
371 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
375 else I16# (0x10000# -# x#)
378 fromInteger (S# i#) = I16# (intToInt16# i#)
379 fromInteger (J# s# d#) = I16# (intToInt16# (integer2Int# s# d#))
382 instance Bounded Int16 where
386 instance Real Int16 where
387 toRational x = toInteger x % 1
389 instance Integral Int16 where
391 | x > 0 && y < 0 = quotInt16 (x-y-1) y
392 | x < 0 && y > 0 = quotInt16 (x-y+1) y
393 | otherwise = quotInt16 x y
395 quot x@(I16# _) y@(I16# y#)
396 | y# /=# 0# = x `quotInt16` y
397 | otherwise = divZeroError "quot{Int16}" x
398 rem x@(I16# _) y@(I16# y#)
399 | y# /=# 0# = x `remInt16` y
400 | otherwise = divZeroError "rem{Int16}" x
402 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
404 where r = remInt16 x y
406 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
407 toInteger i16 = toInteger (int16ToInt i16)
408 toInt i16 = int16ToInt i16
410 remInt16, quotInt16 :: Int16 -> Int16 -> Int16
411 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((i16ToInt# x) `remInt#` (i16ToInt# y)))
412 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((i16ToInt# x) `quotInt#` (i16ToInt# y)))
414 instance Ix Int16 where
417 | inRange b i = int16ToInt (i - m)
418 | otherwise = indexError i b "Int16"
419 inRange (m,n) i = m <= i && i <= n
421 instance Enum Int16 where
423 | i == maxBound = succError "Int16"
427 | i == minBound = predError "Int16"
431 | x >= toInt (minBound::Int16) && x <= toInt (maxBound::Int16)
434 = toEnumError "Int16" x (minBound::Int16, maxBound::Int16)
436 fromEnum = int16ToInt
438 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int16)]
439 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int16)]
442 | otherwise = maxBound
444 instance Read Int16 where
445 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
447 instance Show Int16 where
448 showsPrec p i16 = showsPrec p (int16ToInt i16)
450 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
451 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
453 instance Bits Int16 where
454 (I16# x) .&. (I16# y) = I16# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
455 (I16# x) .|. (I16# y) = I16# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
456 (I16# x) `xor` (I16# y) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
457 complement (I16# x) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffff#)))
458 shift (I16# x) i@(I# i#)
459 | i > 0 = I16# (intToInt16# (iShiftL# (i16ToInt# x) i#))
460 | otherwise = I16# (intToInt16# (iShiftRA# (i16ToInt# x) (negateInt# i#)))
461 i16@(I16# x) `rotate` (I# i)
464 I16# (intToInt16# (word2Int# (
465 (int2Word# (iShiftL# (i16ToInt# x) i'))
467 (int2Word# (iShiftRA# ( word2Int# (
468 (int2Word# x) `and#` (int2Word# (0x100# -# pow2# i2))))
470 | otherwise = rotate i16 (I# (16# +# i))
472 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
475 setBit x i = x .|. bit i
476 clearBit x i = x .&. complement (bit i)
477 complementBit x i = x `xor` bit i
478 testBit x i = (x .&. bit i) /= 0
482 sizeofInt16 :: Word32
488 \subsection[Int32]{The @Int32@ interface}
493 data Int32 = I32# Int#
494 instance CCallable Int32
495 instance CReturnable Int32
497 int32ToInt (I32# x) = I# (i32ToInt# x)
498 int32ToInt# (I32# x) = i32ToInt# x
500 i32ToInt# :: Int# -> Int#
501 #if WORD_SIZE_IN_BYTES > 4
502 i32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
503 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
508 intToInt32 (I# x) = I32# (intToInt32# x)
509 intToInt32# :: Int# -> Int#
510 #if WORD_SIZE_IN_BYTES > 4
511 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
516 instance Eq Int32 where
517 (I32# x#) == (I32# y#) = x# ==# y#
518 (I32# x#) /= (I32# y#) = x# /=# y#
520 instance Ord Int32 where
521 compare (I32# x#) (I32# y#) = compareInt# (i32ToInt# x#) (i32ToInt# y#)
523 instance Num Int32 where
524 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
525 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
526 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
527 #if WORD_SIZE_IN_BYTES > 4
531 else I32# (intToInt32# (0x100000000# -# x'))
533 negate (I32# x) = I32# (negateInt# x)
537 fromInteger (S# i#) = I32# (intToInt32# i#)
538 fromInteger (J# s# d#) = I32# (intToInt32# (integer2Int# s# d#))
541 instance Bounded Int32 where
542 minBound = fromInt minBound
543 maxBound = fromInt maxBound
545 instance Real Int32 where
546 toRational x = toInteger x % 1
548 instance Integral Int32 where
550 | x > 0 && y < 0 = quotInt32 (x-y-1) y
551 | x < 0 && y > 0 = quotInt32 (x-y+1) y
552 | otherwise = quotInt32 x y
553 quot x@(I32# _) y@(I32# y#)
554 | y# /=# 0# = x `quotInt32` y
555 | otherwise = divZeroError "quot{Int32}" x
556 rem x@(I32# _) y@(I32# y#)
557 | y# /=# 0# = x `remInt32` y
558 | otherwise = divZeroError "rem{Int32}" x
560 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
562 where r = remInt32 x y
564 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
565 toInteger i32 = toInteger (int32ToInt i32)
566 toInt i32 = int32ToInt i32
568 remInt32, quotInt32 :: Int32 -> Int32 -> Int32
569 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((i32ToInt# x) `remInt#` (i32ToInt# y)))
570 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((i32ToInt# x) `quotInt#` (i32ToInt# y)))
572 instance Ix Int32 where
575 | inRange b i = int32ToInt (i - m)
576 | otherwise = indexError i b "Int32"
577 inRange (m,n) i = m <= i && i <= n
579 instance Enum Int32 where
581 | i == maxBound = succError "Int32"
585 | i == minBound = predError "Int32"
589 -- with Int having the same range as Int32, the following test
590 -- shouldn't fail. However, having it here
591 | x >= toInt (minBound::Int32) && x <= toInt (maxBound::Int32)
594 = toEnumError "Int32" x (minBound::Int32, maxBound::Int32)
596 fromEnum = int32ToInt
598 enumFrom e1 = map toEnum [fromEnum e1 .. fromEnum (maxBound::Int32)]
599 enumFromThen e1 e2 = map toEnum [fromEnum e1, fromEnum e2 .. fromEnum (last::Int32)]
603 | otherwise = maxBound
605 instance Read Int32 where
606 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
608 instance Show Int32 where
609 showsPrec p i32 = showsPrec p (int32ToInt i32)
611 instance Bits Int32 where
612 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
613 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
614 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
615 #if WORD_SIZE_IN_BYTES > 4
616 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
618 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
620 shift (I32# x) i@(I# i#)
621 | i > 0 = I32# (intToInt32# (iShiftL# (i32ToInt# x) i#))
622 | otherwise = I32# (intToInt32# (iShiftRA# (i32ToInt# x) (negateInt# i#)))
623 i32@(I32# x) `rotate` (I# i)
626 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
627 I32# (intToInt32# ( word2Int# (
628 (int2Word# (iShiftL# (i32ToInt# x) i'))
630 (int2Word# (iShiftRA# (word2Int# (
633 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
635 | otherwise = rotate i32 (I# (32# +# i))
637 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
639 (I32# maxBound#) = maxBound
641 setBit x i = x .|. bit i
642 clearBit x i = x .&. complement (bit i)
643 complementBit x i = x `xor` bit i
644 testBit x i = (x .&. bit i) /= 0
648 sizeofInt32 :: Word32
652 \subsection[Int64]{The @Int64@ interface}
656 #if WORD_SIZE_IN_BYTES == 8
657 --data Int64 = I64# Int#
659 int32ToInt64 :: Int32 -> Int64
660 int32ToInt64 (I32# i#) = I64# i#
662 intToInt32# :: Int# -> Int#
663 intToInt32# i# = word2Int# ((int2Word# i#) `and#` (case (maxBound::Word32) of W# x# -> x#))
665 int64ToInt32 :: Int64 -> Int32
666 int64ToInt32 (I64# i#) = I32# (intToInt32# w#)
668 instance Eq Int64 where
669 (I64# x) == (I64# y) = x `eqInt#` y
670 (I64# x) /= (I64# y) = x `neInt#` y
672 instance Ord Int32 where
673 compare (I64# x#) (I64# y#) = compareInt# x# y#
675 instance Num Int64 where
676 (I64# x) + (I64# y) = I64# (x +# y)
677 (I64# x) - (I64# y) = I64# (x -# y)
678 (I64# x) * (I64# y) = I64# (x *# y)
679 negate w@(I64# x) = I64# (negateInt# x)
682 fromInteger (S# i#) = I64# i#
683 fromInteger (J# s# d#) = I64# (integer2Int# s# d#)
686 instance Bounded Int64 where
687 minBound = integerToInt64 (-0x8000000000000000)
688 maxBound = integerToInt64 0x7fffffffffffffff
690 instance Integral Int64 where
692 | x > 0 && y < 0 = quotInt64 (x-y-1) y
693 | x < 0 && y > 0 = quotInt64 (x-y+1) y
694 | otherwise = quotInt64 x y
696 quot x@(I64# _) y@(I64# y#)
697 | y# /=# 0# = x `quotInt64` y
698 | otherwise = divZeroError "quot{Int64}" x
700 rem x@(I64# _) y@(I64# y#)
701 | y# /=# 0# = x `remInt64` y
702 | otherwise = divZeroError "rem{Int64}" x
705 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
707 where r = remInt64 x y
709 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
710 toInteger (I64# i#) = toInteger (I# i#)
711 toInt (I64# i#) = I# i#
713 instance Bits Int64 where
714 (I64# x) .&. (I64# y) = I64# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
715 (I64# x) .|. (I64# y) = I64# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
716 (I64# x) `xor` (I64# y) = I64# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
717 complement (I64# x) = I64# (negateInt# x)
718 shift (I64# x) i@(I# i#)
719 | i > 0 = I64# (iShiftL# x i#)
720 | otherwise = I64# (iShiftRA# x (negateInt# i#))
721 i64@(I64# x) `rotate` (I# i)
724 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
726 (int2Word# (iShiftL# x i'))
728 (int2Word# (iShiftRA# (word2Int# (
731 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
733 | otherwise = rotate i64 (I# (64# +# i))
735 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
737 (I64# maxBound#) = maxBound
739 setBit x i = x .|. bit i
740 clearBit x i = x .&. complement (bit i)
741 complementBit x i = x `xor` bit i
742 testBit x i = (x .&. bit i) /= 0
748 remInt64 (I64# x) (I64# y) = I64# (x `remInt#` y)
749 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt#` y)
751 int64ToInteger :: Int64 -> Integer
752 int64ToInteger (I64# i#) = toInteger (I# i#)
754 integerToInt64 :: Integer -> Int64
755 integerToInt64 i = case fromInteger i of { I# i# -> I64# i# }
757 intToInt64 :: Int -> Int64
758 intToInt64 (I# i#) = I64# i#
760 int64ToInt :: Int64 -> Int
761 int64ToInt (I64# i#) = I# i#
764 --assume: support for long-longs
765 --data Int64 = I64 Int64# deriving (Eq, Ord, Bounded)
767 int32ToInt64 :: Int32 -> Int64
768 int32ToInt64 (I32# i#) = I64# (intToInt64# i#)
770 int64ToInt32 :: Int64 -> Int32
771 int64ToInt32 (I64# i#) = I32# (int64ToInt# i#)
773 int64ToInteger :: Int64 -> Integer
774 int64ToInteger (I64# x#) =
775 case int64ToInteger# x# of
776 (# s#, p# #) -> J# s# p#
778 integerToInt64 :: Integer -> Int64
779 integerToInt64 (S# i#) = I64# (intToInt64# i#)
780 integerToInt64 (J# s# d#) = I64# (integerToInt64# s# d#)
782 instance Eq Int64 where
783 (I64# x) == (I64# y) = x `eqInt64#` y
784 (I64# x) /= (I64# y) = x `neInt64#` y
786 instance Ord Int64 where
787 compare (I64# x) (I64# y) = compareInt64# x y
788 (<) (I64# x) (I64# y) = x `ltInt64#` y
789 (<=) (I64# x) (I64# y) = x `leInt64#` y
790 (>=) (I64# x) (I64# y) = x `geInt64#` y
791 (>) (I64# x) (I64# y) = x `gtInt64#` y
792 max x@(I64# x#) y@(I64# y#) =
793 case (compareInt64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
794 min x@(I64# x#) y@(I64# y#) =
795 case (compareInt64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
797 instance Num Int64 where
798 (I64# x) + (I64# y) = I64# (x `plusInt64#` y)
799 (I64# x) - (I64# y) = I64# (x `minusInt64#` y)
800 (I64# x) * (I64# y) = I64# (x `timesInt64#` y)
801 negate (I64# x) = I64# (negateInt64# x)
804 fromInteger i = integerToInt64 i
805 fromInt i = intToInt64 i
807 compareInt64# :: Int64# -> Int64# -> Ordering
809 | i# `ltInt64#` j# = LT
810 | i# `eqInt64#` j# = EQ
813 instance Bounded Int64 where
814 minBound = integerToInt64 (-0x8000000000000000)
815 maxBound = integerToInt64 0x7fffffffffffffff
817 instance Integral Int64 where
819 | x > 0 && y < 0 = quotInt64 (x-y-1) y
820 | x < 0 && y > 0 = quotInt64 (x-y+1) y
821 | otherwise = quotInt64 x y
823 quot x@(I64# _) y@(I64# y#)
824 | y# `neInt64#` (intToInt64# 0#) = x `quotInt64` y
825 | otherwise = divZeroError "quot{Int64}" x
827 rem x@(I64# _) y@(I64# y#)
828 | y# `neInt64#` (intToInt64# 0#) = x `remInt64` y
829 | otherwise = divZeroError "rem{Int64}" x
832 | x > 0 && y < 0 || x < 0 && y > 0 = if r/=0 then r+y else 0
834 where r = remInt64 x y
836 a@(I64# _) `quotRem` b@(I64# _) = (a `quotInt64` b, a `remInt64` b)
837 toInteger i = int64ToInteger i
838 toInt i = int64ToInt i
840 instance Bits Int64 where
841 (I64# x) .&. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `and64#` (int64ToWord64# y)))
842 (I64# x) .|. (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `or64#` (int64ToWord64# y)))
843 (I64# x) `xor` (I64# y) = I64# (word64ToInt64# ((int64ToWord64# x) `xor64#` (int64ToWord64# y)))
844 complement (I64# x) = I64# (negateInt64# x)
845 shift (I64# x) i@(I# i#)
846 | i > 0 = I64# (iShiftL64# x i#)
847 | otherwise = I64# (iShiftRA64# x (negateInt# i#))
848 i64@(I64# x) `rotate` (I# i)
851 -- ( (x<<i') | ((x&(0x10000000000000000-2^i2))>>i2) )
852 I64# (word64ToInt64# (
853 (int64ToWord64# (iShiftL64# x i')) `or64#`
854 (int64ToWord64# (iShiftRA64# (word64ToInt64# ((int64ToWord64# x) `and64#`
855 (int64ToWord64# (maxBound# `minusInt64#` (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
857 | otherwise = rotate i64 (I# (64# +# i))
859 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
861 (I64# maxBound#) = maxBound
863 setBit x i = x .|. bit i
864 clearBit x i = x .&. complement (bit i)
865 complementBit x i = x `xor` bit i
866 testBit x i = (x .&. bit i) /= 0
870 remInt64, quotInt64 :: Int64 -> Int64 -> Int64
871 remInt64 (I64# x) (I64# y) = I64# (x `remInt64#` y)
872 quotInt64 (I64# x) (I64# y) = I64# (x `quotInt64#` y)
874 intToInt64 :: Int -> Int64
875 intToInt64 (I# i#) = I64# (intToInt64# i#)
877 int64ToInt :: Int64 -> Int
878 int64ToInt (I64# i#) = I# (int64ToInt# i#)
880 -- Word64# primop wrappers:
882 ltInt64# :: Int64# -> Int64# -> Bool
884 case stg_ltInt64 x# y# of
888 leInt64# :: Int64# -> Int64# -> Bool
890 case stg_leInt64 x# y# of
894 eqInt64# :: Int64# -> Int64# -> Bool
896 case stg_eqInt64 x# y# of
900 neInt64# :: Int64# -> Int64# -> Bool
902 case stg_neInt64 x# y# of
906 geInt64# :: Int64# -> Int64# -> Bool
908 case stg_geInt64 x# y# of
912 gtInt64# :: Int64# -> Int64# -> Bool
914 case stg_gtInt64 x# y# of
918 plusInt64# :: Int64# -> Int64# -> Int64#
920 case stg_plusInt64 a# b# of
923 minusInt64# :: Int64# -> Int64# -> Int64#
925 case stg_minusInt64 a# b# of
928 timesInt64# :: Int64# -> Int64# -> Int64#
930 case stg_timesInt64 a# b# of
933 quotInt64# :: Int64# -> Int64# -> Int64#
935 case stg_quotInt64 a# b# of
938 remInt64# :: Int64# -> Int64# -> Int64#
940 case stg_remInt64 a# b# of
943 negateInt64# :: Int64# -> Int64#
945 case stg_negateInt64 a# of
948 and64# :: Word64# -> Word64# -> Word64#
950 case stg_and64 a# b# of
953 or64# :: Word64# -> Word64# -> Word64#
955 case stg_or64 a# b# of
958 xor64# :: Word64# -> Word64# -> Word64#
960 case stg_xor64 a# b# of
963 not64# :: Word64# -> Word64#
968 shiftL64# :: Word64# -> Int# -> Word64#
970 case stg_shiftL64 a# b# of
973 iShiftL64# :: Int64# -> Int# -> Int64#
975 case stg_iShiftL64 a# b# of
978 iShiftRL64# :: Int64# -> Int# -> Int64#
980 case stg_iShiftRL64 a# b# of
983 iShiftRA64# :: Int64# -> Int# -> Int64#
985 case stg_iShiftRA64 a# b# of
988 shiftRL64# :: Word64# -> Int# -> Word64#
990 case stg_shiftRL64 a# b# of
993 int64ToInt# :: Int64# -> Int#
995 case stg_int64ToInt i64# of
998 wordToWord64# :: Word# -> Word64#
1000 case stg_wordToWord64 w# of
1003 word64ToInt64# :: Word64# -> Int64#
1005 case stg_word64ToInt64 w# of
1008 int64ToWord64# :: Int64# -> Word64#
1010 case stg_int64ToWord64 i# of
1013 intToInt64# :: Int# -> Int64#
1015 case stg_intToInt64 i# of
1018 foreign import "stg_intToInt64" stg_intToInt64 :: Int# -> Int64
1019 foreign import "stg_int64ToWord64" stg_int64ToWord64 :: Int64# -> Word64
1020 foreign import "stg_word64ToInt64" stg_word64ToInt64 :: Word64# -> Int64
1021 foreign import "stg_wordToWord64" stg_wordToWord64 :: Word# -> Word64
1022 foreign import "stg_int64ToInt" stg_int64ToInt :: Int64# -> Int
1023 foreign import "stg_shiftRL64" stg_shiftRL64 :: Word64# -> Int# -> Word64
1024 foreign import "stg_iShiftRA64" stg_iShiftRA64 :: Int64# -> Int# -> Int64
1025 foreign import "stg_iShiftRL64" stg_iShiftRL64 :: Int64# -> Int# -> Int64
1026 foreign import "stg_iShiftL64" stg_iShiftL64 :: Int64# -> Int# -> Int64
1027 foreign import "stg_shiftL64" stg_shiftL64 :: Word64# -> Int# -> Word64
1028 foreign import "stg_not64" stg_not64 :: Word64# -> Word64
1029 foreign import "stg_xor64" stg_xor64 :: Word64# -> Word64# -> Word64
1030 foreign import "stg_or64" stg_or64 :: Word64# -> Word64# -> Word64
1031 foreign import "stg_and64" stg_and64 :: Word64# -> Word64# -> Word64
1032 foreign import "stg_negateInt64" stg_negateInt64 :: Int64# -> Int64
1033 foreign import "stg_remInt64" stg_remInt64 :: Int64# -> Int64# -> Int64
1034 foreign import "stg_quotInt64" stg_quotInt64 :: Int64# -> Int64# -> Int64
1035 foreign import "stg_timesInt64" stg_timesInt64 :: Int64# -> Int64# -> Int64
1036 foreign import "stg_minusInt64" stg_minusInt64 :: Int64# -> Int64# -> Int64
1037 foreign import "stg_plusInt64" stg_plusInt64 :: Int64# -> Int64# -> Int64
1038 foreign import "stg_gtInt64" stg_gtInt64 :: Int64# -> Int64# -> Int
1039 foreign import "stg_geInt64" stg_geInt64 :: Int64# -> Int64# -> Int
1040 foreign import "stg_neInt64" stg_neInt64 :: Int64# -> Int64# -> Int
1041 foreign import "stg_eqInt64" stg_eqInt64 :: Int64# -> Int64# -> Int
1042 foreign import "stg_leInt64" stg_leInt64 :: Int64# -> Int64# -> Int
1043 foreign import "stg_ltInt64" stg_ltInt64 :: Int64# -> Int64# -> Int
1048 -- Code that's independent of Int64 rep.
1050 instance Enum Int64 where
1052 | i == maxBound = succError "Int64"
1056 | i == minBound = predError "Int64"
1059 toEnum i = intToInt64 i
1061 | x >= intToInt64 (minBound::Int) && x <= intToInt64 (maxBound::Int)
1064 = fromEnumError "Int64" x
1066 enumFrom e1 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger (maxBound::Int64)]
1067 enumFromTo e1 e2 = map integerToInt64 [int64ToInteger e1 .. int64ToInteger e2]
1068 enumFromThen e1 e2 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger last]
1072 | e2 < e1 = minBound
1073 | otherwise = maxBound
1075 enumFromThenTo e1 e2 e3 = map integerToInt64 [int64ToInteger e1, int64ToInteger e2 .. int64ToInteger e3]
1078 instance Show Int64 where
1079 showsPrec p i64 = showsPrec p (int64ToInteger i64)
1081 instance Read Int64 where
1082 readsPrec _ s = [ (integerToInt64 x,r) | (x,r) <- readDec s ]
1085 instance Ix Int64 where
1086 range (m,n) = [m..n]
1088 | inRange b i = int64ToInt (i-m)
1089 | otherwise = indexError i b "Int64"
1090 inRange (m,n) i = m <= i && i <= n
1092 instance Real Int64 where
1093 toRational x = toInteger x % 1
1096 sizeofInt64 :: Word32
1099 int8ToInteger :: Int8 -> Integer
1100 int8ToInteger i = toInteger i
1102 int16ToInteger :: Int16 -> Integer
1103 int16ToInteger i = toInteger i
1105 int32ToInteger :: Int32 -> Integer
1106 int32ToInteger i = toInteger i
1108 int64ToInt8 :: Int64 -> Int8
1109 int64ToInt8 = int32ToInt8 . int64ToInt32
1111 int64ToInt16 :: Int64 -> Int16
1112 int64ToInt16 = int32ToInt16 . int64ToInt32
1114 integerToInt8 :: Integer -> Int8
1115 integerToInt8 = fromInteger
1117 integerToInt16 :: Integer -> Int16
1118 integerToInt16 = fromInteger
1120 integerToInt32 :: Integer -> Int32
1121 integerToInt32 = fromInteger
1127 \subsection[Int Utils]{Miscellaneous utilities}
1131 Code copied from the Prelude
1134 absReal :: (Ord a, Num a) => a -> a
1135 absReal x | x >= 0 = x
1138 signumReal :: (Ord a, Num a) => a -> a
1139 signumReal x | x == 0 = 0
1145 indexInt8OffAddr :: Addr -> Int -> Int8
1146 indexInt8OffAddr (A# a#) (I# i#) = intToInt8 (I# (ord# (indexCharOffAddr# a# i#)))
1148 indexInt16OffAddr :: Addr -> Int -> Int16
1149 indexInt16OffAddr a i =
1150 #ifdef WORDS_BIGENDIAN
1151 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1153 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1157 l = indexInt8OffAddr a byte_idx
1158 h = indexInt8OffAddr a (byte_idx+1)
1160 indexInt32OffAddr :: Addr -> Int -> Int32
1161 indexInt32OffAddr (A# a#) i = intToInt32 (I# (indexIntOffAddr# a# i'#))
1163 -- adjust index to be in Int units, not Int32 ones.
1165 #if WORD_SIZE_IN_BYTES==8
1171 indexInt64OffAddr :: Addr -> Int -> Int64
1172 indexInt64OffAddr (A# a#) (I# i#)
1173 #if WORD_SIZE_IN_BYTES==8
1174 = I64# (indexIntOffAddr# a# i#)
1176 = I64# (indexInt64OffAddr# a# i#)
1179 #ifndef __PARALLEL_HASKELL__
1181 indexInt8OffForeignObj :: ForeignObj -> Int -> Int8
1182 indexInt8OffForeignObj (ForeignObj fo#) (I# i#) = intToInt8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
1184 indexInt16OffForeignObj :: ForeignObj -> Int -> Int16
1185 indexInt16OffForeignObj fo i =
1186 # ifdef WORDS_BIGENDIAN
1187 intToInt16 ( int8ToInt l + (int8ToInt maxBound) * int8ToInt h)
1189 intToInt16 ( int8ToInt h + (int8ToInt maxBound) * int8ToInt l)
1193 l = indexInt8OffForeignObj fo byte_idx
1194 h = indexInt8OffForeignObj fo (byte_idx+1)
1196 indexInt32OffForeignObj :: ForeignObj -> Int -> Int32
1197 indexInt32OffForeignObj (ForeignObj fo#) i = intToInt32 (I# (indexIntOffForeignObj# fo# i'#))
1199 -- adjust index to be in Int units, not Int32 ones.
1201 # if WORD_SIZE_IN_BYTES==8
1207 indexInt64OffForeignObj :: ForeignObj -> Int -> Int64
1208 indexInt64OffForeignObj (ForeignObj fo#) (I# i#)
1209 # if WORD_SIZE_IN_BYTES==8
1210 = I64# (indexIntOffForeignObj# fo# i#)
1212 = I64# (indexInt64OffForeignObj# fo# i#)
1215 #endif /* __PARALLEL_HASKELL__ */
1218 Read words out of mutable memory:
1221 readInt8OffAddr :: Addr -> Int -> IO Int8
1222 readInt8OffAddr a i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' a i
1224 readInt16OffAddr :: Addr -> Int -> IO Int16
1225 readInt16OffAddr a i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' a i
1227 readInt32OffAddr :: Addr -> Int -> IO Int32
1228 readInt32OffAddr a i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' a i
1230 readInt64OffAddr :: Addr -> Int -> IO Int64
1231 #if WORD_SIZE_IN_BYTES==8
1232 readInt64OffAddr a i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' a i
1234 readInt64OffAddr a i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' a i
1237 #ifndef __PARALLEL_HASKELL__
1239 readInt8OffForeignObj :: ForeignObj -> Int -> IO Int8
1240 readInt8OffForeignObj fo i = _casm_ `` %r=(StgInt8)(((StgInt8*)%0)[(StgInt)%1]); '' fo i
1242 readInt16OffForeignObj :: ForeignObj -> Int -> IO Int16
1243 readInt16OffForeignObj fo i = _casm_ `` %r=(StgInt16)(((StgInt16*)%0)[(StgInt)%1]); '' fo i
1245 readInt32OffForeignObj :: ForeignObj -> Int -> IO Int32
1246 readInt32OffForeignObj fo i = _casm_ `` %r=(StgInt32)(((StgInt32*)%0)[(StgInt)%1]); '' fo i
1248 readInt64OffForeignObj :: ForeignObj -> Int -> IO Int64
1249 # if WORD_SIZE_IN_BYTES==8
1250 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt)(((StgInt*)%0)[(StgInt)%1]); '' fo i
1252 readInt64OffForeignObj fo i = _casm_ `` %r=(StgInt64)(((StgInt64*)%0)[(StgInt)%1]); '' fo i
1255 #endif /* __PARALLEL_HASKELL__ */
1259 writeInt8OffAddr :: Addr -> Int -> Int8 -> IO ()
1260 writeInt8OffAddr a i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' a i e
1262 writeInt16OffAddr :: Addr -> Int -> Int16 -> IO ()
1263 writeInt16OffAddr a i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' a i e
1265 writeInt32OffAddr :: Addr -> Int -> Int32 -> IO ()
1266 writeInt32OffAddr a i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' a i e
1268 writeInt64OffAddr :: Addr -> Int -> Int64 -> IO ()
1269 #if WORD_SIZE_IN_BYTES==8
1270 writeInt64OffAddr a i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' a i e
1272 writeInt64OffAddr a i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' a i e
1275 #ifndef __PARALLEL_HASKELL__
1277 writeInt8OffForeignObj :: ForeignObj -> Int -> Int8 -> IO ()
1278 writeInt8OffForeignObj fo i e = _casm_ `` (((StgInt8*)%0)[(StgInt)%1])=(StgInt8)%2; '' fo i e
1280 writeInt16OffForeignObj :: ForeignObj -> Int -> Int16 -> IO ()
1281 writeInt16OffForeignObj fo i e = _casm_ `` (((StgInt16*)%0)[(StgInt)%1])=(StgInt16)%2; '' fo i e
1283 writeInt32OffForeignObj :: ForeignObj -> Int -> Int32 -> IO ()
1284 writeInt32OffForeignObj fo i e = _casm_ `` (((StgInt32*)%0)[(StgInt)%1])=(StgInt32)%2; '' fo i e
1286 writeInt64OffForeignObj :: ForeignObj -> Int -> Int64 -> IO ()
1287 # if WORD_SIZE_IN_BYTES==8
1288 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt*)%0)[(StgInt)%1])=(StgInt)%2; '' fo i e
1290 writeInt64OffForeignObj fo i e = _casm_ `` (((StgInt64*)%0)[(StgInt)%1])=(StgInt64)%2; '' fo i e
1293 #endif /* __PARALLEL_HASKELL__ */
1301 {-# NOINLINE indexError #-}
1302 indexError :: Show a => a -> (a,a) -> String -> b
1304 = error (showString "Ix{" . showString tp . showString "}.index: Index " .
1305 showParen True (showsPrec 0 i) .
1306 showString " out of range " $
1307 showParen True (showsPrec 0 rng) "")
1310 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1311 toEnumError inst_ty tag bnds
1312 = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1313 (showParen True (showsPrec 0 tag) $
1314 " is outside of bounds " ++
1317 fromEnumError :: (Show a,Show b) => String -> a -> b
1318 fromEnumError inst_ty tag
1319 = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1320 (showParen True (showsPrec 0 tag) $
1321 " is outside of Int's bounds " ++
1322 show (minBound::Int,maxBound::Int)))
1324 succError :: String -> a
1326 = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1328 predError :: String -> a
1330 = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1332 divZeroError :: (Show a) => String -> a -> b
1334 = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")
1340 -----------------------------------------------------------------------------
1341 -- The "official" coercion functions
1342 -----------------------------------------------------------------------------
1344 int8ToInt :: Int8 -> Int
1345 intToInt8 :: Int -> Int8
1346 int16ToInt :: Int16 -> Int
1347 intToInt16 :: Int -> Int16
1348 int32ToInt :: Int32 -> Int
1349 intToInt32 :: Int -> Int32
1351 -- And some non-exported ones
1353 int8ToInt16 :: Int8 -> Int16
1354 int8ToInt32 :: Int8 -> Int32
1355 int16ToInt8 :: Int16 -> Int8
1356 int16ToInt32 :: Int16 -> Int32
1357 int32ToInt8 :: Int32 -> Int8
1358 int32ToInt16 :: Int32 -> Int16
1360 int8ToInt16 = I16 . int8ToInt
1361 int8ToInt32 = I32 . int8ToInt
1362 int16ToInt8 = I8 . int16ToInt
1363 int16ToInt32 = I32 . int16ToInt
1364 int32ToInt8 = I8 . int32ToInt
1365 int32ToInt16 = I16 . int32ToInt
1367 -----------------------------------------------------------------------------
1369 -----------------------------------------------------------------------------
1371 newtype Int8 = I8 Int
1373 int8ToInt (I8 x) = if x' <= 0x7f then x' else x' - 0x100
1374 where x' = x `primAndInt` 0xff
1377 instance Eq Int8 where (==) = binop (==)
1378 instance Ord Int8 where compare = binop compare
1380 instance Num Int8 where
1381 x + y = to (binop (+) x y)
1382 x - y = to (binop (-) x y)
1383 negate = to . negate . from
1384 x * y = to (binop (*) x y)
1387 fromInteger = to . fromInteger
1390 instance Bounded Int8 where
1394 instance Real Int8 where
1395 toRational x = toInteger x % 1
1397 instance Integral Int8 where
1398 x `div` y = to (binop div x y)
1399 x `quot` y = to (binop quot x y)
1400 x `rem` y = to (binop rem x y)
1401 x `mod` y = to (binop mod x y)
1402 x `quotRem` y = to2 (binop quotRem x y)
1404 toInteger = toInteger . from
1405 toInt = toInt . from
1407 instance Ix Int8 where
1408 range (m,n) = [m..n]
1410 | inRange b i = from (i - m)
1411 | otherwise = error "index: Index out of range"
1412 inRange (m,n) i = m <= i && i <= n
1414 instance Enum Int8 where
1417 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int8)]
1418 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int8)]
1419 where last = if d < c then minBound else maxBound
1421 instance Read Int8 where
1422 readsPrec p s = [ (to x,r) | (x,r) <- readsPrec p s ]
1424 instance Show Int8 where
1425 showsPrec p = showsPrec p . from
1427 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
1428 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
1430 instance Bits Int8 where
1431 x .&. y = int32ToInt8 (binop8 (.&.) x y)
1432 x .|. y = int32ToInt8 (binop8 (.|.) x y)
1433 x `xor` y = int32ToInt8 (binop8 xor x y)
1434 complement = int32ToInt8 . complement . int8ToInt32
1435 x `shift` i = int32ToInt8 (int8ToInt32 x `shift` i)
1437 bit = int32ToInt8 . bit
1438 setBit x i = int32ToInt8 (setBit (int8ToInt32 x) i)
1439 clearBit x i = int32ToInt8 (clearBit (int8ToInt32 x) i)
1440 complementBit x i = int32ToInt8 (complementBit (int8ToInt32 x) i)
1441 testBit x i = testBit (int8ToInt32 x) i
1445 int8ToInteger = error "TODO: int8ToInteger"
1446 integerToInt8 = error "TODO: integerToInt8"
1448 --intToInt8 = fromInt
1451 sizeofInt8 :: Word32
1454 -----------------------------------------------------------------------------
1456 -----------------------------------------------------------------------------
1458 newtype Int16 = I16 Int
1460 int16ToInt (I16 x) = if x' <= 0x7fff then x' else x' - 0x10000
1461 where x' = x `primAndInt` 0xffff
1464 instance Eq Int16 where (==) = binop (==)
1465 instance Ord Int16 where compare = binop compare
1467 instance Num Int16 where
1468 x + y = to (binop (+) x y)
1469 x - y = to (binop (-) x y)
1470 negate = to . negate . from
1471 x * y = to (binop (*) x y)
1474 fromInteger = to . fromInteger
1477 instance Bounded Int16 where
1481 instance Real Int16 where
1482 toRational x = toInteger x % 1
1484 instance Integral Int16 where
1485 x `div` y = to (binop div x y)
1486 x `quot` y = to (binop quot x y)
1487 x `rem` y = to (binop rem x y)
1488 x `mod` y = to (binop mod x y)
1489 x `quotRem` y = to2 (binop quotRem x y)
1491 toInteger = toInteger . from
1492 toInt = toInt . from
1494 instance Ix Int16 where
1495 range (m,n) = [m..n]
1497 | inRange b i = from (i - m)
1498 | otherwise = error "index: Index out of range"
1499 inRange (m,n) i = m <= i && i <= n
1501 instance Enum Int16 where
1504 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int16)]
1505 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int16)]
1506 where last = if d < c then minBound else maxBound
1508 instance Read Int16 where
1509 readsPrec p s = [ (to x,r) | (x,r) <- readsPrec p s ]
1511 instance Show Int16 where
1512 showsPrec p = showsPrec p . from
1514 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
1515 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
1517 instance Bits Int16 where
1518 x .&. y = int32ToInt16 (binop16 (.&.) x y)
1519 x .|. y = int32ToInt16 (binop16 (.|.) x y)
1520 x `xor` y = int32ToInt16 (binop16 xor x y)
1521 complement = int32ToInt16 . complement . int16ToInt32
1522 x `shift` i = int32ToInt16 (int16ToInt32 x `shift` i)
1524 bit = int32ToInt16 . bit
1525 setBit x i = int32ToInt16 (setBit (int16ToInt32 x) i)
1526 clearBit x i = int32ToInt16 (clearBit (int16ToInt32 x) i)
1527 complementBit x i = int32ToInt16 (complementBit (int16ToInt32 x) i)
1528 testBit x i = testBit (int16ToInt32 x) i
1532 int16ToInteger = error "TODO: int16ToInteger"
1533 integerToInt16 = error "TODO: integerToInt16"
1535 --intToInt16 = fromInt
1536 --int16ToInt = toInt
1538 sizeofInt16 :: Word32
1541 -----------------------------------------------------------------------------
1543 -----------------------------------------------------------------------------
1545 newtype Int32 = I32 Int
1547 int32ToInt (I32 x) = x
1550 instance Eq Int32 where (==) = binop (==)
1551 instance Ord Int32 where compare = binop compare
1553 instance Num Int32 where
1554 x + y = to (binop (+) x y)
1555 x - y = to (binop (-) x y)
1556 negate = to . negate . from
1557 x * y = to (binop (*) x y)
1560 fromInteger = to . fromInteger
1563 instance Bounded Int32 where
1564 minBound = to minBound
1565 maxBound = to maxBound
1567 instance Real Int32 where
1568 toRational x = toInteger x % 1
1570 instance Integral Int32 where
1571 x `div` y = to (binop div x y)
1572 x `quot` y = to (binop quot x y)
1573 x `rem` y = to (binop rem x y)
1574 x `mod` y = to (binop mod x y)
1575 x `quotRem` y = to2 (binop quotRem x y)
1577 toInteger = toInteger . from
1578 toInt = toInt . from
1580 instance Ix Int32 where
1581 range (m,n) = [m..n]
1583 | inRange b i = from (i - m)
1584 | otherwise = error "index: Index out of range"
1585 inRange (m,n) i = m <= i && i <= n
1587 instance Enum Int32 where
1590 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int32)]
1591 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int32)]
1592 where last = if d < c then minBound else maxBound
1594 instance Read Int32 where
1595 readsPrec p s = [ (to x,r) | (x,r) <- readsPrec p s ]
1597 instance Show Int32 where
1598 showsPrec p = showsPrec p . from
1600 instance Bits Int32 where
1601 (.&.) x y = to (binop primAndInt x y)
1602 (.|.) x y = to (binop primOrInt x y)
1603 xor x y = to (binop primXorInt x y)
1605 complement = xor ((-1) :: Int32)
1606 x `shift` i | i == 0 = x
1607 | i > 0 = to (primShiftLInt (from x) i)
1608 | i < 0 = to (primShiftRAInt (from x) (-i))
1611 setBit x i = x .|. bit i
1612 clearBit x i = x .&. complement (bit i)
1613 complementBit x i = x `xor` bit i
1615 testBit x i = (0x1 .&. shift x i) == (0x1 :: Int32)
1620 int32ToInteger = error "TODO: int32ToInteger"
1621 integerToInt32 = error "TODO: integerToInt32"
1623 sizeofInt32 :: Word32
1626 -----------------------------------------------------------------------------
1629 -- This is not ideal, but does have the advantage that you can
1630 -- now typecheck generated code that include Int64 statements.
1632 -----------------------------------------------------------------------------
1634 type Int64 = Integer
1636 int64ToInteger = error "TODO: int64ToInteger"
1638 integerToInt64 = error "TODO: integerToInt64"
1640 int64ToInt32 = error "TODO: int64ToInt32"
1641 int64ToInt16 = error "TODO: int64ToInt16"
1642 int64ToInt8 = error "TODO: int64ToInt8"
1644 int32ToInt64 = error "TODO: int32ToInt64"
1645 int16ToInt64 = error "TODO: int16ToInt64"
1646 int8ToInt64 = error "TODO: int8ToInt64"
1648 intToInt64 = fromInt
1651 sizeofInt64 :: Word32
1654 -----------------------------------------------------------------------------
1655 -- End of exported definitions
1657 -- The remainder of this file consists of definitions which are only
1658 -- used in the implementation.
1659 -----------------------------------------------------------------------------
1661 -----------------------------------------------------------------------------
1662 -- Coercions - used to make the instance declarations more uniform
1663 -----------------------------------------------------------------------------
1665 class Coerce a where
1669 instance Coerce Int32 where
1673 instance Coerce Int8 where
1677 instance Coerce Int16 where
1681 binop :: Coerce int => (Int -> Int -> a) -> (int -> int -> a)
1682 binop op x y = from x `op` from y
1684 to2 :: Coerce int => (Int, Int) -> (int, int)
1685 to2 (x,y) = (to x, to y)
1687 -----------------------------------------------------------------------------
1689 -----------------------------------------------------------------------------
1691 --primitive primAnd "primAndInt" :: Int -> Int -> Int
1693 --primitive primAndInt :: Int32 -> Int32 -> Int32
1694 --primitive primOrInt :: Int32 -> Int32 -> Int32
1695 --primitive primXorInt :: Int32 -> Int32 -> Int32
1696 --primitive primComplementInt :: Int32 -> Int32
1697 --primitive primShiftInt :: Int32 -> Int -> Int32
1698 --primitive primBitInt :: Int -> Int32
1699 --primitive primTestInt :: Int32 -> Int -> Bool
1701 -----------------------------------------------------------------------------
1702 -- Code copied from the Prelude
1703 -----------------------------------------------------------------------------
1705 absReal x | x >= 0 = x
1708 signumReal x | x == 0 = 0
1712 -----------------------------------------------------------------------------
1714 -----------------------------------------------------------------------------
1716 intToWord :: Int -> Word
1717 intToWord i = primIntToWord i