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#))
253 instance Bounded Word8 where
257 instance Real Word8 where
258 toRational x = toInteger x % 1
260 -- Note: no need to mask results here
261 -- as they cannot overflow.
262 instance Integral Word8 where
263 div x@(W8# x#) (W8# y#)
264 | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
265 | otherwise = divZeroError "div{Word8}" x
267 quot x@(W8# x#) (W8# y#)
268 | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
269 | otherwise = divZeroError "quot{Word8}" x
271 rem x@(W8# x#) (W8# y#)
272 | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
273 | otherwise = divZeroError "rem{Word8}" x
275 mod x@(W8# x#) (W8# y#)
276 | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
277 | otherwise = divZeroError "mod{Word8}" x
279 quotRem (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
280 divMod (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
282 toInteger = toInteger . toInt
285 instance Ix Word8 where
288 | inRange b i = word8ToInt (i-m)
289 | otherwise = indexError b i "Word8"
290 inRange (m,n) i = m <= i && i <= n
292 instance Enum Word8 where
294 | w == maxBound = succError "Word8"
297 | w == minBound = predError "Word8"
301 | i >= toInt (minBound::Word8) && i <= toInt (maxBound::Word8)
302 = W8# (intToWord8# i#)
304 = toEnumError "Word8" i (minBound::Word8,maxBound::Word8)
306 fromEnum (W8# w) = I# (word2Int# w)
308 enumFrom = boundedEnumFrom
309 enumFromThen = boundedEnumFromThen
311 instance Read Word8 where
312 readsPrec _ = readDec
314 instance Show Word8 where
315 showsPrec p w8 = showsPrec p (word8ToInt w8)
317 instance Bits Word8 where
318 (W8# x) .&. (W8# y) = W8# (x `and#` y)
319 (W8# x) .|. (W8# y) = W8# (x `or#` y)
320 (W8# x) `xor` (W8# y) = W8# (x `xor#` y)
321 complement (W8# x) = W8# (x `xor#` int2Word# 0xff#)
322 shift (W8# x#) i@(I# i#)
323 | i > 0 = W8# (wordToWord8# (shiftL# x# i#))
324 | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
325 w@(W8# x) `rotate` (I# i)
327 | i ># 0# = W8# ((wordToWord8# (shiftL# x i')) `or#`
329 (int2Word# (0x100# -# pow2# i2)))
331 | otherwise = rotate w (I# (8# +# i))
333 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
337 | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
338 | otherwise = 0 -- We'll be overbearing, for now..
340 testBit (W8# x#) (I# i#)
341 | i# <# 8# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
342 | otherwise = False -- for now, this is really an error.
347 pow2# :: Int# -> Int#
348 pow2# x# = word2Int# (shiftL# (int2Word# 1#) x#)
350 pow2_64# :: Int# -> Int64#
351 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
353 -- ---------------------------------------------------------------------------
355 -- ---------------------------------------------------------------------------
357 -- The double byte type @Word16@ is represented in the Haskell
358 -- heap by boxing up a machine word, @Word#@. An invariant
359 -- for this representation is that only the lower 16 bits are
360 -- `active', any bits above are {\em always} zeroed out.
361 -- A consequence of this is that operations that could possibly
362 -- overflow have to mask out anything above the lower two bytes
363 -- before putting together the resulting @Word16@.
365 data Word16 = W16# Word#
367 instance CCallable Word16
368 instance CReturnable Word16
370 word16ToWord8 (W16# x) = W8# (wordToWord8# x)
371 word16ToWord32 (W16# x) = W32# x
373 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
375 -- mask out upper 16 bits.
376 intToWord16# :: Int# -> Word#
377 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
379 wordToWord16# :: Word# -> Word#
380 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
382 instance Eq Word16 where
383 (W16# x) == (W16# y) = x `eqWord#` y
384 (W16# x) /= (W16# y) = x `neWord#` y
386 instance Ord Word16 where
387 compare (W16# x#) (W16# y#) = compareWord# x# y#
388 (<) (W16# x) (W16# y) = x `ltWord#` y
389 (<=) (W16# x) (W16# y) = x `leWord#` y
390 (>=) (W16# x) (W16# y) = x `geWord#` y
391 (>) (W16# x) (W16# y) = x `gtWord#` y
392 max x@(W16# x#) y@(W16# y#) =
393 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
394 min x@(W16# x#) y@(W16# y#) =
395 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
399 instance Num Word16 where
400 (W16# x) + (W16# y) =
401 W16# (intToWord16# (word2Int# x +# word2Int# y))
402 (W16# x) - (W16# y) =
403 W16# (intToWord16# (word2Int# x -# word2Int# y))
404 (W16# x) * (W16# y) =
405 W16# (intToWord16# (word2Int# x *# word2Int# y))
409 else W16# (int2Word# (0x10000# -# x'))
414 fromInteger (S# i#) = W16# (wordToWord16# (int2Word# i#))
415 fromInteger (J# s# d#) = W16# (wordToWord16# (integer2Word# s# d#))
417 instance Bounded Word16 where
421 instance Real Word16 where
422 toRational x = toInteger x % 1
424 instance Integral Word16 where
425 div x@(W16# x#) (W16# y#)
426 | y# `neWord#` (int2Word# 0#) = W16# (x# `quotWord#` y#)
427 | otherwise = divZeroError "div{Word16}" x
429 quot x@(W16# x#) (W16# y#)
430 | y# `neWord#`(int2Word# 0#) = W16# (x# `quotWord#` y#)
431 | otherwise = divZeroError "quot{Word16}" x
433 rem x@(W16# x#) (W16# y#)
434 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
435 | otherwise = divZeroError "rem{Word16}" x
437 mod x@(W16# x#) (W16# y#)
438 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
439 | otherwise = divZeroError "mod{Word16}" x
441 quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
442 divMod (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
444 toInteger = toInteger . toInt
447 instance Ix Word16 where
450 | inRange b i = word16ToInt (i - m)
451 | otherwise = indexError b i "Word16"
452 inRange (m,n) i = m <= i && i <= n
454 instance Enum Word16 where
456 | w == maxBound = succError "Word16"
459 | w == minBound = predError "Word16"
463 | i >= toInt (minBound::Word16) && i <= toInt (maxBound::Word16)
464 = W16# (intToWord16# i#)
466 = toEnumError "Word16" i (minBound::Word16,maxBound::Word16)
468 fromEnum (W16# w) = I# (word2Int# w)
469 enumFrom = boundedEnumFrom
470 enumFromThen = boundedEnumFromThen
472 instance Read Word16 where
473 readsPrec _ = readDec
475 instance Show Word16 where
476 showsPrec p w16 = showsPrec p (word16ToInt w16)
478 instance Bits Word16 where
479 (W16# x) .&. (W16# y) = W16# (x `and#` y)
480 (W16# x) .|. (W16# y) = W16# (x `or#` y)
481 (W16# x) `xor` (W16# y) = W16# (x `xor#` y)
482 complement (W16# x) = W16# (x `xor#` int2Word# 0xffff#)
483 shift (W16# x#) i@(I# i#)
484 | i > 0 = W16# (wordToWord16# (shiftL# x# i#))
485 | otherwise = W16# (shiftRL# x# (negateInt# i#))
486 w@(W16# x) `rotate` (I# i)
488 | i ># 0# = W16# ((wordToWord16# (shiftL# x i')) `or#`
490 (int2Word# (0x10000# -# pow2# i2)))
492 | otherwise = rotate w (I# (16# +# i'))
494 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
497 | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
498 | otherwise = 0 -- We'll be overbearing, for now..
500 testBit (W16# x#) (I# i#)
501 | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
502 | otherwise = False -- for now, this is really an error.
507 -- ---------------------------------------------------------------------------
509 -- ---------------------------------------------------------------------------
511 -- The quad byte type @Word32@ is represented in the Haskell
512 -- heap by boxing up a machine word, @Word#@. An invariant
513 -- for this representation is that any bits above the lower
514 -- 32 are {\em always} zeroed out. A consequence of this is that
515 -- operations that could possibly overflow have to mask
516 -- the result before building the resulting @Word16@.
518 data Word32 = W32# Word#
520 instance CCallable Word32
521 instance CReturnable Word32
523 instance Eq Word32 where
524 (W32# x) == (W32# y) = x `eqWord#` y
525 (W32# x) /= (W32# y) = x `neWord#` y
527 instance Ord Word32 where
528 compare (W32# x#) (W32# y#) = compareWord# x# y#
529 (<) (W32# x) (W32# y) = x `ltWord#` y
530 (<=) (W32# x) (W32# y) = x `leWord#` y
531 (>=) (W32# x) (W32# y) = x `geWord#` y
532 (>) (W32# x) (W32# y) = x `gtWord#` y
533 max x@(W32# x#) y@(W32# y#) =
534 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
535 min x@(W32# x#) y@(W32# y#) =
536 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
538 instance Num Word32 where
539 (W32# x) + (W32# y) =
540 W32# (intToWord32# (word2Int# x +# word2Int# y))
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 #if WORD_SIZE_IN_BYTES == 8
549 else W32# (intToWord32# (0x100000000# -# x'))
553 negate (W32# x) = W32# (intToWord32# (negateInt# (word2Int# x)))
557 fromInteger (S# i#) = W32# (intToWord32# i#)
558 fromInteger (J# s# d#) = W32# (wordToWord32# (integer2Word# s# d#))
559 -- ToDo: restrict fromInt{eger} range.
561 intToWord32# :: Int# -> Word#
562 wordToWord32# :: Word# -> Word#
564 #if WORD_SIZE_IN_BYTES == 8
565 intToWord32# i# = (int2Word# i#) `and#` (int2Word# 0xffffffff#)
566 wordToWord32# w# = w# `and#` (int2Word# 0xffffffff#)
567 wordToWord64# w# = w#
569 intToWord32# i# = int2Word# i#
570 wordToWord32# w# = w#
573 instance Bounded Word32 where
575 #if WORD_SIZE_IN_BYTES == 8
576 maxBound = 0xffffffff
578 maxBound = minBound - 1
581 instance Real Word32 where
582 toRational x = toInteger x % 1
584 instance Integral Word32 where
586 | y /= 0 = quotWord32 x y
587 | otherwise = divZeroError "div{Word32}" x
590 | y /= 0 = quotWord32 x y
591 | otherwise = divZeroError "quot{Word32}" x
594 | y /= 0 = remWord32 x y
595 | otherwise = divZeroError "rem{Word32}" x
598 | y /= 0 = remWord32 x y
599 | otherwise = divZeroError "mod{Word32}" x
601 quotRem a b = (a `quot` b, a `rem` b)
602 divMod x y = quotRem x y
604 toInteger = word32ToInteger
608 {-# INLINE quotWord32 #-}
609 {-# INLINE remWord32 #-}
610 remWord32, quotWord32 :: Word32 -> Word32 -> Word32
611 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
612 (W32# x) `remWord32` (W32# y) = W32# (x `remWord#` y)
615 instance Ix Word32 where
618 | inRange b i = word32ToInt (i - m)
619 | otherwise = indexError b i "Word32"
620 inRange (m,n) i = m <= i && i <= n
622 instance Enum Word32 where
624 | w == maxBound = succError "Word32"
627 | w == minBound = predError "Word32"
630 -- the toEnum/fromEnum will fail if the mapping isn't legal,
631 -- use the intTo* & *ToInt coercion functions to 'bypass' these range checks.
633 | x >= 0 = intToWord32 x
635 = toEnumError "Word32" x (minBound::Word32,maxBound::Word32)
638 | x <= intToWord32 (maxBound::Int)
641 = fromEnumError "Word32" x
643 enumFrom w = [w .. maxBound]
645 | w1 <= w2 = eftt32 True{-increasing-} w1 diff_f last
651 enumFromThen w1 w2 = [w1,w2 .. last]
656 | otherwise = minBound
658 enumFromThenTo w1 w2 wend = eftt32 increasing w1 step_f last
660 increasing = w1 <= w2
665 | increasing = (> wend)
666 | otherwise = (< wend)
669 | increasing = \ x -> x + diff1
670 | otherwise = \ x -> x - diff2
672 eftt32 :: Bool -> Word32 -> (Word32 -> Word32) -> (Word32-> Bool) -> [Word32]
673 eftt32 increasing init stepper done = go init
677 | increasing && now > nxt = [now] -- oflow
678 | not increasing && now < nxt = [now] -- uflow
679 | otherwise = now : go nxt
683 instance Read Word32 where
684 readsPrec _ = readDec
686 instance Show Word32 where
687 showsPrec p w = showsPrec p (word32ToInteger w)
689 instance Bits Word32 where
690 (W32# x) .&. (W32# y) = W32# (x `and#` y)
691 (W32# x) .|. (W32# y) = W32# (x `or#` y)
692 (W32# x) `xor` (W32# y) = W32# (x `xor#` y)
693 complement (W32# x) = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
694 shift (W32# x) i@(I# i#)
695 | i > 0 = W32# (wordToWord32# (shiftL# x i#))
696 | otherwise = W32# (shiftRL# x (negateInt# i#))
697 w@(W32# x) `rotate` (I# i)
699 | i ># 0# = W32# ((wordToWord32# (shiftL# x i')) `or#`
701 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
703 | otherwise = rotate w (I# (32# +# i))
705 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
707 (W32# maxBound#) = maxBound
710 | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
711 | otherwise = 0 -- We'll be overbearing, for now..
713 testBit (W32# x#) (I# i#)
714 | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
715 | otherwise = False -- for now, this is really an error.
719 -- -----------------------------------------------------------------------------
721 -- -----------------------------------------------------------------------------
723 #if WORD_SIZE_IN_BYTES == 8
724 data Word64 = W64# Word#
726 word32ToWord64 (W32 w#) = W64# w#
728 word8ToWord64 (W8# w#) = W64# w#
729 word64ToWord8 (W64# w#) = W8# (w# `and#` (int2Word# 0xff#))
731 word16ToWord64 (W16# w#) = W64# w#
732 word64ToWord16 (W64# w#) = W16# (w# `and#` (int2Word# 0xffff#))
734 wordToWord32# :: Word# -> Word#
735 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
737 word64ToWord32 :: Word64 -> Word32
738 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
740 wordToWord64# w# = w#
741 word64ToWord# w# = w#
743 instance Eq Word64 where
744 (W64# x) == (W64# y) = x `eqWord#` y
745 (W64# x) /= (W64# y) = x `neWord#` y
747 instance Ord Word64 where
748 compare (W64# x#) (W64# y#) = compareWord# x# y#
749 (<) (W64# x) (W64# y) = x `ltWord#` y
750 (<=) (W64# x) (W64# y) = x `leWord#` y
751 (>=) (W64# x) (W64# y) = x `geWord#` y
752 (>) (W64# x) (W64# y) = x `gtWord#` y
753 max x@(W64# x#) y@(W64# y#) =
754 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
755 min x@(W64# x#) y@(W64# y#) =
756 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
758 instance Num Word64 where
759 (W64# x) + (W64# y) =
760 W64# (intToWord64# (word2Int# x +# word2Int# y))
761 (W64# x) - (W64# y) =
762 W64# (intToWord64# (word2Int# x -# word2Int# y))
763 (W64# x) * (W64# y) =
764 W64# (intToWord64# (word2Int# x *# word2Int# y))
768 else W64# (int2Word# (0x100# -# x'))
773 fromInteger (S# i#) = W64# (int2Word# i#)
774 fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
776 -- Note: no need to mask results here
777 -- as they cannot overflow.
778 instance Integral Word64 where
779 div x@(W64# x#) (W64# y#)
780 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
781 | otherwise = divZeroError "div{Word64}" x
783 quot x@(W64# x#) (W64# y#)
784 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
785 | otherwise = divZeroError "quot{Word64}" x
787 rem x@(W64# x#) (W64# y#)
788 | y# `neWord#` (int2Word# 0#) = W64# (x# `remWord#` y#)
789 | otherwise = divZeroError "rem{Word64}" x
791 mod (W64# x) (W64# y)
792 | y# `neWord#` (int2Word# 0#) = W64# (x `remWord#` y)
793 | otherwise = divZeroError "mod{Word64}" x
795 quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
796 divMod (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
798 toInteger (W64# x) = word2Integer# x
799 toInt x = word64ToInt x
801 #else /* WORD_SIZE_IN_BYTES < 8 */
803 data Word64 = W64# Word64#
805 -- for completeness sake
806 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
807 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
809 word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
810 word64ToWord8 (W64# w#) = W8# ((word64ToWord# w#) `and#` (int2Word# 0xff#))
812 word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
813 word64ToWord16 (W64# w#) = W16# ((word64ToWord# w#) `and#` (int2Word# 0xffff#))
815 word64ToInteger (W64# w#) =
816 case word64ToInteger# w# of
817 (# s#, p# #) -> J# s# p#
819 case w `quotRem` 0x100000000 of
820 (_,l) -> toInt (word64ToWord32 l)
822 intToWord64# :: Int# -> Word64#
823 intToWord64# i# = wordToWord64# (int2Word# i#)
825 intToWord64 (I# i#) = W64# (intToWord64# i#)
827 integerToWord64 (S# i#) = W64# (intToWord64# i#)
828 integerToWord64 (J# s# d#) = W64# (integerToWord64# s# d#)
830 instance Eq Word64 where
831 (W64# x) == (W64# y) = x `eqWord64#` y
832 (W64# x) /= (W64# y) = not (x `eqWord64#` y)
834 instance Ord Word64 where
835 compare (W64# x#) (W64# y#) = compareWord64# x# y#
836 (<) (W64# x) (W64# y) = x `ltWord64#` y
837 (<=) (W64# x) (W64# y) = x `leWord64#` y
838 (>=) (W64# x) (W64# y) = x `geWord64#` y
839 (>) (W64# x) (W64# y) = x `gtWord64#` y
840 max x@(W64# x#) y@(W64# y#) =
841 case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
842 min x@(W64# x#) y@(W64# y#) =
843 case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
845 instance Num Word64 where
846 (W64# x) + (W64# y) =
847 W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
848 (W64# x) - (W64# y) =
849 W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
850 (W64# x) * (W64# y) =
851 W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
854 | otherwise = maxBound - w
858 fromInteger i = integerToWord64 i
860 -- Note: no need to mask results here as they cannot overflow.
861 -- ToDo: protect against div by zero.
862 instance Integral Word64 where
863 div (W64# x) (W64# y) = W64# (x `quotWord64#` y)
864 quot (W64# x) (W64# y) = W64# (x `quotWord64#` y)
865 rem (W64# x) (W64# y) = W64# (x `remWord64#` y)
866 mod (W64# x) (W64# y) = W64# (x `remWord64#` y)
867 quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
868 divMod (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
869 toInteger w64 = word64ToInteger w64
870 toInt x = word64ToInt x
872 compareWord64# :: Word64# -> Word64# -> Ordering
874 | i# `ltWord64#` j# = LT
875 | i# `eqWord64#` j# = EQ
878 -- Word64# primop wrappers:
880 ltWord64# :: Word64# -> Word64# -> Bool
881 ltWord64# x# y# = stg_ltWord64 x# y# /=# 0#
883 leWord64# :: Word64# -> Word64# -> Bool
884 leWord64# x# y# = stg_leWord64 x# y# /=# 0#
886 eqWord64# :: Word64# -> Word64# -> Bool
887 eqWord64# x# y# = stg_eqWord64 x# y# /=# 0#
889 neWord64# :: Word64# -> Word64# -> Bool
890 neWord64# x# y# = stg_neWord64 x# y# /=# 0#
892 geWord64# :: Word64# -> Word64# -> Bool
893 geWord64# x# y# = stg_geWord64 x# y# /=# 0#
895 gtWord64# :: Word64# -> Word64# -> Bool
896 gtWord64# x# y# = stg_gtWord64 x# y# /=# 0#
898 foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64#
899 foreign import "stg_int64ToWord64" unsafe int64ToWord64# :: Int64# -> Word64#
900 foreign import "stg_word64ToInt64" unsafe word64ToInt64# :: Word64# -> Int64#
901 foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64#
902 foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word#
903 foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64#
904 foreign import "stg_remWord64" unsafe remWord64# :: Word64# -> Word64# -> Word64#
905 foreign import "stg_quotWord64" unsafe quotWord64# :: Word64# -> Word64# -> Word64#
906 foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64#
907 foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64#
908 foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64#
909 foreign import "stg_gtWord64" unsafe stg_gtWord64 :: Word64# -> Word64# -> Int#
910 foreign import "stg_geWord64" unsafe stg_geWord64 :: Word64# -> Word64# -> Int#
911 foreign import "stg_neWord64" unsafe stg_neWord64 :: Word64# -> Word64# -> Int#
912 foreign import "stg_eqWord64" unsafe stg_eqWord64 :: Word64# -> Word64# -> Int#
913 foreign import "stg_leWord64" unsafe stg_leWord64 :: Word64# -> Word64# -> Int#
914 foreign import "stg_ltWord64" unsafe stg_ltWord64 :: Word64# -> Word64# -> Int#
918 instance CCallable Word64
919 instance CReturnable Word64
921 instance Enum Word64 where
923 | w == maxBound = succError "Word64"
926 | w == minBound = predError "Word64"
930 | i >= 0 = intToWord64 i
932 = toEnumError "Word64" i (minBound::Word64,maxBound::Word64)
935 | w <= intToWord64 (maxBound::Int)
938 = fromEnumError "Word64" w
940 enumFrom e1 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger maxBound]
941 enumFromTo e1 e2 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger e2]
942 enumFromThen e1 e2 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger last]
947 | otherwise = maxBound
949 enumFromThenTo e1 e2 e3 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger e3]
951 instance Show Word64 where
952 showsPrec p x = showsPrec p (word64ToInteger x)
954 instance Read Word64 where
955 readsPrec _ s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
957 instance Ix Word64 where
960 | inRange b i = word64ToInt (i-m)
961 | otherwise = indexError b i "Word64"
962 inRange (m,n) i = m <= i && i <= n
964 instance Bounded Word64 where
966 maxBound = minBound - 1
968 instance Real Word64 where
969 toRational x = toInteger x % 1
971 #if WORD_SIZE_IN_BYTES == 8
973 instance Bits Word64 where
974 (W64# x) .&. (W64# y) = W64# (x `and#` y)
975 (W64# x) .|. (W64# y) = W64# (x `or#` y)
976 (W64# x) `xor` (W64# y) = W64# (x `xor#` y)
977 complement (W64# x) = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
978 shift (W64# x#) i@(I# i#)
979 | i > 0 = W64# (shiftL# x# i#)
980 | otherwise = W64# (shiftRL# x# (negateInt# i#))
982 w@(W64# x) `rotate` (I# i)
984 | i ># 0# = W64# (shiftL# x i') `or#`
986 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
988 | otherwise = rotate w (I# (64# +# i))
990 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
992 (W64# maxBound#) = maxBound
995 | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
996 | otherwise = 0 -- We'll be overbearing, for now..
998 testBit (W64# x#) (I# i#)
999 | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
1000 | otherwise = False -- for now, this is really an error.
1005 #else /* WORD_SIZE_IN_BYTES < 8 */
1007 instance Bits Word64 where
1008 (W64# x) .&. (W64# y) = W64# (x `and64#` y)
1009 (W64# x) .|. (W64# y) = W64# (x `or64#` y)
1010 (W64# x) `xor` (W64# y) = W64# (x `xor64#` y)
1011 complement (W64# x) = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
1012 shift (W64# x#) i@(I# i#)
1013 | i > 0 = W64# (shiftL64# x# i#)
1014 | otherwise = W64# (shiftRL64# x# (negateInt# i#))
1016 w@(W64# x) `rotate` (I# i)
1018 | i ># 0# = W64# ((shiftL64# x i') `or64#`
1019 (shiftRL64# (x `and64#`
1020 (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#`
1021 (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
1023 | otherwise = rotate w (I# (64# +# i))
1025 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
1027 (W64# maxBound#) = maxBound
1030 | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
1031 | otherwise = 0 -- We'll be overbearing, for now..
1033 testBit (W64# x#) (I# i#)
1034 | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
1035 | otherwise = False -- for now, this is really an error.
1040 foreign import "stg_not64" unsafe not64# :: Word64# -> Word64#
1041 foreign import "stg_xor64" unsafe xor64# :: Word64# -> Word64# -> Word64#
1042 foreign import "stg_or64" unsafe or64# :: Word64# -> Word64# -> Word64#
1043 foreign import "stg_and64" unsafe and64# :: Word64# -> Word64# -> Word64#
1044 foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> Word64#
1045 foreign import "stg_shiftL64" unsafe shiftL64# :: Word64# -> Int# -> Word64#
1047 #endif /* WORD_SIZE_IN_BYTES < 8 */
1053 signumReal :: (Ord a, Num a) => a -> a
1054 signumReal x | x == 0 = 0
1059 Utils for generating friendly error messages.
1062 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1063 toEnumError inst_ty tag bnds
1064 = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1065 (showParen True (showsPrec 0 tag) $
1066 " is outside of bounds " ++
1069 fromEnumError :: (Show a,Show b) => String -> a -> b
1070 fromEnumError inst_ty tag
1071 = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1072 (showParen True (showsPrec 0 tag) $
1073 " is outside of Int's bounds " ++
1074 show (minBound::Int,maxBound::Int)))
1076 succError :: String -> a
1078 = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1080 predError :: String -> a
1082 = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1084 divZeroError :: (Show a) => String -> a -> b
1086 = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")