[project @ 2002-09-18 11:32:43 by simonmar]
[ghc-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     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 CCallable Word
73 instance CReturnable Word
74
75 instance Show Word where
76     showsPrec p x = showsPrec p (toInteger x)
77
78 instance Num Word where
79     (W# x#) + (W# y#)      = W# (x# `plusWord#` y#)
80     (W# x#) - (W# y#)      = W# (x# `minusWord#` y#)
81     (W# x#) * (W# y#)      = W# (x# `timesWord#` y#)
82     negate (W# x#)         = W# (int2Word# (negateInt# (word2Int# x#)))
83     abs x                  = x
84     signum 0               = 0
85     signum _               = 1
86     fromInteger (S# i#)    = W# (int2Word# i#)
87     fromInteger (J# s# d#) = W# (integer2Word# s# d#)
88
89 instance Real Word where
90     toRational x = toInteger x % 1
91
92 instance Enum Word where
93     succ x
94         | x /= maxBound = x + 1
95         | otherwise     = succError "Word"
96     pred x
97         | x /= minBound = x - 1
98         | otherwise     = predError "Word"
99     toEnum i@(I# i#)
100         | i >= 0        = W# (int2Word# i#)
101         | otherwise     = toEnumError "Word" i (minBound::Word, maxBound::Word)
102     fromEnum x@(W# x#)
103         | x <= fromIntegral (maxBound::Int)
104                         = I# (word2Int# x#)
105         | otherwise     = fromEnumError "Word" x
106     enumFrom            = integralEnumFrom
107     enumFromThen        = integralEnumFromThen
108     enumFromTo          = integralEnumFromTo
109     enumFromThenTo      = integralEnumFromThenTo
110
111 instance Integral Word where
112     quot    x@(W# x#) y@(W# y#)
113         | y /= 0                = W# (x# `quotWord#` y#)
114         | otherwise             = divZeroError
115     rem     x@(W# x#) y@(W# y#)
116         | y /= 0                = W# (x# `remWord#` y#)
117         | otherwise             = divZeroError
118     div     x@(W# x#) y@(W# y#)
119         | y /= 0                = W# (x# `quotWord#` y#)
120         | otherwise             = divZeroError
121     mod     x@(W# x#) y@(W# y#)
122         | y /= 0                = W# (x# `remWord#` y#)
123         | otherwise             = divZeroError
124     quotRem x@(W# x#) y@(W# y#)
125         | y /= 0                = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
126         | otherwise             = divZeroError
127     divMod  x@(W# x#) y@(W# y#)
128         | y /= 0                = (W# (x# `quotWord#` y#), W# (x# `remWord#` y#))
129         | otherwise             = divZeroError
130     toInteger (W# x#)
131         | i# >=# 0#             = S# i#
132         | otherwise             = case word2Integer# x# of (# s, d #) -> J# s d
133         where
134         i# = word2Int# x#
135
136 instance Bounded Word where
137     minBound = 0
138 #if WORD_SIZE_IN_BITS == 31
139     maxBound = 0x7FFFFFFF
140 #elif WORD_SIZE_IN_BITS == 32
141     maxBound = 0xFFFFFFFF
142 #else
143     maxBound = 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 CCallable Word8
189 instance CReturnable Word8
190
191 instance Show Word8 where
192     showsPrec p x = showsPrec p (fromIntegral x :: Int)
193
194 instance Num Word8 where
195     (W8# x#) + (W8# y#)    = W8# (narrow8Word# (x# `plusWord#` y#))
196     (W8# x#) - (W8# y#)    = W8# (narrow8Word# (x# `minusWord#` y#))
197     (W8# x#) * (W8# y#)    = W8# (narrow8Word# (x# `timesWord#` y#))
198     negate (W8# x#)        = W8# (narrow8Word# (int2Word# (negateInt# (word2Int# x#))))
199     abs x                  = x
200     signum 0               = 0
201     signum _               = 1
202     fromInteger (S# i#)    = W8# (narrow8Word# (int2Word# i#))
203     fromInteger (J# s# d#) = W8# (narrow8Word# (integer2Word# s# d#))
204
205 instance Real Word8 where
206     toRational x = toInteger x % 1
207
208 instance Enum Word8 where
209     succ x
210         | x /= maxBound = x + 1
211         | otherwise     = succError "Word8"
212     pred x
213         | x /= minBound = x - 1
214         | otherwise     = predError "Word8"
215     toEnum i@(I# i#)
216         | i >= 0 && i <= fromIntegral (maxBound::Word8)
217                         = W8# (int2Word# i#)
218         | otherwise     = toEnumError "Word8" i (minBound::Word8, maxBound::Word8)
219     fromEnum (W8# x#)   = I# (word2Int# x#)
220     enumFrom            = boundedEnumFrom
221     enumFromThen        = boundedEnumFromThen
222
223 instance Integral Word8 where
224     quot    x@(W8# x#) y@(W8# y#)
225         | y /= 0                  = W8# (x# `quotWord#` y#)
226         | otherwise               = divZeroError
227     rem     x@(W8# x#) y@(W8# y#)
228         | y /= 0                  = W8# (x# `remWord#` y#)
229         | otherwise               = divZeroError
230     div     x@(W8# x#) y@(W8# y#)
231         | y /= 0                  = W8# (x# `quotWord#` y#)
232         | otherwise               = divZeroError
233     mod     x@(W8# x#) y@(W8# y#)
234         | y /= 0                  = W8# (x# `remWord#` y#)
235         | otherwise               = divZeroError
236     quotRem x@(W8# x#) y@(W8# y#)
237         | y /= 0                  = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
238         | otherwise               = divZeroError
239     divMod  x@(W8# x#) y@(W8# y#)
240         | y /= 0                  = (W8# (x# `quotWord#` y#), W8# (x# `remWord#` y#))
241         | otherwise               = divZeroError
242     toInteger (W8# x#)            = S# (word2Int# x#)
243
244 instance Bounded Word8 where
245     minBound = 0
246     maxBound = 0xFF
247
248 instance Ix Word8 where
249     range (m,n)              = [m..n]
250     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
251     inRange (m,n) i          = m <= i && i <= n
252     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
253
254 instance Read Word8 where
255     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
256
257 instance Bits Word8 where
258     (W8# x#) .&.   (W8# y#)   = W8# (x# `and#` y#)
259     (W8# x#) .|.   (W8# y#)   = W8# (x# `or#`  y#)
260     (W8# x#) `xor` (W8# y#)   = W8# (x# `xor#` y#)
261     complement (W8# x#)       = W8# (x# `xor#` mb#) where W8# mb# = maxBound
262     (W8# x#) `shift` (I# i#)
263         | i# >=# 0#           = W8# (narrow8Word# (x# `shiftL#` i#))
264         | otherwise           = W8# (x# `shiftRL#` negateInt# i#)
265     (W8# x#) `rotate` (I# i#)
266         | i'# ==# 0# = W8# x#
267         | otherwise  = W8# (narrow8Word# ((x# `shiftL#` i'#) `or#`
268                                           (x# `shiftRL#` (8# -# i'#))))
269         where
270         i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
271     bitSize  _                = 8
272     isSigned _                = False
273
274 {-# RULES
275 "fromIntegral/Word8->Word8"   fromIntegral = id :: Word8 -> Word8
276 "fromIntegral/Word8->Integer" fromIntegral = toInteger :: Word8 -> Integer
277 "fromIntegral/a->Word8"       fromIntegral = \x -> case fromIntegral x of W# x# -> W8# (narrow8Word# x#)
278 "fromIntegral/Word8->a"       fromIntegral = \(W8# x#) -> fromIntegral (W# x#)
279   #-}
280
281 ------------------------------------------------------------------------
282 -- type Word16
283 ------------------------------------------------------------------------
284
285 -- Word16 is represented in the same way as Word. Operations may assume
286 -- and must ensure that it holds only values from its logical range.
287
288 data Word16 = W16# Word# deriving (Eq, Ord)
289 -- ^ 16-bit unsigned integer type
290
291 instance CCallable Word16
292 instance CReturnable Word16
293
294 instance Show Word16 where
295     showsPrec p x = showsPrec p (fromIntegral x :: Int)
296
297 instance Num Word16 where
298     (W16# x#) + (W16# y#)  = W16# (narrow16Word# (x# `plusWord#` y#))
299     (W16# x#) - (W16# y#)  = W16# (narrow16Word# (x# `minusWord#` y#))
300     (W16# x#) * (W16# y#)  = W16# (narrow16Word# (x# `timesWord#` y#))
301     negate (W16# x#)       = W16# (narrow16Word# (int2Word# (negateInt# (word2Int# x#))))
302     abs x                  = x
303     signum 0               = 0
304     signum _               = 1
305     fromInteger (S# i#)    = W16# (narrow16Word# (int2Word# i#))
306     fromInteger (J# s# d#) = W16# (narrow16Word# (integer2Word# s# d#))
307
308 instance Real Word16 where
309     toRational x = toInteger x % 1
310
311 instance Enum Word16 where
312     succ x
313         | x /= maxBound = x + 1
314         | otherwise     = succError "Word16"
315     pred x
316         | x /= minBound = x - 1
317         | otherwise     = predError "Word16"
318     toEnum i@(I# i#)
319         | i >= 0 && i <= fromIntegral (maxBound::Word16)
320                         = W16# (int2Word# i#)
321         | otherwise     = toEnumError "Word16" i (minBound::Word16, maxBound::Word16)
322     fromEnum (W16# x#)  = I# (word2Int# x#)
323     enumFrom            = boundedEnumFrom
324     enumFromThen        = boundedEnumFromThen
325
326 instance Integral Word16 where
327     quot    x@(W16# x#) y@(W16# y#)
328         | y /= 0                    = W16# (x# `quotWord#` y#)
329         | otherwise                 = divZeroError
330     rem     x@(W16# x#) y@(W16# y#)
331         | y /= 0                    = W16# (x# `remWord#` y#)
332         | otherwise                 = divZeroError
333     div     x@(W16# x#) y@(W16# y#)
334         | y /= 0                    = W16# (x# `quotWord#` y#)
335         | otherwise                 = divZeroError
336     mod     x@(W16# x#) y@(W16# y#)
337         | y /= 0                    = W16# (x# `remWord#` y#)
338         | otherwise                 = divZeroError
339     quotRem x@(W16# x#) y@(W16# y#)
340         | y /= 0                    = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
341         | otherwise                 = divZeroError
342     divMod  x@(W16# x#) y@(W16# y#)
343         | y /= 0                    = (W16# (x# `quotWord#` y#), W16# (x# `remWord#` y#))
344         | otherwise                 = divZeroError
345     toInteger (W16# x#)             = S# (word2Int# x#)
346
347 instance Bounded Word16 where
348     minBound = 0
349     maxBound = 0xFFFF
350
351 instance Ix Word16 where
352     range (m,n)              = [m..n]
353     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
354     inRange (m,n) i          = m <= i && i <= n
355     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
356
357 instance Read Word16 where
358     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
359
360 instance Bits Word16 where
361     (W16# x#) .&.   (W16# y#)  = W16# (x# `and#` y#)
362     (W16# x#) .|.   (W16# y#)  = W16# (x# `or#`  y#)
363     (W16# x#) `xor` (W16# y#)  = W16# (x# `xor#` y#)
364     complement (W16# x#)       = W16# (x# `xor#` mb#) where W16# mb# = maxBound
365     (W16# x#) `shift` (I# i#)
366         | i# >=# 0#            = W16# (narrow16Word# (x# `shiftL#` i#))
367         | otherwise            = W16# (x# `shiftRL#` negateInt# i#)
368     (W16# x#) `rotate` (I# i#)
369         | i'# ==# 0# = W16# x#
370         | otherwise  = W16# (narrow16Word# ((x# `shiftL#` i'#) `or#`
371                                             (x# `shiftRL#` (16# -# i'#))))
372         where
373         i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
374     bitSize  _                = 16
375     isSigned _                = False
376
377 {-# RULES
378 "fromIntegral/Word8->Word16"   fromIntegral = \(W8# x#) -> W16# x#
379 "fromIntegral/Word16->Word16"  fromIntegral = id :: Word16 -> Word16
380 "fromIntegral/Word16->Integer" fromIntegral = toInteger :: Word16 -> Integer
381 "fromIntegral/a->Word16"       fromIntegral = \x -> case fromIntegral x of W# x# -> W16# (narrow16Word# x#)
382 "fromIntegral/Word16->a"       fromIntegral = \(W16# x#) -> fromIntegral (W# x#)
383   #-}
384
385 ------------------------------------------------------------------------
386 -- type Word32
387 ------------------------------------------------------------------------
388
389 #if WORD_SIZE_IN_BITS < 32
390
391 data Word32 = W32# Word32#
392 -- ^ 32-bit unsigned integer type
393
394 instance Eq Word32 where
395     (W32# x#) == (W32# y#) = x# `eqWord32#` y#
396     (W32# x#) /= (W32# y#) = x# `neWord32#` y#
397
398 instance Ord Word32 where
399     (W32# x#) <  (W32# y#) = x# `ltWord32#` y#
400     (W32# x#) <= (W32# y#) = x# `leWord32#` y#
401     (W32# x#) >  (W32# y#) = x# `gtWord32#` y#
402     (W32# x#) >= (W32# y#) = x# `geWord32#` y#
403
404 instance Num Word32 where
405     (W32# x#) + (W32# y#)  = W32# (int32ToWord32# (word32ToInt32# x# `plusInt32#` word32ToInt32# y#))
406     (W32# x#) - (W32# y#)  = W32# (int32ToWord32# (word32ToInt32# x# `minusInt32#` word32ToInt32# y#))
407     (W32# x#) * (W32# y#)  = W32# (int32ToWord32# (word32ToInt32# x# `timesInt32#` word32ToInt32# y#))
408     negate (W32# x#)       = W32# (int32ToWord32# (negateInt32# (word32ToInt32# x#)))
409     abs x                  = x
410     signum 0               = 0
411     signum _               = 1
412     fromInteger (S# i#)    = W32# (int32ToWord32# (intToInt32# i#))
413     fromInteger (J# s# d#) = W32# (integerToWord32# s# d#)
414
415 instance Enum Word32 where
416     succ x
417         | x /= maxBound = x + 1
418         | otherwise     = succError "Word32"
419     pred x
420         | x /= minBound = x - 1
421         | otherwise     = predError "Word32"
422     toEnum i@(I# i#)
423         | i >= 0        = W32# (wordToWord32# (int2Word# i#))
424         | otherwise     = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
425     fromEnum x@(W32# x#)
426         | x <= fromIntegral (maxBound::Int)
427                         = I# (word2Int# (word32ToWord# x#))
428         | otherwise     = fromEnumError "Word32" x
429     enumFrom            = integralEnumFrom
430     enumFromThen        = integralEnumFromThen
431     enumFromTo          = integralEnumFromTo
432     enumFromThenTo      = integralEnumFromThenTo
433
434 instance Integral Word32 where
435     quot    x@(W32# x#) y@(W32# y#)
436         | y /= 0                    = W32# (x# `quotWord32#` y#)
437         | otherwise                 = divZeroError
438     rem     x@(W32# x#) y@(W32# y#)
439         | y /= 0                    = W32# (x# `remWord32#` y#)
440         | otherwise                 = divZeroError
441     div     x@(W32# x#) y@(W32# y#)
442         | y /= 0                    = W32# (x# `quotWord32#` y#)
443         | otherwise                 = divZeroError
444     mod     x@(W32# x#) y@(W32# y#)
445         | y /= 0                    = W32# (x# `remWord32#` y#)
446         | otherwise                 = divZeroError
447     quotRem x@(W32# x#) y@(W32# y#)
448         | y /= 0                    = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#))
449         | otherwise                 = divZeroError
450     divMod  x@(W32# x#) y@(W32# y#)
451         | y /= 0                    = (W32# (x# `quotWord32#` y#), W32# (x# `remWord32#` y#))
452         | otherwise                 = divZeroError
453     toInteger x@(W32# x#)
454         | x <= fromIntegral (maxBound::Int)  = S# (word2Int# (word32ToWord# x#))
455         | otherwise                 = case word32ToInteger# x# of (# s, d #) -> J# s d
456
457 instance Bits Word32 where
458     (W32# x#) .&.   (W32# y#)  = W32# (x# `and32#` y#)
459     (W32# x#) .|.   (W32# y#)  = W32# (x# `or32#`  y#)
460     (W32# x#) `xor` (W32# y#)  = W32# (x# `xor32#` y#)
461     complement (W32# x#)       = W32# (not32# x#)
462     (W32# x#) `shift` (I# i#)
463         | i# >=# 0#            = W32# (x# `shiftL32#` i#)
464         | otherwise            = W32# (x# `shiftRL32#` negateInt# i#)
465     (W32# x#) `rotate` (I# i#)
466         | i'# ==# 0# = W32# x#
467         | otherwise  = W32# ((x# `shiftL32#` i'#) `or32#`
468                              (x# `shiftRL32#` (32# -# i'#)))
469         where
470         i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
471     bitSize  _                = 32
472     isSigned _                = False
473
474 foreign import unsafe "stg_eqWord32"      eqWord32#      :: Word32# -> Word32# -> Bool
475 foreign import unsafe "stg_neWord32"      neWord32#      :: Word32# -> Word32# -> Bool
476 foreign import unsafe "stg_ltWord32"      ltWord32#      :: Word32# -> Word32# -> Bool
477 foreign import unsafe "stg_leWord32"      leWord32#      :: Word32# -> Word32# -> Bool
478 foreign import unsafe "stg_gtWord32"      gtWord32#      :: Word32# -> Word32# -> Bool
479 foreign import unsafe "stg_geWord32"      geWord32#      :: Word32# -> Word32# -> Bool
480 foreign import unsafe "stg_int32ToWord32" int32ToWord32# :: Int32# -> Word32#
481 foreign import unsafe "stg_word32ToInt32" word32ToInt32# :: Word32# -> Int32#
482 foreign import unsafe "stg_intToInt32"    intToInt32#    :: Int# -> Int32#
483 foreign import unsafe "stg_wordToWord32"  wordToWord32#  :: Word# -> Word32#
484 foreign import unsafe "stg_word32ToWord"  word32ToWord#  :: Word32# -> Word#
485 foreign import unsafe "stg_plusInt32"     plusInt32#     :: Int32# -> Int32# -> Int32#
486 foreign import unsafe "stg_minusInt32"    minusInt32#    :: Int32# -> Int32# -> Int32#
487 foreign import unsafe "stg_timesInt32"    timesInt32#    :: Int32# -> Int32# -> Int32#
488 foreign import unsafe "stg_negateInt32"   negateInt32#   :: Int32# -> Int32#
489 foreign import unsafe "stg_quotWord32"    quotWord32#    :: Word32# -> Word32# -> Word32#
490 foreign import unsafe "stg_remWord32"     remWord32#     :: Word32# -> Word32# -> Word32#
491 foreign import unsafe "stg_and32"         and32#         :: Word32# -> Word32# -> Word32#
492 foreign import unsafe "stg_or32"          or32#          :: Word32# -> Word32# -> Word32#
493 foreign import unsafe "stg_xor32"         xor32#         :: Word32# -> Word32# -> Word32#
494 foreign import unsafe "stg_not32"         not32#         :: Word32# -> Word32#
495 foreign import unsafe "stg_shiftL32"      shiftL32#      :: Word32# -> Int# -> Word32#
496 foreign import unsafe "stg_shiftRL32"     shiftRL32#     :: Word32# -> Int# -> Word32#
497
498 {-# RULES
499 "fromIntegral/Int->Word32"    fromIntegral = \(I#   x#) -> W32# (int32ToWord32# (intToInt32# x#))
500 "fromIntegral/Word->Word32"   fromIntegral = \(W#   x#) -> W32# (wordToWord32# x#)
501 "fromIntegral/Word32->Int"    fromIntegral = \(W32# x#) -> I#   (word2Int# (word32ToWord# x#))
502 "fromIntegral/Word32->Word"   fromIntegral = \(W32# x#) -> W#   (word32ToWord# x#)
503 "fromIntegral/Word32->Word32" fromIntegral = id :: Word32 -> Word32
504   #-}
505
506 #else 
507
508 -- Word32 is represented in the same way as Word.
509 #if WORD_SIZE_IN_BITS > 32
510 -- Operations may assume and must ensure that it holds only values
511 -- from its logical range.
512 #endif
513
514 data Word32 = W32# Word# deriving (Eq, Ord)
515 -- ^ 32-bit unsigned integer type
516
517 instance Num Word32 where
518     (W32# x#) + (W32# y#)  = W32# (narrow32Word# (x# `plusWord#` y#))
519     (W32# x#) - (W32# y#)  = W32# (narrow32Word# (x# `minusWord#` y#))
520     (W32# x#) * (W32# y#)  = W32# (narrow32Word# (x# `timesWord#` y#))
521     negate (W32# x#)       = W32# (narrow32Word# (int2Word# (negateInt# (word2Int# x#))))
522     abs x                  = x
523     signum 0               = 0
524     signum _               = 1
525     fromInteger (S# i#)    = W32# (narrow32Word# (int2Word# i#))
526     fromInteger (J# s# d#) = W32# (narrow32Word# (integer2Word# s# d#))
527
528 instance Enum Word32 where
529     succ x
530         | x /= maxBound = x + 1
531         | otherwise     = succError "Word32"
532     pred x
533         | x /= minBound = x - 1
534         | otherwise     = predError "Word32"
535     toEnum i@(I# i#)
536         | i >= 0
537 #if WORD_SIZE_IN_BITS > 32
538           && i <= fromIntegral (maxBound::Word32)
539 #endif
540                         = W32# (int2Word# i#)
541         | otherwise     = toEnumError "Word32" i (minBound::Word32, maxBound::Word32)
542 #if WORD_SIZE_IN_BITS == 32
543     fromEnum x@(W32# x#)
544         | x <= fromIntegral (maxBound::Int)
545                         = I# (word2Int# x#)
546         | otherwise     = fromEnumError "Word32" x
547     enumFrom            = integralEnumFrom
548     enumFromThen        = integralEnumFromThen
549     enumFromTo          = integralEnumFromTo
550     enumFromThenTo      = integralEnumFromThenTo
551 #else
552     fromEnum (W32# x#)  = I# (word2Int# x#)
553     enumFrom            = boundedEnumFrom
554     enumFromThen        = boundedEnumFromThen
555 #endif
556
557 instance Integral Word32 where
558     quot    x@(W32# x#) y@(W32# y#)
559         | y /= 0                    = W32# (x# `quotWord#` y#)
560         | otherwise                 = divZeroError
561     rem     x@(W32# x#) y@(W32# y#)
562         | y /= 0                    = W32# (x# `remWord#` y#)
563         | otherwise                 = divZeroError
564     div     x@(W32# x#) y@(W32# y#)
565         | y /= 0                    = W32# (x# `quotWord#` y#)
566         | otherwise                 = divZeroError
567     mod     x@(W32# x#) y@(W32# y#)
568         | y /= 0                    = W32# (x# `remWord#` y#)
569         | otherwise                 = divZeroError
570     quotRem x@(W32# x#) y@(W32# y#)
571         | y /= 0                    = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
572         | otherwise                 = divZeroError
573     divMod  x@(W32# x#) y@(W32# y#)
574         | y /= 0                    = (W32# (x# `quotWord#` y#), W32# (x# `remWord#` y#))
575         | otherwise                 = divZeroError
576     toInteger (W32# x#)
577 #if WORD_SIZE_IN_BITS == 32
578         | i# >=# 0#                 = S# i#
579         | otherwise                 = case word2Integer# x# of (# s, d #) -> J# s d
580         where
581         i# = word2Int# x#
582 #else
583                                     = S# (word2Int# x#)
584 #endif
585
586 instance Bits Word32 where
587     (W32# x#) .&.   (W32# y#)  = W32# (x# `and#` y#)
588     (W32# x#) .|.   (W32# y#)  = W32# (x# `or#`  y#)
589     (W32# x#) `xor` (W32# y#)  = W32# (x# `xor#` y#)
590     complement (W32# x#)       = W32# (x# `xor#` mb#) where W32# mb# = maxBound
591     (W32# x#) `shift` (I# i#)
592         | i# >=# 0#            = W32# (narrow32Word# (x# `shiftL#` i#))
593         | otherwise            = W32# (x# `shiftRL#` negateInt# i#)
594     (W32# x#) `rotate` (I# i#)
595         | i'# ==# 0# = W32# x#
596         | otherwise  = W32# (narrow32Word# ((x# `shiftL#` i'#) `or#`
597                                             (x# `shiftRL#` (32# -# i'#))))
598         where
599         i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
600     bitSize  _                = 32
601     isSigned _                = False
602
603 {-# RULES
604 "fromIntegral/Word8->Word32"   fromIntegral = \(W8# x#) -> W32# x#
605 "fromIntegral/Word16->Word32"  fromIntegral = \(W16# x#) -> W32# x#
606 "fromIntegral/Word32->Word32"  fromIntegral = id :: Word32 -> Word32
607 "fromIntegral/Word32->Integer" fromIntegral = toInteger :: Word32 -> Integer
608 "fromIntegral/a->Word32"       fromIntegral = \x -> case fromIntegral x of W# x# -> W32# (narrow32Word# x#)
609 "fromIntegral/Word32->a"       fromIntegral = \(W32# x#) -> fromIntegral (W# x#)
610   #-}
611
612 #endif
613
614 instance CCallable Word32
615 instance CReturnable Word32
616
617 instance Show Word32 where
618 #if WORD_SIZE_IN_BITS < 33
619     showsPrec p x = showsPrec p (toInteger x)
620 #else
621     showsPrec p x = showsPrec p (fromIntegral x :: Int)
622 #endif
623
624
625 instance Real Word32 where
626     toRational x = toInteger x % 1
627
628 instance Bounded Word32 where
629     minBound = 0
630     maxBound = 0xFFFFFFFF
631
632 instance Ix Word32 where
633     range (m,n)              = [m..n]
634     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
635     inRange (m,n) i          = m <= i && i <= n
636     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
637
638 instance Read Word32 where  
639 #if WORD_SIZE_IN_BITS < 33
640     readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
641 #else
642     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
643 #endif
644
645 ------------------------------------------------------------------------
646 -- type Word64
647 ------------------------------------------------------------------------
648
649 #if WORD_SIZE_IN_BITS < 64
650
651 data Word64 = W64# Word64#
652 -- ^ 64-bit unsigned integer type
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
698     rem     x@(W64# x#) y@(W64# y#)
699         | y /= 0                    = W64# (x# `remWord64#` y#)
700         | otherwise                 = divZeroError
701     div     x@(W64# x#) y@(W64# y#)
702         | y /= 0                    = W64# (x# `quotWord64#` y#)
703         | otherwise                 = divZeroError
704     mod     x@(W64# x#) y@(W64# y#)
705         | y /= 0                    = W64# (x# `remWord64#` y#)
706         | otherwise                 = divZeroError
707     quotRem x@(W64# x#) y@(W64# y#)
708         | y /= 0                    = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
709         | otherwise                 = divZeroError
710     divMod  x@(W64# x#) y@(W64# y#)
711         | y /= 0                    = (W64# (x# `quotWord64#` y#), W64# (x# `remWord64#` y#))
712         | otherwise                 = divZeroError
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 -- ^ 64-bit unsigned integer type
791
792 instance Num Word64 where
793     (W64# x#) + (W64# y#)  = W64# (x# `plusWord#` y#)
794     (W64# x#) - (W64# y#)  = W64# (x# `minusWord#` y#)
795     (W64# x#) * (W64# y#)  = W64# (x# `timesWord#` y#)
796     negate (W64# x#)       = W64# (int2Word# (negateInt# (word2Int# x#)))
797     abs x                  = x
798     signum 0               = 0
799     signum _               = 1
800     fromInteger (S# i#)    = W64# (int2Word# i#)
801     fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
802
803 instance Enum Word64 where
804     succ x
805         | x /= maxBound = x + 1
806         | otherwise     = succError "Word64"
807     pred x
808         | x /= minBound = x - 1
809         | otherwise     = predError "Word64"
810     toEnum i@(I# i#)
811         | i >= 0        = W64# (int2Word# i#)
812         | otherwise     = toEnumError "Word64" i (minBound::Word64, maxBound::Word64)
813     fromEnum x@(W64# x#)
814         | x <= fromIntegral (maxBound::Int)
815                         = I# (word2Int# x#)
816         | otherwise     = fromEnumError "Word64" x
817     enumFrom            = integralEnumFrom
818     enumFromThen        = integralEnumFromThen
819     enumFromTo          = integralEnumFromTo
820     enumFromThenTo      = integralEnumFromThenTo
821
822 instance Integral Word64 where
823     quot    x@(W64# x#) y@(W64# y#)
824         | y /= 0                    = W64# (x# `quotWord#` y#)
825         | otherwise                 = divZeroError
826     rem     x@(W64# x#) y@(W64# y#)
827         | y /= 0                    = W64# (x# `remWord#` y#)
828         | otherwise                 = divZeroError
829     div     x@(W64# x#) y@(W64# y#)
830         | y /= 0                    = W64# (x# `quotWord#` y#)
831         | otherwise                 = divZeroError
832     mod     x@(W64# x#) y@(W64# y#)
833         | y /= 0                    = W64# (x# `remWord#` y#)
834         | otherwise                 = divZeroError
835     quotRem x@(W64# x#) y@(W64# y#)
836         | y /= 0                    = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
837         | otherwise                 = divZeroError
838     divMod  x@(W64# x#) y@(W64# y#)
839         | y /= 0                    = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#))
840         | otherwise                 = divZeroError
841     toInteger (W64# x#)
842         | i# >=# 0#                 = S# i#
843         | otherwise                 = case word2Integer# x# of (# s, d #) -> J# s d
844         where
845         i# = word2Int# x#
846
847 instance Bits Word64 where
848     (W64# x#) .&.   (W64# y#)  = W64# (x# `and#` y#)
849     (W64# x#) .|.   (W64# y#)  = W64# (x# `or#`  y#)
850     (W64# x#) `xor` (W64# y#)  = W64# (x# `xor#` y#)
851     complement (W64# x#)       = W64# (x# `xor#` mb#) where W64# mb# = maxBound
852     (W64# x#) `shift` (I# i#)
853         | i# >=# 0#            = W64# (x# `shiftL#` i#)
854         | otherwise            = W64# (x# `shiftRL#` negateInt# i#)
855     (W64# x#) `rotate` (I# i#)
856         | i'# ==# 0# = W64# x#
857         | otherwise  = W64# ((x# `shiftL#` i'#) `or#`
858                              (x# `shiftRL#` (64# -# i'#)))
859         where
860         i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
861     bitSize  _                = 64
862     isSigned _                = False
863
864 {-# RULES
865 "fromIntegral/a->Word64" fromIntegral = \x -> case fromIntegral x of W# x# -> W64# x#
866 "fromIntegral/Word64->a" fromIntegral = \(W64# x#) -> fromIntegral (W# x#)
867   #-}
868
869 #endif
870
871 instance CCallable Word64
872 instance CReturnable Word64
873
874 instance Show Word64 where
875     showsPrec p x = showsPrec p (toInteger x)
876
877 instance Real Word64 where
878     toRational x = toInteger x % 1
879
880 instance Bounded Word64 where
881     minBound = 0
882     maxBound = 0xFFFFFFFFFFFFFFFF
883
884 instance Ix Word64 where
885     range (m,n)              = [m..n]
886     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
887     inRange (m,n) i          = m <= i && i <= n
888     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
889
890 instance Read Word64 where
891     readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]