[project @ 2005-01-11 16:04:08 by simonmar]
[packages/old-time.git] / GHC / Word.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module : GHC.Word
5 -- Copyright : (c) The University of Glasgow, 1997-2002
6 -- License : see libraries/base/LICENSE
7 --
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC Extensions)
11 --
12 -- Sized unsigned integral types: 'Word', 'Word8', 'Word16', 'Word32', and
13 -- 'Word64'.
14 --
15 -----------------------------------------------------------------------------
16
17 #include "MachDeps.h"
18
19 module GHC.Word (
20 Word(..), Word8(..), Word16(..), Word32(..), Word64(..),
21 toEnumError, fromEnumError, succError, predError)
22 where
23
24 import Data.Bits
25
26 import {-# SOURCE #-} GHC.Err
27 import GHC.Base
28 import GHC.Enum
29 import GHC.Num
30 import GHC.Real
31 import GHC.Read
32 import GHC.Arr
33 import GHC.Show
34
35 ------------------------------------------------------------------------
36 -- Helper functions
37 ------------------------------------------------------------------------
38
39 {-# NOINLINE toEnumError #-}
40 toEnumError :: (Show a) => String -> Int -> (a,a) -> b
41 toEnumError inst_ty i bnds =
42 error $ "Enum.toEnum{" ++ inst_ty ++ "}: tag (" ++
43 show i ++
44 ") is outside of bounds " ++
45 show bnds
46
47 {-# NOINLINE fromEnumError #-}
48 fromEnumError :: (Show a) => String -> a -> b
49 fromEnumError inst_ty x =
50 error $ "Enum.fromEnum{" ++ inst_ty ++ "}: value (" ++
51 show x ++
52 ") is outside of Int's bounds " ++
53 show (minBound::Int, maxBound::Int)
54
55 {-# NOINLINE succError #-}
56 succError :: String -> a
57 succError inst_ty =
58 error $ "Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound"
59
60 {-# NOINLINE predError #-}
61 predError :: String -> a
62 predError inst_ty =
63 error $ "Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound"
64
65 ------------------------------------------------------------------------
66 -- type Word
67 ------------------------------------------------------------------------
68
69 -- |A 'Word' is an unsigned integral type, with the same size as 'Int'.
70 data Word = W# Word# deriving (Eq, Ord)
71
72 instance Show Word where
73 showsPrec p x = showsPrec p (toInteger x)
74
75 instance Num Word where
76 (W# x#) + (W# y#) = W# (x# `plusWord#` y#)
77 (W# x#) - (W# y#) = W# (x# `minusWord#` y#)
78 (W# x#) * (W# y#) = W# (x# `timesWord#` y#)
79 negate (W# x#) = W# (int2Word# (negateInt# (word2Int# x#)))
80 abs x = x
81 signum 0 = 0
82 signum _ = 1
83 fromInteger (S# i#) = W# (int2Word# i#)
84 fromInteger (J# s# d#) = W# (integer2Word# s# d#)
85
86 instance Real Word where
87 toRational x = toInteger x % 1
88
89 instance Enum Word where
90 succ x
91 | x /= maxBound = x + 1
92 | otherwise = succError "Word"
93 pred x
94 | x /= minBound = x - 1
95 | otherwise = predError "Word"
96 toEnum i@(I# i#)
97 | i >= 0 = W# (int2Word# i#)
98 | otherwise = toEnumError "Word" i (minBound::Word, maxBound::Word)
99 fromEnum x@(W# x#)
100 | x <= fromIntegral (maxBound::Int)
101 = I# (word2Int# x#)
102 | otherwise = fromEnumError "Word" x
103 enumFrom = integralEnumFrom
104 enumFromThen = integralEnumFromThen
105 enumFromTo = integralEnumFromTo
106 enumFromThenTo = integralEnumFromThenTo
107
108 instance Integral Word where
109 quot x@(W# x#) y@(W# y#)
110 | y /= 0 = W# (x# `quotWord#` y#)
111 | otherwise = divZeroError
112 rem x@(W# x#) y@(W# y#)
113 | y /= 0 = W# (x# `remWord#` y#)
114 | otherwise = divZeroError
115 div x@(W# x#) y@(W# y#)
116 | y /= 0 = W# (x# `quotWord#` y#)
117 | otherwise = divZeroError
118 mod x@(W# x#) y@(W# y#)
119 | y /= 0 = W# (x# `remWord#` y#)
120 | otherwise = divZeroError
121 quotRem x@(W# x#) y@(W# y#)
122 | y /= 0 = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
123 | otherwise = divZeroError
124 divMod x@(W# x#) y@(W# y#)
125 | y /= 0 = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
126 | otherwise = divZeroError
127 toInteger (W# x#)
128 | i# >=# 0# = S# i#
129 | otherwise = case word2Integer# x# of (# s, d #) -> J# s d
130 where
131 i# = word2Int# x#
132
133 instance Bounded Word where
134 minBound = 0
135
136 -- use unboxed literals for maxBound, because GHC doesn't optimise
137 -- (fromInteger 0xffffffff :: Word).
138 #if WORD_SIZE_IN_BITS == 31
139 maxBound = W# (int2Word# 0x7FFFFFFF#)
140 #elif WORD_SIZE_IN_BITS == 32
141 maxBound = W# (int2Word# 0xFFFFFFFF#)
142 #else
143 maxBound = W# (int2Word# 0xFFFFFFFFFFFFFFFF#)
144 #endif
145
146 instance Ix Word where
147 range (m,n) = [m..n]
148 unsafeIndex b@(m,_) i = fromIntegral (i - m)
149 inRange (m,n) i = m <= i && i <= n
150 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
151
152 instance Read Word where
153 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
154
155 instance Bits Word where
156 (W# x#) .&. (W# y#) = W# (x# `and#` y#)
157 (W# x#) .|. (W# y#) = W# (x# `or#` y#)
158 (W# x#) `xor` (W# y#) = W# (x# `xor#` y#)
159 complement (W# x#) = W# (x# `xor#` mb#) where W# mb# = maxBound
160 (W# x#) `shift` (I# i#)
161 | i# >=# 0# = W# (x# `shiftL#` i#)
162 | otherwise = W# (x# `shiftRL#` negateInt# i#)
163 (W# x#) `rotate` (I# i#)
164 | i'# ==# 0# = W# x#
165 | otherwise = W# ((x# `shiftL#` i'#) `or#` (x# `shiftRL#` (wsib -# i'#)))
166 where
167 i'# = word2Int# (int2Word# i# `and#` int2Word# (wsib -# 1#))
168 wsib = WORD_SIZE_IN_BITS# {- work around preprocessor problem (??) -}
169 bitSize _ = WORD_SIZE_IN_BITS
170 isSigned _ = False
171
172 {-# RULES
173 "fromIntegral/Int->Word" fromIntegral = \(I# x#) -> W# (int2Word# x#)
174 "fromIntegral/Word->Int" fromIntegral = \(W# x#) -> I# (word2Int# x#)
175 "fromIntegral/Word->Word" fromIntegral = id :: Word -> Word
176 #-}
177
178 ------------------------------------------------------------------------
179 -- type Word8
180 ------------------------------------------------------------------------
181
182 -- Word8 is represented in the same way as Word. Operations may assume
183 -- and must ensure that it holds only values from its logical range.
184
185 data Word8 = W8# Word# deriving (Eq, Ord)
186 -- ^ 8-bit unsigned integer type
187
188 instance Show Word8 where
189 showsPrec p x = showsPrec p (fromIntegral x :: Int)
190
191 instance Num Word8 where
192 (W8# x#) + (W8# y#) = W8# (narrow8Word# (x# `plusWord#` y#))
193 (W8# x#) - (W8# y#) = W8# (narrow8Word# (x# `minusWord#` y#))
194 (W8# x#) * (W8# y#) = W8# (narrow8Word# (x# `timesWord#` y#))
195 negate (W8# x#) = W8# (narrow8Word# (int2Word# (negateInt# (word2Int# x#))))
196 abs x = x
197 signum 0 = 0
198 signum _ = 1
199 fromInteger (S# i#) = W8# (narrow8Word# (int2Word# i#))
200 fromInteger (J# s# d#) = W8# (narrow8Word# (integer2Word# s# d#))
201
202 instance Real Word8 where
203 toRational x = toInteger x % 1
204
205 instance Enum Word8 where
206 succ x
207 | x /= maxBound = x + 1
208 | otherwise = succError "Word8"
209 pred x
210 | x /= minBound = x - 1
211 | otherwise = predError "Word8"
212 toEnum i@(I# i#)
213 | i >= 0 && i <= fromIntegral (maxBound::Word8)
214 = W8# (int2Word# i#)
215 | otherwise = toEnumError "Word8" i (minBound::Word8, maxBound::Word8)
216 fromEnum (W8# x#) = I# (word2Int# x#)
217 enumFrom = boundedEnumFrom
218 enumFromThen = boundedEnumFromThen
219
220 instance Integral Word8 where
221 quot x@(W8# x#) y@(W8# y#)
222 | y /= 0 = W8# (x# `quotWord#` y#)
223 | otherwise = divZeroError
224 rem x@(W8# x#) y@(W8# y#)
225 | y /= 0 = W8# (x# `remWord#` y#)
226 | otherwise = divZeroError
227 div x@(W8# x#) y@(W8# y#)
228 | y /= 0 = W8# (x# `quotWord#` y#)
229 | otherwise = divZeroError
230 mod x@(W8# x#) y@(W8# y#)
231 | y /= 0 = W8# (x# `remWord#` y#)
232 | otherwise = divZeroError
233 quotRem x@(W8# x#) y@(W8# y#)
234 | y /= 0 = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
235 | otherwise = divZeroError
236 divMod x@(W8# x#) y@(W8# y#)
237 | y /= 0 = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
238 | otherwise = divZeroError
239 toInteger (W8# x#) = S# (word2Int# x#)
240
241 instance Bounded Word8 where
242 minBound = 0
243 maxBound = 0xFF
244
245 instance Ix Word8 where
246 range (m,n) = [m..n]
247 unsafeIndex b@(m,_) i = fromIntegral (i - m)
248 inRange (m,n) i = m <= i && i <= n
249 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
250
251 instance Read Word8 where
252 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
253
254 instance Bits Word8 where
255 (W8# x#) .&. (W8# y#) = W8# (x# `and#` y#)
256 (W8# x#) .|. (W8# y#) = W8# (x# `or#` y#)
257 (W8# x#) `xor` (W8# y#) = W8# (x# `xor#` y#)
258 complement (W8# x#) = W8# (x# `xor#` mb#) where W8# mb# = maxBound
259 (W8# x#) `shift` (I# i#)
260 | i# >=# 0# = W8# (narrow8Word# (x# `shiftL#` i#))
261 | otherwise = W8# (x# `shiftRL#` negateInt# i#)
262 (W8# x#) `rotate` (I# i#)
263 | i'# ==# 0# = W8# x#
264 | otherwise = W8# (narrow8Word# ((x# `shiftL#` i'#) `or#`
265 (x# `shiftRL#` (8# -# i'#))))
266 where
267 i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
268 bitSize _ = 8
269 isSigned _ = False
270
271 {-# RULES
272 "fromIntegral/Word8->Word8" fromIntegral = id :: Word8 -> Word8
273 "fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer
274 "fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (narrow8Word# x#)
275 "fromIntegral/Word8->a" fromIntegral = \(W8# x#) -> fromIntegral (W# x#)
276 #-}
277
278 ------------------------------------------------------------------------
279 -- type Word16
280 ------------------------------------------------------------------------
281
282 -- Word16 is represented in the same way as Word. Operations may assume
283 -- and must ensure that it holds only values from its logical range.
284
285 data Word16 = W16# Word# deriving (Eq, Ord)
286 -- ^ 16-bit unsigned integer type
287
288 instance Show Word16 where
289 showsPrec p x = showsPrec p (fromIntegral x :: Int)
290
291 instance Num Word16 where
292 (W16# x#) + (W16# y#) = W16# (narrow16Word# (x# `plusWord#` y#))
293 (W16# x#) - (W16# y#) = W16# (narrow16Word# (x# `minusWord#` y#))
294 (W16# x#) * (W16# y#) = W16# (narrow16Word# (x# `timesWord#` y#))
295 negate (W16# x#) = W16# (narrow16Word# (int2Word# (negateInt# (word2Int# x#))))
296 abs x = x
297 signum 0 = 0
298 signum _ = 1
299 fromInteger (S# i#) = W16# (narrow16Word# (int2Word# i#))
300 fromInteger (J# s# d#) = W16# (narrow16Word# (integer2Word# s# d#))
301
302 instance Real Word16 where
303 toRational x = toInteger x % 1
304
305 instance Enum Word16 where
306 succ x
307 | x /= maxBound = x + 1
308 | otherwise = succError "Word16"
309 pred x
310 | x /= minBound = x - 1
311 | otherwise = predError "Word16"
312 toEnum i@(I# i#)
313 | i >= 0 && i <= fromIntegral (maxBound::Word16)
314 = W16# (int2Word# i#)
315 | otherwise = toEnumError "Word16" i (minBound::Word16, maxBound::Word16)
316 fromEnum (W16# x#) = I# (word2Int# x#)
317 enumFrom = boundedEnumFrom
318 enumFromThen = boundedEnumFromThen
319
320 instance Integral Word16 where
321 quot x@(W16# x#) y@(W16# y#)
322 | y /= 0 = W16# (x# `quotWord#` y#)
323 | otherwise = divZeroError
324 rem x@(W16# x#) y@(W16# y#)
325 | y /= 0 = W16# (x# `remWord#` y#)
326 | otherwise = divZeroError
327 div x@(W16# x#) y@(W16# y#)
328 | y /= 0 = W16# (x# `quotWord#` y#)
329 | otherwise = divZeroError
330 mod x@(W16# x#) y@(W16# y#)
331 | y /= 0 = W16# (x# `remWord#` y#)
332 | otherwise = divZeroError
333 quotRem x@(W16# x#) y@(W16# y#)
334 | y /= 0 = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
335 | otherwise = divZeroError
336 divMod x@(W16# x#) y@(W16# y#)
337 | y /= 0 = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
338 | otherwise = divZeroError
339 toInteger (W16# x#) = S# (word2Int# x#)
340
341 instance Bounded Word16 where
342 minBound = 0
343 maxBound = 0xFFFF
344
345 instance Ix Word16 where
346 range (m,n) = [m..n]
347 unsafeIndex b@(m,_) i = fromIntegral (i - m)
348 inRange (m,n) i = m <= i && i <= n
349 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
350
351 instance Read Word16 where
352 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
353
354 instance Bits Word16 where
355 (W16# x#) .&. (W16# y#) = W16# (x# `and#` y#)
356 (W16# x#) .|. (W16# y#) = W16# (x# `or#` y#)
357 (W16# x#) `xor` (W16# y#) = W16# (x# `xor#` y#)
358 complement (W16# x#) = W16# (x# `xor#` mb#) where W16# mb# = maxBound
359 (W16# x#) `shift` (I# i#)
360 | i# >=# 0# = W16# (narrow16Word# (x# `shiftL#` i#))
361 | otherwise = W16# (x# `shiftRL#` negateInt# i#)
362 (W16# x#) `rotate` (I# i#)
363 | i'# ==# 0# = W16# x#
364 | otherwise = W16# (narrow16Word# ((x# `shiftL#` i'#) `or#`
365 (x# `shiftRL#` (16# -# i'#))))
366 where
367 i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
368 bitSize _ = 16
369 isSigned _ = False
370
371 {-# RULES
372 "fromIntegral/Word8->Word16" fromIntegral = \(W8# x#) -> W16# x#
373 "fromIntegral/Word16->Word16" fromIntegral = id :: Word16 -> Word16
374 "fromIntegral/Word16->Integer" fromIntegral = toInteger :: Word16 -> Integer
375 "fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (narrow16Word# x#)
376 "fromIntegral/Word16->a" fromIntegral = \(W16# x#) -> fromIntegral (W# x#)
377 #-}
378
379 ------------------------------------------------------------------------
380 -- type Word32
381 ------------------------------------------------------------------------
382
383 #if WORD_SIZE_IN_BITS < 32
384
385 data Word32 = W32# Word32#
386 -- ^ 32-bit unsigned integer type
387
388 instance Eq Word32 where
389 (W32# x#) == (W32# y#) = x# `eqWord32#` y#
390 (W32# x#) /= (W32# y#) = x# `neWord32#` y#
391
392 instance Ord Word32 where
393 (W32# x#) < (W32# y#) = x# `ltWord32#` y#
394 (W32# x#) <= (W32# y#) = x# `leWord32#` y#
395 (W32# x#) > (W32# y#) = x# `gtWord32#` y#
396 (W32# x#) >= (W32# y#) = x# `geWord32#` y#
397
398 instance Num Word32 where
399 (W32# x#) + (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `plusInt32#` word32ToInt32# y#))
400 (W32# x#) - (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `minusInt32#` word32ToInt32# y#))
401 (W32# x#) * (W32# y#) = W32# (int32ToWord32# (word32ToInt32# x# `timesInt32#` word32ToInt32# y#))
402 negate (W32# x#) = W32# (int32ToWord32# (negateInt32# (word32ToInt32# x#)))
403 abs x = x
404 signum 0 = 0
405 signum _ = 1
406 fromInteger (S# i#) = W32# (int32ToWord32# (intToInt32# i#))
407 fromInteger (J# s# d#) = W32# (integerToWord32# s# d#)
408
409 instance Enum Word32 where
410 succ x
411 | x /= maxBound = x + 1
412 | otherwise = succError "Word32"
413 pred x
414 | x /= minBound = x - 1
415 | otherwise = predError "Word32"
416 toEnum i@(I# i#)
417 | i >= 0 = W32# (wordToWord32# (int2Word# i#))
418 | otherwise = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
419 fromEnum x@(W32# x#)
420 | x <= fromIntegral (maxBound::Int)
421 = I# (word2Int# (word32ToWord# x#))
422 | otherwise = fromEnumError "Word32" x
423 enumFrom = integralEnumFrom
424 enumFromThen = integralEnumFromThen
425 enumFromTo = integralEnumFromTo
426 enumFromThenTo = integralEnumFromThenTo
427
428 instance Integral Word32 where
429 quot x@(W32# x#) y@(W32# y#)
430 | y /= 0 = W32# (x# `quotWord32#` y#)
431 | otherwise = divZeroError
432 rem x@(W32# x#) y@(W32# y#)
433 | y /= 0 = W32# (x# `remWord32#` y#)
434 | otherwise = divZeroError
435 div x@(W32# x#) y@(W32# y#)
436 | y /= 0 = W32# (x# `quotWord32#` y#)
437 | otherwise = divZeroError
438 mod x@(W32# x#) y@(W32# y#)
439 | y /= 0 = W32# (x# `remWord32#` y#)
440 | otherwise = divZeroError
441 quotRem x@(W32# x#) y@(W32# y#)
442 | y /= 0 = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#))
443 | otherwise = divZeroError
444 divMod x@(W32# x#) y@(W32# y#)
445 | y /= 0 = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#))
446 | otherwise = divZeroError
447 toInteger x@(W32# x#)
448 | x <= fromIntegral (maxBound::Int) = S# (word2Int# (word32ToWord# x#))
449 | otherwise = case word32ToInteger# x# of (# s, d #) -> J# s d
450
451 instance Bits Word32 where
452 (W32# x#) .&. (W32# y#) = W32# (x# `and32#` y#)
453 (W32# x#) .|. (W32# y#) = W32# (x# `or32#` y#)
454 (W32# x#) `xor` (W32# y#) = W32# (x# `xor32#` y#)
455 complement (W32# x#) = W32# (not32# x#)
456 (W32# x#) `shift` (I# i#)
457 | i# >=# 0# = W32# (x# `shiftL32#` i#)
458 | otherwise = W32# (x# `shiftRL32#` negateInt# i#)
459 (W32# x#) `rotate` (I# i#)
460 | i'# ==# 0# = W32# x#
461 | otherwise = W32# ((x# `shiftL32#` i'#) `or32#`
462 (x# `shiftRL32#` (32# -# i'#)))
463 where
464 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
465 bitSize _ = 32
466 isSigned _ = False
467
468 foreign import unsafe "stg_eqWord32" eqWord32# :: Word32# -> Word32# -> Bool
469 foreign import unsafe "stg_neWord32" neWord32# :: Word32# -> Word32# -> Bool
470 foreign import unsafe "stg_ltWord32" ltWord32# :: Word32# -> Word32# -> Bool
471 foreign import unsafe "stg_leWord32" leWord32# :: Word32# -> Word32# -> Bool
472 foreign import unsafe "stg_gtWord32" gtWord32# :: Word32# -> Word32# -> Bool
473 foreign import unsafe "stg_geWord32" geWord32# :: Word32# -> Word32# -> Bool
474 foreign import unsafe "stg_int32ToWord32" int32ToWord32# :: Int32# -> Word32#
475 foreign import unsafe "stg_word32ToInt32" word32ToInt32# :: Word32# -> Int32#
476 foreign import unsafe "stg_intToInt32" intToInt32# :: Int# -> Int32#
477 foreign import unsafe "stg_wordToWord32" wordToWord32# :: Word# -> Word32#
478 foreign import unsafe "stg_word32ToWord" word32ToWord# :: Word32# -> Word#
479 foreign import unsafe "stg_plusInt32" plusInt32# :: Int32# -> Int32# -> Int32#
480 foreign import unsafe "stg_minusInt32" minusInt32# :: Int32# -> Int32# -> Int32#
481 foreign import unsafe "stg_timesInt32" timesInt32# :: Int32# -> Int32# -> Int32#
482 foreign import unsafe "stg_negateInt32" negateInt32# :: Int32# -> Int32#
483 foreign import unsafe "stg_quotWord32" quotWord32# :: Word32# -> Word32# -> Word32#
484 foreign import unsafe "stg_remWord32" remWord32# :: Word32# -> Word32# -> Word32#
485 foreign import unsafe "stg_and32" and32# :: Word32# -> Word32# -> Word32#
486 foreign import unsafe "stg_or32" or32# :: Word32# -> Word32# -> Word32#
487 foreign import unsafe "stg_xor32" xor32# :: Word32# -> Word32# -> Word32#
488 foreign import unsafe "stg_not32" not32# :: Word32# -> Word32#
489 foreign import unsafe "stg_shiftL32" shiftL32# :: Word32# -> Int# -> Word32#
490 foreign import unsafe "stg_shiftRL32" shiftRL32# :: Word32# -> Int# -> Word32#
491
492 {-# RULES
493 "fromIntegral/Int->Word32" fromIntegral = \(I# x#) -> W32# (int32ToWord32# (intToInt32# x#))
494 "fromIntegral/Word->Word32" fromIntegral = \(W# x#) -> W32# (wordToWord32# x#)
495 "fromIntegral/Word32->Int" fromIntegral = \(W32# x#) -> I# (word2Int# (word32ToWord# x#))
496 "fromIntegral/Word32->Word" fromIntegral = \(W32# x#) -> W# (word32ToWord# x#)
497 "fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32
498 #-}
499
500 #else
501
502 -- Word32 is represented in the same way as Word.
503 #if WORD_SIZE_IN_BITS > 32
504 -- Operations may assume and must ensure that it holds only values
505 -- from its logical range.
506 #endif
507
508 data Word32 = W32# Word# deriving (Eq, Ord)
509 -- ^ 32-bit unsigned integer type
510
511 instance Num Word32 where
512 (W32# x#) + (W32# y#) = W32# (narrow32Word# (x# `plusWord#` y#))
513 (W32# x#) - (W32# y#) = W32# (narrow32Word# (x# `minusWord#` y#))
514 (W32# x#) * (W32# y#) = W32# (narrow32Word# (x# `timesWord#` y#))
515 negate (W32# x#) = W32# (narrow32Word# (int2Word# (negateInt# (word2Int# x#))))
516 abs x = x
517 signum 0 = 0
518 signum _ = 1
519 fromInteger (S# i#) = W32# (narrow32Word# (int2Word# i#))
520 fromInteger (J# s# d#) = W32# (narrow32Word# (integer2Word# s# d#))
521
522 instance Enum Word32 where
523 succ x
524 | x /= maxBound = x + 1
525 | otherwise = succError "Word32"
526 pred x
527 | x /= minBound = x - 1
528 | otherwise = predError "Word32"
529 toEnum i@(I# i#)
530 | i >= 0
531 #if WORD_SIZE_IN_BITS > 32
532 && i <= fromIntegral (maxBound::Word32)
533 #endif
534 = W32# (int2Word# i#)
535 | otherwise = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
536 #if WORD_SIZE_IN_BITS == 32
537 fromEnum x@(W32# x#)
538 | x <= fromIntegral (maxBound::Int)
539 = I# (word2Int# x#)
540 | otherwise = fromEnumError "Word32" x
541 enumFrom = integralEnumFrom
542 enumFromThen = integralEnumFromThen
543 enumFromTo = integralEnumFromTo
544 enumFromThenTo = integralEnumFromThenTo
545 #else
546 fromEnum (W32# x#) = I# (word2Int# x#)
547 enumFrom = boundedEnumFrom
548 enumFromThen = boundedEnumFromThen
549 #endif
550
551 instance Integral Word32 where
552 quot x@(W32# x#) y@(W32# y#)
553 | y /= 0 = W32# (x# `quotWord#` y#)
554 | otherwise = divZeroError
555 rem x@(W32# x#) y@(W32# y#)
556 | y /= 0 = W32# (x# `remWord#` y#)
557 | otherwise = divZeroError
558 div x@(W32# x#) y@(W32# y#)
559 | y /= 0 = W32# (x# `quotWord#` y#)
560 | otherwise = divZeroError
561 mod x@(W32# x#) y@(W32# y#)
562 | y /= 0 = W32# (x# `remWord#` y#)
563 | otherwise = divZeroError
564 quotRem x@(W32# x#) y@(W32# y#)
565 | y /= 0 = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
566 | otherwise = divZeroError
567 divMod x@(W32# x#) y@(W32# y#)
568 | y /= 0 = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
569 | otherwise = divZeroError
570 toInteger (W32# x#)
571 #if WORD_SIZE_IN_BITS == 32
572 | i# >=# 0# = S# i#
573 | otherwise = case word2Integer# x# of (# s, d #) -> J# s d
574 where
575 i# = word2Int# x#
576 #else
577 = S# (word2Int# x#)
578 #endif
579
580 instance Bits Word32 where
581 (W32# x#) .&. (W32# y#) = W32# (x# `and#` y#)
582 (W32# x#) .|. (W32# y#) = W32# (x# `or#` y#)
583 (W32# x#) `xor` (W32# y#) = W32# (x# `xor#` y#)
584 complement (W32# x#) = W32# (x# `xor#` mb#) where W32# mb# = maxBound
585 (W32# x#) `shift` (I# i#)
586 | i# >=# 0# = W32# (narrow32Word# (x# `shiftL#` i#))
587 | otherwise = W32# (x# `shiftRL#` negateInt# i#)
588 (W32# x#) `rotate` (I# i#)
589 | i'# ==# 0# = W32# x#
590 | otherwise = W32# (narrow32Word# ((x# `shiftL#` i'#) `or#`
591 (x# `shiftRL#` (32# -# i'#))))
592 where
593 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
594 bitSize _ = 32
595 isSigned _ = False
596
597 {-# RULES
598 "fromIntegral/Word8->Word32" fromIntegral = \(W8# x#) -> W32# x#
599 "fromIntegral/Word16->Word32" fromIntegral = \(W16# x#) -> W32# x#
600 "fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32
601 "fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer
602 "fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (narrow32Word# x#)
603 "fromIntegral/Word32->a" fromIntegral = \(W32# x#) -> fromIntegral (W# x#)
604 #-}
605
606 #endif
607
608 instance Show Word32 where
609 #if WORD_SIZE_IN_BITS < 33
610 showsPrec p x = showsPrec p (toInteger x)
611 #else
612 showsPrec p x = showsPrec p (fromIntegral x :: Int)
613 #endif
614
615
616 instance Real Word32 where
617 toRational x = toInteger x % 1
618
619 instance Bounded Word32 where
620 minBound = 0
621 maxBound = 0xFFFFFFFF
622
623 instance Ix Word32 where
624 range (m,n) = [m..n]
625 unsafeIndex b@(m,_) i = fromIntegral (i - m)
626 inRange (m,n) i = m <= i && i <= n
627 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
628
629 instance Read Word32 where
630 #if WORD_SIZE_IN_BITS < 33
631 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
632 #else
633 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
634 #endif
635
636 ------------------------------------------------------------------------
637 -- type Word64
638 ------------------------------------------------------------------------
639
640 #if WORD_SIZE_IN_BITS < 64
641
642 data Word64 = W64# Word64#
643 -- ^ 64-bit unsigned integer type
644
645 instance Eq Word64 where
646 (W64# x#) == (W64# y#) = x# `eqWord64#` y#
647 (W64# x#) /= (W64# y#) = x# `neWord64#` y#
648
649 instance Ord Word64 where
650 (W64# x#) < (W64# y#) = x# `ltWord64#` y#
651 (W64# x#) <= (W64# y#) = x# `leWord64#` y#
652 (W64# x#) > (W64# y#) = x# `gtWord64#` y#
653 (W64# x#) >= (W64# y#) = x# `geWord64#` y#
654
655 instance Num Word64 where
656 (W64# x#) + (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `plusInt64#` word64ToInt64# y#))
657 (W64# x#) - (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `minusInt64#` word64ToInt64# y#))
658 (W64# x#) * (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `timesInt64#` word64ToInt64# y#))
659 negate (W64# x#) = W64# (int64ToWord64# (negateInt64# (word64ToInt64# x#)))
660 abs x = x
661 signum 0 = 0
662 signum _ = 1
663 fromInteger (S# i#) = W64# (int64ToWord64# (intToInt64# i#))
664 fromInteger (J# s# d#) = W64# (integerToWord64# s# d#)
665
666 instance Enum Word64 where
667 succ x
668 | x /= maxBound = x + 1
669 | otherwise = succError "Word64"
670 pred x
671 | x /= minBound = x - 1
672 | otherwise = predError "Word64"
673 toEnum i@(I# i#)
674 | i >= 0 = W64# (wordToWord64# (int2Word# i#))
675 | otherwise = toEnumError "Word64" i (minBound::Word64, maxBound::Word64)
676 fromEnum x@(W64# x#)
677 | x <= fromIntegral (maxBound::Int)
678 = I# (word2Int# (word64ToWord# x#))
679 | otherwise = fromEnumError "Word64" x
680 enumFrom = integralEnumFrom
681 enumFromThen = integralEnumFromThen
682 enumFromTo = integralEnumFromTo
683 enumFromThenTo = integralEnumFromThenTo
684
685 instance Integral Word64 where
686 quot x@(W64# x#) y@(W64# y#)
687 | y /= 0 = W64# (x# `quotWord64#` y#)
688 | otherwise = divZeroError
689 rem x@(W64# x#) y@(W64# y#)
690 | y /= 0 = W64# (x# `remWord64#` y#)
691 | otherwise = divZeroError
692 div x@(W64# x#) y@(W64# y#)
693 | y /= 0 = W64# (x# `quotWord64#` y#)
694 | otherwise = divZeroError
695 mod x@(W64# x#) y@(W64# y#)
696 | y /= 0 = W64# (x# `remWord64#` y#)
697 | otherwise = divZeroError
698 quotRem x@(W64# x#) y@(W64# y#)
699 | y /= 0 = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
700 | otherwise = divZeroError
701 divMod x@(W64# x#) y@(W64# y#)
702 | y /= 0 = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
703 | otherwise = divZeroError
704 toInteger x@(W64# x#)
705 | x <= 0x7FFFFFFF = S# (word2Int# (word64ToWord# x#))
706 | otherwise = case word64ToInteger# x# of (# s, d #) -> J# s d
707
708 instance Bits Word64 where
709 (W64# x#) .&. (W64# y#) = W64# (x# `and64#` y#)
710 (W64# x#) .|. (W64# y#) = W64# (x# `or64#` y#)
711 (W64# x#) `xor` (W64# y#) = W64# (x# `xor64#` y#)
712 complement (W64# x#) = W64# (not64# x#)
713 (W64# x#) `shift` (I# i#)
714 | i# >=# 0# = W64# (x# `shiftL64#` i#)
715 | otherwise = W64# (x# `shiftRL64#` negateInt# i#)
716 (W64# x#) `rotate` (I# i#)
717 | i'# ==# 0# = W64# x#
718 | otherwise = W64# ((x# `uncheckedShiftL64#` i'#) `or64#`
719 (x# `uncheckedShiftRL64#` (64# -# i'#)))
720 where
721 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
722 bitSize _ = 64
723 isSigned _ = False
724
725 -- give the 64-bit shift operations the same treatment as the 32-bit
726 -- ones (see GHC.Base), namely we wrap them in tests to catch the
727 -- cases when we're shifting more than 64 bits to avoid unspecified
728 -- behaviour in the C shift operations.
729
730 shiftL64#, shiftRL64# :: Word64# -> Int# -> Word64#
731
732 a `shiftL64#` b | b >=# 64# = wordToWord64# (int2Word# 0#)
733 | otherwise = a `uncheckedShiftL64#` b
734
735 a `shiftRL64#` b | b >=# 64# = wordToWord64# (int2Word# 0#)
736 | otherwise = a `uncheckedShiftRL64#` b
737
738
739 foreign import ccall unsafe "stg_eqWord64" eqWord64# :: Word64# -> Word64# -> Bool
740 foreign import ccall unsafe "stg_neWord64" neWord64# :: Word64# -> Word64# -> Bool
741 foreign import ccall unsafe "stg_ltWord64" ltWord64# :: Word64# -> Word64# -> Bool
742 foreign import ccall unsafe "stg_leWord64" leWord64# :: Word64# -> Word64# -> Bool
743 foreign import ccall unsafe "stg_gtWord64" gtWord64# :: Word64# -> Word64# -> Bool
744 foreign import ccall unsafe "stg_geWord64" geWord64# :: Word64# -> Word64# -> Bool
745 foreign import ccall unsafe "stg_int64ToWord64" int64ToWord64# :: Int64# -> Word64#
746 foreign import ccall unsafe "stg_word64ToInt64" word64ToInt64# :: Word64# -> Int64#
747 foreign import ccall unsafe "stg_intToInt64" intToInt64# :: Int# -> Int64#
748 foreign import ccall unsafe "stg_wordToWord64" wordToWord64# :: Word# -> Word64#
749 foreign import ccall unsafe "stg_word64ToWord" word64ToWord# :: Word64# -> Word#
750 foreign import ccall unsafe "stg_plusInt64" plusInt64# :: Int64# -> Int64# -> Int64#
751 foreign import ccall unsafe "stg_minusInt64" minusInt64# :: Int64# -> Int64# -> Int64#
752 foreign import ccall unsafe "stg_timesInt64" timesInt64# :: Int64# -> Int64# -> Int64#
753 foreign import ccall unsafe "stg_negateInt64" negateInt64# :: Int64# -> Int64#
754 foreign import ccall unsafe "stg_quotWord64" quotWord64# :: Word64# -> Word64# -> Word64#
755 foreign import ccall unsafe "stg_remWord64" remWord64# :: Word64# -> Word64# -> Word64#
756 foreign import ccall unsafe "stg_and64" and64# :: Word64# -> Word64# -> Word64#
757 foreign import ccall unsafe "stg_or64" or64# :: Word64# -> Word64# -> Word64#
758 foreign import ccall unsafe "stg_xor64" xor64# :: Word64# -> Word64# -> Word64#
759 foreign import ccall unsafe "stg_not64" not64# :: Word64# -> Word64#
760 foreign import ccall unsafe "stg_uncheckedShiftL64" uncheckedShiftL64# :: Word64# -> Int# -> Word64#
761 foreign import ccall unsafe "stg_uncheckedShiftRL64" uncheckedShiftRL64# :: Word64# -> Int# -> Word64#
762
763 foreign import ccall unsafe "stg_integerToWord64" integerToWord64# :: Int# -> ByteArray# -> Word64#
764
765
766 {-# RULES
767 "fromIntegral/Int->Word64" fromIntegral = \(I# x#) -> W64# (int64ToWord64# (intToInt64# x#))
768 "fromIntegral/Word->Word64" fromIntegral = \(W# x#) -> W64# (wordToWord64# x#)
769 "fromIntegral/Word64->Int" fromIntegral = \(W64# x#) -> I# (word2Int# (word64ToWord# x#))
770 "fromIntegral/Word64->Word" fromIntegral = \(W64# x#) -> W# (word64ToWord# x#)
771 "fromIntegral/Word64->Word64" fromIntegral = id :: Word64 -> Word64
772 #-}
773
774 #else
775
776 -- Word64 is represented in the same way as Word.
777 -- Operations may assume and must ensure that it holds only values
778 -- from its logical range.
779
780 data Word64 = W64# Word# deriving (Eq, Ord)
781 -- ^ 64-bit unsigned integer type
782
783 instance Num Word64 where
784 (W64# x#) + (W64# y#) = W64# (x# `plusWord#` y#)
785 (W64# x#) - (W64# y#) = W64# (x# `minusWord#` y#)
786 (W64# x#) * (W64# y#) = W64# (x# `timesWord#` y#)
787 negate (W64# x#) = W64# (int2Word# (negateInt# (word2Int# x#)))
788 abs x = x
789 signum 0 = 0
790 signum _ = 1
791 fromInteger (S# i#) = W64# (int2Word# i#)
792 fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
793
794 instance Enum Word64 where
795 succ x
796 | x /= maxBound = x + 1
797 | otherwise = succError "Word64"
798 pred x
799 | x /= minBound = x - 1
800 | otherwise = predError "Word64"
801 toEnum i@(I# i#)
802 | i >= 0 = W64# (int2Word# i#)
803 | otherwise = toEnumError "Word64" i (minBound::Word64, maxBound::Word64)
804 fromEnum x@(W64# x#)
805 | x <= fromIntegral (maxBound::Int)
806 = I# (word2Int# x#)
807 | otherwise = fromEnumError "Word64" x
808 enumFrom = integralEnumFrom
809 enumFromThen = integralEnumFromThen
810 enumFromTo = integralEnumFromTo
811 enumFromThenTo = integralEnumFromThenTo
812
813 instance Integral Word64 where
814 quot x@(W64# x#) y@(W64# y#)
815 | y /= 0 = W64# (x# `quotWord#` y#)
816 | otherwise = divZeroError
817 rem x@(W64# x#) y@(W64# y#)
818 | y /= 0 = W64# (x# `remWord#` y#)
819 | otherwise = divZeroError
820 div x@(W64# x#) y@(W64# y#)
821 | y /= 0 = W64# (x# `quotWord#` y#)
822 | otherwise = divZeroError
823 mod x@(W64# x#) y@(W64# y#)
824 | y /= 0 = W64# (x# `remWord#` y#)
825 | otherwise = divZeroError
826 quotRem x@(W64# x#) y@(W64# y#)
827 | y /= 0 = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
828 | otherwise = divZeroError
829 divMod x@(W64# x#) y@(W64# y#)
830 | y /= 0 = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
831 | otherwise = divZeroError
832 toInteger (W64# x#)
833 | i# >=# 0# = S# i#
834 | otherwise = case word2Integer# x# of (# s, d #) -> J# s d
835 where
836 i# = word2Int# x#
837
838 instance Bits Word64 where
839 (W64# x#) .&. (W64# y#) = W64# (x# `and#` y#)
840 (W64# x#) .|. (W64# y#) = W64# (x# `or#` y#)
841 (W64# x#) `xor` (W64# y#) = W64# (x# `xor#` y#)
842 complement (W64# x#) = W64# (x# `xor#` mb#) where W64# mb# = maxBound
843 (W64# x#) `shift` (I# i#)
844 | i# >=# 0# = W64# (x# `shiftL#` i#)
845 | otherwise = W64# (x# `shiftRL#` negateInt# i#)
846 (W64# x#) `rotate` (I# i#)
847 | i'# ==# 0# = W64# x#
848 | otherwise = W64# ((x# `shiftL#` i'#) `or#`
849 (x# `shiftRL#` (64# -# i'#)))
850 where
851 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
852 bitSize _ = 64
853 isSigned _ = False
854
855 {-# RULES
856 "fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x#
857 "fromIntegral/Word64->a" fromIntegral = \(W64# x#) -> fromIntegral (W# x#)
858 #-}
859
860 #endif
861
862 instance Show Word64 where
863 showsPrec p x = showsPrec p (toInteger x)
864
865 instance Real Word64 where
866 toRational x = toInteger x % 1
867
868 instance Bounded Word64 where
869 minBound = 0
870 maxBound = 0xFFFFFFFFFFFFFFFF
871
872 instance Ix Word64 where
873 range (m,n) = [m..n]
874 unsafeIndex b@(m,_) i = fromIntegral (i - m)
875 inRange (m,n) i = m <= i && i <= n
876 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
877
878 instance Read Word64 where
879 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]