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