1581c4a868041017d80319aae32efbe3f4d98dfc
[packages/base.git] / GHC / Word.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash, UnboxedTuples #-}
3 {-# OPTIONS_HADDOCK hide #-}
4
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : GHC.Word
8 -- Copyright : (c) The University of Glasgow, 1997-2002
9 -- License : see libraries/base/LICENSE
10 --
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (GHC Extensions)
14 --
15 -- Sized unsigned integral types: 'Word', 'Word8', 'Word16', 'Word32', and
16 -- 'Word64'.
17 --
18 -----------------------------------------------------------------------------
19
20 #include "MachDeps.h"
21
22 -- #hide
23 module GHC.Word (
24 Word(..), Word8(..), Word16(..), Word32(..), Word64(..),
25 uncheckedShiftL64#,
26 uncheckedShiftRL64#,
27 byteSwap16,
28 byteSwap32,
29 byteSwap64
30 ) where
31
32 import Data.Bits
33 import Data.Maybe
34
35 #if WORD_SIZE_IN_BITS < 64
36 import GHC.IntWord64
37 #endif
38
39 -- import {-# SOURCE #-} GHC.Exception
40 import GHC.Base
41 import GHC.Enum
42 import GHC.Num
43 import GHC.Real
44 import GHC.Read
45 import GHC.Arr
46 import GHC.Show
47 import GHC.Float () -- for RealFrac methods
48
49 ------------------------------------------------------------------------
50 -- type Word8
51 ------------------------------------------------------------------------
52
53 -- Word8 is represented in the same way as Word. Operations may assume
54 -- and must ensure that it holds only values from its logical range.
55
56 data {-# CTYPE "HsWord8" #-} Word8 = W8# Word# deriving (Eq, Ord)
57 -- ^ 8-bit unsigned integer type
58
59 instance Show Word8 where
60 showsPrec p x = showsPrec p (fromIntegral x :: Int)
61
62 instance Num Word8 where
63 (W8# x#) + (W8# y#) = W8# (narrow8Word# (x# `plusWord#` y#))
64 (W8# x#) - (W8# y#) = W8# (narrow8Word# (x# `minusWord#` y#))
65 (W8# x#) * (W8# y#) = W8# (narrow8Word# (x# `timesWord#` y#))
66 negate (W8# x#) = W8# (narrow8Word# (int2Word# (negateInt# (word2Int# x#))))
67 abs x = x
68 signum 0 = 0
69 signum _ = 1
70 fromInteger i = W8# (narrow8Word# (integerToWord i))
71
72 instance Real Word8 where
73 toRational x = toInteger x % 1
74
75 instance Enum Word8 where
76 succ x
77 | x /= maxBound = x + 1
78 | otherwise = succError "Word8"
79 pred x
80 | x /= minBound = x - 1
81 | otherwise = predError "Word8"
82 toEnum i@(I# i#)
83 | i >= 0 && i <= fromIntegral (maxBound::Word8)
84 = W8# (int2Word# i#)
85 | otherwise = toEnumError "Word8" i (minBound::Word8, maxBound::Word8)
86 fromEnum (W8# x#) = I# (word2Int# x#)
87 enumFrom = boundedEnumFrom
88 enumFromThen = boundedEnumFromThen
89
90 instance Integral Word8 where
91 quot (W8# x#) y@(W8# y#)
92 | y /= 0 = W8# (x# `quotWord#` y#)
93 | otherwise = divZeroError
94 rem (W8# x#) y@(W8# y#)
95 | y /= 0 = W8# (x# `remWord#` y#)
96 | otherwise = divZeroError
97 div (W8# x#) y@(W8# y#)
98 | y /= 0 = W8# (x# `quotWord#` y#)
99 | otherwise = divZeroError
100 mod (W8# x#) y@(W8# y#)
101 | y /= 0 = W8# (x# `remWord#` y#)
102 | otherwise = divZeroError
103 quotRem (W8# x#) y@(W8# y#)
104 | y /= 0 = case x# `quotRemWord#` y# of
105 (# q, r #) ->
106 (W8# q, W8# r)
107 | otherwise = divZeroError
108 divMod (W8# x#) y@(W8# y#)
109 | y /= 0 = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
110 | otherwise = divZeroError
111 toInteger (W8# x#) = smallInteger (word2Int# x#)
112
113 instance Bounded Word8 where
114 minBound = 0
115 maxBound = 0xFF
116
117 instance Ix Word8 where
118 range (m,n) = [m..n]
119 unsafeIndex (m,_) i = fromIntegral (i - m)
120 inRange (m,n) i = m <= i && i <= n
121
122 instance Read Word8 where
123 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
124
125 instance Bits Word8 where
126 {-# INLINE shift #-}
127 {-# INLINE bit #-}
128 {-# INLINE testBit #-}
129
130 (W8# x#) .&. (W8# y#) = W8# (x# `and#` y#)
131 (W8# x#) .|. (W8# y#) = W8# (x# `or#` y#)
132 (W8# x#) `xor` (W8# y#) = W8# (x# `xor#` y#)
133 complement (W8# x#) = W8# (x# `xor#` mb#)
134 where !(W8# mb#) = maxBound
135 (W8# x#) `shift` (I# i#)
136 | isTrue# (i# >=# 0#) = W8# (narrow8Word# (x# `shiftL#` i#))
137 | otherwise = W8# (x# `shiftRL#` negateInt# i#)
138 (W8# x#) `shiftL` (I# i#) = W8# (narrow8Word# (x# `shiftL#` i#))
139 (W8# x#) `unsafeShiftL` (I# i#) =
140 W8# (narrow8Word# (x# `uncheckedShiftL#` i#))
141 (W8# x#) `shiftR` (I# i#) = W8# (x# `shiftRL#` i#)
142 (W8# x#) `unsafeShiftR` (I# i#) = W8# (x# `uncheckedShiftRL#` i#)
143 (W8# x#) `rotate` (I# i#)
144 | isTrue# (i'# ==# 0#) = W8# x#
145 | otherwise = W8# (narrow8Word# ((x# `uncheckedShiftL#` i'#) `or#`
146 (x# `uncheckedShiftRL#` (8# -# i'#))))
147 where
148 !i'# = word2Int# (int2Word# i# `and#` 7##)
149 bitSizeMaybe i = Just (finiteBitSize i)
150 bitSize i = finiteBitSize i
151 isSigned _ = False
152 popCount (W8# x#) = I# (word2Int# (popCnt8# x#))
153 bit = bitDefault
154 testBit = testBitDefault
155
156 instance FiniteBits Word8 where
157 finiteBitSize _ = 8
158
159 {-# RULES
160 "fromIntegral/Word8->Word8" fromIntegral = id :: Word8 -> Word8
161 "fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer
162 "fromIntegral/a->Word8" fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (narrow8Word# x#)
163 "fromIntegral/Word8->a" fromIntegral = \(W8# x#) -> fromIntegral (W# x#)
164 #-}
165
166 {-# RULES
167 "properFraction/Float->(Word8,Float)"
168 properFraction = \x ->
169 case properFraction x of {
170 (n, y) -> ((fromIntegral :: Int -> Word8) n, y :: Float) }
171 "truncate/Float->Word8"
172 truncate = (fromIntegral :: Int -> Word8) . (truncate :: Float -> Int)
173 "floor/Float->Word8"
174 floor = (fromIntegral :: Int -> Word8) . (floor :: Float -> Int)
175 "ceiling/Float->Word8"
176 ceiling = (fromIntegral :: Int -> Word8) . (ceiling :: Float -> Int)
177 "round/Float->Word8"
178 round = (fromIntegral :: Int -> Word8) . (round :: Float -> Int)
179 #-}
180
181 {-# RULES
182 "properFraction/Double->(Word8,Double)"
183 properFraction = \x ->
184 case properFraction x of {
185 (n, y) -> ((fromIntegral :: Int -> Word8) n, y :: Double) }
186 "truncate/Double->Word8"
187 truncate = (fromIntegral :: Int -> Word8) . (truncate :: Double -> Int)
188 "floor/Double->Word8"
189 floor = (fromIntegral :: Int -> Word8) . (floor :: Double -> Int)
190 "ceiling/Double->Word8"
191 ceiling = (fromIntegral :: Int -> Word8) . (ceiling :: Double -> Int)
192 "round/Double->Word8"
193 round = (fromIntegral :: Int -> Word8) . (round :: Double -> Int)
194 #-}
195
196 ------------------------------------------------------------------------
197 -- type Word16
198 ------------------------------------------------------------------------
199
200 -- Word16 is represented in the same way as Word. Operations may assume
201 -- and must ensure that it holds only values from its logical range.
202
203 data {-# CTYPE "HsWord16" #-} Word16 = W16# Word# deriving (Eq, Ord)
204 -- ^ 16-bit unsigned integer type
205
206 instance Show Word16 where
207 showsPrec p x = showsPrec p (fromIntegral x :: Int)
208
209 instance Num Word16 where
210 (W16# x#) + (W16# y#) = W16# (narrow16Word# (x# `plusWord#` y#))
211 (W16# x#) - (W16# y#) = W16# (narrow16Word# (x# `minusWord#` y#))
212 (W16# x#) * (W16# y#) = W16# (narrow16Word# (x# `timesWord#` y#))
213 negate (W16# x#) = W16# (narrow16Word# (int2Word# (negateInt# (word2Int# x#))))
214 abs x = x
215 signum 0 = 0
216 signum _ = 1
217 fromInteger i = W16# (narrow16Word# (integerToWord i))
218
219 instance Real Word16 where
220 toRational x = toInteger x % 1
221
222 instance Enum Word16 where
223 succ x
224 | x /= maxBound = x + 1
225 | otherwise = succError "Word16"
226 pred x
227 | x /= minBound = x - 1
228 | otherwise = predError "Word16"
229 toEnum i@(I# i#)
230 | i >= 0 && i <= fromIntegral (maxBound::Word16)
231 = W16# (int2Word# i#)
232 | otherwise = toEnumError "Word16" i (minBound::Word16, maxBound::Word16)
233 fromEnum (W16# x#) = I# (word2Int# x#)
234 enumFrom = boundedEnumFrom
235 enumFromThen = boundedEnumFromThen
236
237 instance Integral Word16 where
238 quot (W16# x#) y@(W16# y#)
239 | y /= 0 = W16# (x# `quotWord#` y#)
240 | otherwise = divZeroError
241 rem (W16# x#) y@(W16# y#)
242 | y /= 0 = W16# (x# `remWord#` y#)
243 | otherwise = divZeroError
244 div (W16# x#) y@(W16# y#)
245 | y /= 0 = W16# (x# `quotWord#` y#)
246 | otherwise = divZeroError
247 mod (W16# x#) y@(W16# y#)
248 | y /= 0 = W16# (x# `remWord#` y#)
249 | otherwise = divZeroError
250 quotRem (W16# x#) y@(W16# y#)
251 | y /= 0 = case x# `quotRemWord#` y# of
252 (# q, r #) ->
253 (W16# q, W16# r)
254 | otherwise = divZeroError
255 divMod (W16# x#) y@(W16# y#)
256 | y /= 0 = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
257 | otherwise = divZeroError
258 toInteger (W16# x#) = smallInteger (word2Int# x#)
259
260 instance Bounded Word16 where
261 minBound = 0
262 maxBound = 0xFFFF
263
264 instance Ix Word16 where
265 range (m,n) = [m..n]
266 unsafeIndex (m,_) i = fromIntegral (i - m)
267 inRange (m,n) i = m <= i && i <= n
268
269 instance Read Word16 where
270 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
271
272 instance Bits Word16 where
273 {-# INLINE shift #-}
274 {-# INLINE bit #-}
275 {-# INLINE testBit #-}
276
277 (W16# x#) .&. (W16# y#) = W16# (x# `and#` y#)
278 (W16# x#) .|. (W16# y#) = W16# (x# `or#` y#)
279 (W16# x#) `xor` (W16# y#) = W16# (x# `xor#` y#)
280 complement (W16# x#) = W16# (x# `xor#` mb#)
281 where !(W16# mb#) = maxBound
282 (W16# x#) `shift` (I# i#)
283 | isTrue# (i# >=# 0#) = W16# (narrow16Word# (x# `shiftL#` i#))
284 | otherwise = W16# (x# `shiftRL#` negateInt# i#)
285 (W16# x#) `shiftL` (I# i#) = W16# (narrow16Word# (x# `shiftL#` i#))
286 (W16# x#) `unsafeShiftL` (I# i#) =
287 W16# (narrow16Word# (x# `uncheckedShiftL#` i#))
288 (W16# x#) `shiftR` (I# i#) = W16# (x# `shiftRL#` i#)
289 (W16# x#) `unsafeShiftR` (I# i#) = W16# (x# `uncheckedShiftRL#` i#)
290 (W16# x#) `rotate` (I# i#)
291 | isTrue# (i'# ==# 0#) = W16# x#
292 | otherwise = W16# (narrow16Word# ((x# `uncheckedShiftL#` i'#) `or#`
293 (x# `uncheckedShiftRL#` (16# -# i'#))))
294 where
295 !i'# = word2Int# (int2Word# i# `and#` 15##)
296 bitSizeMaybe i = Just (finiteBitSize i)
297 bitSize i = finiteBitSize i
298 isSigned _ = False
299 popCount (W16# x#) = I# (word2Int# (popCnt16# x#))
300 bit = bitDefault
301 testBit = testBitDefault
302
303 instance FiniteBits Word16 where
304 finiteBitSize _ = 16
305
306 byteSwap16 :: Word16 -> Word16
307 byteSwap16 (W16# w#) = W16# (narrow16Word# (byteSwap16# w#))
308
309 {-# RULES
310 "fromIntegral/Word8->Word16" fromIntegral = \(W8# x#) -> W16# x#
311 "fromIntegral/Word16->Word16" fromIntegral = id :: Word16 -> Word16
312 "fromIntegral/Word16->Integer" fromIntegral = toInteger :: Word16 -> Integer
313 "fromIntegral/a->Word16" fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (narrow16Word# x#)
314 "fromIntegral/Word16->a" fromIntegral = \(W16# x#) -> fromIntegral (W# x#)
315 #-}
316
317 {-# RULES
318 "properFraction/Float->(Word16,Float)"
319 properFraction = \x ->
320 case properFraction x of {
321 (n, y) -> ((fromIntegral :: Int -> Word16) n, y :: Float) }
322 "truncate/Float->Word16"
323 truncate = (fromIntegral :: Int -> Word16) . (truncate :: Float -> Int)
324 "floor/Float->Word16"
325 floor = (fromIntegral :: Int -> Word16) . (floor :: Float -> Int)
326 "ceiling/Float->Word16"
327 ceiling = (fromIntegral :: Int -> Word16) . (ceiling :: Float -> Int)
328 "round/Float->Word16"
329 round = (fromIntegral :: Int -> Word16) . (round :: Float -> Int)
330 #-}
331
332 {-# RULES
333 "properFraction/Double->(Word16,Double)"
334 properFraction = \x ->
335 case properFraction x of {
336 (n, y) -> ((fromIntegral :: Int -> Word16) n, y :: Double) }
337 "truncate/Double->Word16"
338 truncate = (fromIntegral :: Int -> Word16) . (truncate :: Double -> Int)
339 "floor/Double->Word16"
340 floor = (fromIntegral :: Int -> Word16) . (floor :: Double -> Int)
341 "ceiling/Double->Word16"
342 ceiling = (fromIntegral :: Int -> Word16) . (ceiling :: Double -> Int)
343 "round/Double->Word16"
344 round = (fromIntegral :: Int -> Word16) . (round :: Double -> Int)
345 #-}
346
347 ------------------------------------------------------------------------
348 -- type Word32
349 ------------------------------------------------------------------------
350
351 -- Word32 is represented in the same way as Word.
352 #if WORD_SIZE_IN_BITS > 32
353 -- Operations may assume and must ensure that it holds only values
354 -- from its logical range.
355
356 -- We can use rewrite rules for the RealFrac methods
357
358 {-# RULES
359 "properFraction/Float->(Word32,Float)"
360 properFraction = \x ->
361 case properFraction x of {
362 (n, y) -> ((fromIntegral :: Int -> Word32) n, y :: Float) }
363 "truncate/Float->Word32"
364 truncate = (fromIntegral :: Int -> Word32) . (truncate :: Float -> Int)
365 "floor/Float->Word32"
366 floor = (fromIntegral :: Int -> Word32) . (floor :: Float -> Int)
367 "ceiling/Float->Word32"
368 ceiling = (fromIntegral :: Int -> Word32) . (ceiling :: Float -> Int)
369 "round/Float->Word32"
370 round = (fromIntegral :: Int -> Word32) . (round :: Float -> Int)
371 #-}
372
373 {-# RULES
374 "properFraction/Double->(Word32,Double)"
375 properFraction = \x ->
376 case properFraction x of {
377 (n, y) -> ((fromIntegral :: Int -> Word32) n, y :: Double) }
378 "truncate/Double->Word32"
379 truncate = (fromIntegral :: Int -> Word32) . (truncate :: Double -> Int)
380 "floor/Double->Word32"
381 floor = (fromIntegral :: Int -> Word32) . (floor :: Double -> Int)
382 "ceiling/Double->Word32"
383 ceiling = (fromIntegral :: Int -> Word32) . (ceiling :: Double -> Int)
384 "round/Double->Word32"
385 round = (fromIntegral :: Int -> Word32) . (round :: Double -> Int)
386 #-}
387
388 #endif
389
390 data {-# CTYPE "HsWord32" #-} Word32 = W32# Word# deriving (Eq, Ord)
391 -- ^ 32-bit unsigned integer type
392
393 instance Num Word32 where
394 (W32# x#) + (W32# y#) = W32# (narrow32Word# (x# `plusWord#` y#))
395 (W32# x#) - (W32# y#) = W32# (narrow32Word# (x# `minusWord#` y#))
396 (W32# x#) * (W32# y#) = W32# (narrow32Word# (x# `timesWord#` y#))
397 negate (W32# x#) = W32# (narrow32Word# (int2Word# (negateInt# (word2Int# x#))))
398 abs x = x
399 signum 0 = 0
400 signum _ = 1
401 fromInteger i = W32# (narrow32Word# (integerToWord i))
402
403 instance Enum Word32 where
404 succ x
405 | x /= maxBound = x + 1
406 | otherwise = succError "Word32"
407 pred x
408 | x /= minBound = x - 1
409 | otherwise = predError "Word32"
410 toEnum i@(I# i#)
411 | i >= 0
412 #if WORD_SIZE_IN_BITS > 32
413 && i <= fromIntegral (maxBound::Word32)
414 #endif
415 = W32# (int2Word# i#)
416 | otherwise = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
417 #if WORD_SIZE_IN_BITS == 32
418 fromEnum x@(W32# x#)
419 | x <= fromIntegral (maxBound::Int)
420 = I# (word2Int# x#)
421 | otherwise = fromEnumError "Word32" x
422 enumFrom = integralEnumFrom
423 enumFromThen = integralEnumFromThen
424 enumFromTo = integralEnumFromTo
425 enumFromThenTo = integralEnumFromThenTo
426 #else
427 fromEnum (W32# x#) = I# (word2Int# x#)
428 enumFrom = boundedEnumFrom
429 enumFromThen = boundedEnumFromThen
430 #endif
431
432 instance Integral Word32 where
433 quot (W32# x#) y@(W32# y#)
434 | y /= 0 = W32# (x# `quotWord#` y#)
435 | otherwise = divZeroError
436 rem (W32# x#) y@(W32# y#)
437 | y /= 0 = W32# (x# `remWord#` y#)
438 | otherwise = divZeroError
439 div (W32# x#) y@(W32# y#)
440 | y /= 0 = W32# (x# `quotWord#` y#)
441 | otherwise = divZeroError
442 mod (W32# x#) y@(W32# y#)
443 | y /= 0 = W32# (x# `remWord#` y#)
444 | otherwise = divZeroError
445 quotRem (W32# x#) y@(W32# y#)
446 | y /= 0 = case x# `quotRemWord#` y# of
447 (# q, r #) ->
448 (W32# q, W32# r)
449 | otherwise = divZeroError
450 divMod (W32# x#) y@(W32# y#)
451 | y /= 0 = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
452 | otherwise = divZeroError
453 toInteger (W32# x#)
454 #if WORD_SIZE_IN_BITS == 32
455 | isTrue# (i# >=# 0#) = smallInteger i#
456 | otherwise = wordToInteger x#
457 where
458 !i# = word2Int# x#
459 #else
460 = smallInteger (word2Int# x#)
461 #endif
462
463 instance Bits Word32 where
464 {-# INLINE shift #-}
465 {-# INLINE bit #-}
466 {-# INLINE testBit #-}
467
468 (W32# x#) .&. (W32# y#) = W32# (x# `and#` y#)
469 (W32# x#) .|. (W32# y#) = W32# (x# `or#` y#)
470 (W32# x#) `xor` (W32# y#) = W32# (x# `xor#` y#)
471 complement (W32# x#) = W32# (x# `xor#` mb#)
472 where !(W32# mb#) = maxBound
473 (W32# x#) `shift` (I# i#)
474 | isTrue# (i# >=# 0#) = W32# (narrow32Word# (x# `shiftL#` i#))
475 | otherwise = W32# (x# `shiftRL#` negateInt# i#)
476 (W32# x#) `shiftL` (I# i#) = W32# (narrow32Word# (x# `shiftL#` i#))
477 (W32# x#) `unsafeShiftL` (I# i#) =
478 W32# (narrow32Word# (x# `uncheckedShiftL#` i#))
479 (W32# x#) `shiftR` (I# i#) = W32# (x# `shiftRL#` i#)
480 (W32# x#) `unsafeShiftR` (I# i#) = W32# (x# `uncheckedShiftRL#` i#)
481 (W32# x#) `rotate` (I# i#)
482 | isTrue# (i'# ==# 0#) = W32# x#
483 | otherwise = W32# (narrow32Word# ((x# `uncheckedShiftL#` i'#) `or#`
484 (x# `uncheckedShiftRL#` (32# -# i'#))))
485 where
486 !i'# = word2Int# (int2Word# i# `and#` 31##)
487 bitSizeMaybe i = Just (finiteBitSize i)
488 bitSize i = finiteBitSize i
489 isSigned _ = False
490 popCount (W32# x#) = I# (word2Int# (popCnt32# x#))
491 bit = bitDefault
492 testBit = testBitDefault
493
494 instance FiniteBits Word32 where
495 finiteBitSize _ = 32
496
497 {-# RULES
498 "fromIntegral/Word8->Word32" fromIntegral = \(W8# x#) -> W32# x#
499 "fromIntegral/Word16->Word32" fromIntegral = \(W16# x#) -> W32# x#
500 "fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32
501 "fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer
502 "fromIntegral/a->Word32" fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (narrow32Word# x#)
503 "fromIntegral/Word32->a" fromIntegral = \(W32# x#) -> fromIntegral (W# x#)
504 #-}
505
506 instance Show Word32 where
507 #if WORD_SIZE_IN_BITS < 33
508 showsPrec p x = showsPrec p (toInteger x)
509 #else
510 showsPrec p x = showsPrec p (fromIntegral x :: Int)
511 #endif
512
513
514 instance Real Word32 where
515 toRational x = toInteger x % 1
516
517 instance Bounded Word32 where
518 minBound = 0
519 maxBound = 0xFFFFFFFF
520
521 instance Ix Word32 where
522 range (m,n) = [m..n]
523 unsafeIndex (m,_) i = fromIntegral (i - m)
524 inRange (m,n) i = m <= i && i <= n
525
526 instance Read Word32 where
527 #if WORD_SIZE_IN_BITS < 33
528 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
529 #else
530 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
531 #endif
532
533 byteSwap32 :: Word32 -> Word32
534 byteSwap32 (W32# w#) = W32# (narrow32Word# (byteSwap32# w#))
535
536 ------------------------------------------------------------------------
537 -- type Word64
538 ------------------------------------------------------------------------
539
540 #if WORD_SIZE_IN_BITS < 64
541
542 data {-# CTYPE "HsWord64" #-} Word64 = W64# Word64#
543 -- ^ 64-bit unsigned integer type
544
545 instance Eq Word64 where
546 (W64# x#) == (W64# y#) = isTrue# (x# `eqWord64#` y#)
547 (W64# x#) /= (W64# y#) = isTrue# (x# `neWord64#` y#)
548
549 instance Ord Word64 where
550 (W64# x#) < (W64# y#) = isTrue# (x# `ltWord64#` y#)
551 (W64# x#) <= (W64# y#) = isTrue# (x# `leWord64#` y#)
552 (W64# x#) > (W64# y#) = isTrue# (x# `gtWord64#` y#)
553 (W64# x#) >= (W64# y#) = isTrue# (x# `geWord64#` y#)
554
555 instance Num Word64 where
556 (W64# x#) + (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `plusInt64#` word64ToInt64# y#))
557 (W64# x#) - (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `minusInt64#` word64ToInt64# y#))
558 (W64# x#) * (W64# y#) = W64# (int64ToWord64# (word64ToInt64# x# `timesInt64#` word64ToInt64# y#))
559 negate (W64# x#) = W64# (int64ToWord64# (negateInt64# (word64ToInt64# x#)))
560 abs x = x
561 signum 0 = 0
562 signum _ = 1
563 fromInteger i = W64# (integerToWord64 i)
564
565 instance Enum Word64 where
566 succ x
567 | x /= maxBound = x + 1
568 | otherwise = succError "Word64"
569 pred x
570 | x /= minBound = x - 1
571 | otherwise = predError "Word64"
572 toEnum i@(I# i#)
573 | i >= 0 = W64# (wordToWord64# (int2Word# i#))
574 | otherwise = toEnumError "Word64" i (minBound::Word64, maxBound::Word64)
575 fromEnum x@(W64# x#)
576 | x <= fromIntegral (maxBound::Int)
577 = I# (word2Int# (word64ToWord# x#))
578 | otherwise = fromEnumError "Word64" x
579 enumFrom = integralEnumFrom
580 enumFromThen = integralEnumFromThen
581 enumFromTo = integralEnumFromTo
582 enumFromThenTo = integralEnumFromThenTo
583
584 instance Integral Word64 where
585 quot (W64# x#) y@(W64# y#)
586 | y /= 0 = W64# (x# `quotWord64#` y#)
587 | otherwise = divZeroError
588 rem (W64# x#) y@(W64# y#)
589 | y /= 0 = W64# (x# `remWord64#` y#)
590 | otherwise = divZeroError
591 div (W64# x#) y@(W64# y#)
592 | y /= 0 = W64# (x# `quotWord64#` y#)
593 | otherwise = divZeroError
594 mod (W64# x#) y@(W64# y#)
595 | y /= 0 = W64# (x# `remWord64#` y#)
596 | otherwise = divZeroError
597 quotRem (W64# x#) y@(W64# y#)
598 | y /= 0 = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
599 | otherwise = divZeroError
600 divMod (W64# x#) y@(W64# y#)
601 | y /= 0 = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
602 | otherwise = divZeroError
603 toInteger (W64# x#) = word64ToInteger x#
604
605 instance Bits Word64 where
606 {-# INLINE shift #-}
607 {-# INLINE bit #-}
608 {-# INLINE testBit #-}
609
610 (W64# x#) .&. (W64# y#) = W64# (x# `and64#` y#)
611 (W64# x#) .|. (W64# y#) = W64# (x# `or64#` y#)
612 (W64# x#) `xor` (W64# y#) = W64# (x# `xor64#` y#)
613 complement (W64# x#) = W64# (not64# x#)
614 (W64# x#) `shift` (I# i#)
615 | isTrue# (i# >=# 0#) = W64# (x# `shiftL64#` i#)
616 | otherwise = W64# (x# `shiftRL64#` negateInt# i#)
617 (W64# x#) `shiftL` (I# i#) = W64# (x# `shiftL64#` i#)
618 (W64# x#) `unsafeShiftL` (I# i#) = W64# (x# `uncheckedShiftL64#` i#)
619 (W64# x#) `shiftR` (I# i#) = W64# (x# `shiftRL64#` i#)
620 (W64# x#) `unsafeShiftR` (I# i#) = W64# (x# `uncheckedShiftRL64#` i#)
621 (W64# x#) `rotate` (I# i#)
622 | isTrue# (i'# ==# 0#) = W64# x#
623 | otherwise = W64# ((x# `uncheckedShiftL64#` i'#) `or64#`
624 (x# `uncheckedShiftRL64#` (64# -# i'#)))
625 where
626 !i'# = word2Int# (int2Word# i# `and#` 63##)
627 bitSizeMaybe i = Just (finiteBitSize i)
628 bitSize i = finiteBitSize i
629 isSigned _ = False
630 popCount (W64# x#) = I# (word2Int# (popCnt64# x#))
631 bit = bitDefault
632 testBit = testBitDefault
633
634 -- give the 64-bit shift operations the same treatment as the 32-bit
635 -- ones (see GHC.Base), namely we wrap them in tests to catch the
636 -- cases when we're shifting more than 64 bits to avoid unspecified
637 -- behaviour in the C shift operations.
638
639 shiftL64#, shiftRL64# :: Word64# -> Int# -> Word64#
640
641 a `shiftL64#` b | isTrue# (b >=# 64#) = wordToWord64# 0##
642 | otherwise = a `uncheckedShiftL64#` b
643
644 a `shiftRL64#` b | isTrue# (b >=# 64#) = wordToWord64# 0##
645 | otherwise = a `uncheckedShiftRL64#` b
646
647 {-# RULES
648 "fromIntegral/Int->Word64" fromIntegral = \(I# x#) -> W64# (int64ToWord64# (intToInt64# x#))
649 "fromIntegral/Word->Word64" fromIntegral = \(W# x#) -> W64# (wordToWord64# x#)
650 "fromIntegral/Word64->Int" fromIntegral = \(W64# x#) -> I# (word2Int# (word64ToWord# x#))
651 "fromIntegral/Word64->Word" fromIntegral = \(W64# x#) -> W# (word64ToWord# x#)
652 "fromIntegral/Word64->Word64" fromIntegral = id :: Word64 -> Word64
653 #-}
654
655 #else
656
657 -- Word64 is represented in the same way as Word.
658 -- Operations may assume and must ensure that it holds only values
659 -- from its logical range.
660
661 data {-# CTYPE "HsWord64" #-} Word64 = W64# Word# deriving (Eq, Ord)
662 -- ^ 64-bit unsigned integer type
663
664 instance Num Word64 where
665 (W64# x#) + (W64# y#) = W64# (x# `plusWord#` y#)
666 (W64# x#) - (W64# y#) = W64# (x# `minusWord#` y#)
667 (W64# x#) * (W64# y#) = W64# (x# `timesWord#` y#)
668 negate (W64# x#) = W64# (int2Word# (negateInt# (word2Int# x#)))
669 abs x = x
670 signum 0 = 0
671 signum _ = 1
672 fromInteger i = W64# (integerToWord i)
673
674 instance Enum Word64 where
675 succ x
676 | x /= maxBound = x + 1
677 | otherwise = succError "Word64"
678 pred x
679 | x /= minBound = x - 1
680 | otherwise = predError "Word64"
681 toEnum i@(I# i#)
682 | i >= 0 = W64# (int2Word# i#)
683 | otherwise = toEnumError "Word64" i (minBound::Word64, maxBound::Word64)
684 fromEnum x@(W64# x#)
685 | x <= fromIntegral (maxBound::Int)
686 = I# (word2Int# x#)
687 | otherwise = fromEnumError "Word64" x
688 enumFrom = integralEnumFrom
689 enumFromThen = integralEnumFromThen
690 enumFromTo = integralEnumFromTo
691 enumFromThenTo = integralEnumFromThenTo
692
693 instance Integral Word64 where
694 quot (W64# x#) y@(W64# y#)
695 | y /= 0 = W64# (x# `quotWord#` y#)
696 | otherwise = divZeroError
697 rem (W64# x#) y@(W64# y#)
698 | y /= 0 = W64# (x# `remWord#` y#)
699 | otherwise = divZeroError
700 div (W64# x#) y@(W64# y#)
701 | y /= 0 = W64# (x# `quotWord#` y#)
702 | otherwise = divZeroError
703 mod (W64# x#) y@(W64# y#)
704 | y /= 0 = W64# (x# `remWord#` y#)
705 | otherwise = divZeroError
706 quotRem (W64# x#) y@(W64# y#)
707 | y /= 0 = case x# `quotRemWord#` y# of
708 (# q, r #) ->
709 (W64# q, W64# r)
710 | otherwise = divZeroError
711 divMod (W64# x#) y@(W64# y#)
712 | y /= 0 = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
713 | otherwise = divZeroError
714 toInteger (W64# x#)
715 | isTrue# (i# >=# 0#) = smallInteger i#
716 | otherwise = wordToInteger x#
717 where
718 !i# = word2Int# x#
719
720 instance Bits Word64 where
721 {-# INLINE shift #-}
722 {-# INLINE bit #-}
723 {-# INLINE testBit #-}
724
725 (W64# x#) .&. (W64# y#) = W64# (x# `and#` y#)
726 (W64# x#) .|. (W64# y#) = W64# (x# `or#` y#)
727 (W64# x#) `xor` (W64# y#) = W64# (x# `xor#` y#)
728 complement (W64# x#) = W64# (x# `xor#` mb#)
729 where !(W64# mb#) = maxBound
730 (W64# x#) `shift` (I# i#)
731 | isTrue# (i# >=# 0#) = W64# (x# `shiftL#` i#)
732 | otherwise = W64# (x# `shiftRL#` negateInt# i#)
733 (W64# x#) `shiftL` (I# i#) = W64# (x# `shiftL#` i#)
734 (W64# x#) `unsafeShiftL` (I# i#) = W64# (x# `uncheckedShiftL#` i#)
735 (W64# x#) `shiftR` (I# i#) = W64# (x# `shiftRL#` i#)
736 (W64# x#) `unsafeShiftR` (I# i#) = W64# (x# `uncheckedShiftRL#` i#)
737 (W64# x#) `rotate` (I# i#)
738 | isTrue# (i'# ==# 0#) = W64# x#
739 | otherwise = W64# ((x# `uncheckedShiftL#` i'#) `or#`
740 (x# `uncheckedShiftRL#` (64# -# i'#)))
741 where
742 !i'# = word2Int# (int2Word# i# `and#` 63##)
743 bitSizeMaybe i = Just (finiteBitSize i)
744 bitSize i = finiteBitSize i
745 isSigned _ = False
746 popCount (W64# x#) = I# (word2Int# (popCnt64# x#))
747 bit = bitDefault
748 testBit = testBitDefault
749
750 {-# RULES
751 "fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x#
752 "fromIntegral/Word64->a" fromIntegral = \(W64# x#) -> fromIntegral (W# x#)
753 #-}
754
755 uncheckedShiftL64# :: Word# -> Int# -> Word#
756 uncheckedShiftL64# = uncheckedShiftL#
757
758 uncheckedShiftRL64# :: Word# -> Int# -> Word#
759 uncheckedShiftRL64# = uncheckedShiftRL#
760
761 #endif
762
763 instance FiniteBits Word64 where
764 finiteBitSize _ = 64
765
766 instance Show Word64 where
767 showsPrec p x = showsPrec p (toInteger x)
768
769 instance Real Word64 where
770 toRational x = toInteger x % 1
771
772 instance Bounded Word64 where
773 minBound = 0
774 maxBound = 0xFFFFFFFFFFFFFFFF
775
776 instance Ix Word64 where
777 range (m,n) = [m..n]
778 unsafeIndex (m,_) i = fromIntegral (i - m)
779 inRange (m,n) i = m <= i && i <= n
780
781 instance Read Word64 where
782 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
783
784 #if WORD_SIZE_IN_BITS < 64
785 byteSwap64 :: Word64 -> Word64
786 byteSwap64 (W64# w#) = W64# (byteSwap64# w#)
787 #else
788 byteSwap64 :: Word64 -> Word64
789 byteSwap64 (W64# w#) = W64# (byteSwap# w#)
790 #endif