2 % (c) The University of Glasgow, 1997-2001
4 \section[PrelWord]{Module @PrelWord@}
7 {-# OPTIONS -fno-implicit-prelude #-}
12 Word(..), Word8(..), Word16(..), Word32(..), Word64(..),
13 divZeroError, toEnumError, fromEnumError, succError, predError)
25 ------------------------------------------------------------------------
27 ------------------------------------------------------------------------
29 {-# NOINLINE divZeroError #-}
30 divZeroError :: (Show a) => String -> a -> b
32 error $ "Integral." ++ meth ++ ": divide by 0 (" ++ show x ++ " / 0)"
34 {-# NOINLINE toEnumError #-}
35 toEnumError :: (Show a) => String -> Int -> (a,a) -> b
36 toEnumError inst_ty i bnds =
37 error $ "Enum.toEnum{" ++ inst_ty ++ "}: tag (" ++
39 ") is outside of bounds " ++
42 {-# NOINLINE fromEnumError #-}
43 fromEnumError :: (Show a) => String -> a -> b
44 fromEnumError inst_ty x =
45 error $ "Enum.fromEnum{" ++ inst_ty ++ "}: value (" ++
47 ") is outside of Int's bounds " ++
48 show (minBound::Int, maxBound::Int)
50 {-# NOINLINE succError #-}
51 succError :: String -> a
53 error $ "Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound"
55 {-# NOINLINE predError #-}
56 predError :: String -> a
58 error $ "Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound"
60 ------------------------------------------------------------------------
62 ------------------------------------------------------------------------
64 -- A Word is an unsigned integral type, with the same size as Int.
66 data Word = W# Word# deriving (Eq, Ord)
68 instance CCallable Word
69 instance CReturnable Word
71 instance Show Word where
72 showsPrec p x = showsPrec p (toInteger x)
74 instance Num Word where
75 (W# x#) + (W# y#) = W# (x# `plusWord#` y#)
76 (W# x#) - (W# y#) = W# (x# `minusWord#` y#)
77 (W# x#) * (W# y#) = W# (x# `timesWord#` y#)
78 negate (W# x#) = W# (int2Word# (negateInt# (word2Int# x#)))
82 fromInteger (S# i#) = W# (int2Word# i#)
83 fromInteger (J# s# d#) = W# (integer2Word# s# d#)
85 instance Real Word where
86 toRational x = toInteger x % 1
88 instance Enum Word where
90 | x /= maxBound = x + 1
91 | otherwise = succError "Word"
93 | x /= minBound = x - 1
94 | otherwise = predError "Word"
96 | i >= 0 = W# (int2Word# i#)
97 | otherwise = toEnumError "Word" i (minBound::Word, maxBound::Word)
99 | x <= fromIntegral (maxBound::Int)
101 | otherwise = fromEnumError "Word" x
102 enumFrom = integralEnumFrom
103 enumFromThen = integralEnumFromThen
104 enumFromTo = integralEnumFromTo
105 enumFromThenTo = integralEnumFromThenTo
107 instance Integral Word where
108 quot x@(W# x#) y@(W# y#)
109 | y /= 0 = W# (x# `quotWord#` y#)
110 | otherwise = divZeroError "quot{Word}" x
111 rem x@(W# x#) y@(W# y#)
112 | y /= 0 = W# (x# `remWord#` y#)
113 | otherwise = divZeroError "rem{Word}" x
114 div x@(W# x#) y@(W# y#)
115 | y /= 0 = W# (x# `quotWord#` y#)
116 | otherwise = divZeroError "div{Word}" x
117 mod x@(W# x#) y@(W# y#)
118 | y /= 0 = W# (x# `remWord#` y#)
119 | otherwise = divZeroError "mod{Word}" x
120 quotRem x@(W# x#) y@(W# y#)
121 | y /= 0 = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
122 | otherwise = divZeroError "quotRem{Word}" x
123 divMod x@(W# x#) y@(W# y#)
124 | y /= 0 = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
125 | otherwise = divZeroError "divMod{Word}" x
128 | otherwise = case word2Integer# x# of (# s, d #) -> J# s d
132 instance Bounded Word where
134 #if WORD_SIZE_IN_BITS == 31
135 maxBound = 0x7FFFFFFF
136 #elif WORD_SIZE_IN_BITS == 32
137 maxBound = 0xFFFFFFFF
139 maxBound = 0xFFFFFFFFFFFFFFFF
142 instance Ix Word where
145 | inRange b i = fromIntegral (i - m)
146 | otherwise = indexError b i "Word"
147 inRange (m,n) i = m <= i && i <= n
149 instance Read Word where
150 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
152 instance Bits Word where
153 (W# x#) .&. (W# y#) = W# (x# `and#` y#)
154 (W# x#) .|. (W# y#) = W# (x# `or#` y#)
155 (W# x#) `xor` (W# y#) = W# (x# `xor#` y#)
156 complement (W# x#) = W# (x# `xor#` mb#) where W# mb# = maxBound
157 (W# x#) `shift` (I# i#)
158 | i# >=# 0# = W# (x# `shiftL#` i#)
159 | otherwise = W# (x# `shiftRL#` negateInt# i#)
160 (W# x#) `rotate` (I# i#) = W# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (wsib -# i'#)))
162 i'# = word2Int# (int2Word# i# `and#` int2Word# (wsib -# 1#))
163 wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -}
164 bitSize _ = WORD_SIZE_IN_BITS
168 "fromIntegral/Int->Word" fromIntegral = \(I# x#) -> W# (int2Word# x#)
169 "fromIntegral/Word->Int" fromIntegral = \(W# x#) -> I# (word2Int# x#)
170 "fromIntegral/Word->Word" fromIntegral = id :: Word -> Word
173 ------------------------------------------------------------------------
175 ------------------------------------------------------------------------
177 -- Word8 is represented in the same way as Word. Operations may assume
178 -- and must ensure that it holds only values from its logical range.
180 data Word8 = W8# Word# deriving (Eq, Ord)
182 instance CCallable Word8
183 instance CReturnable Word8
185 instance Show Word8 where
186 showsPrec p x = showsPrec p (fromIntegral x :: Int)
188 instance Num Word8 where
189 (W8# x#) + (W8# y#) = W8# (narrow8Word# (x# `plusWord#` y#))
190 (W8# x#) - (W8# y#) = W8# (narrow8Word# (x# `minusWord#` y#))
191 (W8# x#) * (W8# y#) = W8# (narrow8Word# (x# `timesWord#` y#))
192 negate (W8# x#) = W8# (narrow8Word# (int2Word# (negateInt# (word2Int# x#))))
196 fromInteger (S# i#) = W8# (narrow8Word# (int2Word# i#))
197 fromInteger (J# s# d#) = W8# (narrow8Word# (integer2Word# s# d#))
199 instance Real Word8 where
200 toRational x = toInteger x % 1
202 instance Enum Word8 where
204 | x /= maxBound = x + 1
205 | otherwise = succError "Word8"
207 | x /= minBound = x - 1
208 | otherwise = predError "Word8"
210 | i >= 0 && i <= fromIntegral (maxBound::Word8)
212 | otherwise = toEnumError "Word8" i (minBound::Word8, maxBound::Word8)
213 fromEnum (W8# x#) = I# (word2Int# x#)
214 enumFrom = boundedEnumFrom
215 enumFromThen = boundedEnumFromThen
217 instance Integral Word8 where
218 quot x@(W8# x#) y@(W8# y#)
219 | y /= 0 = W8# (x# `quotWord#` y#)
220 | otherwise = divZeroError "quot{Word8}" x
221 rem x@(W8# x#) y@(W8# y#)
222 | y /= 0 = W8# (x# `remWord#` y#)
223 | otherwise = divZeroError "rem{Word8}" x
224 div x@(W8# x#) y@(W8# y#)
225 | y /= 0 = W8# (x# `quotWord#` y#)
226 | otherwise = divZeroError "div{Word8}" x
227 mod x@(W8# x#) y@(W8# y#)
228 | y /= 0 = W8# (x# `remWord#` y#)
229 | otherwise = divZeroError "mod{Word8}" x
230 quotRem x@(W8# x#) y@(W8# y#)
231 | y /= 0 = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
232 | otherwise = divZeroError "quotRem{Word8}" x
233 divMod x@(W8# x#) y@(W8# y#)
234 | y /= 0 = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
235 | otherwise = divZeroError "quotRem{Word8}" x
236 toInteger (W8# x#) = S# (word2Int# x#)
238 instance Bounded Word8 where
242 instance Ix Word8 where
245 | inRange b i = fromIntegral (i - m)
246 | otherwise = indexError b i "Word8"
247 inRange (m,n) i = m <= i && i <= n
249 instance Read Word8 where
250 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
252 instance Bits Word8 where
253 (W8# x#) .&. (W8# y#) = W8# (x# `and#` y#)
254 (W8# x#) .|. (W8# y#) = W8# (x# `or#` y#)
255 (W8# x#) `xor` (W8# y#) = W8# (x# `xor#` y#)
256 complement (W8# x#) = W8# (x# `xor#` mb#) where W8# mb# = maxBound
257 (W8# x#) `shift` (I# i#)
258 | i# >=# 0# = W8# (narrow8Word# (x# `shiftL#` i#))
259 | otherwise = W8# (x# `shiftRL#` negateInt# i#)
260 (W8# x#) `rotate` (I# i#) = W8# (narrow8Word# ((x# `shiftL#` i'#) `or#`
261 (x# `shiftRL#` (8# -# i'#))))
263 i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
268 "fromIntegral/Word8->Word8" fromIntegral = id :: Word8 -> Word8
269 "fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer
270 "fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (narrow8Word# x#)
271 "fromIntegral/Word8->a" fromIntegral = \(W8# x#) -> fromIntegral (W# x#)
274 ------------------------------------------------------------------------
276 ------------------------------------------------------------------------
278 -- Word16 is represented in the same way as Word. Operations may assume
279 -- and must ensure that it holds only values from its logical range.
281 data Word16 = W16# Word# deriving (Eq, Ord)
283 instance CCallable Word16
284 instance CReturnable Word16
286 instance Show Word16 where
287 showsPrec p x = showsPrec p (fromIntegral x :: Int)
289 instance Num Word16 where
290 (W16# x#) + (W16# y#) = W16# (narrow16Word# (x# `plusWord#` y#))
291 (W16# x#) - (W16# y#) = W16# (narrow16Word# (x# `minusWord#` y#))
292 (W16# x#) * (W16# y#) = W16# (narrow16Word# (x# `timesWord#` y#))
293 negate (W16# x#) = W16# (narrow16Word# (int2Word# (negateInt# (word2Int# x#))))
297 fromInteger (S# i#) = W16# (narrow16Word# (int2Word# i#))
298 fromInteger (J# s# d#) = W16# (narrow16Word# (integer2Word# s# d#))
300 instance Real Word16 where
301 toRational x = toInteger x % 1
303 instance Enum Word16 where
305 | x /= maxBound = x + 1
306 | otherwise = succError "Word16"
308 | x /= minBound = x - 1
309 | otherwise = predError "Word16"
311 | i >= 0 && i <= fromIntegral (maxBound::Word16)
312 = W16# (int2Word# i#)
313 | otherwise = toEnumError "Word16" i (minBound::Word16, maxBound::Word16)
314 fromEnum (W16# x#) = I# (word2Int# x#)
315 enumFrom = boundedEnumFrom
316 enumFromThen = boundedEnumFromThen
318 instance Integral Word16 where
319 quot x@(W16# x#) y@(W16# y#)
320 | y /= 0 = W16# (x# `quotWord#` y#)
321 | otherwise = divZeroError "quot{Word16}" x
322 rem x@(W16# x#) y@(W16# y#)
323 | y /= 0 = W16# (x# `remWord#` y#)
324 | otherwise = divZeroError "rem{Word16}" x
325 div x@(W16# x#) y@(W16# y#)
326 | y /= 0 = W16# (x# `quotWord#` y#)
327 | otherwise = divZeroError "div{Word16}" x
328 mod x@(W16# x#) y@(W16# y#)
329 | y /= 0 = W16# (x# `remWord#` y#)
330 | otherwise = divZeroError "mod{Word16}" x
331 quotRem x@(W16# x#) y@(W16# y#)
332 | y /= 0 = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
333 | otherwise = divZeroError "quotRem{Word16}" x
334 divMod x@(W16# x#) y@(W16# y#)
335 | y /= 0 = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
336 | otherwise = divZeroError "quotRem{Word16}" x
337 toInteger (W16# x#) = S# (word2Int# x#)
339 instance Bounded Word16 where
343 instance Ix Word16 where
346 | inRange b i = fromIntegral (i - m)
347 | otherwise = indexError b i "Word16"
348 inRange (m,n) i = m <= i && i <= n
350 instance Read Word16 where
351 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
353 instance Bits Word16 where
354 (W16# x#) .&. (W16# y#) = W16# (x# `and#` y#)
355 (W16# x#) .|. (W16# y#) = W16# (x# `or#` y#)
356 (W16# x#) `xor` (W16# y#) = W16# (x# `xor#` y#)
357 complement (W16# x#) = W16# (x# `xor#` mb#) where W16# mb# = maxBound
358 (W16# x#) `shift` (I# i#)
359 | i# >=# 0# = W16# (narrow16Word# (x# `shiftL#` i#))
360 | otherwise = W16# (x# `shiftRL#` negateInt# i#)
361 (W16# x#) `rotate` (I# i#) = W16# (narrow16Word# ((x# `shiftL#` i'#) `or#`
362 (x# `shiftRL#` (16# -# i'#))))
364 i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
369 "fromIntegral/Word8->Word16" fromIntegral = \(W8# x#) -> W16# x#
370 "fromIntegral/Word16->Word16" fromIntegral = id :: Word16 -> Word16
371 "fromIntegral/Word16->Integer" fromIntegral = toInteger :: Word16 -> Integer
372 "fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (narrow16Word# x#)
373 "fromIntegral/Word16->a" fromIntegral = \(W16# x#) -> fromIntegral (W# x#)
376 ------------------------------------------------------------------------
378 ------------------------------------------------------------------------
380 #if WORD_SIZE_IN_BITS < 32
382 data Word32 = W32# Word32#
384 instance Eq Word32 where
385 (W32# x#) == (W32# y#) = x# `eqWord32#` y#
386 (W32# x#) /= (W32# y#) = x# `neWord32#` y#
388 instance Ord Word32 where
389 (W32# x#) < (W32# y#) = x# `ltWord32#` y#
390 (W32# x#) <= (W32# y#) = x# `leWord32#` y#
391 (W32# x#) > (W32# y#) = x# `gtWord32#` y#
392 (W32# x#) >= (W32# y#) = x# `geWord32#` y#
394 instance Num Word32 where
395 (W32# x#) + (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `plusInt32#` word32ToInt32# y#))
396 (W32# x#) - (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `minusInt32#` word32ToInt32# y#))
397 (W32# x#) * (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `timesInt32#` word32ToInt32# y#))
398 negate (W32# x#) = W32# (int32ToWord32# (negateInt32# (word32ToInt32# x#)))
402 fromInteger (S# i#) = W32# (int32ToWord32# (intToInt32# i#))
403 fromInteger (J# s# d#) = W32# (integerToWord32# s# d#)
405 instance Enum Word32 where
407 | x /= maxBound = x + 1
408 | otherwise = succError "Word32"
410 | x /= minBound = x - 1
411 | otherwise = predError "Word32"
413 | i >= 0 = W32# (wordToWord32# (int2Word# i#))
414 | otherwise = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
416 | x <= fromIntegral (maxBound::Int)
417 = I# (word2Int# (word32ToWord# x#))
418 | otherwise = fromEnumError "Word32" x
419 enumFrom = integralEnumFrom
420 enumFromThen = integralEnumFromThen
421 enumFromTo = integralEnumFromTo
422 enumFromThenTo = integralEnumFromThenTo
424 instance Integral Word32 where
425 quot x@(W32# x#) y@(W32# y#)
426 | y /= 0 = W32# (x# `quotWord32#` y#)
427 | otherwise = divZeroError "quot{Word32}" x
428 rem x@(W32# x#) y@(W32# y#)
429 | y /= 0 = W32# (x# `remWord32#` y#)
430 | otherwise = divZeroError "rem{Word32}" x
431 div x@(W32# x#) y@(W32# y#)
432 | y /= 0 = W32# (x# `quotWord32#` y#)
433 | otherwise = divZeroError "div{Word32}" x
434 mod x@(W32# x#) y@(W32# y#)
435 | y /= 0 = W32# (x# `remWord32#` y#)
436 | otherwise = divZeroError "mod{Word32}" x
437 quotRem x@(W32# x#) y@(W32# y#)
438 | y /= 0 = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#))
439 | otherwise = divZeroError "quotRem{Word32}" x
440 divMod x@(W32# x#) y@(W32# y#)
441 | y /= 0 = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#))
442 | otherwise = divZeroError "quotRem{Word32}" x
443 toInteger x@(W32# x#)
444 | x <= fromIntegral (maxBound::Int) = S# (word2Int# (word32ToWord# x#))
445 | otherwise = case word32ToInteger# x# of (# s, d #) -> J# s d
447 instance Bits Word32 where
448 (W32# x#) .&. (W32# y#) = W32# (x# `and32#` y#)
449 (W32# x#) .|. (W32# y#) = W32# (x# `or32#` y#)
450 (W32# x#) `xor` (W32# y#) = W32# (x# `xor32#` y#)
451 complement (W32# x#) = W32# (not32# x#)
452 (W32# x#) `shift` (I# i#)
453 | i# >=# 0# = W32# (x# `shiftL32#` i#)
454 | otherwise = W32# (x# `shiftRL32#` negateInt# i#)
455 (W32# x#) `rotate` (I# i#) = W32# ((x# `shiftL32#` i'#) `or32#`
456 (x# `shiftRL32#` (32# -# i'#)))
458 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
462 foreign import "stg_eqWord32" unsafe eqWord32# :: Word32# -> Word32# -> Bool
463 foreign import "stg_neWord32" unsafe neWord32# :: Word32# -> Word32# -> Bool
464 foreign import "stg_ltWord32" unsafe ltWord32# :: Word32# -> Word32# -> Bool
465 foreign import "stg_leWord32" unsafe leWord32# :: Word32# -> Word32# -> Bool
466 foreign import "stg_gtWord32" unsafe gtWord32# :: Word32# -> Word32# -> Bool
467 foreign import "stg_geWord32" unsafe geWord32# :: Word32# -> Word32# -> Bool
468 foreign import "stg_int32ToWord32" unsafe int32ToWord32# :: Int32# -> Word32#
469 foreign import "stg_word32ToInt32" unsafe word32ToInt32# :: Word32# -> Int32#
470 foreign import "stg_intToInt32" unsafe intToInt32# :: Int# -> Int32#
471 foreign import "stg_wordToWord32" unsafe wordToWord32# :: Word# -> Word32#
472 foreign import "stg_word32ToWord" unsafe word32ToWord# :: Word32# -> Word#
473 foreign import "stg_plusInt32" unsafe plusInt32# :: Int32# -> Int32# -> Int32#
474 foreign import "stg_minusInt32" unsafe minusInt32# :: Int32# -> Int32# -> Int32#
475 foreign import "stg_timesInt32" unsafe timesInt32# :: Int32# -> Int32# -> Int32#
476 foreign import "stg_negateInt32" unsafe negateInt32# :: Int32# -> Int32#
477 foreign import "stg_quotWord32" unsafe quotWord32# :: Word32# -> Word32# -> Word32#
478 foreign import "stg_remWord32" unsafe remWord32# :: Word32# -> Word32# -> Word32#
479 foreign import "stg_and32" unsafe and32# :: Word32# -> Word32# -> Word32#
480 foreign import "stg_or32" unsafe or32# :: Word32# -> Word32# -> Word32#
481 foreign import "stg_xor32" unsafe xor32# :: Word32# -> Word32# -> Word32#
482 foreign import "stg_not32" unsafe not32# :: Word32# -> Word32#
483 foreign import "stg_shiftL32" unsafe shiftL32# :: Word32# -> Int# -> Word32#
484 foreign import "stg_shiftRL32" unsafe shiftRL32# :: Word32# -> Int# -> Word32#
487 "fromIntegral/Int->Word32" fromIntegral = \(I# x#) -> W32# (int32ToWord32# (intToInt32# x#))
488 "fromIntegral/Word->Word32" fromIntegral = \(W# x#) -> W32# (wordToWord32# x#)
489 "fromIntegral/Word32->Int" fromIntegral = \(W32# x#) -> I# (word2Int# (word32ToWord# x#))
490 "fromIntegral/Word32->Word" fromIntegral = \(W32# x#) -> W# (word32ToWord# x#)
491 "fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32
496 -- Word32 is represented in the same way as Word.
497 #if WORD_SIZE_IN_BITS > 32
498 -- Operations may assume and must ensure that it holds only values
499 -- from its logical range.
502 data Word32 = W32# Word# deriving (Eq, Ord)
504 instance Num Word32 where
505 (W32# x#) + (W32# y#) = W32# (narrow32Word# (x# `plusWord#` y#))
506 (W32# x#) - (W32# y#) = W32# (narrow32Word# (x# `minusWord#` y#))
507 (W32# x#) * (W32# y#) = W32# (narrow32Word# (x# `timesWord#` y#))
508 negate (W32# x#) = W32# (narrow32Word# (int2Word# (negateInt# (word2Int# x#))))
512 fromInteger (S# i#) = W32# (narrow32Word# (int2Word# i#))
513 fromInteger (J# s# d#) = W32# (narrow32Word# (integer2Word# s# d#))
515 instance Enum Word32 where
517 | x /= maxBound = x + 1
518 | otherwise = succError "Word32"
520 | x /= minBound = x - 1
521 | otherwise = predError "Word32"
524 #if WORD_SIZE_IN_BITS > 32
525 && i <= fromIntegral (maxBound::Word32)
527 = W32# (int2Word# i#)
528 | otherwise = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
529 #if WORD_SIZE_IN_BITS == 32
531 | x <= fromIntegral (maxBound::Int)
533 | otherwise = fromEnumError "Word32" x
534 enumFrom = integralEnumFrom
535 enumFromThen = integralEnumFromThen
536 enumFromTo = integralEnumFromTo
537 enumFromThenTo = integralEnumFromThenTo
539 fromEnum (W32# x#) = I# (word2Int# x#)
540 enumFrom = boundedEnumFrom
541 enumFromThen = boundedEnumFromThen
544 instance Integral Word32 where
545 quot x@(W32# x#) y@(W32# y#)
546 | y /= 0 = W32# (x# `quotWord#` y#)
547 | otherwise = divZeroError "quot{Word32}" x
548 rem x@(W32# x#) y@(W32# y#)
549 | y /= 0 = W32# (x# `remWord#` y#)
550 | otherwise = divZeroError "rem{Word32}" x
551 div x@(W32# x#) y@(W32# y#)
552 | y /= 0 = W32# (x# `quotWord#` y#)
553 | otherwise = divZeroError "div{Word32}" x
554 mod x@(W32# x#) y@(W32# y#)
555 | y /= 0 = W32# (x# `remWord#` y#)
556 | otherwise = divZeroError "mod{Word32}" x
557 quotRem x@(W32# x#) y@(W32# y#)
558 | y /= 0 = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
559 | otherwise = divZeroError "quotRem{Word32}" x
560 divMod x@(W32# x#) y@(W32# y#)
561 | y /= 0 = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
562 | otherwise = divZeroError "quotRem{Word32}" x
564 #if WORD_SIZE_IN_BITS == 32
566 | otherwise = case word2Integer# x# of (# s, d #) -> J# s d
573 instance Bits Word32 where
574 (W32# x#) .&. (W32# y#) = W32# (x# `and#` y#)
575 (W32# x#) .|. (W32# y#) = W32# (x# `or#` y#)
576 (W32# x#) `xor` (W32# y#) = W32# (x# `xor#` y#)
577 complement (W32# x#) = W32# (x# `xor#` mb#) where W32# mb# = maxBound
578 (W32# x#) `shift` (I# i#)
579 | i# >=# 0# = W32# (narrow32Word# (x# `shiftL#` i#))
580 | otherwise = W32# (x# `shiftRL#` negateInt# i#)
581 (W32# x#) `rotate` (I# i#) = W32# (narrow32Word# ((x# `shiftL#` i'#) `or#`
582 (x# `shiftRL#` (32# -# i'#))))
584 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
589 "fromIntegral/Word8->Word32" fromIntegral = \(W8# x#) -> W32# x#
590 "fromIntegral/Word16->Word32" fromIntegral = \(W16# x#) -> W32# x#
591 "fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32
592 "fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer
593 "fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (narrow32Word# x#)
594 "fromIntegral/Word32->a" fromIntegral = \(W32# x#) -> fromIntegral (W# x#)
599 instance CCallable Word32
600 instance CReturnable Word32
602 instance Show Word32 where
603 #if WORD_SIZE_IN_BITS < 33
604 showsPrec p x = showsPrec p (toInteger x)
606 showsPrec p x = showsPrec p (fromIntegral x :: Int)
610 instance Real Word32 where
611 toRational x = toInteger x % 1
613 instance Bounded Word32 where
615 maxBound = 0xFFFFFFFF
617 instance Ix Word32 where
620 | inRange b i = fromIntegral (i - m)
621 | otherwise = indexError b i "Word32"
622 inRange (m,n) i = m <= i && i <= n
624 instance Read Word32 where
625 #if WORD_SIZE_IN_BITS < 33
626 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
628 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
631 ------------------------------------------------------------------------
633 ------------------------------------------------------------------------
635 #if WORD_SIZE_IN_BITS < 64
637 data Word64 = W64# Word64#
639 instance Eq Word64 where
640 (W64# x#) == (W64# y#) = x# `eqWord64#` y#
641 (W64# x#) /= (W64# y#) = x# `neWord64#` y#
643 instance Ord Word64 where
644 (W64# x#) < (W64# y#) = x# `ltWord64#` y#
645 (W64# x#) <= (W64# y#) = x# `leWord64#` y#
646 (W64# x#) > (W64# y#) = x# `gtWord64#` y#
647 (W64# x#) >= (W64# y#) = x# `geWord64#` y#
649 instance Num Word64 where
650 (W64# x#) + (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `plusInt64#` word64ToInt64# y#))
651 (W64# x#) - (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `minusInt64#` word64ToInt64# y#))
652 (W64# x#) * (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `timesInt64#` word64ToInt64# y#))
653 negate (W64# x#) = W64# (int64ToWord64# (negateInt64# (word64ToInt64# x#)))
657 fromInteger (S# i#) = W64# (int64ToWord64# (intToInt64# i#))
658 fromInteger (J# s# d#) = W64# (integerToWord64# s# d#)
660 instance Enum Word64 where
662 | x /= maxBound = x + 1
663 | otherwise = succError "Word64"
665 | x /= minBound = x - 1
666 | otherwise = predError "Word64"
668 | i >= 0 = W64# (wordToWord64# (int2Word# i#))
669 | otherwise = toEnumError "Word64" i (minBound::Word64, maxBound::Word64)
671 | x <= fromIntegral (maxBound::Int)
672 = I# (word2Int# (word64ToWord# x#))
673 | otherwise = fromEnumError "Word64" x
674 enumFrom = integralEnumFrom
675 enumFromThen = integralEnumFromThen
676 enumFromTo = integralEnumFromTo
677 enumFromThenTo = integralEnumFromThenTo
679 instance Integral Word64 where
680 quot x@(W64# x#) y@(W64# y#)
681 | y /= 0 = W64# (x# `quotWord64#` y#)
682 | otherwise = divZeroError "quot{Word64}" x
683 rem x@(W64# x#) y@(W64# y#)
684 | y /= 0 = W64# (x# `remWord64#` y#)
685 | otherwise = divZeroError "rem{Word64}" x
686 div x@(W64# x#) y@(W64# y#)
687 | y /= 0 = W64# (x# `quotWord64#` y#)
688 | otherwise = divZeroError "div{Word64}" x
689 mod x@(W64# x#) y@(W64# y#)
690 | y /= 0 = W64# (x# `remWord64#` y#)
691 | otherwise = divZeroError "mod{Word64}" x
692 quotRem x@(W64# x#) y@(W64# y#)
693 | y /= 0 = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
694 | otherwise = divZeroError "quotRem{Word64}" x
695 divMod x@(W64# x#) y@(W64# y#)
696 | y /= 0 = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
697 | otherwise = divZeroError "quotRem{Word64}" x
698 toInteger x@(W64# x#)
699 | x <= 0x7FFFFFFF = S# (word2Int# (word64ToWord# x#))
700 | otherwise = case word64ToInteger# x# of (# s, d #) -> J# s d
702 instance Bits Word64 where
703 (W64# x#) .&. (W64# y#) = W64# (x# `and64#` y#)
704 (W64# x#) .|. (W64# y#) = W64# (x# `or64#` y#)
705 (W64# x#) `xor` (W64# y#) = W64# (x# `xor64#` y#)
706 complement (W64# x#) = W64# (not64# x#)
707 (W64# x#) `shift` (I# i#)
708 | i# >=# 0# = W64# (x# `shiftL64#` i#)
709 | otherwise = W64# (x# `shiftRL64#` negateInt# i#)
710 (W64# x#) `rotate` (I# i#) = W64# ((x# `shiftL64#` i'#) `or64#`
711 (x# `shiftRL64#` (64# -# i'#)))
713 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
717 foreign import "stg_eqWord64" unsafe eqWord64# :: Word64# -> Word64# -> Bool
718 foreign import "stg_neWord64" unsafe neWord64# :: Word64# -> Word64# -> Bool
719 foreign import "stg_ltWord64" unsafe ltWord64# :: Word64# -> Word64# -> Bool
720 foreign import "stg_leWord64" unsafe leWord64# :: Word64# -> Word64# -> Bool
721 foreign import "stg_gtWord64" unsafe gtWord64# :: Word64# -> Word64# -> Bool
722 foreign import "stg_geWord64" unsafe geWord64# :: Word64# -> Word64# -> Bool
723 foreign import "stg_int64ToWord64" unsafe int64ToWord64# :: Int64# -> Word64#
724 foreign import "stg_word64ToInt64" unsafe word64ToInt64# :: Word64# -> Int64#
725 foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64#
726 foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64#
727 foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word#
728 foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64#
729 foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64#
730 foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64#
731 foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64#
732 foreign import "stg_quotWord64" unsafe quotWord64# :: Word64# -> Word64# -> Word64#
733 foreign import "stg_remWord64" unsafe remWord64# :: Word64# -> Word64# -> Word64#
734 foreign import "stg_and64" unsafe and64# :: Word64# -> Word64# -> Word64#
735 foreign import "stg_or64" unsafe or64# :: Word64# -> Word64# -> Word64#
736 foreign import "stg_xor64" unsafe xor64# :: Word64# -> Word64# -> Word64#
737 foreign import "stg_not64" unsafe not64# :: Word64# -> Word64#
738 foreign import "stg_shiftL64" unsafe shiftL64# :: Word64# -> Int# -> Word64#
739 foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> Word64#
742 "fromIntegral/Int->Word64" fromIntegral = \(I# x#) -> W64# (int64ToWord64# (intToInt64# x#))
743 "fromIntegral/Word->Word64" fromIntegral = \(W# x#) -> W64# (wordToWord64# x#)
744 "fromIntegral/Word64->Int" fromIntegral = \(W64# x#) -> I# (word2Int# (word64ToWord# x#))
745 "fromIntegral/Word64->Word" fromIntegral = \(W64# x#) -> W# (word64ToWord# x#)
746 "fromIntegral/Word64->Word64" fromIntegral = id :: Word64 -> Word64
751 -- Word64 is represented in the same way as Word.
752 -- Operations may assume and must ensure that it holds only values
753 -- from its logical range.
755 data Word64 = W64# Word# deriving (Eq, Ord)
757 instance Num Word64 where
758 (W64# x#) + (W64# y#) = W64# (x# `plusWord#` y#)
759 (W64# x#) - (W64# y#) = W64# (x# `minusWord#` y#)
760 (W64# x#) * (W64# y#) = W64# (x# `timesWord#` y#)
761 negate (W64# x#) = W64# (int2Word# (negateInt# (word2Int# x#)))
765 fromInteger (S# i#) = W64# (int2Word# i#)
766 fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
768 instance Enum Word64 where
770 | x /= maxBound = x + 1
771 | otherwise = succError "Word64"
773 | x /= minBound = x - 1
774 | otherwise = predError "Word64"
776 | i >= 0 = W64# (int2Word# i#)
777 | otherwise = toEnumError "Word64" i (minBound::Word64, maxBound::Word64)
779 | x <= fromIntegral (maxBound::Int)
781 | otherwise = fromEnumError "Word64" x
782 enumFrom = integralEnumFrom
783 enumFromThen = integralEnumFromThen
784 enumFromTo = integralEnumFromTo
785 enumFromThenTo = integralEnumFromThenTo
787 instance Integral Word64 where
788 quot x@(W64# x#) y@(W64# y#)
789 | y /= 0 = W64# (x# `quotWord#` y#)
790 | otherwise = divZeroError "quot{Word64}" x
791 rem x@(W64# x#) y@(W64# y#)
792 | y /= 0 = W64# (x# `remWord#` y#)
793 | otherwise = divZeroError "rem{Word64}" x
794 div x@(W64# x#) y@(W64# y#)
795 | y /= 0 = W64# (x# `quotWord#` y#)
796 | otherwise = divZeroError "div{Word64}" x
797 mod x@(W64# x#) y@(W64# y#)
798 | y /= 0 = W64# (x# `remWord#` y#)
799 | otherwise = divZeroError "mod{Word64}" x
800 quotRem x@(W64# x#) y@(W64# y#)
801 | y /= 0 = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
802 | otherwise = divZeroError "quotRem{Word64}" x
803 divMod x@(W64# x#) y@(W64# y#)
804 | y /= 0 = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
805 | otherwise = divZeroError "quotRem{Word64}" x
808 | otherwise = case word2Integer# x# of (# s, d #) -> J# s d
812 instance Bits Word64 where
813 (W64# x#) .&. (W64# y#) = W64# (x# `and#` y#)
814 (W64# x#) .|. (W64# y#) = W64# (x# `or#` y#)
815 (W64# x#) `xor` (W64# y#) = W64# (x# `xor#` y#)
816 complement (W64# x#) = W64# (x# `xor#` mb#) where W64# mb# = maxBound
817 (W64# x#) `shift` (I# i#)
818 | i# >=# 0# = W64# (x# `shiftL#` i#)
819 | otherwise = W64# (x# `shiftRL#` negateInt# i#)
820 (W64# x#) `rotate` (I# i#) = W64# ((x# `shiftL#` i'#) `or#`
821 (x# `shiftRL#` (64# -# i'#)))
823 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
828 "fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x#
829 "fromIntegral/Word64->a" fromIntegral = \(W64# x#) -> fromIntegral (W# x#)
834 instance CCallable Word64
835 instance CReturnable Word64
837 instance Show Word64 where
838 showsPrec p x = showsPrec p (toInteger x)
840 instance Real Word64 where
841 toRational x = toInteger x % 1
843 instance Bounded Word64 where
845 maxBound = 0xFFFFFFFFFFFFFFFF
847 instance Ix Word64 where
850 | inRange b i = fromIntegral (i - m)
851 | otherwise = indexError b i "Word64"
852 inRange (m,n) i = m <= i && i <= n
854 instance Read Word64 where
855 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]