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
284 instance Ix Word8 where
287 | inRange b i = word8ToInt (i-m)
288 | otherwise = indexError b i "Word8"
289 inRange (m,n) i = m <= i && i <= n
291 instance Enum Word8 where
293 | w == maxBound = succError "Word8"
296 | w == minBound = predError "Word8"
300 | i >= fromIntegral (minBound::Word8) && i <= fromIntegral (maxBound::Word8)
301 = W8# (intToWord8# i#)
303 = toEnumError "Word8" i (minBound::Word8,maxBound::Word8)
305 fromEnum (W8# w) = I# (word2Int# w)
307 enumFrom = boundedEnumFrom
308 enumFromThen = boundedEnumFromThen
310 instance Read Word8 where
311 readsPrec _ = readDec
313 instance Show Word8 where
314 showsPrec p w8 = showsPrec p (word8ToInt w8)
316 instance Bits Word8 where
317 (W8# x) .&. (W8# y) = W8# (x `and#` y)
318 (W8# x) .|. (W8# y) = W8# (x `or#` y)
319 (W8# x) `xor` (W8# y) = W8# (x `xor#` y)
320 complement (W8# x) = W8# (x `xor#` int2Word# 0xff#)
321 shift (W8# x#) i@(I# i#)
322 | i > 0 = W8# (wordToWord8# (shiftL# x# i#))
323 | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
324 w@(W8# x) `rotate` (I# i)
326 | i ># 0# = W8# ((wordToWord8# (shiftL# x i')) `or#`
328 (int2Word# (0x100# -# pow2# i2)))
330 | otherwise = rotate w (I# (8# +# i))
332 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
336 | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
337 | otherwise = 0 -- We'll be overbearing, for now..
339 testBit (W8# x#) (I# i#)
340 | i# <# 8# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
341 | otherwise = False -- for now, this is really an error.
346 pow2# :: Int# -> Int#
347 pow2# x# = word2Int# (shiftL# (int2Word# 1#) x#)
349 pow2_64# :: Int# -> Int64#
350 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
352 -- ---------------------------------------------------------------------------
354 -- ---------------------------------------------------------------------------
356 -- The double byte type @Word16@ is represented in the Haskell
357 -- heap by boxing up a machine word, @Word#@. An invariant
358 -- for this representation is that only the lower 16 bits are
359 -- `active', any bits above are {\em always} zeroed out.
360 -- A consequence of this is that operations that could possibly
361 -- overflow have to mask out anything above the lower two bytes
362 -- before putting together the resulting @Word16@.
364 data Word16 = W16# Word#
366 instance CCallable Word16
367 instance CReturnable Word16
369 word16ToWord8 (W16# x) = W8# (wordToWord8# x)
370 word16ToWord32 (W16# x) = W32# x
372 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
374 -- mask out upper 16 bits.
375 intToWord16# :: Int# -> Word#
376 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
378 wordToWord16# :: Word# -> Word#
379 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
381 instance Eq Word16 where
382 (W16# x) == (W16# y) = x `eqWord#` y
383 (W16# x) /= (W16# y) = x `neWord#` y
385 instance Ord Word16 where
386 compare (W16# x#) (W16# y#) = compareWord# x# y#
387 (<) (W16# x) (W16# y) = x `ltWord#` y
388 (<=) (W16# x) (W16# y) = x `leWord#` y
389 (>=) (W16# x) (W16# y) = x `geWord#` y
390 (>) (W16# x) (W16# y) = x `gtWord#` y
391 max x@(W16# x#) y@(W16# y#) =
392 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
393 min x@(W16# x#) y@(W16# y#) =
394 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
398 instance Num Word16 where
399 (W16# x) + (W16# y) =
400 W16# (intToWord16# (word2Int# x +# word2Int# y))
401 (W16# x) - (W16# y) =
402 W16# (intToWord16# (word2Int# x -# word2Int# y))
403 (W16# x) * (W16# y) =
404 W16# (intToWord16# (word2Int# x *# word2Int# y))
408 else W16# (int2Word# (0x10000# -# x'))
413 fromInteger (S# i#) = W16# (wordToWord16# (int2Word# i#))
414 fromInteger (J# s# d#) = W16# (wordToWord16# (integer2Word# s# d#))
416 instance Bounded Word16 where
420 instance Real Word16 where
421 toRational x = toInteger x % 1
423 instance Integral Word16 where
424 div x@(W16# x#) (W16# y#)
425 | y# `neWord#` (int2Word# 0#) = W16# (x# `quotWord#` y#)
426 | otherwise = divZeroError "div{Word16}" x
428 quot x@(W16# x#) (W16# y#)
429 | y# `neWord#`(int2Word# 0#) = W16# (x# `quotWord#` y#)
430 | otherwise = divZeroError "quot{Word16}" x
432 rem x@(W16# x#) (W16# y#)
433 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
434 | otherwise = divZeroError "rem{Word16}" x
436 mod x@(W16# x#) (W16# y#)
437 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
438 | otherwise = divZeroError "mod{Word16}" x
440 quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
441 divMod (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
443 toInteger = toInteger . word16ToInt
445 instance Ix Word16 where
448 | inRange b i = word16ToInt (i - m)
449 | otherwise = indexError b i "Word16"
450 inRange (m,n) i = m <= i && i <= n
452 instance Enum Word16 where
454 | w == maxBound = succError "Word16"
457 | w == minBound = predError "Word16"
461 | i >= fromIntegral (minBound::Word16) && i <= fromIntegral (maxBound::Word16)
462 = W16# (intToWord16# i#)
464 = toEnumError "Word16" i (minBound::Word16,maxBound::Word16)
466 fromEnum (W16# w) = I# (word2Int# w)
467 enumFrom = boundedEnumFrom
468 enumFromThen = boundedEnumFromThen
470 instance Read Word16 where
471 readsPrec _ = readDec
473 instance Show Word16 where
474 showsPrec p w16 = showsPrec p (word16ToInt w16)
476 instance Bits Word16 where
477 (W16# x) .&. (W16# y) = W16# (x `and#` y)
478 (W16# x) .|. (W16# y) = W16# (x `or#` y)
479 (W16# x) `xor` (W16# y) = W16# (x `xor#` y)
480 complement (W16# x) = W16# (x `xor#` int2Word# 0xffff#)
481 shift (W16# x#) i@(I# i#)
482 | i > 0 = W16# (wordToWord16# (shiftL# x# i#))
483 | otherwise = W16# (shiftRL# x# (negateInt# i#))
484 w@(W16# x) `rotate` (I# i)
486 | i ># 0# = W16# ((wordToWord16# (shiftL# x i')) `or#`
488 (int2Word# (0x10000# -# pow2# i2)))
490 | otherwise = rotate w (I# (16# +# i'))
492 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
495 | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
496 | otherwise = 0 -- We'll be overbearing, for now..
498 testBit (W16# x#) (I# i#)
499 | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
500 | otherwise = False -- for now, this is really an error.
505 -- ---------------------------------------------------------------------------
507 -- ---------------------------------------------------------------------------
509 -- The quad byte type @Word32@ is represented in the Haskell
510 -- heap by boxing up a machine word, @Word#@. An invariant
511 -- for this representation is that any bits above the lower
512 -- 32 are {\em always} zeroed out. A consequence of this is that
513 -- operations that could possibly overflow have to mask
514 -- the result before building the resulting @Word16@.
516 data Word32 = W32# Word#
518 instance CCallable Word32
519 instance CReturnable Word32
521 instance Eq Word32 where
522 (W32# x) == (W32# y) = x `eqWord#` y
523 (W32# x) /= (W32# y) = x `neWord#` y
525 instance Ord Word32 where
526 compare (W32# x#) (W32# y#) = compareWord# x# y#
527 (<) (W32# x) (W32# y) = x `ltWord#` y
528 (<=) (W32# x) (W32# y) = x `leWord#` y
529 (>=) (W32# x) (W32# y) = x `geWord#` y
530 (>) (W32# x) (W32# y) = x `gtWord#` y
531 max x@(W32# x#) y@(W32# y#) =
532 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
533 min x@(W32# x#) y@(W32# y#) =
534 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
536 instance Num Word32 where
537 (W32# x) + (W32# y) =
538 W32# (intToWord32# (word2Int# x +# word2Int# y))
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 #if WORD_SIZE_IN_BYTES == 8
547 else W32# (intToWord32# (0x100000000# -# x'))
551 negate (W32# x) = W32# (intToWord32# (negateInt# (word2Int# x)))
555 fromInteger (S# i#) = W32# (intToWord32# i#)
556 fromInteger (J# s# d#) = W32# (wordToWord32# (integer2Word# s# d#))
557 -- ToDo: restrict fromInt{eger} range.
559 intToWord32# :: Int# -> Word#
560 wordToWord32# :: Word# -> Word#
562 #if WORD_SIZE_IN_BYTES == 8
563 intToWord32# i# = (int2Word# i#) `and#` (int2Word# 0xffffffff#)
564 wordToWord32# w# = w# `and#` (int2Word# 0xffffffff#)
565 wordToWord64# w# = w#
567 intToWord32# i# = int2Word# i#
568 wordToWord32# w# = w#
571 instance Bounded Word32 where
573 #if WORD_SIZE_IN_BYTES == 8
574 maxBound = 0xffffffff
576 maxBound = minBound - 1
579 instance Real Word32 where
580 toRational x = toInteger x % 1
582 instance Integral Word32 where
584 | y /= 0 = quotWord32 x y
585 | otherwise = divZeroError "div{Word32}" x
588 | y /= 0 = quotWord32 x y
589 | otherwise = divZeroError "quot{Word32}" x
592 | y /= 0 = remWord32 x y
593 | otherwise = divZeroError "rem{Word32}" x
596 | y /= 0 = remWord32 x y
597 | otherwise = divZeroError "mod{Word32}" x
599 quotRem a b = (a `quot` b, a `rem` b)
600 divMod x y = quotRem x y
602 toInteger = word32ToInteger
605 {-# INLINE quotWord32 #-}
606 {-# INLINE remWord32 #-}
607 remWord32, quotWord32 :: Word32 -> Word32 -> Word32
608 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
609 (W32# x) `remWord32` (W32# y) = W32# (x `remWord#` y)
612 instance Ix Word32 where
615 | inRange b i = word32ToInt (i - m)
616 | otherwise = indexError b i "Word32"
617 inRange (m,n) i = m <= i && i <= n
619 instance Enum Word32 where
621 | w == maxBound = succError "Word32"
624 | w == minBound = predError "Word32"
627 -- the toEnum/fromEnum will fail if the mapping isn't legal,
628 -- use the intTo* & *ToInt coercion functions to 'bypass' these range checks.
630 | x >= 0 = intToWord32 x
632 = toEnumError "Word32" x (minBound::Word32,maxBound::Word32)
635 | x <= intToWord32 (maxBound::Int)
638 = fromEnumError "Word32" x
640 enumFrom w = [w .. maxBound]
642 | w1 <= w2 = eftt32 True{-increasing-} w1 diff_f last
648 enumFromThen w1 w2 = [w1,w2 .. last]
653 | otherwise = minBound
655 enumFromThenTo w1 w2 wend = eftt32 increasing w1 step_f last
657 increasing = w1 <= w2
662 | increasing = (> wend)
663 | otherwise = (< wend)
666 | increasing = \ x -> x + diff1
667 | otherwise = \ x -> x - diff2
669 eftt32 :: Bool -> Word32 -> (Word32 -> Word32) -> (Word32-> Bool) -> [Word32]
670 eftt32 increasing init stepper done = go init
674 | increasing && now > nxt = [now] -- oflow
675 | not increasing && now < nxt = [now] -- uflow
676 | otherwise = now : go nxt
680 instance Read Word32 where
681 readsPrec _ = readDec
683 instance Show Word32 where
684 showsPrec p w = showsPrec p (word32ToInteger w)
686 instance Bits Word32 where
687 (W32# x) .&. (W32# y) = W32# (x `and#` y)
688 (W32# x) .|. (W32# y) = W32# (x `or#` y)
689 (W32# x) `xor` (W32# y) = W32# (x `xor#` y)
690 complement (W32# x) = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
691 shift (W32# x) i@(I# i#)
692 | i > 0 = W32# (wordToWord32# (shiftL# x i#))
693 | otherwise = W32# (shiftRL# x (negateInt# i#))
694 w@(W32# x) `rotate` (I# i)
696 | i ># 0# = W32# ((wordToWord32# (shiftL# x i')) `or#`
698 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
700 | otherwise = rotate w (I# (32# +# i))
702 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
704 (W32# maxBound#) = maxBound
707 | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
708 | otherwise = 0 -- We'll be overbearing, for now..
710 testBit (W32# x#) (I# i#)
711 | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
712 | otherwise = False -- for now, this is really an error.
716 -- -----------------------------------------------------------------------------
718 -- -----------------------------------------------------------------------------
720 #if WORD_SIZE_IN_BYTES == 8
721 data Word64 = W64# Word#
723 word32ToWord64 (W32 w#) = W64# w#
725 word8ToWord64 (W8# w#) = W64# w#
726 word64ToWord8 (W64# w#) = W8# (w# `and#` (int2Word# 0xff#))
728 word16ToWord64 (W16# w#) = W64# w#
729 word64ToWord16 (W64# w#) = W16# (w# `and#` (int2Word# 0xffff#))
731 wordToWord32# :: Word# -> Word#
732 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
734 word64ToWord32 :: Word64 -> Word32
735 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
737 wordToWord64# w# = w#
738 word64ToWord# w# = w#
740 instance Eq Word64 where
741 (W64# x) == (W64# y) = x `eqWord#` y
742 (W64# x) /= (W64# y) = x `neWord#` y
744 instance Ord Word64 where
745 compare (W64# x#) (W64# y#) = compareWord# x# y#
746 (<) (W64# x) (W64# y) = x `ltWord#` y
747 (<=) (W64# x) (W64# y) = x `leWord#` y
748 (>=) (W64# x) (W64# y) = x `geWord#` y
749 (>) (W64# x) (W64# y) = x `gtWord#` y
750 max x@(W64# x#) y@(W64# y#) =
751 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
752 min x@(W64# x#) y@(W64# y#) =
753 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
755 instance Num Word64 where
756 (W64# x) + (W64# y) =
757 W64# (intToWord64# (word2Int# x +# word2Int# y))
758 (W64# x) - (W64# y) =
759 W64# (intToWord64# (word2Int# x -# word2Int# y))
760 (W64# x) * (W64# y) =
761 W64# (intToWord64# (word2Int# x *# word2Int# y))
765 else W64# (int2Word# (0x100# -# x'))
770 fromInteger (S# i#) = W64# (int2Word# i#)
771 fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
773 -- Note: no need to mask results here
774 -- as they cannot overflow.
775 instance Integral Word64 where
776 div x@(W64# x#) (W64# y#)
777 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
778 | otherwise = divZeroError "div{Word64}" x
780 quot x@(W64# x#) (W64# y#)
781 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
782 | otherwise = divZeroError "quot{Word64}" x
784 rem x@(W64# x#) (W64# y#)
785 | y# `neWord#` (int2Word# 0#) = W64# (x# `remWord#` y#)
786 | otherwise = divZeroError "rem{Word64}" x
788 mod (W64# x) (W64# y)
789 | y# `neWord#` (int2Word# 0#) = W64# (x `remWord#` y)
790 | otherwise = divZeroError "mod{Word64}" x
792 quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
793 divMod (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
795 toInteger (W64# x) = word2Integer# x
797 #else /* WORD_SIZE_IN_BYTES < 8 */
799 data Word64 = W64# Word64#
801 -- for completeness sake
802 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
803 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
805 word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
806 word64ToWord8 (W64# w#) = W8# ((word64ToWord# w#) `and#` (int2Word# 0xff#))
808 word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
809 word64ToWord16 (W64# w#) = W16# ((word64ToWord# w#) `and#` (int2Word# 0xffff#))
811 word64ToInteger (W64# w#) =
812 case word64ToInteger# w# of
813 (# s#, p# #) -> J# s# p#
814 word64ToInt (W64# w#) = I# (word2Int# (word64ToWord# w#))
816 intToWord64# :: Int# -> Word64#
817 intToWord64# i# = wordToWord64# (int2Word# i#)
819 intToWord64 (I# i#) = W64# (intToWord64# i#)
821 integerToWord64 (S# i#) = W64# (intToWord64# i#)
822 integerToWord64 (J# s# d#) = W64# (integerToWord64# s# d#)
824 instance Eq Word64 where
825 (W64# x) == (W64# y) = x `eqWord64#` y
826 (W64# x) /= (W64# y) = not (x `eqWord64#` y)
828 instance Ord Word64 where
829 compare (W64# x#) (W64# y#) = compareWord64# x# y#
830 (<) (W64# x) (W64# y) = x `ltWord64#` y
831 (<=) (W64# x) (W64# y) = x `leWord64#` y
832 (>=) (W64# x) (W64# y) = x `geWord64#` y
833 (>) (W64# x) (W64# y) = x `gtWord64#` y
834 max x@(W64# x#) y@(W64# y#) =
835 case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
836 min x@(W64# x#) y@(W64# y#) =
837 case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
839 instance Num Word64 where
840 (W64# x) + (W64# y) =
841 W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
842 (W64# x) - (W64# y) =
843 W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
844 (W64# x) * (W64# y) =
845 W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
848 | otherwise = maxBound - w
852 fromInteger i = integerToWord64 i
854 -- Note: no need to mask results here as they cannot overflow.
855 -- ToDo: protect against div by zero.
856 instance Integral Word64 where
857 div (W64# x) (W64# y) = W64# (x `quotWord64#` y)
858 quot (W64# x) (W64# y) = W64# (x `quotWord64#` y)
859 rem (W64# x) (W64# y) = W64# (x `remWord64#` y)
860 mod (W64# x) (W64# y) = W64# (x `remWord64#` y)
861 quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
862 divMod (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
863 toInteger w64 = word64ToInteger w64
865 compareWord64# :: Word64# -> Word64# -> Ordering
867 | i# `ltWord64#` j# = LT
868 | i# `eqWord64#` j# = EQ
871 -- Word64# primop wrappers:
873 ltWord64# :: Word64# -> Word64# -> Bool
874 ltWord64# x# y# = stg_ltWord64 x# y# /=# 0#
876 leWord64# :: Word64# -> Word64# -> Bool
877 leWord64# x# y# = stg_leWord64 x# y# /=# 0#
879 eqWord64# :: Word64# -> Word64# -> Bool
880 eqWord64# x# y# = stg_eqWord64 x# y# /=# 0#
882 neWord64# :: Word64# -> Word64# -> Bool
883 neWord64# x# y# = stg_neWord64 x# y# /=# 0#
885 geWord64# :: Word64# -> Word64# -> Bool
886 geWord64# x# y# = stg_geWord64 x# y# /=# 0#
888 gtWord64# :: Word64# -> Word64# -> Bool
889 gtWord64# x# y# = stg_gtWord64 x# y# /=# 0#
891 foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64#
892 foreign import "stg_int64ToWord64" unsafe int64ToWord64# :: Int64# -> Word64#
893 foreign import "stg_word64ToInt64" unsafe word64ToInt64# :: Word64# -> Int64#
894 foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64#
895 foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word#
896 foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64#
897 foreign import "stg_remWord64" unsafe remWord64# :: Word64# -> Word64# -> Word64#
898 foreign import "stg_quotWord64" unsafe quotWord64# :: Word64# -> Word64# -> Word64#
899 foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64#
900 foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64#
901 foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64#
902 foreign import "stg_gtWord64" unsafe stg_gtWord64 :: Word64# -> Word64# -> Int#
903 foreign import "stg_geWord64" unsafe stg_geWord64 :: Word64# -> Word64# -> Int#
904 foreign import "stg_neWord64" unsafe stg_neWord64 :: Word64# -> Word64# -> Int#
905 foreign import "stg_eqWord64" unsafe stg_eqWord64 :: Word64# -> Word64# -> Int#
906 foreign import "stg_leWord64" unsafe stg_leWord64 :: Word64# -> Word64# -> Int#
907 foreign import "stg_ltWord64" unsafe stg_ltWord64 :: Word64# -> Word64# -> Int#
911 instance CCallable Word64
912 instance CReturnable Word64
914 instance Enum Word64 where
916 | w == maxBound = succError "Word64"
919 | w == minBound = predError "Word64"
923 | i >= 0 = intToWord64 i
925 = toEnumError "Word64" i (minBound::Word64,maxBound::Word64)
928 | w <= intToWord64 (maxBound::Int)
931 = fromEnumError "Word64" w
933 enumFrom e1 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger maxBound]
934 enumFromTo e1 e2 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger e2]
935 enumFromThen e1 e2 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger last]
940 | otherwise = maxBound
942 enumFromThenTo e1 e2 e3 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger e3]
944 instance Show Word64 where
945 showsPrec p x = showsPrec p (word64ToInteger x)
947 instance Read Word64 where
948 readsPrec _ s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
950 instance Ix Word64 where
953 | inRange b i = word64ToInt (i-m)
954 | otherwise = indexError b i "Word64"
955 inRange (m,n) i = m <= i && i <= n
957 instance Bounded Word64 where
959 maxBound = minBound - 1
961 instance Real Word64 where
962 toRational x = toInteger x % 1
964 #if WORD_SIZE_IN_BYTES == 8
966 instance Bits Word64 where
967 (W64# x) .&. (W64# y) = W64# (x `and#` y)
968 (W64# x) .|. (W64# y) = W64# (x `or#` y)
969 (W64# x) `xor` (W64# y) = W64# (x `xor#` y)
970 complement (W64# x) = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
971 shift (W64# x#) i@(I# i#)
972 | i > 0 = W64# (shiftL# x# i#)
973 | otherwise = W64# (shiftRL# x# (negateInt# i#))
975 w@(W64# x) `rotate` (I# i)
977 | i ># 0# = W64# (shiftL# x i') `or#`
979 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
981 | otherwise = rotate w (I# (64# +# i))
983 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
985 (W64# maxBound#) = maxBound
988 | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
989 | otherwise = 0 -- We'll be overbearing, for now..
991 testBit (W64# x#) (I# i#)
992 | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
993 | otherwise = False -- for now, this is really an error.
998 #else /* WORD_SIZE_IN_BYTES < 8 */
1000 instance Bits Word64 where
1001 (W64# x) .&. (W64# y) = W64# (x `and64#` y)
1002 (W64# x) .|. (W64# y) = W64# (x `or64#` y)
1003 (W64# x) `xor` (W64# y) = W64# (x `xor64#` y)
1004 complement (W64# x) = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
1005 shift (W64# x#) i@(I# i#)
1006 | i > 0 = W64# (shiftL64# x# i#)
1007 | otherwise = W64# (shiftRL64# x# (negateInt# i#))
1009 w@(W64# x) `rotate` (I# i)
1011 | i ># 0# = W64# ((shiftL64# x i') `or64#`
1012 (shiftRL64# (x `and64#`
1013 (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#`
1014 (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
1016 | otherwise = rotate w (I# (64# +# i))
1018 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
1020 (W64# maxBound#) = maxBound
1023 | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
1024 | otherwise = 0 -- We'll be overbearing, for now..
1026 testBit (W64# x#) (I# i#)
1027 | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
1028 | otherwise = False -- for now, this is really an error.
1033 foreign import "stg_not64" unsafe not64# :: Word64# -> Word64#
1034 foreign import "stg_xor64" unsafe xor64# :: Word64# -> Word64# -> Word64#
1035 foreign import "stg_or64" unsafe or64# :: Word64# -> Word64# -> Word64#
1036 foreign import "stg_and64" unsafe and64# :: Word64# -> Word64# -> Word64#
1037 foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> Word64#
1038 foreign import "stg_shiftL64" unsafe shiftL64# :: Word64# -> Int# -> Word64#
1040 #endif /* WORD_SIZE_IN_BYTES < 8 */
1046 signumReal :: (Ord a, Num a) => a -> a
1047 signumReal x | x == 0 = 0
1052 Utils for generating friendly error messages.
1055 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1056 toEnumError inst_ty tag bnds
1057 = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1058 (showParen True (showsPrec 0 tag) $
1059 " is outside of bounds " ++
1062 fromEnumError :: (Show a,Show b) => String -> a -> b
1063 fromEnumError inst_ty tag
1064 = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1065 (showParen True (showsPrec 0 tag) $
1066 " is outside of Int's bounds " ++
1067 show (minBound::Int,maxBound::Int)))
1069 succError :: String -> a
1071 = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1073 predError :: String -> a
1075 = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1077 divZeroError :: (Show a) => String -> a -> b
1079 = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")