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.
11 -----------------------------------------------------------------------------
13 -- Suitable for use with Hugs 1.4 on 32 bit systems.
14 -----------------------------------------------------------------------------
21 , int8ToInt -- :: Int8 -> Int
22 , intToInt8 -- :: Int -> Int8
23 , int16ToInt -- :: Int16 -> Int
24 , intToInt16 -- :: Int -> Int16
25 , int32ToInt -- :: Int32 -> Int
26 , intToInt32 -- :: Int -> Int32
27 , intToInt64 -- :: Int -> Int64
28 -- plus Eq, Ord, Num, Bounded, Real, Integral, Ix, Enum, Read,
29 -- Show and Bits instances for each of Int8, Int16, Int32 and Int64
40 -----------------------------------------------------------------------------
41 -- The "official" coercion functions
42 -----------------------------------------------------------------------------
44 int8ToInt :: Int8 -> Int
45 intToInt8 :: Int -> Int8
46 int16ToInt :: Int16 -> Int
47 intToInt16 :: Int -> Int16
48 int32ToInt :: Int32 -> Int
49 intToInt32 :: Int -> Int32
51 -- And some non-exported ones
53 int8ToInt16 :: Int8 -> Int16
54 int8ToInt32 :: Int8 -> Int32
55 int16ToInt8 :: Int16 -> Int8
56 int16ToInt32 :: Int16 -> Int32
57 int32ToInt8 :: Int32 -> Int8
58 int32ToInt16 :: Int32 -> Int16
60 int8ToInt16 (I8# x) = I16# x
61 int8ToInt32 (I8# x) = I32# x
62 int16ToInt8 (I16# x) = I8# x
63 int16ToInt32 (I16# x) = I32# x
64 int32ToInt8 (I32# x) = I8# x
65 int32ToInt16 (I32# x) = I16# x
68 \subsection[Int8]{The @Int8@ interface}
72 instance CCallable Int8
73 instance CReturnable Int8
75 int8ToInt (I8# x) = I# (int8ToInt# x)
76 int8ToInt# x = if x' <=# 0x7f# then x' else x' -# 0x100#
77 where x' = word2Int# (int2Word# x `and#` int2Word# 0xff#)
80 -- This doesn't perform any bounds checking
81 -- on the value it is passed, nor its sign.
82 -- i.e., show (intToInt8 511) => "-1"
84 intToInt8 (I# x) = I8# (intToInt8# x)
85 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
87 instance Eq Int8 where
88 (I8# x#) == (I8# y#) = x# ==# y#
89 (I8# x#) /= (I8# y#) = x# /=# y#
91 instance Ord Int8 where
92 compare (I8# x#) (I8# y#) = compareInt# (int8ToInt# x#) (int8ToInt# y#)
94 compareInt# :: Int# -> Int# -> Ordering
100 instance Num Int8 where
101 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
102 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
103 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
107 else I8# (0x100# -# x#)
111 fromInteger (J# a# s# d#)
112 = case (integer2Int# a# s# d#) of { i# -> I8# (intToInt8# i#) }
115 instance Bounded Int8 where
119 instance Real Int8 where
120 toRational x = toInteger x % 1
122 instance Integral Int8 where
123 div x@(I8# x#) y@(I8# y#) =
124 if x > 0 && y < 0 then quotInt8 (x-y-1) y
125 else if x < 0 && y > 0 then quotInt8 (x-y+1) y
127 quot x@(I8# _) y@(I8# y#) =
130 else error "Integral.Int8.quot: divide by 0\n"
131 rem x@(I8# _) y@(I8# y#) =
134 else error "Integral.Int8.rem: divide by 0\n"
135 mod x@(I8# x#) y@(I8# y#) =
136 if x > 0 && y < 0 || x < 0 && y > 0 then
137 if r/=0 then r+y else 0
140 where r = remInt8 x y
141 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
142 toInteger i8 = toInteger (int8ToInt i8)
143 toInt i8 = int8ToInt i8
145 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `remInt#` (int8ToInt# y)))
146 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `quotInt#` (int8ToInt# y)))
148 instance Ix Int8 where
151 | inRange b i = int8ToInt (i - m)
152 | otherwise = error (showString "Ix{Int8}.index: Index " .
153 showParen True (showsPrec 0 i) .
154 showString " out of range " $
155 showParen True (showsPrec 0 b) "")
156 inRange (m,n) i = m <= i && i <= n
158 instance Enum Int8 where
161 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int8)]
162 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int8)]
163 where last = if d < c then minBound else maxBound
165 instance Read Int8 where
166 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
168 instance Show Int8 where
169 showsPrec p i8 = showsPrec p (int8ToInt i8)
171 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
172 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
174 instance Bits Int8 where
175 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
176 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
177 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
178 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
179 shift (I8# x) i@(I# i#)
180 | i > 0 = I8# (intToInt8# (iShiftL# (int8ToInt# x) i#))
181 | otherwise = I8# (intToInt8# (iShiftRA# (int8ToInt# x) i#))
182 i8@(I8# x) `rotate` (I# i)
185 I8# (intToInt8# ( word2Int# (
186 (int2Word# (iShiftL# (int8ToInt# x) i'))
188 (int2Word# (iShiftRA# (word2Int# (
190 (int2Word# (0x100# -# pow2# i2))))
192 | otherwise = rotate i8 (I# (8# +# i))
194 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
197 setBit x i = x .|. bit i
198 clearBit x i = x .&. complement (bit i)
199 complementBit x i = x `xor` bit i
200 testBit x i = (x .&. bit i) /= 0
204 pow2# :: Int# -> Int#
205 pow2# x# = iShiftL# 1# x#
208 \subsection[Int16]{The @Int16@ interface}
211 data Int16 = I16# Int#
212 instance CCallable Int16
213 instance CReturnable Int16
215 int16ToInt (I16# x) = I# (int16ToInt# x)
217 int16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
218 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
220 intToInt16 (I# x) = I16# (intToInt16# x)
221 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
223 instance Eq Int16 where
224 (I16# x#) == (I16# y#) = x# ==# y#
225 (I16# x#) /= (I16# y#) = x# /=# y#
227 instance Ord Int16 where
228 compare (I16# x#) (I16# y#) = compareInt# (int16ToInt# x#) (int16ToInt# y#)
230 instance Num Int16 where
231 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
232 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
233 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
237 else I16# (0x10000# -# x#)
240 fromInteger (J# a# s# d#)
241 = case (integer2Int# a# s# d#) of { i# -> I16# (intToInt16# i#) }
244 instance Bounded Int16 where
248 instance Real Int16 where
249 toRational x = toInteger x % 1
251 instance Integral Int16 where
252 div x@(I16# x#) y@(I16# y#) =
253 if x > 0 && y < 0 then quotInt16 (x-y-1) y
254 else if x < 0 && y > 0 then quotInt16 (x-y+1) y
256 quot x@(I16# _) y@(I16# y#) =
259 else error "Integral.Int16.quot: divide by 0\n"
260 rem x@(I16# _) y@(I16# y#) =
263 else error "Integral.Int16.rem: divide by 0\n"
264 mod x@(I16# x#) y@(I16# y#) =
265 if x > 0 && y < 0 || x < 0 && y > 0 then
266 if r/=0 then r+y else 0
269 where r = remInt16 x y
270 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
271 toInteger i16 = toInteger (int16ToInt i16)
272 toInt i16 = int16ToInt i16
274 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `remInt#` (int16ToInt# y)))
275 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `quotInt#` (int16ToInt# y)))
277 instance Ix Int16 where
280 | inRange b i = int16ToInt (i - m)
281 | otherwise = error (showString "Ix{Int16}.index: Index " .
282 showParen True (showsPrec 0 i) .
283 showString " out of range " $
284 showParen True (showsPrec 0 b) "")
285 inRange (m,n) i = m <= i && i <= n
287 instance Enum Int16 where
289 fromEnum = int16ToInt
290 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int16)]
291 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int16)]
292 where last = if d < c then minBound else maxBound
294 instance Read Int16 where
295 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
297 instance Show Int16 where
298 showsPrec p i16 = showsPrec p (int16ToInt i16)
300 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
301 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
303 instance Bits Int16 where
304 (I16# x) .&. (I16# y) = I16# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
305 (I16# x) .|. (I16# y) = I16# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
306 (I16# x) `xor` (I16# y) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
307 complement (I16# x) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffff#)))
308 shift (I16# x) i@(I# i#)
309 | i > 0 = I16# (intToInt16# (iShiftL# (int16ToInt# x) i#))
310 | otherwise = I16# (intToInt16# (iShiftRA# (int16ToInt# x) i#))
311 i16@(I16# x) `rotate` (I# i)
314 I16# (intToInt16# (word2Int# (
315 (int2Word# (iShiftL# (int16ToInt# x) i'))
317 (int2Word# (iShiftRA# ( word2Int# (
318 (int2Word# x) `and#` (int2Word# (0x100# -# pow2# i2))))
320 | otherwise = rotate i16 (I# (16# +# i))
322 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
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
335 \subsection[Int32]{The @Int32@ interface}
340 data Int32 = I32# Int#
341 instance CCallable Int32
342 instance CReturnable Int32
344 int32ToInt (I32# x) = I# (int32ToInt# x)
346 int32ToInt# :: Int# -> Int#
347 #if WORD_SIZE_IN_BYTES > 4
348 int32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
349 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
354 intToInt32 (I# x) = I32# (intToInt32# x)
355 intToInt32# :: Int# -> Int#
356 #if WORD_SIZE_IN_BYTES > 4
357 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
362 instance Eq Int32 where
363 (I32# x#) == (I32# y#) = x# ==# y#
364 (I32# x#) /= (I32# y#) = x# /=# y#
366 instance Ord Int32 where
367 compare (I32# x#) (I32# y#) = compareInt# (int32ToInt# x#) (int32ToInt# y#)
369 instance Num Int32 where
370 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
371 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
372 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
373 #if WORD_SIZE_IN_BYTES > 4
377 else I32# (intToInt32# (0x100000000# -# x'))
379 negate (I32# x) = I32# (negateInt# x)
383 fromInteger (J# a# s# d#)
384 = case (integer2Int# a# s# d#) of { i# -> I32# (intToInt32# i#) }
387 -- ToDo: remove LitLit when minBound::Int is fixed (currently it's one
388 -- too high, and doesn't allow the correct minBound to be defined here).
389 instance Bounded Int32 where
390 minBound = case ``0x80000000'' of { I# x -> I32# x }
391 maxBound = I32# 0x7fffffff#
393 instance Real Int32 where
394 toRational x = toInteger x % 1
396 instance Integral Int32 where
397 div x@(I32# x#) y@(I32# y#) =
398 if x > 0 && y < 0 then quotInt32 (x-y-1) y
399 else if x < 0 && y > 0 then quotInt32 (x-y+1) y
401 quot x@(I32# _) y@(I32# y#) =
404 else error "Integral.Int32.quot: divide by 0\n"
405 rem x@(I32# _) y@(I32# y#) =
408 else error "Integral.Int32.rem: divide by 0\n"
409 mod x@(I32# x#) y@(I32# y#) =
410 if x > 0 && y < 0 || x < 0 && y > 0 then
411 if r/=0 then r+y else 0
414 where r = remInt32 x y
415 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
416 toInteger i32 = toInteger (int32ToInt i32)
417 toInt i32 = int32ToInt i32
419 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `remInt#` (int32ToInt# y)))
420 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `quotInt#` (int32ToInt# y)))
422 instance Ix Int32 where
425 | inRange b i = int32ToInt (i - m)
426 | otherwise = error (showString "Ix{Int32}.index: Index " .
427 showParen True (showsPrec 0 i) .
428 showString " out of range " $
429 showParen True (showsPrec 0 b) "")
430 inRange (m,n) i = m <= i && i <= n
432 instance Enum Int32 where
434 fromEnum = int32ToInt
435 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int32)]
436 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int32)]
437 where last = if d < c then minBound else maxBound
439 instance Read Int32 where
440 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
442 instance Show Int32 where
443 showsPrec p i32 = showsPrec p (int32ToInt i32)
445 instance Bits Int32 where
446 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
447 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
448 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
449 #if WORD_SIZE_IN_BYTES > 4
450 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
452 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
454 shift (I32# x) i@(I# i#)
455 | i > 0 = I32# (intToInt32# (iShiftL# (int32ToInt# x) i#))
456 | otherwise = I32# (intToInt32# (iShiftRA# (int32ToInt# x) i#))
457 i32@(I32# x) `rotate` (I# i)
460 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
461 I32# (intToInt32# ( word2Int# (
462 (int2Word# (iShiftL# (int32ToInt# x) i'))
464 (int2Word# (iShiftRA# (word2Int# (
467 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
469 | otherwise = rotate i32 (I# (32# +# i))
471 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
473 (I32# maxBound#) = maxBound
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
484 \subsection[Int64]{The @Int64@ interface}
487 data Int64 = I64 {lo,hi::Int32} deriving (Eq, Ord, Bounded)
489 i64ToInteger I64{lo,hi} = toInteger lo + 0x100000000 * toInteger hi
490 integerToI64 x = case x `quotRem` 0x100000000 of
491 (h,l) -> I64{lo=fromInteger l, hi=fromInteger h}
493 intToInt64 :: Int -> Int64
494 intToInt64 x = I64{lo=intToInt32 x, hi=0}
496 instance Show Int64 where
497 showsPrec p x = showsPrec p (i64ToInteger x)
499 instance Read Int64 where
500 readsPrec p s = [ (integerToI64 x,r) | (x,r) <- readDec s ]
506 \subsection[Int Utils]{Miscellaneous utilities}
510 Code copied from the Prelude
513 absReal x | x >= 0 = x
516 signumReal x | x == 0 = 0