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
20 -- (last two are GHC specific.)
22 , word8ToWord16 -- :: Word8 -> Word16
23 , word8ToWord32 -- :: Word8 -> Word32
24 , word8ToWord64 -- :: Word8 -> Word64
26 , word16ToWord8 -- :: Word16 -> Word32
27 , word16ToWord32 -- :: Word16 -> Word32
28 , word16ToWord64 -- :: Word8 -> Word64
30 , word32ToWord8 -- :: Word32 -> Word8
31 , word32ToWord16 -- :: Word32 -> Word16
32 , word32ToWord64 -- :: Word32 -> Word64
34 , word64ToWord8 -- :: Word64 -> Word8
35 , word64ToWord16 -- :: Word64 -> Word16
36 , word64ToWord32 -- :: Word64 -> Word32
38 , word8ToInt -- :: Word8 -> Int
39 , word16ToInt -- :: Word16 -> Int
40 , word32ToInt -- :: Word32 -> Int
41 , word64ToInt -- :: Word64 -> Int
43 , intToWord8 -- :: Int -> Word8
44 , intToWord16 -- :: Int -> Word16
45 , intToWord32 -- :: Int -> Word32
46 , intToWord64 -- :: Int -> Word64
48 , word8ToInteger -- :: Word8 -> Integer
49 , word16ToInteger -- :: Word16 -> Integer
50 , word32ToInteger -- :: Word32 -> Integer
51 , word64ToInteger -- :: Word64 -> Integer
53 , integerToWord8 -- :: Integer -> Word8
54 , integerToWord16 -- :: Integer -> Word16
55 , integerToWord32 -- :: Integer -> Word32
56 , integerToWord64 -- :: Integer -> Word64
59 , wordToWord8 -- :: Word -> Word8
60 , wordToWord16 -- :: Word -> Word16
61 , wordToWord32 -- :: Word -> Word32
62 , wordToWord64 -- :: Word -> Word64
64 , word8ToWord -- :: Word8 -> Word
65 , word16ToWord -- :: Word16 -> Word
66 , word32ToWord -- :: Word32 -> Word
67 , word64ToWord -- :: Word64 -> Word
69 -- The "official" place to get these from is Addr.
90 -- The "official" place to get these from is Foreign
91 #ifndef __PARALLEL_HASKELL__
92 , indexWord8OffForeignObj
93 , indexWord16OffForeignObj
94 , indexWord32OffForeignObj
95 , indexWord64OffForeignObj
97 , readWord8OffForeignObj
98 , readWord16OffForeignObj
99 , readWord32OffForeignObj
100 , readWord64OffForeignObj
102 , writeWord8OffForeignObj
103 , writeWord16OffForeignObj
104 , writeWord32OffForeignObj
105 , writeWord64OffForeignObj
108 -- non-standard, GHC specific
111 -- Internal, do not use.
119 import PreludeBuiltin
130 import Numeric (readDec, showInt)
132 -----------------------------------------------------------------------------
133 -- The "official" coercion functions
134 -----------------------------------------------------------------------------
136 word8ToWord32 :: Word8 -> Word32
137 word16ToWord32 :: Word16 -> Word32
138 word32ToWord8 :: Word32 -> Word8
139 word32ToWord16 :: Word32 -> Word16
141 word8ToInt :: Word8 -> Int
142 word16ToInt :: Word16 -> Int
143 intToWord8 :: Int -> Word8
144 intToWord16 :: Int -> Word16
146 integerToWord8 :: Integer -> Word8
147 integerToWord16 :: Integer -> Word16
149 word8ToInt = word32ToInt . word8ToWord32
150 intToWord8 = word32ToWord8 . intToWord32
151 word16ToInt = word32ToInt . word16ToWord32
152 intToWord16 = word32ToWord16 . intToWord32
153 word8ToInteger = word32ToInteger . word8ToWord32
154 word16ToInteger = word32ToInteger . word16ToWord32
155 integerToWord8 = fromInteger
156 integerToWord16 = fromInteger
158 intToWord32 :: Int -> Word32
159 intToWord32 (I# x) = W32# ((int2Word# x) `and#` (case (maxBound::Word32) of W32# x# -> x#))
160 --intToWord32 (I# x) = W32# (int2Word# x)
162 word32ToInt :: Word32 -> Int
163 word32ToInt (W32# x) = I# (word2Int# x)
165 word32ToInteger :: Word32 -> Integer
166 word32ToInteger (W32# x) = word2Integer x
168 integerToWord32 :: Integer -> Word32
169 integerToWord32 = fromInteger
171 wordToInt :: Word -> Int
172 wordToInt (W# w#) = I# (word2Int# w#)
176 \subsection[Word8]{The @Word8@ interface}
178 The byte type @Word8@ is represented in the Haskell
179 heap by boxing up a 32-bit quantity, @Word#@. An invariant
180 for this representation is that the higher 24 bits are
181 *always* zeroed out. A consequence of this is that
182 operations that could possibly overflow have to mask
183 out the top three bytes before building the resulting @Word8@.
186 data Word8 = W8# Word#
188 instance CCallable Word8
189 instance CReturnable Word8
191 word8ToWord32 (W8# x) = W32# x
192 word8ToWord16 (W8# x) = W16# x
193 word32ToWord8 (W32# x) = W8# (wordToWord8# x)
195 -- mask out upper three bytes.
196 intToWord8# :: Int# -> Word#
197 intToWord8# i# = (int2Word# i#) `and#` (int2Word# 0xff#)
199 wordToWord8# :: Word# -> Word#
200 wordToWord8# w# = w# `and#` (int2Word# 0xff#)
202 instance Eq Word8 where
203 (W8# x) == (W8# y) = x `eqWord#` y
204 (W8# x) /= (W8# y) = x `neWord#` y
206 instance Ord Word8 where
207 compare (W8# x#) (W8# y#) = compareWord# x# y#
208 (<) (W8# x) (W8# y) = x `ltWord#` y
209 (<=) (W8# x) (W8# y) = x `leWord#` y
210 (>=) (W8# x) (W8# y) = x `geWord#` y
211 (>) (W8# x) (W8# y) = x `gtWord#` y
212 max x@(W8# x#) y@(W8# y#) =
213 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
214 min x@(W8# x#) y@(W8# y#) =
215 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
217 -- Helper function, used by Ord Word* instances.
218 compareWord# :: Word# -> Word# -> Ordering
220 | x# `ltWord#` y# = LT
221 | x# `eqWord#` y# = EQ
224 instance Num Word8 where
226 W8# (intToWord8# (word2Int# x +# word2Int# y))
228 W8# (intToWord8# (word2Int# x -# word2Int# y))
230 W8# (intToWord8# (word2Int# x *# word2Int# y))
234 else W8# (int2Word# (0x100# -# x'))
239 fromInteger (J# a# s# d#) = W8# (wordToWord8# (integer2Word# a# s# d#))
242 instance Bounded Word8 where
246 instance Real Word8 where
247 toRational x = toInteger x % 1
249 -- Note: no need to mask results here
250 -- as they cannot overflow.
251 instance Integral Word8 where
252 div x@(W8# x#) (W8# y#)
253 | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
254 | otherwise = divZeroError "div{Word8}" x
256 quot x@(W8# x#) (W8# y#)
257 | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
258 | otherwise = divZeroError "quot{Word8}" x
260 rem x@(W8# x#) (W8# y#)
261 | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
262 | otherwise = divZeroError "rem{Word8}" x
264 mod x@(W8# x#) (W8# y#)
265 | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
266 | otherwise = divZeroError "mod{Word8}" x
268 quotRem (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
269 divMod (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
271 toInteger (W8# x) = word2Integer x
272 toInt x = word8ToInt x
274 instance Ix Word8 where
277 | inRange b i = word8ToInt (i-m)
278 | otherwise = indexError i b "Word8"
279 inRange (m,n) i = m <= i && i <= n
281 instance Enum Word8 where
283 | w == maxBound = succError "Word8"
286 | w == minBound = predError "Word8"
290 | i >= toInt (minBound::Word8) && i <= toInt (maxBound::Word8)
291 = W8# (intToWord8# i#)
293 = toEnumError "Word8" i (minBound::Word8,maxBound::Word8)
295 fromEnum (W8# w) = I# (word2Int# w)
296 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Word8)]
297 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum last]
302 | otherwise = maxBound
304 instance Read Word8 where
305 readsPrec _ = readDec
307 instance Show Word8 where
308 showsPrec _ = showInt
311 -- Word8s are represented by an (unboxed) 32-bit Word.
312 -- The invariant is that the upper 24 bits are always zeroed out.
314 instance Bits Word8 where
315 (W8# x) .&. (W8# y) = W8# (x `and#` y)
316 (W8# x) .|. (W8# y) = W8# (x `or#` y)
317 (W8# x) `xor` (W8# y) = W8# (x `xor#` y)
318 complement (W8# x) = W8# (x `xor#` int2Word# 0xff#)
319 shift (W8# x#) i@(I# i#)
320 | i > 0 = W8# (wordToWord8# (shiftL# x# i#))
321 | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
322 w@(W8# x) `rotate` (I# i)
324 | i ># 0# = W8# ((wordToWord8# (shiftL# x i')) `or#`
326 (int2Word# (0x100# -# pow2# i2)))
328 | otherwise = rotate w (I# (8# +# i))
330 i' = word2Int# (int2Word# i `and#` int2Word# 7#)
334 | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
335 | otherwise = 0 -- We'll be overbearing, for now..
337 setBit x i = x .|. bit i
338 clearBit x i = x .&. complement (bit i)
339 complementBit x i = x `xor` bit i
341 testBit (W8# x#) (I# i#)
342 | i# <# 8# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
343 | otherwise = False -- for now, this is really an error.
348 pow2# :: Int# -> Int#
349 pow2# x# = word2Int# (shiftL# (int2Word# 1#) x#)
351 word2Integer :: Word# -> Integer
352 word2Integer w = case word2Integer# w of
353 (# a, s, d #) -> J# a s d
355 pow2_64# :: Int# -> Int64#
356 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
358 sizeofWord8 :: Word32
363 \subsection[Word16]{The @Word16@ interface}
365 The double byte type @Word16@ is represented in the Haskell
366 heap by boxing up a machine word, @Word#@. An invariant
367 for this representation is that only the lower 16 bits are
368 `active', any bits above are {\em always} zeroed out.
369 A consequence of this is that operations that could possibly
370 overflow have to mask out anything above the lower two bytes
371 before putting together the resulting @Word16@.
374 data Word16 = W16# Word#
375 instance CCallable Word16
376 instance CReturnable Word16
378 word16ToWord32 (W16# x) = W32# x
379 word16ToWord8 (W16# x) = W8# (wordToWord8# x)
380 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
382 -- mask out upper 16 bits.
383 intToWord16# :: Int# -> Word#
384 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
386 wordToWord16# :: Word# -> Word#
387 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
389 instance Eq Word16 where
390 (W16# x) == (W16# y) = x `eqWord#` y
391 (W16# x) /= (W16# y) = x `neWord#` y
393 instance Ord Word16 where
394 compare (W16# x#) (W16# y#) = compareWord# x# y#
395 (<) (W16# x) (W16# y) = x `ltWord#` y
396 (<=) (W16# x) (W16# y) = x `leWord#` y
397 (>=) (W16# x) (W16# y) = x `geWord#` y
398 (>) (W16# x) (W16# y) = x `gtWord#` y
399 max x@(W16# x#) y@(W16# y#) =
400 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
401 min x@(W16# x#) y@(W16# y#) =
402 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
404 instance Num Word16 where
405 (W16# x) + (W16# y) =
406 W16# (intToWord16# (word2Int# x +# word2Int# y))
407 (W16# x) - (W16# y) =
408 W16# (intToWord16# (word2Int# x -# word2Int# y))
409 (W16# x) * (W16# y) =
410 W16# (intToWord16# (word2Int# x *# word2Int# y))
414 else W16# (int2Word# (0x10000# -# x'))
419 fromInteger (J# a# s# d#) = W16# (wordToWord16# (integer2Word# a# s# d#))
420 fromInt = intToWord16
422 instance Bounded Word16 where
426 instance Real Word16 where
427 toRational x = toInteger x % 1
429 instance Integral Word16 where
430 div x@(W16# x#) (W16# y#)
431 | y# `neWord#` (int2Word# 0#) = W16# (x# `quotWord#` y#)
432 | otherwise = divZeroError "div{Word16}" x
434 quot x@(W16# x#) (W16# y#)
435 | y# `neWord#`(int2Word# 0#) = W16# (x# `quotWord#` y#)
436 | otherwise = divZeroError "quot{Word16}" x
438 rem x@(W16# x#) (W16# y#)
439 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
440 | otherwise = divZeroError "rem{Word16}" x
442 mod x@(W16# x#) (W16# y#)
443 | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
444 | otherwise = divZeroError "mod{Word16}" x
446 quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
447 divMod (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
449 toInteger (W16# x) = word2Integer x
450 toInt x = word16ToInt x
452 instance Ix Word16 where
455 | inRange b i = word16ToInt (i - m)
456 | otherwise = indexError i b "Word16"
457 inRange (m,n) i = m <= i && i <= n
459 instance Enum Word16 where
461 | w == maxBound = succError "Word16"
464 | w == minBound = predError "Word16"
468 | i >= toInt (minBound::Word16) && i <= toInt (maxBound::Word16)
469 = W16# (intToWord16# i#)
471 = toEnumError "Word16" i (minBound::Word16,maxBound::Word16)
473 fromEnum (W16# w) = I# (word2Int# w)
474 enumFrom c = map toEnum [fromEnum c .. fromEnum (maxBound::Word16)]
475 enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum last]
480 | otherwise = maxBound
482 instance Read Word16 where
483 readsPrec _ = readDec
485 instance Show Word16 where
486 showsPrec _ = showInt
488 instance Bits Word16 where
489 (W16# x) .&. (W16# y) = W16# (x `and#` y)
490 (W16# x) .|. (W16# y) = W16# (x `or#` y)
491 (W16# x) `xor` (W16# y) = W16# (x `xor#` y)
492 complement (W16# x) = W16# (x `xor#` int2Word# 0xffff#)
493 shift (W16# x#) i@(I# i#)
494 | i > 0 = W16# (wordToWord16# (shiftL# x# i#))
495 | otherwise = W16# (shiftRL# x# (negateInt# i#))
496 w@(W16# x) `rotate` (I# i)
498 | i ># 0# = W16# ((wordToWord16# (shiftL# x i')) `or#`
500 (int2Word# (0x10000# -# pow2# i2)))
502 | otherwise = rotate w (I# (16# +# i'))
504 i' = word2Int# (int2Word# i `and#` int2Word# 15#)
507 | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
508 | otherwise = 0 -- We'll be overbearing, for now..
510 setBit x i = x .|. bit i
511 clearBit x i = x .&. complement (bit i)
512 complementBit x i = x `xor` bit i
514 testBit (W16# x#) (I# i#)
515 | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
516 | otherwise = False -- for now, this is really an error.
522 sizeofWord16 :: Word32
527 \subsection[Word32]{The @Word32@ interface}
529 The quad byte type @Word32@ is represented in the Haskell
530 heap by boxing up a machine word, @Word#@. An invariant
531 for this representation is that any bits above the lower
532 32 are {\em always} zeroed out. A consequence of this is that
533 operations that could possibly overflow have to mask
534 the result before building the resulting @Word16@.
537 data Word32 = W32# Word#
539 instance CCallable Word32
540 instance CReturnable Word32
542 instance Eq Word32 where
543 (W32# x) == (W32# y) = x `eqWord#` y
544 (W32# x) /= (W32# y) = x `neWord#` y
546 instance Ord Word32 where
547 compare (W32# x#) (W32# y#) = compareWord# x# y#
548 (<) (W32# x) (W32# y) = x `ltWord#` y
549 (<=) (W32# x) (W32# y) = x `leWord#` y
550 (>=) (W32# x) (W32# y) = x `geWord#` y
551 (>) (W32# x) (W32# y) = x `gtWord#` y
552 max x@(W32# x#) y@(W32# y#) =
553 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
554 min x@(W32# x#) y@(W32# y#) =
555 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
557 instance Num Word32 where
558 (W32# x) + (W32# y) =
559 W32# (intToWord32# (word2Int# x +# word2Int# y))
560 (W32# x) - (W32# y) =
561 W32# (intToWord32# (word2Int# x -# word2Int# y))
562 (W32# x) * (W32# y) =
563 W32# (intToWord32# (word2Int# x *# word2Int# y))
564 #if WORD_SIZE_IN_BYTES == 8
568 else W32# (intToWord32# (0x100000000# -# x'))
572 negate (W32# x) = W32# (intToWord32# (negateInt# (word2Int# x)))
576 fromInteger (J# a# s# d#) = W32# (integer2Word# a# s# d#)
577 fromInt (I# x) = W32# (intToWord32# x)
578 -- ToDo: restrict fromInt{eger} range.
580 intToWord32# :: Int# -> Word#
581 wordToWord32# :: Word# -> Word#
583 #if WORD_SIZE_IN_BYTES == 8
584 intToWord32# i# = (int2Word# i#) `and#` (int2Word# 0xffffffff#)
585 wordToWord32# w# = w# `and#` (int2Word# 0xffffffff#)
586 wordToWord64# w# = w#
588 intToWord32# i# = int2Word# i#
589 wordToWord32# w# = w#
593 instance Bounded Word32 where
595 #if WORD_SIZE_IN_BYTES == 8
596 maxBound = 0xffffffff
598 maxBound = minBound - 1
601 instance Real Word32 where
602 toRational x = toInteger x % 1
604 instance Integral Word32 where
606 | y /= 0 = quotWord32 x y
607 | otherwise = divZeroError "div{Word32}" x
610 | y /= 0 = quotWord32 x y
611 | otherwise = divZeroError "quot{Word32}" x
614 | y /= 0 = remWord32 x y
615 | otherwise = divZeroError "rem{Word32}" x
618 | y /= 0 = remWord32 x y
619 | otherwise = divZeroError "mod{Word32}" x
621 quotRem a b = (a `quotWord32` b, a `remWord32` b)
622 divMod x y = quotRem x y
624 toInteger (W32# x) = word2Integer x
625 toInt (W32# x) = I# (word2Int# x)
627 {-# INLINE quotWord32 #-}
628 {-# INLINE remWord32 #-}
629 remWord32, quotWord32 :: Word32 -> Word32 -> Word32
630 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
631 (W32# x) `remWord32` (W32# y) = W32# (x `remWord#` y)
633 instance Ix Word32 where
636 | inRange b i = word32ToInt (i - m)
637 | otherwise = indexError i b "Word32"
638 inRange (m,n) i = m <= i && i <= n
640 instance Enum Word32 where
642 | w == maxBound = succError "Word32"
645 | w == minBound = predError "Word32"
648 -- the toEnum/fromEnum will fail if the mapping isn't legal,
649 -- use the intTo* & *ToInt coercion functions to 'bypass' these range checks.
651 | x >= 0 = intToWord32 x
653 = toEnumError "Word32" x (minBound::Word32,maxBound::Word32)
656 | x <= intToWord32 (maxBound::Int)
659 = fromEnumError "Word32" x
661 enumFrom w = [w .. maxBound]
663 | w1 <= w2 = eftt32 True{-increasing-} w1 diff_f last
669 enumFromThen w1 w2 = [w1,w2 .. last]
674 | otherwise = minBound
676 enumFromThenTo w1 w2 wend = eftt32 increasing w1 step_f last
678 increasing = w1 <= w2
683 | increasing = (> wend)
684 | otherwise = (< wend)
687 | increasing = \ x -> x + diff1
688 | otherwise = \ x -> x - diff2
691 eftt32 :: Bool -> Word32 -> (Word32 -> Word32) -> (Word32-> Bool) -> [Word32]
692 eftt32 increasing init stepper done = go init
696 | increasing && now > nxt = [now] -- oflow
697 | not increasing && now < nxt = [now] -- uflow
698 | otherwise = now : go nxt
703 instance Read Word32 where
704 readsPrec _ = readDec
706 instance Show Word32 where
707 showsPrec _ = showInt
709 instance Bits Word32 where
710 (W32# x) .&. (W32# y) = W32# (x `and#` y)
711 (W32# x) .|. (W32# y) = W32# (x `or#` y)
712 (W32# x) `xor` (W32# y) = W32# (x `xor#` y)
713 complement (W32# x) = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
714 shift (W32# x) i@(I# i#)
715 | i > 0 = W32# (wordToWord32# (shiftL# x i#))
716 | otherwise = W32# (shiftRL# x (negateInt# i#))
717 w@(W32# x) `rotate` (I# i)
719 | i ># 0# = W32# ((wordToWord32# (shiftL# x i')) `or#`
721 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
723 | otherwise = rotate w (I# (32# +# i))
725 i' = word2Int# (int2Word# i `and#` int2Word# 31#)
727 (W32# maxBound#) = maxBound
730 | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
731 | otherwise = 0 -- We'll be overbearing, for now..
733 setBit x i = x .|. bit i
734 clearBit x i = x .&. complement (bit i)
735 complementBit x i = x `xor` bit i
737 testBit (W32# x#) (I# i#)
738 | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
739 | otherwise = False -- for now, this is really an error.
743 sizeofWord32 :: Word32
747 \subsection[Word64]{The @Word64@ interface}
750 #if WORD_SIZE_IN_BYTES == 8
751 --data Word64 = W64# Word#
753 word32ToWord64 :: Word32 -> Word64
754 word32ToWord64 (W32 w#) = W64# w#
756 word8ToWord64 :: Word8 -> Word64
757 word8ToWord64 (W8# w#) = W64# w#
759 word64ToWord8 :: Word64 -> Word8
760 word64ToWord8 (W64# w#) = W8# (w# `and#` (int2Word# 0xff#))
762 word16ToWord64 :: Word16 -> Word64
763 word16ToWord64 (W16# w#) = W64# w#
765 word64ToWord16 :: Word64 -> Word16
766 word64ToWord16 (W64# w#) = W16# (w# `and#` (int2Word# 0xffff#))
768 wordToWord32# :: Word# -> Word#
769 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
771 word64ToWord32 :: Word64 -> Word32
772 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
774 wordToWord64# w# = w#
775 word64ToWord# w# = w#
777 instance Eq Word64 where
778 (W64# x) == (W64# y) = x `eqWord#` y
779 (W64# x) /= (W64# y) = x `neWord#` y
781 instance Ord Word64 where
782 compare (W64# x#) (W64# y#) = compareWord# x# y#
783 (<) (W64# x) (W64# y) = x `ltWord#` y
784 (<=) (W64# x) (W64# y) = x `leWord#` y
785 (>=) (W64# x) (W64# y) = x `geWord#` y
786 (>) (W64# x) (W64# y) = x `gtWord#` y
787 max x@(W64# x#) y@(W64# y#) =
788 case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
789 min x@(W64# x#) y@(W64# y#) =
790 case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
792 instance Num Word64 where
793 (W64# x) + (W64# y) =
794 W64# (intToWord64# (word2Int# x +# word2Int# y))
795 (W64# x) - (W64# y) =
796 W64# (intToWord64# (word2Int# x -# word2Int# y))
797 (W64# x) * (W64# y) =
798 W64# (intToWord64# (word2Int# x *# word2Int# y))
802 else W64# (int2Word# (0x100# -# x'))
807 fromInteger (J# a# s# d#) = W64# (integer2Word# a# s# d#)
808 fromInt = intToWord64
810 -- Note: no need to mask results here
811 -- as they cannot overflow.
812 instance Integral Word64 where
813 div x@(W64# x#) (W64# y#)
814 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
815 | otherwise = divZeroError "div{Word64}" x
817 quot x@(W64# x#) (W64# y#)
818 | y# `neWord#` (int2Word# 0#) = W64# (x# `quotWord#` y#)
819 | otherwise = divZeroError "quot{Word64}" x
821 rem x@(W64# x#) (W64# y#)
822 | y# `neWord#` (int2Word# 0#) = W64# (x# `remWord#` y#)
823 | otherwise = divZeroError "rem{Word64}" x
825 mod (W64# x) (W64# y)
826 | y# `neWord#` (int2Word# 0#) = W64# (x `remWord#` y)
827 | otherwise = divZeroError "mod{Word64}" x
829 quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
830 divMod (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
832 toInteger (W64# x) = word2Integer# x
833 toInt x = word64ToInt x
836 instance Bits Word64 where
837 (W64# x) .&. (W64# y) = W64# (x `and#` y)
838 (W64# x) .|. (W64# y) = W64# (x `or#` y)
839 (W64# x) `xor` (W64# y) = W64# (x `xor#` y)
840 complement (W64# x) = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
841 shift (W64# x#) i@(I# i#)
842 | i > 0 = W64# (shiftL# x# i#)
843 | otherwise = W64# (shiftRL# x# (negateInt# i#))
845 w@(W64# x) `rotate` (I# i)
847 | i ># 0# = W64# (shiftL# x i') `or#`
849 (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
851 | otherwise = rotate w (I# (64# +# i))
853 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
855 (W64# maxBound#) = maxBound
858 | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
859 | otherwise = 0 -- We'll be overbearing, for now..
861 setBit x i = x .|. bit i
862 clearBit x i = x .&. complement (bit i)
863 complementBit x i = x `xor` bit i
865 testBit (W64# x#) (I# i#)
866 | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
867 | otherwise = False -- for now, this is really an error.
873 --defined in PrelCCall: data Word64 = W64 Word64# deriving (Eq, Ord, Bounded)
875 -- for completeness sake
876 word32ToWord64 :: Word32 -> Word64
877 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
879 word64ToWord32 :: Word64 -> Word32
880 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
882 word8ToWord64 :: Word8 -> Word64
883 word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
885 word64ToWord8 :: Word64 -> Word8
886 word64ToWord8 (W64# w#) = W8# ((word64ToWord# w#) `and#` (int2Word# 0xff#))
888 word16ToWord64 :: Word16 -> Word64
889 word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
891 word64ToWord16 :: Word64 -> Word16
892 word64ToWord16 (W64# w#) = W16# ((word64ToWord# w#) `and#` (int2Word# 0xffff#))
895 word64ToInteger :: Word64 -> Integer
896 word64ToInteger (W64# w#) =
897 case word64ToInteger# w# of
898 (# a#, s#, p# #) -> J# a# s# p#
900 word64ToInt :: Word64 -> Int
902 case w `quotRem` 0x100000000 of
903 (_,l) -> toInt (word64ToWord32 l)
905 intToWord64# :: Int# -> Word64#
906 intToWord64# i# = wordToWord64# (int2Word# i#)
908 intToWord64 :: Int -> Word64
909 intToWord64 (I# i#) = W64# (intToWord64# i#)
911 integerToWord64 :: Integer -> Word64
912 integerToWord64 (J# a# s# d#) = W64# (integerToWord64# a# s# d#)
914 instance Eq Word64 where
915 (W64# x) == (W64# y) = x `eqWord64#` y
916 (W64# x) /= (W64# y) = not (x `eqWord64#` y)
918 instance Ord Word64 where
919 compare (W64# x#) (W64# y#) = compareWord64# x# y#
920 (<) (W64# x) (W64# y) = x `ltWord64#` y
921 (<=) (W64# x) (W64# y) = x `leWord64#` y
922 (>=) (W64# x) (W64# y) = x `geWord64#` y
923 (>) (W64# x) (W64# y) = x `gtWord64#` y
924 max x@(W64# x#) y@(W64# y#) =
925 case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
926 min x@(W64# x#) y@(W64# y#) =
927 case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
929 instance Num Word64 where
930 (W64# x) + (W64# y) =
931 W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
932 (W64# x) - (W64# y) =
933 W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
934 (W64# x) * (W64# y) =
935 W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
938 | otherwise = maxBound - w
942 fromInteger i = integerToWord64 i
943 fromInt = intToWord64
945 -- Note: no need to mask results here
946 -- as they cannot overflow.
947 -- ToDo: protect against div by zero.
948 instance Integral Word64 where
949 div (W64# x) (W64# y) = W64# (x `quotWord64#` y)
950 quot (W64# x) (W64# y) = W64# (x `quotWord64#` y)
951 rem (W64# x) (W64# y) = W64# (x `remWord64#` y)
952 mod (W64# x) (W64# y) = W64# (x `remWord64#` y)
953 quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
954 divMod (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
955 toInteger w64 = word64ToInteger w64
956 toInt x = word64ToInt x
959 instance Bits Word64 where
960 (W64# x) .&. (W64# y) = W64# (x `and64#` y)
961 (W64# x) .|. (W64# y) = W64# (x `or64#` y)
962 (W64# x) `xor` (W64# y) = W64# (x `xor64#` y)
963 complement (W64# x) = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
964 shift (W64# x#) i@(I# i#)
965 | i > 0 = W64# (shiftL64# x# i#)
966 | otherwise = W64# (shiftRL64# x# (negateInt# i#))
968 w@(W64# x) `rotate` (I# i)
970 | i ># 0# = W64# ((shiftL64# x i') `or64#`
971 (shiftRL64# (x `and64#`
972 (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#`
973 (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
975 | otherwise = rotate w (I# (64# +# i))
977 i' = word2Int# (int2Word# i `and#` int2Word# 63#)
979 (W64# maxBound#) = maxBound
982 | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
983 | otherwise = 0 -- We'll be overbearing, for now..
985 setBit x i = x .|. bit i
986 clearBit x i = x .&. complement (bit i)
987 complementBit x i = x `xor` bit i
989 testBit (W64# x#) (I# i#)
990 | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
991 | otherwise = False -- for now, this is really an error.
996 compareWord64# :: Word64# -> Word64# -> Ordering
998 | i# `ltWord64#` j# = LT
999 | i# `eqWord64#` j# = EQ
1002 -- Word64# primop wrappers:
1004 ltWord64# :: Word64# -> Word64# -> Bool
1005 ltWord64# x# y# = unsafePerformIO $ do
1006 v <- _ccall_ stg_ltWord64 x# y#
1011 leWord64# :: Word64# -> Word64# -> Bool
1012 leWord64# x# y# = unsafePerformIO $ do
1013 v <- _ccall_ stg_leWord64 x# y#
1018 eqWord64# :: Word64# -> Word64# -> Bool
1019 eqWord64# x# y# = unsafePerformIO $ do
1020 v <- _ccall_ stg_eqWord64 x# y#
1025 neWord64# :: Word64# -> Word64# -> Bool
1026 neWord64# x# y# = unsafePerformIO $ do
1027 v <- _ccall_ stg_neWord64 x# y#
1032 geWord64# :: Word64# -> Word64# -> Bool
1033 geWord64# x# y# = unsafePerformIO $ do
1034 v <- _ccall_ stg_geWord64 x# y#
1039 gtWord64# :: Word64# -> Word64# -> Bool
1040 gtWord64# x# y# = unsafePerformIO $ do
1041 v <- _ccall_ stg_gtWord64 x# y#
1046 plusInt64# :: Int64# -> Int64# -> Int64#
1048 case (unsafePerformIO (_ccall_ stg_plusInt64 a# b#)) of
1051 minusInt64# :: Int64# -> Int64# -> Int64#
1053 case (unsafePerformIO (_ccall_ stg_minusInt64 a# b#)) of
1056 timesInt64# :: Int64# -> Int64# -> Int64#
1058 case (unsafePerformIO (_ccall_ stg_timesInt64 a# b#)) of
1061 quotWord64# :: Word64# -> Word64# -> Word64#
1063 case (unsafePerformIO (_ccall_ stg_quotWord64 a# b#)) of
1066 remWord64# :: Word64# -> Word64# -> Word64#
1068 case (unsafePerformIO (_ccall_ stg_remWord64 a# b#)) of
1071 negateInt64# :: Int64# -> Int64#
1073 case (unsafePerformIO (_ccall_ stg_negateInt64 a#)) of
1076 and64# :: Word64# -> Word64# -> Word64#
1078 case (unsafePerformIO (_ccall_ stg_and64 a# b#)) of
1081 or64# :: Word64# -> Word64# -> Word64#
1083 case (unsafePerformIO (_ccall_ stg_or64 a# b#)) of
1086 xor64# :: Word64# -> Word64# -> Word64#
1088 case (unsafePerformIO (_ccall_ stg_xor64 a# b#)) of
1091 not64# :: Word64# -> Word64#
1093 case (unsafePerformIO (_ccall_ stg_not64 a#)) of
1096 shiftL64# :: Word64# -> Int# -> Word64#
1098 case (unsafePerformIO (_ccall_ stg_shiftL64 a# b#)) of
1101 shiftRL64# :: Word64# -> Int# -> Word64#
1103 case (unsafePerformIO (_ccall_ stg_shiftRL64 a# b#)) of
1106 word64ToWord# :: Word64# -> Word#
1107 word64ToWord# w64# =
1108 case (unsafePerformIO (_ccall_ stg_word64ToWord w64#)) of
1111 wordToWord64# :: Word# -> Word64#
1113 case (unsafePerformIO (_ccall_ stg_wordToWord64 w#)) of
1116 word64ToInt64# :: Word64# -> Int64#
1117 word64ToInt64# w64# =
1118 case (unsafePerformIO (_ccall_ stg_word64ToInt64 w64#)) of
1121 int64ToWord64# :: Int64# -> Word64#
1122 int64ToWord64# i64# =
1123 case (unsafePerformIO (_ccall_ stg_int64ToWord64 i64#)) of
1126 intToInt64# :: Int# -> Int64#
1128 case (unsafePerformIO (_ccall_ stg_intToInt64 i#)) of
1133 instance Enum Word64 where
1135 | w == maxBound = succError "Word64"
1138 | w == minBound = predError "Word64"
1142 | i >= 0 = intToWord64 i
1144 = toEnumError "Word64" i (minBound::Word64,maxBound::Word64)
1147 | w <= intToWord64 (maxBound::Int)
1150 = fromEnumError "Word64" w
1152 enumFrom e1 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger maxBound]
1153 enumFromTo e1 e2 = map integerToWord64 [word64ToInteger e1 .. word64ToInteger e2]
1154 enumFromThen e1 e2 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger last]
1158 | e2 < e1 = minBound
1159 | otherwise = maxBound
1161 enumFromThenTo e1 e2 e3 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger e3]
1163 instance Show Word64 where
1164 showsPrec p x = showsPrec p (word64ToInteger x)
1166 instance Read Word64 where
1167 readsPrec _ s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
1169 instance Ix Word64 where
1170 range (m,n) = [m..n]
1172 | inRange b i = word64ToInt (i-m)
1173 | otherwise = indexError i b "Word64"
1174 inRange (m,n) i = m <= i && i <= n
1176 instance Bounded Word64 where
1178 maxBound = minBound - 1
1180 instance Real Word64 where
1181 toRational x = toInteger x % 1
1183 sizeofWord64 :: Word32
1190 The Hugs-GHC extension libraries provide functions for going between
1191 Int and the various (un)signed ints. Here we provide the same for
1192 the GHC specific Word type:
1195 wordToWord8 :: Word -> Word8
1196 wordToWord16 :: Word -> Word16
1197 wordToWord32 :: Word -> Word32
1199 word8ToWord :: Word8 -> Word
1200 word16ToWord :: Word16 -> Word
1201 word32ToWord :: Word32 -> Word
1203 word8ToWord# :: Word8 -> Word#
1204 word16ToWord# :: Word16 -> Word#
1205 word32ToWord# :: Word32 -> Word#
1207 word8ToWord (W8# w#) = W# w#
1208 word8ToWord# (W8# w#) = w#
1210 wordToWord8 (W# w#) = W8# (w# `and#` (case (maxBound::Word8) of W8# x# -> x#))
1211 word16ToWord (W16# w#) = W# w#
1212 word16ToWord# (W16# w#) = w#
1214 wordToWord16 (W# w#) = W16# (w# `and#` (case (maxBound::Word16) of W16# x# -> x#))
1215 wordToWord32 (W# w#) = W32# (w# `and#` (case (maxBound::Word32) of W32# x# -> x#))
1217 word32ToWord (W32# w#) = W# w#
1218 word32ToWord# (W32# w#) = w#
1220 wordToWord64 :: Word -> Word64
1221 wordToWord64 (W# w#) = W64# (wordToWord64# w#)
1223 -- lossy on 32-bit platforms, but provided nontheless.
1224 word64ToWord :: Word64 -> Word
1225 word64ToWord (W64# w#) = W# (word64ToWord# w#)
1230 --End of exported definitions
1232 The remainder of this file consists of definitions which are only
1233 used in the implementation.
1236 signumReal :: (Ord a, Num a) => a -> a
1237 signumReal x | x == 0 = 0
1243 NOTE: the index is in units of the size of the type, *not* bytes.
1246 indexWord8OffAddr :: Addr -> Int -> Word8
1247 indexWord8OffAddr (A# a#) (I# i#) = intToWord8 (I# (ord# (indexCharOffAddr# a# i#)))
1249 indexWord16OffAddr :: Addr -> Int -> Word16
1250 indexWord16OffAddr a i =
1251 #ifdef WORDS_BIGENDIAN
1252 intToWord16 ( word8ToInt l + (word8ToInt maxBound) * word8ToInt h)
1254 intToWord16 ( word8ToInt h + (word8ToInt maxBound) * word8ToInt l)
1258 l = indexWord8OffAddr a byte_idx
1259 h = indexWord8OffAddr a (byte_idx+1)
1261 indexWord32OffAddr :: Addr -> Int -> Word32
1262 indexWord32OffAddr (A# a#) i = wordToWord32 (W# (indexWordOffAddr# a# i'#))
1264 -- adjust index to be in Word units, not Word32 ones.
1266 #if WORD_SIZE_IN_BYTES==8
1272 indexWord64OffAddr :: Addr -> Int -> Word64
1273 indexWord64OffAddr (A# a#) (I# i#)
1274 #if WORD_SIZE_IN_BYTES==8
1275 = W64# (indexWordOffAddr# a# i#)
1277 = W64# (indexWord64OffAddr# a# i#)
1280 #ifndef __PARALLEL_HASKELL__
1282 indexWord8OffForeignObj :: ForeignObj -> Int -> Word8
1283 indexWord8OffForeignObj (ForeignObj fo#) (I# i#) = intToWord8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
1285 indexWord16OffForeignObj :: ForeignObj -> Int -> Word16
1286 indexWord16OffForeignObj fo i =
1287 #ifdef WORDS_BIGENDIAN
1288 intToWord16 ( word8ToInt l + (word8ToInt maxBound) * word8ToInt h)
1290 intToWord16 ( word8ToInt h + (word8ToInt maxBound) * word8ToInt l)
1294 l = indexWord8OffForeignObj fo byte_idx
1295 h = indexWord8OffForeignObj fo (byte_idx+1)
1297 indexWord32OffForeignObj :: ForeignObj -> Int -> Word32
1298 indexWord32OffForeignObj (ForeignObj fo#) i = wordToWord32 (W# (indexWordOffForeignObj# fo# i'#))
1300 -- adjust index to be in Word units, not Word32 ones.
1302 #if WORD_SIZE_IN_BYTES==8
1308 indexWord64OffForeignObj :: ForeignObj -> Int -> Word64
1309 indexWord64OffForeignObj (ForeignObj fo#) (I# i#)
1310 #if WORD_SIZE_IN_BYTES==8
1311 = W64# (indexWordOffForeignObj# fo# i#)
1313 = W64# (indexWord64OffForeignObj# fo# i#)
1319 Read words out of mutable memory:
1322 readWord8OffAddr :: Addr -> Int -> IO Word8
1323 readWord8OffAddr a i = _casm_ `` %r=(StgNat8)(((StgNat8*)%0)[(StgInt)%1]); '' a i
1325 readWord16OffAddr :: Addr -> Int -> IO Word16
1326 readWord16OffAddr a i = _casm_ `` %r=(StgNat16)(((StgNat16*)%0)[(StgInt)%1]); '' a i
1328 readWord32OffAddr :: Addr -> Int -> IO Word32
1329 readWord32OffAddr a i = _casm_ `` %r=(StgNat32)(((StgNat32*)%0)[(StgInt)%1]); '' a i
1331 readWord64OffAddr :: Addr -> Int -> IO Word64
1332 #if WORD_SIZE_IN_BYTES==8
1333 readWord64OffAddr a i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' a i
1335 readWord64OffAddr a i = _casm_ `` %r=(StgNat64)(((StgNat64*)%0)[(StgInt)%1]); '' a i
1338 #ifndef __PARALLEL_HASKELL__
1339 readWord8OffForeignObj :: ForeignObj -> Int -> IO Word8
1340 readWord8OffForeignObj fo i = _casm_ `` %r=(StgNat8)(((StgNat8*)%0)[(StgInt)%1]); '' fo i
1342 readWord16OffForeignObj :: ForeignObj -> Int -> IO Word16
1343 readWord16OffForeignObj fo i = _casm_ `` %r=(StgNat16)(((StgNat16*)%0)[(StgInt)%1]); '' fo i
1345 readWord32OffForeignObj :: ForeignObj -> Int -> IO Word32
1346 readWord32OffForeignObj fo i = _casm_ `` %r=(StgNat32)(((StgNat32*)%0)[(StgInt)%1]); '' fo i
1348 readWord64OffForeignObj :: ForeignObj -> Int -> IO Word64
1349 #if WORD_SIZE_IN_BYTES==8
1350 readWord64OffForeignObj fo i = _casm_ `` %r=(StgWord)(((StgWord*)%0)[(StgInt)%1]); '' fo i
1352 readWord64OffForeignObj fo i = _casm_ `` %r=(StgNat64)(((StgNat64*)%0)[(StgInt)%1]); '' fo i
1359 Note: we provide primops for the writing via Addrs since that's used
1360 in the IO implementation (a place where we *really* do care about cycles.)
1363 writeWord8OffAddr :: Addr -> Int -> Word8 -> IO ()
1364 writeWord8OffAddr (A# a#) (I# i#) (W8# w#) = IO $ \ s# ->
1365 case (writeCharOffAddr# a# i# (chr# (word2Int# w#)) s#) of s2# -> (# s2#, () #)
1367 writeWord16OffAddr :: Addr -> Int -> Word16 -> IO ()
1368 writeWord16OffAddr a i e = _casm_ `` (((StgNat16*)%0)[(StgInt)%1])=(StgNat16)%2; '' a i e
1370 writeWord32OffAddr :: Addr -> Int -> Word32 -> IO ()
1371 writeWord32OffAddr (A# a#) i (W32# w#) = IO $ \ s# ->
1372 case (writeWordOffAddr# a# i'# w# s#) of s2# -> (# s2#, () #)
1374 -- adjust index to be in Word units, not Word32 ones.
1376 #if WORD_SIZE_IN_BYTES==8
1382 writeWord64OffAddr :: Addr -> Int -> Word64 -> IO ()
1383 #if WORD_SIZE_IN_BYTES==8
1384 writeWord64OffAddr (A# a#) (I# i#) (W64# w#) = IO $ \ s# ->
1385 case (writeWordOffAddr# a# i# w# s#) of s2# -> (# s2#, () #)
1387 writeWord64OffAddr (A# a#) (I# i#) (W64# w#) = IO $ \ s# ->
1388 case (writeWord64OffAddr# a# i# w# s#) of s2# -> (# s2#, () #)
1391 #ifndef __PARALLEL_HASKELL__
1393 writeWord8OffForeignObj :: ForeignObj -> Int -> Word8 -> IO ()
1394 writeWord8OffForeignObj fo i w = _casm_ `` (((StgNat16*)%0)[(StgInt)%1])=(StgNat16)%2; '' fo i w
1396 writeWord16OffForeignObj :: ForeignObj -> Int -> Word16 -> IO ()
1397 writeWord16OffForeignObj fo i w = _casm_ `` (((StgNat16*)%0)[(StgInt)%1])=(StgNat16)%2; '' fo i w
1399 writeWord32OffForeignObj :: ForeignObj -> Int -> Word32 -> IO ()
1400 writeWord32OffForeignObj fo i w = _casm_ `` (((StgNat16*)%0)[(StgInt)%1])=(StgNat16)%2; '' fo i' w
1402 -- adjust index to be in Word units, not Word32 ones.
1404 #if WORD_SIZE_IN_BYTES==8
1410 writeWord64OffForeignObj :: ForeignObj -> Int -> Word64 -> IO ()
1411 # if WORD_SIZE_IN_BYTES==8
1412 writeWord64OffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
1414 writeWord64OffForeignObj fo i e = _casm_ `` (((StgNat64*)%0)[(StgInt)%1])=(StgNat64)%2; '' fo i e
1421 Utils for generating friendly error messages.
1424 {-# NOINLINE indexError #-}
1425 indexError :: (Show a) => a -> (a,a) -> String -> b
1427 = error (showString "Ix{" . showString tp . showString "}.index: Index " .
1428 showParen True (showsPrec 0 i) .
1429 showString " out of range " $
1430 showParen True (showsPrec 0 rng) "")
1432 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1433 toEnumError inst_ty tag bnds
1434 = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1435 (showParen True (showsPrec 0 tag) $
1436 " is outside of bounds " ++
1439 fromEnumError :: (Show a,Show b) => String -> a -> b
1440 fromEnumError inst_ty tag
1441 = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1442 (showParen True (showsPrec 0 tag) $
1443 " is outside of Int's bounds " ++
1444 show (minBound::Int,maxBound::Int)))
1446 succError :: String -> a
1448 = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1450 predError :: String -> a
1452 = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1454 divZeroError :: (Show a) => String -> a -> b
1456 = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")