2 % (c) The AQUA Project, Glasgow University, 1997
4 \section[Word]{Module @Word@}
6 GHC implementation of the standard Hugs/GHC @Word@
7 interface, types and operations over unsigned, sized
14 ( Word8 -- all abstract.
15 , Word16 -- instances: Eq, Ord
16 , Word32 -- Num, Bounded, Real,
17 , Word64 -- Integral, Ix, Enum,
19 -- CCallable, CReturnable
22 , word8ToWord32 -- :: Word8 -> Word32
23 , word32ToWord8 -- :: Word32 -> Word8
24 , word16ToWord32 -- :: Word16 -> Word32
25 , word32ToWord16 -- :: Word32 -> Word16
27 , word8ToInt -- :: Word8 -> Int
28 , intToWord8 -- :: Int -> Word8
29 , word16ToInt -- :: Word16 -> Int
30 , intToWord16 -- :: Int -> Word16
31 , word32ToInt -- :: Word32 -> Int
32 , intToWord32 -- :: Int -> Word32
34 , word32ToWord64 -- :: Word32 -> Word64
35 , word64ToWord32 -- :: Word64 -> Word32
37 , word64ToInteger -- :: Word64 -> Integer
38 , integerToWord64 -- :: Integer -> Word64
41 , wordToWord8 -- :: Word -> Word8
42 , word8ToWord -- :: Word8 -> Word
43 , wordToWord16 -- :: Word -> Word16
44 , word16ToWord -- :: Word16 -> Word
45 , wordToWord32 -- :: Word -> Word32
46 , word32ToWord -- :: Word32 -> Word
47 , wordToWord64 -- :: Word -> Word64
48 , word64ToWord -- :: Word64 -> Word
50 -- The "official" place to get these from is Addr.
71 -- The "official" place to get these from is Foreign
72 #ifndef __PARALLEL_HASKELL__
73 , indexWord8OffForeignObj
74 , indexWord16OffForeignObj
75 , indexWord32OffForeignObj
76 , indexWord64OffForeignObj
78 , readWord8OffForeignObj
79 , readWord16OffForeignObj
80 , readWord32OffForeignObj
81 , readWord64OffForeignObj
83 , writeWord8OffForeignObj
84 , writeWord16OffForeignObj
85 , writeWord32OffForeignObj
86 , writeWord64OffForeignObj
89 -- non-standard, GHC specific
98 import Numeric (readDec, showInt)
102 -----------------------------------------------------------------------------
103 -- The "official" coercion functions
104 -----------------------------------------------------------------------------
106 word8ToWord32 :: Word8 -> Word32
107 word32ToWord8 :: Word32 -> Word8
108 word16ToWord32 :: Word16 -> Word32
109 word32ToWord16 :: Word32 -> Word16
111 word8ToInt :: Word8 -> Int
112 intToWord8 :: Int -> Word8
113 word16ToInt :: Word16 -> Int
114 intToWord16 :: Int -> Word16
116 word8ToInt = word32ToInt . word8ToWord32
117 intToWord8 = word32ToWord8 . intToWord32
118 word16ToInt = word32ToInt . word16ToWord32
119 intToWord16 = word32ToWord16 . intToWord32
121 intToWord32 (I# x) = W32# ((int2Word# x) `and#` (case (maxBound::Word32) of W32# x# -> x#))
122 --intToWord32 (I# x) = W32# (int2Word# x)
123 word32ToInt (W32# x) = I# (word2Int# x)
125 wordToInt :: Word -> Int
126 wordToInt (W# w#) = I# (word2Int# w#)
130 \subsection[Word8]{The @Word8@ interface}
132 The byte type @Word8@ is represented in the Haskell
133 heap by boxing up a 32-bit quantity, @Word#@. An invariant
134 for this representation is that the higher 24 bits are
135 *always* zeroed out. A consequence of this is that
136 operations that could possibly overflow have to mask
137 out the top three bytes before building the resulting @Word8@.
140 data Word8 = W8# Word#
142 instance CCallable Word8
143 instance CReturnable Word8
145 word8ToWord32 (W8# x) = W32# x
146 word32ToWord8 (W32# x) = W8# (wordToWord8# x)
148 -- mask out upper three bytes.
149 intToWord8# :: Int# -> Word#
150 intToWord8# i# = (int2Word# i#) `and#` (int2Word# 0xff#)
152 wordToWord8# :: Word# -> Word#
153 wordToWord8# w# = w# `and#` (int2Word# 0xff#)
155 instance Eq Word8 where
156 (W8# x) == (W8# y) = x `eqWord#` y
157 (W8# x) /= (W8# y) = x `neWord#` y
159 instance Ord Word8 where
160 compare (W8# x#) (W8# y#) = compareWord# x# y#
161 (<) (W8# x) (W8# y) = x `ltWord#` y
162 (<=) (W8# x) (W8# y) = x `leWord#` y
163 (>=) (W8# x) (W8# y) = x `geWord#` y
164 (>) (W8# x) (W8# y) = x `gtWord#` y
165 max x@(W8# x#) y@(W8# y#) =
166 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
167 min x@(W8# x#) y@(W8# y#) =
168 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
170 -- Helper function, used by Ord Word* instances.
171 compareWord# :: Word# -> Word# -> Ordering
173 | x# `ltWord#` y# = LT
174 | x# `eqWord#` y# = EQ
177 instance Num Word8 where
179 W8# (intToWord8# (word2Int# x +# word2Int# y))
181 W8# (intToWord8# (word2Int# x -# word2Int# y))
183 W8# (intToWord8# (word2Int# x *# word2Int# y))
187 else W8# (int2Word# (0x100# -# x'))
192 fromInteger (J# a# s# d#) = W8# (wordToWord8# (integer2Word# a# s# d#))
195 instance Bounded Word8 where
199 instance Real Word8 where
200 toRational x = toInteger x % 1
202 -- Note: no need to mask results here
203 -- as they cannot overflow.
204 instance Integral Word8 where
205 div (W8# x) (W8# y) = W8# (x `quotWord#` y)
206 quot (W8# x) (W8# y) = W8# (x `quotWord#` y)
207 rem (W8# x) (W8# y) = W8# (x `remWord#` y)
208 mod (W8# x) (W8# y) = W8# (x `remWord#` y)
209 quotRem (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
210 divMod (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
211 toInteger (W8# x) = word2Integer# x
212 toInt x = word8ToInt x
214 instance Ix Word8 where
217 | inRange b i = word8ToInt (i-m)
218 | otherwise = error (showString "Ix{Word8}.index: Index " .
219 showParen True (showsPrec 0 i) .
220 showString " out of range " $
221 showParen True (showsPrec 0 b) "")
222 inRange (m,n) i = m <= i && i <= n
224 instance Enum Word8 where
225 toEnum (I# i) = W8# (intToWord8# i)
226 fromEnum (W8# w) = I# (word2Int# w)
227 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Word8)]
228 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Word8)]
229 where last = if d < c then minBound else maxBound
231 instance Read Word8 where
232 readsPrec p = readDec
234 instance Show Word8 where
235 showsPrec p = showInt
238 -- Word8s are represented by an (unboxed) 32-bit Word.
239 -- The invariant is that the upper 24 bits are always zeroed out.
241 instance Bits Word8 where
242 (W8# x) .&. (W8# y) = W8# (x `and#` y)
243 (W8# x) .|. (W8# y) = W8# (x `or#` y)
244 (W8# x) `xor` (W8# y) = W8# (x `xor#` y)
245 complement (W8# x) = W8# (x `xor#` int2Word# 0xff#)
246 shift (W8# x#) i@(I# i#)
247 | i > 0 = W8# (wordToWord8# (shiftL# x# i#))
248 | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
249 w@(W8# x) `rotate` (I# i)
251 | i ># 0# = W8# ((wordToWord8# (shiftL# x i')) `or#`
253 (int2Word# (0x100# -# pow2# i2)))
255 | otherwise = rotate w (I# (8# +# i))
257 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
261 | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
262 | otherwise = 0 -- We'll be overbearing, for now..
264 setBit x i = x .|. bit i
265 clearBit x i = x .&. complement (bit i)
266 complementBit x i = x `xor` bit i
268 testBit (W8# x#) (I# i#)
269 | i# <# 8# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
270 | otherwise = False -- for now, this is really an error.
275 pow2# :: Int# -> Int#
276 pow2# x# = word2Int# (shiftL# (int2Word# 1#) x#)
278 pow2_64# :: Int# -> Int64#
279 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
281 sizeofWord8 :: Word32
286 \subsection[Word16]{The @Word16@ interface}
288 The double byte type @Word16@ is represented in the Haskell
289 heap by boxing up a machine word, @Word#@. An invariant
290 for this representation is that only the lower 16 bits are
291 `active', any bits above are {\em always} zeroed out.
292 A consequence of this is that operations that could possibly
293 overflow have to mask out anything above the lower two bytes
294 before putting together the resulting @Word16@.
297 data Word16 = W16# Word#
298 instance CCallable Word16
299 instance CReturnable Word16
301 word16ToWord32 (W16# x) = W32# x
302 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
304 -- mask out upper 16 bits.
305 intToWord16# :: Int# -> Word#
306 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
308 wordToWord16# :: Word# -> Word#
309 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
311 instance Eq Word16 where
312 (W16# x) == (W16# y) = x `eqWord#` y
313 (W16# x) /= (W16# y) = x `neWord#` y
315 instance Ord Word16 where
316 compare (W16# x#) (W16# y#) = compareWord# x# y#
317 (<) (W16# x) (W16# y) = x `ltWord#` y
318 (<=) (W16# x) (W16# y) = x `leWord#` y
319 (>=) (W16# x) (W16# y) = x `geWord#` y
320 (>) (W16# x) (W16# y) = x `gtWord#` y
321 max x@(W16# x#) y@(W16# y#) =
322 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
323 min x@(W16# x#) y@(W16# y#) =
324 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
326 instance Num Word16 where
327 (W16# x) + (W16# y) =
328 W16# (intToWord16# (word2Int# x +# word2Int# y))
329 (W16# x) - (W16# y) =
330 W16# (intToWord16# (word2Int# x -# word2Int# y))
331 (W16# x) * (W16# y) =
332 W16# (intToWord16# (word2Int# x *# word2Int# y))
336 else W16# (int2Word# (0x10000# -# x'))
341 fromInteger (J# a# s# d#) = W16# (wordToWord16# (integer2Word# a# s# d#))
342 fromInt = intToWord16
344 instance Bounded Word16 where
348 instance Real Word16 where
349 toRational x = toInteger x % 1
351 instance Integral Word16 where
352 div (W16# x) (W16# y) = W16# (x `quotWord#` y)
353 quot (W16# x) (W16# y) = W16# (x `quotWord#` y)
354 rem (W16# x) (W16# y) = W16# (x `remWord#` y)
355 mod (W16# x) (W16# y) = W16# (x `remWord#` y)
356 quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
357 divMod (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
358 toInteger (W16# x) = word2Integer# x
359 toInt x = word16ToInt x
361 instance Ix Word16 where
364 | inRange b i = word16ToInt (i - m)
365 | otherwise = error (showString "Ix{Word16}.index: Index " .
366 showParen True (showsPrec 0 i) .
367 showString " out of range " $
368 showParen True (showsPrec 0 b) "")
369 inRange (m,n) i = m <= i && i <= n
371 instance Enum Word16 where
372 toEnum (I# i) = W16# (intToWord16# i)
373 fromEnum (W16# w) = I# (word2Int# w)
374 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Word16)]
375 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum (last::Word16)]
376 where last = if d < c then minBound else maxBound
378 instance Read Word16 where
379 readsPrec p = readDec
381 instance Show Word16 where
382 showsPrec p = showInt
384 instance Bits Word16 where
385 (W16# x) .&. (W16# y) = W16# (x `and#` y)
386 (W16# x) .|. (W16# y) = W16# (x `or#` y)
387 (W16# x) `xor` (W16# y) = W16# (x `xor#` y)
388 complement (W16# x) = W16# (x `xor#` int2Word# 0xffff#)
389 shift (W16# x#) i@(I# i#)
390 | i > 0 = W16# (wordToWord16# (shiftL# x# i#))
391 | otherwise = W16# (shiftRL# x# (negateInt# i#))
392 w@(W16# x) `rotate` (I# i)
394 | i ># 0# = W16# ((wordToWord16# (shiftL# x i')) `or#`
396 (int2Word# (0x10000# -# pow2# i2)))
398 | otherwise = rotate w (I# (16# +# i'))
400 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
403 | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
404 | otherwise = 0 -- We'll be overbearing, for now..
406 setBit x i = x .|. bit i
407 clearBit x i = x .&. complement (bit i)
408 complementBit x i = x `xor` bit i
410 testBit (W16# x#) (I# i#)
411 | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
412 | otherwise = False -- for now, this is really an error.
418 sizeofWord16 :: Word32
423 \subsection[Word32]{The @Word32@ interface}
425 The quad byte type @Word32@ is represented in the Haskell
426 heap by boxing up a machine word, @Word#@. An invariant
427 for this representation is that any bits above the lower
428 32 are {\em always} zeroed out. A consequence of this is that
429 operations that could possibly overflow have to mask
430 the result before building the resulting @Word16@.
433 data Word32 = W32# Word#
435 instance CCallable Word32
436 instance CReturnable Word32
438 instance Eq Word32 where
439 (W32# x) == (W32# y) = x `eqWord#` y
440 (W32# x) /= (W32# y) = x `neWord#` y
442 instance Ord Word32 where
443 compare (W32# x#) (W32# y#) = compareWord# x# y#
444 (<) (W32# x) (W32# y) = x `ltWord#` y
445 (<=) (W32# x) (W32# y) = x `leWord#` y
446 (>=) (W32# x) (W32# y) = x `geWord#` y
447 (>) (W32# x) (W32# y) = x `gtWord#` y
448 max x@(W32# x#) y@(W32# y#) =
449 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
450 min x@(W32# x#) y@(W32# y#) =
451 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
453 instance Num Word32 where
454 (W32# x) + (W32# y) =
455 W32# (intToWord32# (word2Int# x +# word2Int# y))
456 (W32# x) - (W32# y) =
457 W32# (intToWord32# (word2Int# x -# word2Int# y))
458 (W32# x) * (W32# y) =
459 W32# (intToWord32# (word2Int# x *# word2Int# y))
460 #if WORD_SIZE_IN_BYTES == 8
464 else W32# (intToWord32# (0x100000000# -# x'))
468 negate (W32# x) = W32# (intToWord32# (negateInt# (word2Int# x)))
472 fromInteger (J# a# s# d#) = W32# (integer2Word# a# s# d#)
473 fromInt (I# x) = W32# (intToWord32# x)
474 -- ToDo: restrict fromInt{eger} range.
476 intToWord32# :: Int# -> Word#
477 wordToWord32# :: Word# -> Word#
479 #if WORD_SIZE_IN_BYTES == 8
480 intToWord32# i# = (int2Word# i#) `and#` (int2Word# 0xffffffff)
481 wordToWord32# w# = w# `and#` (int2Word# 0xffffffff)
483 intToWord32# i# = int2Word# i#
484 wordToWord32# w# = w#
488 instance Bounded Word32 where
490 #if WORD_SIZE_IN_BYTES == 8
491 maxBound = 0xffffffff
493 maxBound = minBound - 1
496 instance Real Word32 where
497 toRational x = toInteger x % 1
499 instance Integral Word32 where
500 div x y = quotWord32 x y
501 quot x y = quotWord32 x y
502 rem x y = remWord32 x y
503 mod x y = remWord32 x y
504 quotRem a b = (a `quotWord32` b, a `remWord32` b)
505 divMod x y = quotRem x y
506 toInteger (W32# x) = word2Integer# x
507 toInt (W32# x) = I# (word2Int# x)
509 {-# INLINE quotWord32 #-}
510 {-# INLINE remWord32 #-}
511 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
512 (W32# x) `remWord32` (W32# y) = W32# (x `remWord#` y)
514 instance Ix Word32 where
517 | inRange b i = word32ToInt (i - m)
518 | otherwise = error (showString "Ix{Word32}.index: Index " .
519 showParen True (showsPrec 0 i) .
520 showString " out of range " $
521 showParen True (showsPrec 0 b) "")
522 inRange (m,n) i = m <= i && i <= n
524 instance Enum Word32 where
526 fromEnum = word32ToInt -- lossy, don't use.
527 enumFrom w = eft32 w 1
528 enumFromTo w1 w2 = eftt32 w1 1 (> w2)
529 enumFromThen w1 w2 = eftt32 w1 (w2 - w1) (>last)
532 | w1 < w2 = maxBound::Word32
533 | otherwise = minBound
535 eftt32 :: Word32 -> Word32 -> (Word32->Bool) -> [Word32]
536 eftt32 now step done = go now
540 | otherwise = now : go (now+step)
542 eft32 :: Word32 -> Word32 -> [Word32]
543 eft32 now step = go now
546 | x == maxBound = [x]
547 | otherwise = x:go (x+step)
549 instance Read Word32 where
550 readsPrec p = readDec
552 instance Show Word32 where
553 showsPrec p = showInt
555 instance Bits Word32 where
556 (W32# x) .&. (W32# y) = W32# (x `and#` y)
557 (W32# x) .|. (W32# y) = W32# (x `or#` y)
558 (W32# x) `xor` (W32# y) = W32# (x `xor#` y)
559 complement (W32# x) = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
560 shift (W32# x) i@(I# i#)
561 | i > 0 = W32# (wordToWord32# (shiftL# x i#))
562 | otherwise = W32# (shiftRL# x (negateInt# i#))
563 w@(W32# x) `rotate` (I# i)
565 | i ># 0# = W32# ((wordToWord32# (shiftL# x i')) `or#`
567 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
569 | otherwise = rotate w (I# (32# +# i))
571 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
573 (W32# maxBound#) = maxBound
576 | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
577 | otherwise = 0 -- We'll be overbearing, for now..
579 setBit x i = x .|. bit i
580 clearBit x i = x .&. complement (bit i)
581 complementBit x i = x `xor` bit i
583 testBit (W32# x#) (I# i#)
584 | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
585 | otherwise = False -- for now, this is really an error.
589 sizeofWord32 :: Word32
593 \subsection[Word64]{The @Word64@ interface}
596 #if WORD_SIZE_IN_BYTES == 8
597 data Word64 = W64# Word#
599 word32ToWord64 :: Word32 -> Word64
600 word32ToWord64 (W32 w#) = W64# w#
602 wordToWord32# :: Word# -> Word#
603 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
605 word64ToWord32 :: Word64 -> Word32
606 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
608 wordToWord64# w# = w#
609 word64ToWord# w# = w#
611 instance Eq Word64 where
612 (W64# x) == (W64# y) = x `eqWord#` y
613 (W64# x) /= (W64# y) = x `neWord#` y
615 instance Ord Word64 where
616 compare (W64# x#) (W64# y#) = compareWord# x# y#
617 (<) (W64# x) (W64# y) = x `ltWord#` y
618 (<=) (W64# x) (W64# y) = x `leWord#` y
619 (>=) (W64# x) (W64# y) = x `geWord#` y
620 (>) (W64# x) (W64# y) = x `gtWord#` y
621 max x@(W64# x#) y@(W64# y#) =
622 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
623 min x@(W64# x#) y@(W64# y#) =
624 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
626 instance Num Word64 where
627 (W64# x) + (W64# y) =
628 W64# (intToWord64# (word2Int# x +# word2Int# y))
629 (W64# x) - (W64# y) =
630 W64# (intToWord64# (word2Int# x -# word2Int# y))
631 (W64# x) * (W64# y) =
632 W64# (intToWord64# (word2Int# x *# word2Int# y))
636 else W64# (int2Word# (0x100# -# x'))
641 fromInteger (J# a# s# d#) = W64# (integer2Word# a# s# d#)
642 fromInt = intToWord64
644 instance Bounded Word64 where
646 maxBound = minBound - 1
648 instance Real Word64 where
649 toRational x = toInteger x % 1
651 -- Note: no need to mask results here
652 -- as they cannot overflow.
653 instance Integral Word64 where
654 div (W64# x) (W64# y) = W64# (x `quotWord#` y)
655 quot (W64# x) (W64# y) = W64# (x `quotWord#` y)
656 rem (W64# x) (W64# y) = W64# (x `remWord#` y)
657 mod (W64# x) (W64# y) = W64# (x `remWord#` y)
658 quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
659 divMod (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
660 toInteger (W64# x) = word2Integer# x
661 toInt x = word64ToInt x
663 instance Ix Word64 where
666 | inRange b i = word64ToInt (i-m)
667 | otherwise = error (showString "Ix{Word64}.index: Index " .
668 showParen True (showsPrec 0 i) .
669 showString " out of range " $
670 showParen True (showsPrec 0 b) "")
671 inRange (m,n) i = m <= i && i <= n
673 instance Enum Word64 where
674 toEnum (I# i) = W64# (intToWord# i)
675 fromEnum (W64# w) = I# (word2Int# w) -- lossy, don't use.
676 enumFrom w = eft64 w 1
677 enumFromTo w1 w2 = eftt64 w1 1 (> w2)
678 enumFromThen w1 w2 = eftt64 w1 (w2 - w1) (>last)
681 | w1 < w2 = maxBound::Word64
682 | otherwise = minBound
684 instance Read Word64 where
685 readsPrec p = readDec
687 instance Show Word64 where
688 showsPrec p = showInt
691 instance Bits Word64 where
692 (W64# x) .&. (W64# y) = W64# (x `and#` y)
693 (W64# x) .|. (W64# y) = W64# (x `or#` y)
694 (W64# x) `xor` (W64# y) = W64# (x `xor#` y)
695 complement (W64# x) = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
696 shift (W64# x#) i@(I# i#)
697 | i > 0 = W64# (shiftL# x# i#)
698 | otherwise = W64# (shiftRL# x# (negateInt# i#))
700 w@(W64# x) `rotate` (I# i)
702 | i ># 0# = W64# (shiftL# x i') `or#`
704 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
706 | otherwise = rotate w (I# (64# +# i))
708 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
710 (W64# maxBound#) = maxBound
713 | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
714 | otherwise = 0 -- We'll be overbearing, for now..
716 setBit x i = x .|. bit i
717 clearBit x i = x .&. complement (bit i)
718 complementBit x i = x `xor` bit i
720 testBit (W64# x#) (I# i#)
721 | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
722 | otherwise = False -- for now, this is really an error.
728 --defined in PrelCCall: data Word64 = W64 Word64# deriving (Eq, Ord, Bounded)
730 -- for completeness sake
731 word32ToWord64 :: Word32 -> Word64
732 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
734 word64ToWord32 :: Word64 -> Word32
735 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
737 word64ToInteger :: Word64 -> Integer
738 word64ToInteger (W64# w#) = word64ToInteger# w#
740 word64ToInt :: Word64 -> Int
742 case w `quotRem` 0x100000000 of
743 (h,l) -> toInt (word64ToWord32 l)
745 intToWord64# :: Int# -> Word64#
746 intToWord64# i# = wordToWord64# (int2Word# i#)
748 intToWord64 :: Int -> Word64
749 intToWord64 (I# i#) = W64# (intToWord64# i#)
751 integerToWord64 :: Integer -> Word64
752 integerToWord64 (J# a# s# d#) = W64# (integerToWord64# a# s# d#)
754 instance Show Word64 where
755 showsPrec p x = showsPrec p (word64ToInteger x)
757 instance Read Word64 where
758 readsPrec p s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
760 instance Eq Word64 where
761 (W64# x) == (W64# y) = x `eqWord64#` y
762 (W64# x) /= (W64# y) = not (x `eqWord64#` y)
764 instance Ord Word64 where
765 compare (W64# x#) (W64# y#) = compareWord64# x# y#
766 (<) (W64# x) (W64# y) = x `ltWord64#` y
767 (<=) (W64# x) (W64# y) = x `leWord64#` y
768 (>=) (W64# x) (W64# y) = x `geWord64#` y
769 (>) (W64# x) (W64# y) = x `gtWord64#` y
770 max x@(W64# x#) y@(W64# y#) =
771 case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
772 min x@(W64# x#) y@(W64# y#) =
773 case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
775 instance Num Word64 where
776 (W64# x) + (W64# y) =
777 W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
778 (W64# x) - (W64# y) =
779 W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
780 (W64# x) * (W64# y) =
781 W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
784 | otherwise = maxBound - w
788 fromInteger i = integerToWord64 i
789 fromInt = intToWord64
791 instance Bounded Word64 where
793 maxBound = minBound - 1
795 instance Real Word64 where
796 toRational x = toInteger x % 1
798 -- Note: no need to mask results here
799 -- as they cannot overflow.
800 instance Integral Word64 where
801 div (W64# x) (W64# y) = W64# (x `quotWord64#` y)
802 quot (W64# x) (W64# y) = W64# (x `quotWord64#` y)
803 rem (W64# x) (W64# y) = W64# (x `remWord64#` y)
804 mod (W64# x) (W64# y) = W64# (x `remWord64#` y)
805 quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
806 divMod (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
807 toInteger w64 = word64ToInteger w64
808 toInt x = word64ToInt x
811 instance Ix Word64 where
814 | inRange b i = word64ToInt (i-m)
815 | otherwise = error (showString "Ix{Word64}.index: Index " .
816 showParen True (showsPrec 0 i) .
817 showString " out of range " $
818 showParen True (showsPrec 0 b) "")
819 inRange (m,n) i = m <= i && i <= n
821 instance Enum Word64 where
822 toEnum (I# i) = W64# (intToWord64# i)
823 fromEnum (W64# w) = I# (word2Int# (word64ToWord# w)) -- lossy, don't use.
824 enumFrom w = eft64 w 1
825 enumFromTo w1 w2 = eftt64 w1 1 (> w2)
826 enumFromThen w1 w2 = eftt64 w1 (w2 - w1) (>last)
829 | w1 < w2 = maxBound::Word64
830 | otherwise = minBound
832 instance Bits Word64 where
833 (W64# x) .&. (W64# y) = W64# (x `and64#` y)
834 (W64# x) .|. (W64# y) = W64# (x `or64#` y)
835 (W64# x) `xor` (W64# y) = W64# (x `xor64#` y)
836 complement (W64# x) = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
837 shift (W64# x#) i@(I# i#)
838 | i > 0 = W64# (shiftL64# x# i#)
839 | otherwise = W64# (shiftRL64# x# (negateInt# i#))
841 w@(W64# x) `rotate` (I# i)
843 | i ># 0# = W64# ((shiftL64# x i') `or64#`
844 (shiftRL64# (x `and64#`
845 (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#`
846 (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
848 | otherwise = rotate w (I# (64# +# i))
850 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
852 (W64# maxBound#) = maxBound
855 | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
856 | otherwise = 0 -- We'll be overbearing, for now..
858 setBit x i = x .|. bit i
859 clearBit x i = x .&. complement (bit i)
860 complementBit x i = x `xor` bit i
862 testBit (W64# x#) (I# i#)
863 | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
864 | otherwise = False -- for now, this is really an error.
870 | i# `ltWord64#` j# = LT
871 | i# `eqWord64#` j# = EQ
874 -- Word64# primop wrappers:
876 ltWord64# :: Word64# -> Word64# -> Bool
877 ltWord64# x# y# = unsafePerformIO $ do
878 v <- _ccall_ stg_ltWord64 x# y#
883 leWord64# :: Word64# -> Word64# -> Bool
884 leWord64# x# y# = unsafePerformIO $ do
885 v <- _ccall_ stg_leWord64 x# y#
890 eqWord64# :: Word64# -> Word64# -> Bool
891 eqWord64# x# y# = unsafePerformIO $ do
892 v <- _ccall_ stg_eqWord64 x# y#
897 neWord64# :: Word64# -> Word64# -> Bool
898 neWord64# x# y# = unsafePerformIO $ do
899 v <- _ccall_ stg_neWord64 x# y#
904 geWord64# :: Word64# -> Word64# -> Bool
905 geWord64# x# y# = unsafePerformIO $ do
906 v <- _ccall_ stg_geWord64 x# y#
911 gtWord64# :: Word64# -> Word64# -> Bool
912 gtWord64# x# y# = unsafePerformIO $ do
913 v <- _ccall_ stg_gtWord64 x# y#
918 plusInt64# :: Int64# -> Int64# -> Int64#
920 case (unsafePerformIO (_ccall_ stg_plusInt64 a# b#)) of
923 minusInt64# :: Int64# -> Int64# -> Int64#
925 case (unsafePerformIO (_ccall_ stg_minusInt64 a# b#)) of
928 timesInt64# :: Int64# -> Int64# -> Int64#
930 case (unsafePerformIO (_ccall_ stg_timesInt64 a# b#)) of
933 quotWord64# :: Word64# -> Word64# -> Word64#
935 case (unsafePerformIO (_ccall_ stg_quotWord64 a# b#)) of
938 remWord64# :: Word64# -> Word64# -> Word64#
940 case (unsafePerformIO (_ccall_ stg_remWord64 a# b#)) of
943 negateInt64# :: Int64# -> Int64#
945 case (unsafePerformIO (_ccall_ stg_negateInt64 a#)) of
948 and64# :: Word64# -> Word64# -> Word64#
950 case (unsafePerformIO (_ccall_ stg_and64 a# b#)) of
953 or64# :: Word64# -> Word64# -> Word64#
955 case (unsafePerformIO (_ccall_ stg_or64 a# b#)) of
958 xor64# :: Word64# -> Word64# -> Word64#
960 case (unsafePerformIO (_ccall_ stg_xor64 a# b#)) of
963 not64# :: Word64# -> Word64#
965 case (unsafePerformIO (_ccall_ stg_not64 a#)) of
968 shiftL64# :: Word64# -> Int# -> Word64#
970 case (unsafePerformIO (_ccall_ stg_shiftL64 a# b#)) of
973 shiftRL64# :: Word64# -> Int# -> Word64#
975 case (unsafePerformIO (_ccall_ stg_shiftRL64 a# b#)) of
978 word64ToWord# :: Word64# -> Word#
980 case (unsafePerformIO (_ccall_ stg_word64ToWord w#)) of
983 wordToWord64# :: Word# -> Word64#
985 case (unsafePerformIO (_ccall_ stg_wordToWord64 w#)) of
988 word64ToInt64# :: Word64# -> Int64#
990 case (unsafePerformIO (_ccall_ stg_word64ToInt64 w#)) of
993 int64ToWord64# :: Int64# -> Word64#
995 case (unsafePerformIO (_ccall_ stg_int64ToWord64 w#)) of
998 intToInt64# :: Int# -> Int64#
1000 case (unsafePerformIO (_ccall_ stg_intToInt64 i#)) of
1005 sizeofWord64 :: Word32
1008 -- Enum Word64 helper funs:
1010 eftt64 :: Word64 -> Word64 -> (Word64->Bool) -> [Word64]
1011 eftt64 now step done = go now
1015 | otherwise = now : go (now+step)
1017 eft64 :: Word64 -> Word64 -> [Word64]
1018 eft64 now step = go now
1021 | x == maxBound = [x]
1022 | otherwise = x:go (x+step)
1027 The Hugs-GHC extension libraries provide functions for going between
1028 Int and the various (un)signed ints. Here we provide the same for
1029 the GHC specific Word type:
1032 wordToWord8 :: Word -> Word8
1033 word8ToWord :: Word8 -> Word
1034 wordToWord16 :: Word -> Word16
1035 word16ToWord :: Word16 -> Word
1036 wordToWord32 :: Word -> Word32
1037 word32ToWord :: Word32 -> Word
1039 word8ToWord (W8# w#) = W# w#
1040 wordToWord8 (W# w#) = W8# (w# `and#` (case (maxBound::Word8) of W8# x# -> x#))
1041 word16ToWord (W16# w#) = W# w#
1042 wordToWord16 (W# w#) = W16# (w# `and#` (case (maxBound::Word16) of W16# x# -> x#))
1043 word32ToWord (W32# w#) = W# w#
1044 wordToWord32 (W# w#) = W32# (w# `and#` (case (maxBound::Word32) of W32# x# -> x#))
1046 wordToWord64 :: Word -> Word64
1047 wordToWord64 (W# w#) = W64# (wordToWord64# w#)
1049 -- lossy on 32-bit platforms, but provided nontheless.
1050 word64ToWord :: Word64 -> Word
1051 word64ToWord (W64# w#) = W# (word64ToWord# w#)
1056 --End of exported definitions
1058 The remainder of this file consists of definitions which are only
1059 used in the implementation.
1062 signumReal x | x == 0 = 0
1068 NOTE: the index is in units of the size of the type, *not* bytes.
1071 indexWord8OffAddr :: Addr -> Int -> Word8
1072 indexWord8OffAddr (A# a#) (I# i#) = intToWord8 (I# (ord# (indexCharOffAddr# a# i#)))
1074 indexWord16OffAddr :: Addr -> Int -> Word16
1075 indexWord16OffAddr a i =
1076 #ifdef WORDS_BIGENDIAN
1077 intToWord16 ( word8ToInt l + (word8ToInt maxBound) * word8ToInt h)
1079 intToWord16 ( word8ToInt h + (word8ToInt maxBound) * word8ToInt l)
1083 l = indexWord8OffAddr a byte_idx
1084 h = indexWord8OffAddr a (byte_idx+1)
1086 indexWord32OffAddr :: Addr -> Int -> Word32
1087 indexWord32OffAddr (A# a#) i = wordToWord32 (W# (indexWordOffAddr# a# i'#))
1089 -- adjust index to be in Word units, not Word32 ones.
1091 #if WORD_SIZE_IN_BYTES==8
1097 indexWord64OffAddr :: Addr -> Int -> Word64
1098 indexWord64OffAddr (A# a#) (I# i#)
1099 #if WORD_SIZE_IN_BYTES==8
1100 = W64# (indexWordOffAddr# a# i#)
1102 = W64# (indexWord64OffAddr# a# i#)
1105 #ifndef __PARALLEL_HASKELL__
1107 indexWord8OffForeignObj :: ForeignObj -> Int -> Word8
1108 indexWord8OffForeignObj (ForeignObj fo#) (I# i#) = intToWord8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
1110 indexWord16OffForeignObj :: ForeignObj -> Int -> Word16
1111 indexWord16OffForeignObj fo i =
1112 #ifdef WORDS_BIGENDIAN
1113 intToWord16 ( word8ToInt l + (word8ToInt maxBound) * word8ToInt h)
1115 intToWord16 ( word8ToInt h + (word8ToInt maxBound) * word8ToInt l)
1119 l = indexWord8OffForeignObj fo byte_idx
1120 h = indexWord8OffForeignObj fo (byte_idx+1)
1122 indexWord32OffForeignObj :: ForeignObj -> Int -> Word32
1123 indexWord32OffForeignObj (ForeignObj fo#) i = wordToWord32 (W# (indexWordOffForeignObj# fo# i'#))
1125 -- adjust index to be in Word units, not Word32 ones.
1127 #if WORD_SIZE_IN_BYTES==8
1133 indexWord64OffForeignObj :: ForeignObj -> Int -> Word64
1134 indexWord64OffForeignObj (ForeignObj fo#) (I# i#)
1135 #if WORD_SIZE_IN_BYTES==8
1136 = W64# (indexWordOffForeignObj# fo# i#)
1138 = W64# (indexWord64OffForeignObj# fo# i#)
1144 Read words out of mutable memory:
1147 readWord8OffAddr :: Addr -> Int -> IO Word8
1148 readWord8OffAddr a i = _casm_ `` %r=(StgWord8)(((StgWord8*)%0)[(StgInt)%1]); '' a i
1150 readWord16OffAddr :: Addr -> Int -> IO Word16
1151 readWord16OffAddr a i = _casm_ `` %r=(StgWord16)(((StgWord16*)%0)[(StgInt)%1]); '' a i
1153 readWord32OffAddr :: Addr -> Int -> IO Word32
1154 readWord32OffAddr a i = _casm_ `` %r=(StgWord32)(((StgWord32*)%0)[(StgInt)%1]); '' a i
1156 readWord64OffAddr :: Addr -> Int -> IO Word64
1157 #if WORD_SIZE_IN_BYTES==8
1158 readWord64OffAddr a i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' a i
1160 readWord64OffAddr a i = _casm_ `` %r=(StgWord64)(((StgWord64*)%0)[(StgInt)%1]); '' a i
1163 #ifndef __PARALLEL_HASKELL__
1164 readWord8OffForeignObj :: ForeignObj -> Int -> IO Word8
1165 readWord8OffForeignObj fo i = _casm_ `` %r=(StgWord8)(((StgWord8*)%0)[(StgInt)%1]); '' fo i
1167 readWord16OffForeignObj :: ForeignObj -> Int -> IO Word16
1168 readWord16OffForeignObj fo i = _casm_ `` %r=(StgWord16)(((StgWord16*)%0)[(StgInt)%1]); '' fo i
1170 readWord32OffForeignObj :: ForeignObj -> Int -> IO Word32
1171 readWord32OffForeignObj fo i = _casm_ `` %r=(StgWord32)(((StgWord32*)%0)[(StgInt)%1]); '' fo i
1173 readWord64OffForeignObj :: ForeignObj -> Int -> IO Word64
1174 #if WORD_SIZE_IN_BYTES==8
1175 readWord64OffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
1177 readWord64OffForeignObj fo i = _casm_ `` %r=(StgWord64)(((StgWord64*)%0)[(StgInt)%1]); '' fo i
1184 Note: we provide primops for the writing via Addrs since that's used
1185 in the IO implementation (a place where we *really* do care about cycles.)
1188 writeWord8OffAddr :: Addr -> Int -> Word8 -> IO ()
1189 writeWord8OffAddr (A# a#) (I# i#) (W8# w#) = IO $ \ s# ->
1190 case (writeCharOffAddr# a# i# (chr# (word2Int# w#)) s#) of s2# -> IOok s2# ()
1192 writeWord16OffAddr :: Addr -> Int -> Word16 -> IO ()
1193 writeWord16OffAddr a i e = _casm_ `` (((StgWord16*)%0)[(StgInt)%1])=(StgWord16)%2; '' a i e
1195 writeWord32OffAddr :: Addr -> Int -> Word32 -> IO ()
1196 writeWord32OffAddr (A# a#) i@(I# i#) (W32# w#) = IO $ \ s# ->
1197 case (writeWordOffAddr# a# i'# w# s#) of s2# -> IOok s2# ()
1199 -- adjust index to be in Word units, not Word32 ones.
1201 #if WORD_SIZE_IN_BYTES==8
1207 writeWord64OffAddr :: Addr -> Int -> Word64 -> IO ()
1208 #if WORD_SIZE_IN_BYTES==8
1209 writeWord64OffAddr (A# a#) (I# i#) (W64# w#) = IO $ \ s# ->
1210 case (writeWordOffAddr# a# i# w# s#) of s2# -> IOok s2# ()
1212 writeWord64OffAddr (A# a#) (I# i#) (W64# w#) = IO $ \ s# ->
1213 case (writeWord64OffAddr# a# i# w# s#) of s2# -> IOok s2# ()
1216 #ifndef __PARALLEL_HASKELL__
1218 writeWord8OffForeignObj :: ForeignObj -> Int -> Word8 -> IO ()
1219 writeWord8OffForeignObj fo i w = _casm_ `` (((StgWord16*)%0)[(StgInt)%1])=(StgWord16)%2; '' fo i w
1221 writeWord16OffForeignObj :: ForeignObj -> Int -> Word16 -> IO ()
1222 writeWord16OffForeignObj fo i w = _casm_ `` (((StgWord16*)%0)[(StgInt)%1])=(StgWord16)%2; '' fo i w
1224 writeWord32OffForeignObj :: ForeignObj -> Int -> Word32 -> IO ()
1225 writeWord32OffForeignObj fo i w = _casm_ `` (((StgWord16*)%0)[(StgInt)%1])=(StgWord16)%2; '' fo i' w
1227 -- adjust index to be in Word units, not Word32 ones.
1229 #if WORD_SIZE_IN_BYTES==8
1235 writeWord64OffForeignObj :: ForeignObj -> Int -> Word64 -> IO ()
1236 #if WORD_SIZE_IN_BYTES==8
1237 writeWord64OffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
1239 writeWord64OffForeignObj fo i e = _casm_ `` (((StgWord64*)%0)[(StgInt)%1])=(StgWord64)%2; '' fo i e