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.
10 {-# OPTIONS -fno-implicit-prelude #-}
12 -----------------------------------------------------------------------------
14 -- Suitable for use with Hugs 1.4 on 32 bit systems.
15 -----------------------------------------------------------------------------
22 , int8ToInt -- :: Int8 -> Int
23 , intToInt8 -- :: Int -> Int8
24 , int16ToInt -- :: Int16 -> Int
25 , intToInt16 -- :: Int -> Int16
26 , int32ToInt -- :: Int32 -> Int
27 , intToInt32 -- :: Int -> Int32
28 -- plus Eq, Ord, Num, Bounded, Real, Integral, Ix, Enum, Read,
29 -- Show and Bits instances for each of Int8, Int16 and Int32
36 import GHCerr ( error )
41 -----------------------------------------------------------------------------
42 -- The "official" coercion functions
43 -----------------------------------------------------------------------------
45 int8ToInt :: Int8 -> Int
46 intToInt8 :: Int -> Int8
47 int16ToInt :: Int16 -> Int
48 intToInt16 :: Int -> Int16
49 int32ToInt :: Int32 -> Int
50 intToInt32 :: Int -> Int32
52 -- And some non-exported ones
54 int8ToInt16 :: Int8 -> Int16
55 int8ToInt32 :: Int8 -> Int32
56 int16ToInt8 :: Int16 -> Int8
57 int16ToInt32 :: Int16 -> Int32
58 int32ToInt8 :: Int32 -> Int8
59 int32ToInt16 :: Int32 -> Int16
61 int8ToInt16 (I8# x) = I16# x
62 int8ToInt32 (I8# x) = I32# x
63 int16ToInt8 (I16# x) = I8# x
64 int16ToInt32 (I16# x) = I32# x
65 int32ToInt8 (I32# x) = I8# x
66 int32ToInt16 (I32# x) = I16# x
69 \subsection[Int8]{The @Int8@ interface}
73 instance CCallable Int8
74 instance CReturnable Int8
76 int8ToInt (I8# x) = I# (int8ToInt# x)
77 int8ToInt# x = if x' <=# 0x7f# then x' else x' -# 0x100#
78 where x' = word2Int# (int2Word# x `and#` int2Word# 0xff#)
81 -- This doesn't perform any bounds checking
82 -- on the value it is passed, nor its sign.
83 -- i.e., show (intToInt8 511) => "-1"
85 intToInt8 (I# x) = I8# (intToInt8# x)
86 intToInt8# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xff#)
88 instance Eq Int8 where
89 (I8# x#) == (I8# y#) = x# ==# y#
90 (I8# x#) /= (I8# y#) = x# /=# y#
92 instance Ord Int8 where
93 compare (I8# x#) (I8# y#) = compareInt# (int8ToInt# x#) (int8ToInt# y#)
95 compareInt# :: Int# -> Int# -> Ordering
101 instance Num Int8 where
102 (I8# x#) + (I8# y#) = I8# (intToInt8# (x# +# y#))
103 (I8# x#) - (I8# y#) = I8# (intToInt8# (x# -# y#))
104 (I8# x#) * (I8# y#) = I8# (intToInt8# (x# *# y#))
108 else I8# (0x100# -# x#)
112 fromInteger (J# a# s# d#)
113 = case (integer2Int# a# s# d#) of { i# -> I8# (intToInt8# i#) }
116 instance Bounded Int8 where
120 instance Real Int8 where
121 toRational x = toInteger x % 1
123 instance Integral Int8 where
124 div x@(I8# x#) y@(I8# y#) =
125 if x > 0 && y < 0 then quotInt8 (x-y-1) y
126 else if x < 0 && y > 0 then quotInt8 (x-y+1) y
128 quot x@(I8# _) y@(I8# y#) =
131 else error "Integral.Int8.quot: divide by 0\n"
132 rem x@(I8# _) y@(I8# y#) =
135 else error "Integral.Int8.rem: divide by 0\n"
136 mod x@(I8# x#) y@(I8# y#) =
137 if x > 0 && y < 0 || x < 0 && y > 0 then
138 if r/=0 then r+y else 0
141 where r = remInt8 x y
142 a@(I8# _) `quotRem` b@(I8# _) = (a `quotInt8` b, a `remInt8` b)
143 toInteger i8 = toInteger (int8ToInt i8)
144 toInt i8 = int8ToInt i8
146 remInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `remInt#` (int8ToInt# y)))
147 quotInt8 (I8# x) (I8# y) = I8# (intToInt8# ((int8ToInt# x) `quotInt#` (int8ToInt# y)))
149 instance Ix Int8 where
152 | inRange b i = int8ToInt (i - m)
153 | otherwise = error (showString "Ix{Int8}.index: Index " .
154 showParen True (showsPrec 0 i) .
155 showString " out of range " $
156 showParen True (showsPrec 0 b) "")
157 inRange (m,n) i = m <= i && i <= n
159 instance Enum Int8 where
162 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int8)]
163 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int8)]
164 where last = if d < c then minBound else maxBound
166 instance Read Int8 where
167 readsPrec p s = [ (intToInt8 x,r) | (x,r) <- readsPrec p s ]
169 instance Show Int8 where
170 showsPrec p i8 = showsPrec p (int8ToInt i8)
172 binop8 :: (Int32 -> Int32 -> a) -> (Int8 -> Int8 -> a)
173 binop8 op x y = int8ToInt32 x `op` int8ToInt32 y
175 instance Bits Int8 where
176 (I8# x) .&. (I8# y) = I8# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
177 (I8# x) .|. (I8# y) = I8# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
178 (I8# x) `xor` (I8# y) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
179 complement (I8# x) = I8# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xff#)))
180 shift (I8# x) i@(I# i#)
181 | i > 0 = I8# (intToInt8# (iShiftL# (int8ToInt# x) i#))
182 | otherwise = I8# (intToInt8# (iShiftRA# (int8ToInt# x) i#))
183 i8@(I8# x) `rotate` (I# i)
186 I8# (intToInt8# ( word2Int# (
187 (int2Word# (iShiftL# (int8ToInt# x) i'))
189 (int2Word# (iShiftRA# (word2Int# (
191 (int2Word# (0x100# -# pow2# i2))))
193 | otherwise = rotate i8 (I# (8# +# i))
195 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
198 setBit x i = x .|. bit i
199 clearBit x i = x .&. complement (bit i)
200 complementBit x i = x `xor` bit i
201 testBit x i = (x .&. bit i) /= 0
205 pow2# :: Int# -> Int#
206 pow2# x# = iShiftL# 1# x#
209 \subsection[Int16]{The @Int16@ interface}
212 data Int16 = I16# Int#
213 instance CCallable Int16
214 instance CReturnable Int16
216 int16ToInt (I16# x) = I# (int16ToInt# x)
218 int16ToInt# x = if x' <=# 0x7fff# then x' else x' -# 0x10000#
219 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffff#)
221 intToInt16 (I# x) = I16# (intToInt16# x)
222 intToInt16# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffff#)
224 instance Eq Int16 where
225 (I16# x#) == (I16# y#) = x# ==# y#
226 (I16# x#) /= (I16# y#) = x# /=# y#
228 instance Ord Int16 where
229 compare (I16# x#) (I16# y#) = compareInt# (int16ToInt# x#) (int16ToInt# y#)
231 instance Num Int16 where
232 (I16# x#) + (I16# y#) = I16# (intToInt16# (x# +# y#))
233 (I16# x#) - (I16# y#) = I16# (intToInt16# (x# -# y#))
234 (I16# x#) * (I16# y#) = I16# (intToInt16# (x# *# y#))
238 else I16# (0x10000# -# x#)
241 fromInteger (J# a# s# d#)
242 = case (integer2Int# a# s# d#) of { i# -> I16# (intToInt16# i#) }
245 instance Bounded Int16 where
249 instance Real Int16 where
250 toRational x = toInteger x % 1
252 instance Integral Int16 where
253 div x@(I16# x#) y@(I16# y#) =
254 if x > 0 && y < 0 then quotInt16 (x-y-1) y
255 else if x < 0 && y > 0 then quotInt16 (x-y+1) y
257 quot x@(I16# _) y@(I16# y#) =
260 else error "Integral.Int16.quot: divide by 0\n"
261 rem x@(I16# _) y@(I16# y#) =
264 else error "Integral.Int16.rem: divide by 0\n"
265 mod x@(I16# x#) y@(I16# y#) =
266 if x > 0 && y < 0 || x < 0 && y > 0 then
267 if r/=0 then r+y else 0
270 where r = remInt16 x y
271 a@(I16# _) `quotRem` b@(I16# _) = (a `quotInt16` b, a `remInt16` b)
272 toInteger i16 = toInteger (int16ToInt i16)
273 toInt i16 = int16ToInt i16
275 remInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `remInt#` (int16ToInt# y)))
276 quotInt16 (I16# x) (I16# y) = I16# (intToInt16# ((int16ToInt# x) `quotInt#` (int16ToInt# y)))
278 instance Ix Int16 where
281 | inRange b i = int16ToInt (i - m)
282 | otherwise = error (showString "Ix{Int16}.index: Index " .
283 showParen True (showsPrec 0 i) .
284 showString " out of range " $
285 showParen True (showsPrec 0 b) "")
286 inRange (m,n) i = m <= i && i <= n
288 instance Enum Int16 where
290 fromEnum = int16ToInt
291 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int16)]
292 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int16)]
293 where last = if d < c then minBound else maxBound
295 instance Read Int16 where
296 readsPrec p s = [ (intToInt16 x,r) | (x,r) <- readsPrec p s ]
298 instance Show Int16 where
299 showsPrec p i16 = showsPrec p (int16ToInt i16)
301 binop16 :: (Int32 -> Int32 -> a) -> (Int16 -> Int16 -> a)
302 binop16 op x y = int16ToInt32 x `op` int16ToInt32 y
304 instance Bits Int16 where
305 (I16# x) .&. (I16# y) = I16# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
306 (I16# x) .|. (I16# y) = I16# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
307 (I16# x) `xor` (I16# y) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
308 complement (I16# x) = I16# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffff#)))
309 shift (I16# x) i@(I# i#)
310 | i > 0 = I16# (intToInt16# (iShiftL# (int16ToInt# x) i#))
311 | otherwise = I16# (intToInt16# (iShiftRA# (int16ToInt# x) i#))
312 i16@(I16# x) `rotate` (I# i)
315 I16# (intToInt16# (word2Int# (
316 (int2Word# (iShiftL# (int16ToInt# x) i'))
318 (int2Word# (iShiftRA# ( word2Int# (
319 (int2Word# x) `and#` (int2Word# (0x100# -# pow2# i2))))
321 | otherwise = rotate i16 (I# (16# +# i))
323 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
326 setBit x i = x .|. bit i
327 clearBit x i = x .&. complement (bit i)
328 complementBit x i = x `xor` bit i
329 testBit x i = (x .&. bit i) /= 0
334 \subsection[Int32]{The @Int32@ interface}
337 data Int32 = I32# Int#
338 instance CCallable Int32
339 instance CReturnable Int32
341 int32ToInt (I32# x) = I# (int32ToInt# x)
343 int32ToInt# :: Int# -> Int#
344 #if WORD_SIZE_IN_BYTES > 4
345 int32ToInt# x = if x' <=# 0x7fffffff# then x' else x' -# 0x100000000#
346 where x' = word2Int# (int2Word# x `and#` int2Word# 0xffffffff#)
351 intToInt32 (I# x) = I32# (intToInt32# x)
352 intToInt32# :: Int# -> Int#
353 #if WORD_SIZE_IN_BYTES > 4
354 intToInt32# i# = word2Int# ((int2Word# i#) `and#` int2Word# 0xffffffff#)
359 instance Eq Int32 where
360 (I32# x#) == (I32# y#) = x# ==# y#
361 (I32# x#) /= (I32# y#) = x# /=# y#
363 instance Ord Int32 where
364 compare (I32# x#) (I32# y#) = compareInt# (int32ToInt# x#) (int32ToInt# y#)
366 instance Num Int32 where
367 (I32# x#) + (I32# y#) = I32# (intToInt32# (x# +# y#))
368 (I32# x#) - (I32# y#) = I32# (intToInt32# (x# -# y#))
369 (I32# x#) * (I32# y#) = I32# (intToInt32# (x# *# y#))
370 #if WORD_SIZE_IN_BYTES > 4
374 else I32# (intToInt32# (0x100000000# -# x'))
376 negate (I32# x) = I32# (negateInt# x)
380 fromInteger (J# a# s# d#)
381 = case (integer2Int# a# s# d#) of { i# -> I32# (intToInt32# i#) }
384 -- ToDo: remove LitLit when minBound::Int is fixed (currently it's one
385 -- too high, and doesn't allow the correct minBound to be defined here).
386 instance Bounded Int32 where
387 minBound = case ``0x80000000'' of { I# x -> I32# x }
388 maxBound = I32# 0x7fffffff#
390 instance Real Int32 where
391 toRational x = toInteger x % 1
393 instance Integral Int32 where
394 div x@(I32# x#) y@(I32# y#) =
395 if x > 0 && y < 0 then quotInt32 (x-y-1) y
396 else if x < 0 && y > 0 then quotInt32 (x-y+1) y
398 quot x@(I32# _) y@(I32# y#) =
401 else error "Integral.Int32.quot: divide by 0\n"
402 rem x@(I32# _) y@(I32# y#) =
405 else error "Integral.Int32.rem: divide by 0\n"
406 mod x@(I32# x#) y@(I32# y#) =
407 if x > 0 && y < 0 || x < 0 && y > 0 then
408 if r/=0 then r+y else 0
411 where r = remInt32 x y
412 a@(I32# _) `quotRem` b@(I32# _) = (a `quotInt32` b, a `remInt32` b)
413 toInteger i32 = toInteger (int32ToInt i32)
414 toInt i32 = int32ToInt i32
416 remInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `remInt#` (int32ToInt# y)))
417 quotInt32 (I32# x) (I32# y) = I32# (intToInt32# ((int32ToInt# x) `quotInt#` (int32ToInt# y)))
419 instance Ix Int32 where
422 | inRange b i = int32ToInt (i - m)
423 | otherwise = error (showString "Ix{Int32}.index: Index " .
424 showParen True (showsPrec 0 i) .
425 showString " out of range " $
426 showParen True (showsPrec 0 b) "")
427 inRange (m,n) i = m <= i && i <= n
429 instance Enum Int32 where
431 fromEnum = int32ToInt
432 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Int32)]
433 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Int32)]
434 where last = if d < c then minBound else maxBound
436 instance Read Int32 where
437 readsPrec p s = [ (intToInt32 x,r) | (x,r) <- readsPrec p s ]
439 instance Show Int32 where
440 showsPrec p i32 = showsPrec p (int32ToInt i32)
442 instance Bits Int32 where
443 (I32# x) .&. (I32# y) = I32# (word2Int# ((int2Word# x) `and#` (int2Word# y)))
444 (I32# x) .|. (I32# y) = I32# (word2Int# ((int2Word# x) `or#` (int2Word# y)))
445 (I32# x) `xor` (I32# y) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# y)))
446 #if WORD_SIZE_IN_BYTES > 4
447 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# 0xffffffff#)))
449 complement (I32# x) = I32# (word2Int# ((int2Word# x) `xor#` (int2Word# (negateInt# 1#))))
451 shift (I32# x) i@(I# i#)
452 | i > 0 = I32# (intToInt32# (iShiftL# (int32ToInt# x) i#))
453 | otherwise = I32# (intToInt32# (iShiftRA# (int32ToInt# x) i#))
454 i32@(I32# x) `rotate` (I# i)
457 -- ( (x<<i') | ((x&(0x100000000-2^i2))>>i2)
458 I32# (intToInt32# ( word2Int# (
459 (int2Word# (iShiftL# (int32ToInt# x) i'))
461 (int2Word# (iShiftRA# (word2Int# (
464 (int2Word# (maxBound# -# pow2# i2 +# 1#))))
466 | otherwise = rotate i32 (I# (32# +# i))
468 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
470 (I32# maxBound#) = maxBound
472 setBit x i = x .|. bit i
473 clearBit x i = x .&. complement (bit i)
474 complementBit x i = x `xor` bit i
475 testBit x i = (x .&. bit i) /= 0
479 {-# INLINE wordop #-}
480 wordop op (I# x) (I# y) = I# (word2Int# (int2Word# x `op` int2Word# y))
482 -----------------------------------------------------------------------------
483 -- End of exported definitions
485 -- The remainder of this file consists of definitions which are only
486 -- used in the implementation.
487 -----------------------------------------------------------------------------
489 -----------------------------------------------------------------------------
490 -- Code copied from the Prelude
491 -----------------------------------------------------------------------------
493 absReal x | x >= 0 = x
496 signumReal x | x == 0 = 0