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