2 % (c) The University of Glasgow, 1997-2000
4 \section[PrelWord]{Module @PrelWord@}
7 {-# OPTIONS -monly-3-regs #-}
12 Word(..), Word8(..), Word16(..), Word32(..), Word64(..),
14 -- SUP: deprecated in the new FFI, subsumed by fromIntegral
15 , intToWord8 -- :: Int -> Word8
16 , intToWord16 -- :: Int -> Word16
17 , intToWord32 -- :: Int -> Word32
18 , intToWord64 -- :: Int -> Word64
20 , integerToWord8 -- :: Integer -> Word8
21 , integerToWord16 -- :: Integer -> Word16
22 , integerToWord32 -- :: Integer -> Word32
23 , integerToWord64 -- :: Integer -> Word64
25 , word8ToInt -- :: Word8 -> Int
26 , word8ToInteger -- :: Word8 -> Integer
27 , word8ToWord16 -- :: Word8 -> Word16
28 , word8ToWord32 -- :: Word8 -> Word32
29 , word8ToWord64 -- :: Word8 -> Word64
31 , word16ToInt -- :: Word16 -> Int
32 , word16ToInteger -- :: Word16 -> Integer
33 , word16ToWord8 -- :: Word16 -> Word8
34 , word16ToWord32 -- :: Word16 -> Word32
35 , word16ToWord64 -- :: Word16 -> Word64
37 , word32ToInt -- :: Word32 -> Int
38 , word32ToInteger -- :: Word32 -> Integer
39 , word32ToWord8 -- :: Word32 -> Word8
40 , word32ToWord16 -- :: Word32 -> Word16
41 , word32ToWord64 -- :: Word32 -> Word64
43 , word64ToInt -- :: Word64 -> Int
44 , word64ToInteger -- :: Word64 -> Integer
45 , word64ToWord8 -- :: Word64 -> Word8
46 , word64ToWord16 -- :: Word64 -> Word16
47 , word64ToWord32 -- :: Word64 -> Word32
50 , wordToWord8#, wordToWord16#, wordToWord32#, wordToWord64#
52 , word64ToInt64#, int64ToWord64#
53 , wordToWord64#, word64ToWord#
55 , toEnumError, fromEnumError, succError, predError, divZeroError
66 -- ---------------------------------------------------------------------------
68 -- ---------------------------------------------------------------------------
70 -- A Word is an unsigned integral type, with the same number of bits as Int.
71 data Word = W# Word# deriving (Eq, Ord)
73 instance CCallable Word
74 instance CReturnable Word
76 -- ---------------------------------------------------------------------------
77 -- Coercion functions (DEPRECATED)
78 -- ---------------------------------------------------------------------------
80 intToWord8 :: Int -> Word8
81 intToWord16 :: Int -> Word16
82 intToWord32 :: Int -> Word32
83 intToWord64 :: Int -> Word64
85 integerToWord8 :: Integer -> Word8
86 integerToWord16 :: Integer -> Word16
87 integerToWord32 :: Integer -> Word32
88 integerToWord64 :: Integer -> Word64
90 word8ToInt :: Word8 -> Int
91 word8ToInteger :: Word8 -> Integer
92 word8ToWord16 :: Word8 -> Word16
93 word8ToWord32 :: Word8 -> Word32
94 word8ToWord64 :: Word8 -> Word64
96 word16ToInt :: Word16 -> Int
97 word16ToInteger :: Word16 -> Integer
98 word16ToWord8 :: Word16 -> Word8
99 word16ToWord32 :: Word16 -> Word32
100 word16ToWord64 :: Word16 -> Word64
102 word32ToInt :: Word32 -> Int
103 word32ToInteger :: Word32 -> Integer
104 word32ToWord8 :: Word32 -> Word8
105 word32ToWord16 :: Word32 -> Word16
106 word32ToWord64 :: Word32 -> Word64
108 word64ToInt :: Word64 -> Int
109 word64ToInteger :: Word64 -> Integer
110 word64ToWord8 :: Word64 -> Word8
111 word64ToWord16 :: Word64 -> Word16
112 word64ToWord32 :: Word64 -> Word32
114 intToWord8 = word32ToWord8 . intToWord32
115 intToWord16 = word32ToWord16 . intToWord32
117 integerToWord8 = fromInteger
118 integerToWord16 = fromInteger
120 word8ToInt = word32ToInt . word8ToWord32
121 word8ToInteger = word32ToInteger . word8ToWord32
123 word16ToInt = word32ToInt . word16ToWord32
124 word16ToInteger = word32ToInteger . word16ToWord32
126 #if WORD_SIZE_IN_BYTES > 4
127 intToWord32 (I# x) = W32# ((int2Word# x) `and#` (case (maxBound::Word32) of W32# x# -> x#))
129 intToWord32 (I# x) = W32# (int2Word# x)
132 word32ToInt (W32# x) = I# (word2Int# x)
134 word2Integer :: Word# -> Integer
135 word2Integer w | i >=# 0# = S# i
136 | otherwise = case word2Integer# w of
138 where i = word2Int# w
140 word32ToInteger (W32# x) = word2Integer x
141 integerToWord32 = fromInteger
143 -----------------------------------------------------------------------------
144 -- The following rules for fromIntegral remove the need to export specialized
145 -- conversion functions.
146 -----------------------------------------------------------------------------
149 "fromIntegral/Int->Word8" fromIntegral = intToWord8;
150 "fromIntegral/Int->Word16" fromIntegral = intToWord16;
151 "fromIntegral/Int->Word32" fromIntegral = intToWord32;
152 "fromIntegral/Int->Word64" fromIntegral = intToWord64;
154 "fromIntegral/Integer->Word8" fromIntegral = integerToWord8;
155 "fromIntegral/Integer->Word16" fromIntegral = integerToWord16;
156 "fromIntegral/Integer->Word32" fromIntegral = integerToWord32;
157 "fromIntegral/Integer->Word64" fromIntegral = integerToWord64;
159 "fromIntegral/Word8->Int" fromIntegral = word8ToInt;
160 "fromIntegral/Word8->Integer" fromIntegral = word8ToInteger;
161 "fromIntegral/Word8->Word16" fromIntegral = word8ToWord16;
162 "fromIntegral/Word8->Word32" fromIntegral = word8ToWord32;
163 "fromIntegral/Word8->Word64" fromIntegral = word8ToWord64;
165 "fromIntegral/Word16->Int" fromIntegral = word16ToInt;
166 "fromIntegral/Word16->Integer" fromIntegral = word16ToInteger;
167 "fromIntegral/Word16->Word8" fromIntegral = word16ToWord8;
168 "fromIntegral/Word16->Word32" fromIntegral = word16ToWord32;
169 "fromIntegral/Word16->Word64" fromIntegral = word16ToWord64;
171 "fromIntegral/Word32->Int" fromIntegral = word32ToInt;
172 "fromIntegral/Word32->Integer" fromIntegral = word32ToInteger;
173 "fromIntegral/Word32->Word8" fromIntegral = word32ToWord8;
174 "fromIntegral/Word32->Word16" fromIntegral = word32ToWord16;
175 "fromIntegral/Word32->Word64" fromIntegral = word32ToWord64;
177 "fromIntegral/Word64->Int" fromIntegral = word64ToInt;
178 "fromIntegral/Word64->Integer" fromIntegral = word64ToInteger;
179 "fromIntegral/Word64->Word8" fromIntegral = word64ToWord8;
180 "fromIntegral/Word64->Word16" fromIntegral = word64ToWord16;
181 "fromIntegral/Word64->Word32" fromIntegral = word64ToWord32
186 \subsection[Word8]{The @Word8@ interface}
189 The byte type @Word8@ is represented in the Haskell
190 heap by boxing up a 32-bit quantity, @Word#@. An invariant
191 for this representation is that the higher 24 bits are
192 *always* zeroed out. A consequence of this is that
193 operations that could possibly overflow have to mask
194 out the top three bytes before building the resulting @Word8@.
197 data Word8 = W8# Word#
199 instance CCallable Word8
200 instance CReturnable Word8
202 word8ToWord32 (W8# x) = W32# x
203 word8ToWord16 (W8# x) = W16# x
204 word32ToWord8 (W32# x) = W8# (wordToWord8# x)
206 -- mask out upper three bytes.
207 intToWord8# :: Int# -> Word#
208 intToWord8# i# = (int2Word# i#) `and#` (int2Word# 0xff#)
210 wordToWord8# :: Word# -> Word#
211 wordToWord8# w# = w# `and#` (int2Word# 0xff#)
213 instance Eq Word8 where
214 (W8# x) == (W8# y) = x `eqWord#` y
215 (W8# x) /= (W8# y) = x `neWord#` y
217 instance Ord Word8 where
218 compare (W8# x#) (W8# y#) = compareWord# x# y#
219 (<) (W8# x) (W8# y) = x `ltWord#` y
220 (<=) (W8# x) (W8# y) = x `leWord#` y
221 (>=) (W8# x) (W8# y) = x `geWord#` y
222 (>) (W8# x) (W8# y) = x `gtWord#` y
223 max x@(W8# x#) y@(W8# y#) =
224 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
225 min x@(W8# x#) y@(W8# y#) =
226 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
228 -- Helper function, used by Ord Word* instances.
229 compareWord# :: Word# -> Word# -> Ordering
231 | x# `ltWord#` y# = LT
232 | x# `eqWord#` y# = EQ
235 instance Num Word8 where
237 W8# (intToWord8# (word2Int# x +# word2Int# y))
239 W8# (intToWord8# (word2Int# x -# word2Int# y))
241 W8# (intToWord8# (word2Int# x *# word2Int# y))
245 else W8# (int2Word# (0x100# -# x'))
250 fromInteger (S# i#) = W8# (wordToWord8# (int2Word# i#))
251 fromInteger (J# s# d#) = W8# (wordToWord8# (integer2Word# s# d#))
254 instance Bounded Word8 where
258 instance Real Word8 where
259 toRational x = toInteger x % 1
261 -- Note: no need to mask results here
262 -- as they cannot overflow.
263 instance Integral Word8 where
264 div x@(W8# x#) (W8# y#)
265 | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
266 | otherwise = divZeroError "div{Word8}" x
268 quot x@(W8# x#) (W8# y#)
269 | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
270 | otherwise = divZeroError "quot{Word8}" x
272 rem x@(W8# x#) (W8# y#)
273 | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
274 | otherwise = divZeroError "rem{Word8}" x
276 mod x@(W8# x#) (W8# y#)
277 | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
278 | otherwise = divZeroError "mod{Word8}" x
280 quotRem (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
281 divMod (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
283 toInteger = toInteger . toInt
286 instance Ix Word8 where
289 | inRange b i = word8ToInt (i-m)
290 | otherwise = indexError b i "Word8"
291 inRange (m,n) i = m <= i && i <= n
293 instance Enum Word8 where
295 | w == maxBound = succError "Word8"
298 | w == minBound = predError "Word8"
302 | i >= toInt (minBound::Word8) && i <= toInt (maxBound::Word8)
303 = W8# (intToWord8# i#)
305 = toEnumError "Word8" i (minBound::Word8,maxBound::Word8)
307 fromEnum (W8# w) = I# (word2Int# w)
309 enumFrom = boundedEnumFrom
310 enumFromThen = boundedEnumFromThen
312 instance Read Word8 where
313 readsPrec _ = readDec
315 instance Show Word8 where
316 showsPrec p w8 = showsPrec p (word8ToInt w8)
318 instance Bits Word8 where
319 (W8# x) .&. (W8# y) = W8# (x `and#` y)
320 (W8# x) .|. (W8# y) = W8# (x `or#` y)
321 (W8# x) `xor` (W8# y) = W8# (x `xor#` y)
322 complement (W8# x) = W8# (x `xor#` int2Word# 0xff#)
323 shift (W8# x#) i@(I# i#)
324 | i > 0 = W8# (wordToWord8# (shiftL# x# i#))
325 | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
326 w@(W8# x) `rotate` (I# i)
328 | i ># 0# = W8# ((wordToWord8# (shiftL# x i')) `or#`
330 (int2Word# (0x100# -# pow2# i2)))
332 | otherwise = rotate w (I# (8# +# i))
334 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
338 | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
339 | otherwise = 0 -- We'll be overbearing, for now..
341 testBit (W8# x#) (I# i#)
342 | i# <# 8# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
343 | otherwise = False -- for now, this is really an error.
348 pow2# :: Int# -> Int#
349 pow2# x# = word2Int# (shiftL# (int2Word# 1#) x#)
351 pow2_64# :: Int# -> Int64#
352 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
354 -- ---------------------------------------------------------------------------
356 -- ---------------------------------------------------------------------------
358 -- The double byte type @Word16@ is represented in the Haskell
359 -- heap by boxing up a machine word, @Word#@. An invariant
360 -- for this representation is that only the lower 16 bits are
361 -- `active', any bits above are {\em always} zeroed out.
362 -- A consequence of this is that operations that could possibly
363 -- overflow have to mask out anything above the lower two bytes
364 -- before putting together the resulting @Word16@.
366 data Word16 = W16# Word#
368 instance CCallable Word16
369 instance CReturnable Word16
371 word16ToWord8 (W16# x) = W8# (wordToWord8# x)
372 word16ToWord32 (W16# x) = W32# x
374 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
376 -- mask out upper 16 bits.
377 intToWord16# :: Int# -> Word#
378 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
380 wordToWord16# :: Word# -> Word#
381 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
383 instance Eq Word16 where
384 (W16# x) == (W16# y) = x `eqWord#` y
385 (W16# x) /= (W16# y) = x `neWord#` y
387 instance Ord Word16 where
388 compare (W16# x#) (W16# y#) = compareWord# x# y#
389 (<) (W16# x) (W16# y) = x `ltWord#` y
390 (<=) (W16# x) (W16# y) = x `leWord#` y
391 (>=) (W16# x) (W16# y) = x `geWord#` y
392 (>) (W16# x) (W16# y) = x `gtWord#` y
393 max x@(W16# x#) y@(W16# y#) =
394 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
395 min x@(W16# x#) y@(W16# y#) =
396 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
400 instance Num Word16 where
401 (W16# x) + (W16# y) =
402 W16# (intToWord16# (word2Int# x +# word2Int# y))
403 (W16# x) - (W16# y) =
404 W16# (intToWord16# (word2Int# x -# word2Int# y))
405 (W16# x) * (W16# y) =
406 W16# (intToWord16# (word2Int# x *# word2Int# y))
410 else W16# (int2Word# (0x10000# -# x'))
415 fromInteger (S# i#) = W16# (wordToWord16# (int2Word# i#))
416 fromInteger (J# s# d#) = W16# (wordToWord16# (integer2Word# s# d#))
417 fromInt = intToWord16
419 instance Bounded Word16 where
423 instance Real Word16 where
424 toRational x = toInteger x % 1
426 instance Integral Word16 where
427 div x@(W16# x#) (W16# y#)
428 | y# `neWord#` (int2Word# 0#) = W16# (x# `quotWord#` y#)
429 | otherwise = divZeroError "div{Word16}" x
431 quot x@(W16# x#) (W16# y#)
432 | y# `neWord#`(int2Word# 0#) = W16# (x# `quotWord#` y#)
433 | otherwise = divZeroError "quot{Word16}" x
435 rem x@(W16# x#) (W16# y#)
436 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
437 | otherwise = divZeroError "rem{Word16}" x
439 mod x@(W16# x#) (W16# y#)
440 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
441 | otherwise = divZeroError "mod{Word16}" x
443 quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
444 divMod (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
446 toInteger = toInteger . toInt
449 instance Ix Word16 where
452 | inRange b i = word16ToInt (i - m)
453 | otherwise = indexError b i "Word16"
454 inRange (m,n) i = m <= i && i <= n
456 instance Enum Word16 where
458 | w == maxBound = succError "Word16"
461 | w == minBound = predError "Word16"
465 | i >= toInt (minBound::Word16) && i <= toInt (maxBound::Word16)
466 = W16# (intToWord16# i#)
468 = toEnumError "Word16" i (minBound::Word16,maxBound::Word16)
470 fromEnum (W16# w) = I# (word2Int# w)
471 enumFrom = boundedEnumFrom
472 enumFromThen = boundedEnumFromThen
474 instance Read Word16 where
475 readsPrec _ = readDec
477 instance Show Word16 where
478 showsPrec p w16 = showsPrec p (word16ToInt w16)
480 instance Bits Word16 where
481 (W16# x) .&. (W16# y) = W16# (x `and#` y)
482 (W16# x) .|. (W16# y) = W16# (x `or#` y)
483 (W16# x) `xor` (W16# y) = W16# (x `xor#` y)
484 complement (W16# x) = W16# (x `xor#` int2Word# 0xffff#)
485 shift (W16# x#) i@(I# i#)
486 | i > 0 = W16# (wordToWord16# (shiftL# x# i#))
487 | otherwise = W16# (shiftRL# x# (negateInt# i#))
488 w@(W16# x) `rotate` (I# i)
490 | i ># 0# = W16# ((wordToWord16# (shiftL# x i')) `or#`
492 (int2Word# (0x10000# -# pow2# i2)))
494 | otherwise = rotate w (I# (16# +# i'))
496 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
499 | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
500 | otherwise = 0 -- We'll be overbearing, for now..
502 testBit (W16# x#) (I# i#)
503 | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
504 | otherwise = False -- for now, this is really an error.
509 -- ---------------------------------------------------------------------------
511 -- ---------------------------------------------------------------------------
513 -- The quad byte type @Word32@ is represented in the Haskell
514 -- heap by boxing up a machine word, @Word#@. An invariant
515 -- for this representation is that any bits above the lower
516 -- 32 are {\em always} zeroed out. A consequence of this is that
517 -- operations that could possibly overflow have to mask
518 -- the result before building the resulting @Word16@.
520 data Word32 = W32# Word#
522 instance CCallable Word32
523 instance CReturnable Word32
525 instance Eq Word32 where
526 (W32# x) == (W32# y) = x `eqWord#` y
527 (W32# x) /= (W32# y) = x `neWord#` y
529 instance Ord Word32 where
530 compare (W32# x#) (W32# y#) = compareWord# x# y#
531 (<) (W32# x) (W32# y) = x `ltWord#` y
532 (<=) (W32# x) (W32# y) = x `leWord#` y
533 (>=) (W32# x) (W32# y) = x `geWord#` y
534 (>) (W32# x) (W32# y) = x `gtWord#` y
535 max x@(W32# x#) y@(W32# y#) =
536 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
537 min x@(W32# x#) y@(W32# y#) =
538 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
540 instance Num Word32 where
541 (W32# x) + (W32# y) =
542 W32# (intToWord32# (word2Int# x +# word2Int# y))
543 (W32# x) - (W32# y) =
544 W32# (intToWord32# (word2Int# x -# word2Int# y))
545 (W32# x) * (W32# y) =
546 W32# (intToWord32# (word2Int# x *# word2Int# y))
547 #if WORD_SIZE_IN_BYTES == 8
551 else W32# (intToWord32# (0x100000000# -# x'))
555 negate (W32# x) = W32# (intToWord32# (negateInt# (word2Int# x)))
559 fromInteger (S# i#) = W32# (intToWord32# i#)
560 fromInteger (J# s# d#) = W32# (wordToWord32# (integer2Word# s# d#))
561 fromInt (I# x) = W32# (intToWord32# x)
562 -- ToDo: restrict fromInt{eger} range.
564 intToWord32# :: Int# -> Word#
565 wordToWord32# :: Word# -> Word#
567 #if WORD_SIZE_IN_BYTES == 8
568 intToWord32# i# = (int2Word# i#) `and#` (int2Word# 0xffffffff#)
569 wordToWord32# w# = w# `and#` (int2Word# 0xffffffff#)
570 wordToWord64# w# = w#
572 intToWord32# i# = int2Word# i#
573 wordToWord32# w# = w#
576 instance Bounded Word32 where
578 #if WORD_SIZE_IN_BYTES == 8
579 maxBound = 0xffffffff
581 maxBound = minBound - 1
584 instance Real Word32 where
585 toRational x = toInteger x % 1
587 instance Integral Word32 where
589 | y /= 0 = quotWord32 x y
590 | otherwise = divZeroError "div{Word32}" x
593 | y /= 0 = quotWord32 x y
594 | otherwise = divZeroError "quot{Word32}" x
597 | y /= 0 = remWord32 x y
598 | otherwise = divZeroError "rem{Word32}" x
601 | y /= 0 = remWord32 x y
602 | otherwise = divZeroError "mod{Word32}" x
604 quotRem a b = (a `quot` b, a `rem` b)
605 divMod x y = quotRem x y
607 toInteger = word32ToInteger
611 {-# INLINE quotWord32 #-}
612 {-# INLINE remWord32 #-}
613 remWord32, quotWord32 :: Word32 -> Word32 -> Word32
614 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
615 (W32# x) `remWord32` (W32# y) = W32# (x `remWord#` y)
618 instance Ix Word32 where
621 | inRange b i = word32ToInt (i - m)
622 | otherwise = indexError b i "Word32"
623 inRange (m,n) i = m <= i && i <= n
625 instance Enum Word32 where
627 | w == maxBound = succError "Word32"
630 | w == minBound = predError "Word32"
633 -- the toEnum/fromEnum will fail if the mapping isn't legal,
634 -- use the intTo* & *ToInt coercion functions to 'bypass' these range checks.
636 | x >= 0 = intToWord32 x
638 = toEnumError "Word32" x (minBound::Word32,maxBound::Word32)
641 | x <= intToWord32 (maxBound::Int)
644 = fromEnumError "Word32" x
646 enumFrom w = [w .. maxBound]
648 | w1 <= w2 = eftt32 True{-increasing-} w1 diff_f last
654 enumFromThen w1 w2 = [w1,w2 .. last]
659 | otherwise = minBound
661 enumFromThenTo w1 w2 wend = eftt32 increasing w1 step_f last
663 increasing = w1 <= w2
668 | increasing = (> wend)
669 | otherwise = (< wend)
672 | increasing = \ x -> x + diff1
673 | otherwise = \ x -> x - diff2
675 eftt32 :: Bool -> Word32 -> (Word32 -> Word32) -> (Word32-> Bool) -> [Word32]
676 eftt32 increasing init stepper done = go init
680 | increasing && now > nxt = [now] -- oflow
681 | not increasing && now < nxt = [now] -- uflow
682 | otherwise = now : go nxt
686 instance Read Word32 where
687 readsPrec _ = readDec
689 instance Show Word32 where
690 showsPrec p w = showsPrec p (word32ToInteger w)
692 instance Bits Word32 where
693 (W32# x) .&. (W32# y) = W32# (x `and#` y)
694 (W32# x) .|. (W32# y) = W32# (x `or#` y)
695 (W32# x) `xor` (W32# y) = W32# (x `xor#` y)
696 complement (W32# x) = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
697 shift (W32# x) i@(I# i#)
698 | i > 0 = W32# (wordToWord32# (shiftL# x i#))
699 | otherwise = W32# (shiftRL# x (negateInt# i#))
700 w@(W32# x) `rotate` (I# i)
702 | i ># 0# = W32# ((wordToWord32# (shiftL# x i')) `or#`
704 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
706 | otherwise = rotate w (I# (32# +# i))
708 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
710 (W32# maxBound#) = maxBound
713 | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
714 | otherwise = 0 -- We'll be overbearing, for now..
716 testBit (W32# x#) (I# i#)
717 | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
718 | otherwise = False -- for now, this is really an error.
722 -- -----------------------------------------------------------------------------
724 -- -----------------------------------------------------------------------------
726 #if WORD_SIZE_IN_BYTES == 8
727 data Word64 = W64# Word#
729 word32ToWord64 (W32 w#) = W64# w#
731 word8ToWord64 (W8# w#) = W64# w#
732 word64ToWord8 (W64# w#) = W8# (w# `and#` (int2Word# 0xff#))
734 word16ToWord64 (W16# w#) = W64# w#
735 word64ToWord16 (W64# w#) = W16# (w# `and#` (int2Word# 0xffff#))
737 wordToWord32# :: Word# -> Word#
738 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
740 word64ToWord32 :: Word64 -> Word32
741 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
743 wordToWord64# w# = w#
744 word64ToWord# w# = w#
746 instance Eq Word64 where
747 (W64# x) == (W64# y) = x `eqWord#` y
748 (W64# x) /= (W64# y) = x `neWord#` y
750 instance Ord Word64 where
751 compare (W64# x#) (W64# y#) = compareWord# x# y#
752 (<) (W64# x) (W64# y) = x `ltWord#` y
753 (<=) (W64# x) (W64# y) = x `leWord#` y
754 (>=) (W64# x) (W64# y) = x `geWord#` y
755 (>) (W64# x) (W64# y) = x `gtWord#` y
756 max x@(W64# x#) y@(W64# y#) =
757 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
758 min x@(W64# x#) y@(W64# y#) =
759 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
761 instance Num Word64 where
762 (W64# x) + (W64# y) =
763 W64# (intToWord64# (word2Int# x +# word2Int# y))
764 (W64# x) - (W64# y) =
765 W64# (intToWord64# (word2Int# x -# word2Int# y))
766 (W64# x) * (W64# y) =
767 W64# (intToWord64# (word2Int# x *# word2Int# y))
771 else W64# (int2Word# (0x100# -# x'))
776 fromInteger (S# i#) = W64# (int2Word# i#)
777 fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
778 fromInt = intToWord64
780 -- Note: no need to mask results here
781 -- as they cannot overflow.
782 instance Integral Word64 where
783 div x@(W64# x#) (W64# y#)
784 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
785 | otherwise = divZeroError "div{Word64}" x
787 quot x@(W64# x#) (W64# y#)
788 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
789 | otherwise = divZeroError "quot{Word64}" x
791 rem x@(W64# x#) (W64# y#)
792 | y# `neWord#` (int2Word# 0#) = W64# (x# `remWord#` y#)
793 | otherwise = divZeroError "rem{Word64}" x
795 mod (W64# x) (W64# y)
796 | y# `neWord#` (int2Word# 0#) = W64# (x `remWord#` y)
797 | otherwise = divZeroError "mod{Word64}" x
799 quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
800 divMod (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
802 toInteger (W64# x) = word2Integer# x
803 toInt x = word64ToInt x
805 #else /* WORD_SIZE_IN_BYTES < 8 */
807 data Word64 = W64# Word64#
809 -- for completeness sake
810 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
811 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
813 word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
814 word64ToWord8 (W64# w#) = W8# ((word64ToWord# w#) `and#` (int2Word# 0xff#))
816 word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
817 word64ToWord16 (W64# w#) = W16# ((word64ToWord# w#) `and#` (int2Word# 0xffff#))
819 word64ToInteger (W64# w#) =
820 case word64ToInteger# w# of
821 (# s#, p# #) -> J# s# p#
823 case w `quotRem` 0x100000000 of
824 (_,l) -> toInt (word64ToWord32 l)
826 intToWord64# :: Int# -> Word64#
827 intToWord64# i# = wordToWord64# (int2Word# i#)
829 intToWord64 (I# i#) = W64# (intToWord64# i#)
831 integerToWord64 (S# i#) = W64# (intToWord64# i#)
832 integerToWord64 (J# s# d#) = W64# (integerToWord64# s# d#)
834 instance Eq Word64 where
835 (W64# x) == (W64# y) = x `eqWord64#` y
836 (W64# x) /= (W64# y) = not (x `eqWord64#` y)
838 instance Ord Word64 where
839 compare (W64# x#) (W64# y#) = compareWord64# x# y#
840 (<) (W64# x) (W64# y) = x `ltWord64#` y
841 (<=) (W64# x) (W64# y) = x `leWord64#` y
842 (>=) (W64# x) (W64# y) = x `geWord64#` y
843 (>) (W64# x) (W64# y) = x `gtWord64#` y
844 max x@(W64# x#) y@(W64# y#) =
845 case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
846 min x@(W64# x#) y@(W64# y#) =
847 case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
849 instance Num Word64 where
850 (W64# x) + (W64# y) =
851 W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
852 (W64# x) - (W64# y) =
853 W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
854 (W64# x) * (W64# y) =
855 W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
858 | otherwise = maxBound - w
862 fromInteger i = integerToWord64 i
863 fromInt = intToWord64
865 -- Note: no need to mask results here as they cannot overflow.
866 -- ToDo: protect against div by zero.
867 instance Integral Word64 where
868 div (W64# x) (W64# y) = W64# (x `quotWord64#` y)
869 quot (W64# x) (W64# y) = W64# (x `quotWord64#` y)
870 rem (W64# x) (W64# y) = W64# (x `remWord64#` y)
871 mod (W64# x) (W64# y) = W64# (x `remWord64#` y)
872 quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
873 divMod (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
874 toInteger w64 = word64ToInteger w64
875 toInt x = word64ToInt x
877 compareWord64# :: Word64# -> Word64# -> Ordering
879 | i# `ltWord64#` j# = LT
880 | i# `eqWord64#` j# = EQ
883 -- Word64# primop wrappers:
885 ltWord64# :: Word64# -> Word64# -> Bool
886 ltWord64# x# y# = stg_ltWord64 x# y# /=# 0#
888 leWord64# :: Word64# -> Word64# -> Bool
889 leWord64# x# y# = stg_leWord64 x# y# /=# 0#
891 eqWord64# :: Word64# -> Word64# -> Bool
892 eqWord64# x# y# = stg_eqWord64 x# y# /=# 0#
894 neWord64# :: Word64# -> Word64# -> Bool
895 neWord64# x# y# = stg_neWord64 x# y# /=# 0#
897 geWord64# :: Word64# -> Word64# -> Bool
898 geWord64# x# y# = stg_geWord64 x# y# /=# 0#
900 gtWord64# :: Word64# -> Word64# -> Bool
901 gtWord64# x# y# = stg_gtWord64 x# y# /=# 0#
903 foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64#
904 foreign import "stg_int64ToWord64" unsafe int64ToWord64# :: Int64# -> Word64#
905 foreign import "stg_word64ToInt64" unsafe word64ToInt64# :: Word64# -> Int64#
906 foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64#
907 foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word#
908 foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64#
909 foreign import "stg_remWord64" unsafe remWord64# :: Word64# -> Word64# -> Word64#
910 foreign import "stg_quotWord64" unsafe quotWord64# :: Word64# -> Word64# -> Word64#
911 foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64#
912 foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64#
913 foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64#
914 foreign import "stg_gtWord64" unsafe stg_gtWord64 :: Word64# -> Word64# -> Int#
915 foreign import "stg_geWord64" unsafe stg_geWord64 :: Word64# -> Word64# -> Int#
916 foreign import "stg_neWord64" unsafe stg_neWord64 :: Word64# -> Word64# -> Int#
917 foreign import "stg_eqWord64" unsafe stg_eqWord64 :: Word64# -> Word64# -> Int#
918 foreign import "stg_leWord64" unsafe stg_leWord64 :: Word64# -> Word64# -> Int#
919 foreign import "stg_ltWord64" unsafe stg_ltWord64 :: Word64# -> Word64# -> Int#
923 instance CCallable Word64
924 instance CReturnable Word64
926 instance Enum Word64 where
928 | w == maxBound = succError "Word64"
931 | w == minBound = predError "Word64"
935 | i >= 0 = intToWord64 i
937 = toEnumError "Word64" i (minBound::Word64,maxBound::Word64)
940 | w <= intToWord64 (maxBound::Int)
943 = fromEnumError "Word64" w
945 enumFrom e1 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger maxBound]
946 enumFromTo e1 e2 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger e2]
947 enumFromThen e1 e2 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger last]
952 | otherwise = maxBound
954 enumFromThenTo e1 e2 e3 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger e3]
956 instance Show Word64 where
957 showsPrec p x = showsPrec p (word64ToInteger x)
959 instance Read Word64 where
960 readsPrec _ s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
962 instance Ix Word64 where
965 | inRange b i = word64ToInt (i-m)
966 | otherwise = indexError b i "Word64"
967 inRange (m,n) i = m <= i && i <= n
969 instance Bounded Word64 where
971 maxBound = minBound - 1
973 instance Real Word64 where
974 toRational x = toInteger x % 1
976 #if WORD_SIZE_IN_BYTES == 8
978 instance Bits Word64 where
979 (W64# x) .&. (W64# y) = W64# (x `and#` y)
980 (W64# x) .|. (W64# y) = W64# (x `or#` y)
981 (W64# x) `xor` (W64# y) = W64# (x `xor#` y)
982 complement (W64# x) = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
983 shift (W64# x#) i@(I# i#)
984 | i > 0 = W64# (shiftL# x# i#)
985 | otherwise = W64# (shiftRL# x# (negateInt# i#))
987 w@(W64# x) `rotate` (I# i)
989 | i ># 0# = W64# (shiftL# x i') `or#`
991 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
993 | otherwise = rotate w (I# (64# +# i))
995 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
997 (W64# maxBound#) = maxBound
1000 | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
1001 | otherwise = 0 -- We'll be overbearing, for now..
1003 testBit (W64# x#) (I# i#)
1004 | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
1005 | otherwise = False -- for now, this is really an error.
1010 #else /* WORD_SIZE_IN_BYTES < 8 */
1012 instance Bits Word64 where
1013 (W64# x) .&. (W64# y) = W64# (x `and64#` y)
1014 (W64# x) .|. (W64# y) = W64# (x `or64#` y)
1015 (W64# x) `xor` (W64# y) = W64# (x `xor64#` y)
1016 complement (W64# x) = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
1017 shift (W64# x#) i@(I# i#)
1018 | i > 0 = W64# (shiftL64# x# i#)
1019 | otherwise = W64# (shiftRL64# x# (negateInt# i#))
1021 w@(W64# x) `rotate` (I# i)
1023 | i ># 0# = W64# ((shiftL64# x i') `or64#`
1024 (shiftRL64# (x `and64#`
1025 (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#`
1026 (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
1028 | otherwise = rotate w (I# (64# +# i))
1030 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
1032 (W64# maxBound#) = maxBound
1035 | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
1036 | otherwise = 0 -- We'll be overbearing, for now..
1038 testBit (W64# x#) (I# i#)
1039 | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
1040 | otherwise = False -- for now, this is really an error.
1045 foreign import "stg_not64" unsafe not64# :: Word64# -> Word64#
1046 foreign import "stg_xor64" unsafe xor64# :: Word64# -> Word64# -> Word64#
1047 foreign import "stg_or64" unsafe or64# :: Word64# -> Word64# -> Word64#
1048 foreign import "stg_and64" unsafe and64# :: Word64# -> Word64# -> Word64#
1049 foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> Word64#
1050 foreign import "stg_shiftL64" unsafe shiftL64# :: Word64# -> Int# -> Word64#
1052 #endif /* WORD_SIZE_IN_BYTES < 8 */
1058 signumReal :: (Ord a, Num a) => a -> a
1059 signumReal x | x == 0 = 0
1064 Utils for generating friendly error messages.
1067 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1068 toEnumError inst_ty tag bnds
1069 = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1070 (showParen True (showsPrec 0 tag) $
1071 " is outside of bounds " ++
1074 fromEnumError :: (Show a,Show b) => String -> a -> b
1075 fromEnumError inst_ty tag
1076 = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1077 (showParen True (showsPrec 0 tag) $
1078 " is outside of Int's bounds " ++
1079 show (minBound::Int,maxBound::Int)))
1081 succError :: String -> a
1083 = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1085 predError :: String -> a
1087 = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1089 divZeroError :: (Show a) => String -> a -> b
1091 = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")