e9110f420584acfcddc661f9854b55188cbda2fb
[ghc-hetmet.git] / ghc / lib / std / PrelWord.lhs
1 %
2 % (c) The University of Glasgow, 1997-2000
3 %
4 \section[PrelWord]{Module @PrelWord@}
5
6 \begin{code}
7 {-# OPTIONS -monly-3-regs #-}
8
9 #include "MachDeps.h"
10
11 module PrelWord (
12         Word(..), Word8(..), Word16(..), Word32(..), Word64(..),
13
14         -- SUP: deprecated in the new FFI, subsumed by fromIntegral
15         , intToWord8      -- :: Int     -> Word8
16         , intToWord16     -- :: Int     -> Word16
17         , intToWord32     -- :: Int     -> Word32
18         , intToWord64     -- :: Int     -> Word64
19
20         , integerToWord8  -- :: Integer -> Word8
21         , integerToWord16 -- :: Integer -> Word16
22         , integerToWord32 -- :: Integer -> Word32
23         , integerToWord64 -- :: Integer -> Word64
24
25         , word8ToInt      -- :: Word8   -> Int
26         , word8ToInteger  -- :: Word8   -> Integer
27         , word8ToWord16   -- :: Word8   -> Word16
28         , word8ToWord32   -- :: Word8   -> Word32
29         , word8ToWord64   -- :: Word8   -> Word64
30
31         , word16ToInt     -- :: Word16  -> Int
32         , word16ToInteger -- :: Word16  -> Integer
33         , word16ToWord8   -- :: Word16  -> Word8
34         , word16ToWord32  -- :: Word16  -> Word32
35         , word16ToWord64  -- :: Word16  -> Word64
36
37         , word32ToInt     -- :: Word32  -> Int
38         , word32ToInteger -- :: Word32  -> Integer
39         , word32ToWord8   -- :: Word32  -> Word8
40         , word32ToWord16  -- :: Word32  -> Word16
41         , word32ToWord64  -- :: Word32  -> Word64
42
43         , word64ToInt     -- :: Word64  -> Int
44         , word64ToInteger -- :: Word64  -> Integer
45         , word64ToWord8   -- :: Word64  -> Word8
46         , word64ToWord16  -- :: Word64  -> Word16
47         , word64ToWord32  -- :: Word64  -> Word32
48
49         -- internal stuff
50         , wordToWord8#, wordToWord16#, wordToWord32#, wordToWord64#
51
52         , word64ToInt64#, int64ToWord64#
53         , wordToWord64#, word64ToWord#
54
55         , toEnumError, fromEnumError, succError, predError, divZeroError
56   ) where
57
58 import PrelArr
59 import PrelBits
60 import PrelRead
61 import PrelEnum
62 import PrelReal
63 import PrelNum
64 import PrelBase
65
66 -- ---------------------------------------------------------------------------
67 -- The Word Type
68 -- ---------------------------------------------------------------------------
69
70 -- A Word is an unsigned integral type, with the same number of bits as Int.
71 data Word = W# Word# deriving (Eq, Ord)
72
73 instance CCallable Word
74 instance CReturnable Word
75
76 -- ---------------------------------------------------------------------------
77 -- Coercion functions (DEPRECATED)
78 -- ---------------------------------------------------------------------------
79
80 intToWord8      :: Int     -> Word8
81 intToWord16     :: Int     -> Word16
82 intToWord32     :: Int     -> Word32
83 intToWord64     :: Int     -> Word64
84
85 integerToWord8  :: Integer -> Word8
86 integerToWord16 :: Integer -> Word16
87 integerToWord32 :: Integer -> Word32
88 integerToWord64 :: Integer -> Word64
89
90 word8ToInt      :: Word8   -> Int
91 word8ToInteger  :: Word8   -> Integer
92 word8ToWord16   :: Word8   -> Word16
93 word8ToWord32   :: Word8   -> Word32
94 word8ToWord64   :: Word8   -> Word64
95
96 word16ToInt     :: Word16  -> Int
97 word16ToInteger :: Word16  -> Integer
98 word16ToWord8   :: Word16  -> Word8
99 word16ToWord32  :: Word16  -> Word32
100 word16ToWord64  :: Word16  -> Word64
101
102 word32ToInt     :: Word32  -> Int
103 word32ToInteger :: Word32  -> Integer
104 word32ToWord8   :: Word32  -> Word8
105 word32ToWord16  :: Word32  -> Word16
106 word32ToWord64  :: Word32  -> Word64
107
108 word64ToInt     :: Word64  -> Int
109 word64ToInteger :: Word64  -> Integer
110 word64ToWord8   :: Word64  -> Word8
111 word64ToWord16  :: Word64  -> Word16
112 word64ToWord32  :: Word64  -> Word32
113
114 intToWord8      = word32ToWord8   . intToWord32
115 intToWord16     = word32ToWord16  . intToWord32
116
117 integerToWord8  = fromInteger
118 integerToWord16 = fromInteger
119
120 word8ToInt      = word32ToInt     . word8ToWord32
121 word8ToInteger  = word32ToInteger . word8ToWord32
122
123 word16ToInt     = word32ToInt     . word16ToWord32
124 word16ToInteger = word32ToInteger . word16ToWord32
125
126 #if WORD_SIZE_IN_BYTES > 4
127 intToWord32 (I# x)   = W32# ((int2Word# x) `and#` (case (maxBound::Word32) of W32# x# -> x#))
128 #else
129 intToWord32 (I# x)   = W32# (int2Word# x)
130 #endif
131
132 word32ToInt (W32# x) = I#   (word2Int# x)
133
134 word2Integer :: Word# -> Integer
135 word2Integer w | i >=# 0#   = S# i
136                | otherwise = case word2Integer# w of
137                                 (# s, d #) -> J# s d
138    where i = word2Int# w
139
140 word32ToInteger (W32# x) = word2Integer x
141 integerToWord32 = fromInteger
142
143 -----------------------------------------------------------------------------
144 -- The following rules for fromIntegral remove the need to export specialized
145 -- conversion functions.
146 -----------------------------------------------------------------------------
147
148 {-# RULES
149    "fromIntegral/Int->Word8"        fromIntegral = intToWord8;
150    "fromIntegral/Int->Word16"       fromIntegral = intToWord16;
151    "fromIntegral/Int->Word32"       fromIntegral = intToWord32;
152    "fromIntegral/Int->Word64"       fromIntegral = intToWord64;
153
154    "fromIntegral/Integer->Word8"    fromIntegral = integerToWord8;
155    "fromIntegral/Integer->Word16"   fromIntegral = integerToWord16;
156    "fromIntegral/Integer->Word32"   fromIntegral = integerToWord32;
157    "fromIntegral/Integer->Word64"   fromIntegral = integerToWord64;
158
159    "fromIntegral/Word8->Int"        fromIntegral = word8ToInt;
160    "fromIntegral/Word8->Integer"    fromIntegral = word8ToInteger;
161    "fromIntegral/Word8->Word16"     fromIntegral = word8ToWord16;
162    "fromIntegral/Word8->Word32"     fromIntegral = word8ToWord32;
163    "fromIntegral/Word8->Word64"     fromIntegral = word8ToWord64;
164
165    "fromIntegral/Word16->Int"       fromIntegral = word16ToInt;
166    "fromIntegral/Word16->Integer"   fromIntegral = word16ToInteger;
167    "fromIntegral/Word16->Word8"     fromIntegral = word16ToWord8;
168    "fromIntegral/Word16->Word32"    fromIntegral = word16ToWord32;
169    "fromIntegral/Word16->Word64"    fromIntegral = word16ToWord64;
170
171    "fromIntegral/Word32->Int"       fromIntegral = word32ToInt;
172    "fromIntegral/Word32->Integer"   fromIntegral = word32ToInteger;
173    "fromIntegral/Word32->Word8"     fromIntegral = word32ToWord8;
174    "fromIntegral/Word32->Word16"    fromIntegral = word32ToWord16;
175    "fromIntegral/Word32->Word64"    fromIntegral = word32ToWord64;
176
177    "fromIntegral/Word64->Int"       fromIntegral = word64ToInt;
178    "fromIntegral/Word64->Integer"   fromIntegral = word64ToInteger;
179    "fromIntegral/Word64->Word8"     fromIntegral = word64ToWord8;
180    "fromIntegral/Word64->Word16"    fromIntegral = word64ToWord16;
181    "fromIntegral/Word64->Word32"    fromIntegral = word64ToWord32
182  #-}
183
184 \end{code}
185
186 \subsection[Word8]{The @Word8@ interface}
187
188
189 The byte type @Word8@ is represented in the Haskell
190 heap by boxing up a 32-bit quantity, @Word#@. An invariant
191 for this representation is that the higher 24 bits are
192 *always* zeroed out. A consequence of this is that
193 operations that could possibly overflow have to mask
194 out the top three bytes before building the resulting @Word8@.
195
196 \begin{code}
197 data Word8  = W8# Word#
198
199 instance CCallable Word8
200 instance CReturnable Word8
201
202 word8ToWord32 (W8#  x) = W32# x
203 word8ToWord16 (W8#  x) = W16# x
204 word32ToWord8 (W32# x) = W8# (wordToWord8# x)
205
206 -- mask out upper three bytes.
207 intToWord8# :: Int# -> Word#
208 intToWord8# i# = (int2Word# i#) `and#` (int2Word# 0xff#)
209
210 wordToWord8# :: Word# -> Word#
211 wordToWord8# w# = w# `and#` (int2Word# 0xff#)
212
213 instance Eq  Word8     where 
214   (W8# x) == (W8# y) = x `eqWord#` y
215   (W8# x) /= (W8# y) = x `neWord#` y
216
217 instance Ord Word8     where 
218   compare (W8# x#) (W8# y#) = compareWord# x# y#
219   (<)  (W8# x) (W8# y)      = x `ltWord#` y
220   (<=) (W8# x) (W8# y)      = x `leWord#` y
221   (>=) (W8# x) (W8# y)      = x `geWord#` y
222   (>)  (W8# x) (W8# y)      = x `gtWord#` y
223   max x@(W8# x#) y@(W8# y#) = 
224      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
225   min x@(W8# x#) y@(W8# y#) =
226      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
227
228 -- Helper function, used by Ord Word* instances.
229 compareWord# :: Word# -> Word# -> Ordering
230 compareWord# x# y# 
231  | x# `ltWord#` y# = LT
232  | x# `eqWord#` y# = EQ
233  | otherwise       = GT
234
235 instance Num Word8 where
236   (W8# x) + (W8# y) = 
237       W8# (intToWord8# (word2Int# x +# word2Int# y))
238   (W8# x) - (W8# y) = 
239       W8# (intToWord8# (word2Int# x -# word2Int# y))
240   (W8# x) * (W8# y) = 
241       W8# (intToWord8# (word2Int# x *# word2Int# y))
242   negate w@(W8# x)  = 
243      if x' ==# 0# 
244       then w
245       else W8# (int2Word# (0x100# -# x'))
246      where
247       x' = word2Int# x
248   abs x         = x
249   signum        = signumReal
250   fromInteger (S# i#)    = W8# (wordToWord8# (int2Word# i#))
251   fromInteger (J# s# d#) = W8# (wordToWord8# (integer2Word# s# d#))
252
253 instance Bounded Word8 where
254   minBound = 0
255   maxBound = 0xff
256
257 instance Real Word8 where
258   toRational x = toInteger x % 1
259
260 -- Note: no need to mask results here 
261 -- as they cannot overflow.
262 instance Integral Word8 where
263   div  x@(W8# x#)  (W8# y#) 
264     | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
265     | otherwise                   = divZeroError "div{Word8}" x
266
267   quot x@(W8# x#)  (W8# y#)   
268     | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
269     | otherwise                   = divZeroError "quot{Word8}" x
270
271   rem  x@(W8# x#)  (W8# y#)
272     | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
273     | otherwise                   = divZeroError "rem{Word8}" x
274
275   mod  x@(W8# x#)  (W8# y#)
276     | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
277     | otherwise                   = divZeroError "mod{Word8}" x
278
279   quotRem (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
280   divMod  (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
281
282   toInteger = toInteger . toInt
283   toInt     = word8ToInt
284
285 instance Ix Word8 where
286     range (m,n)          = [m..n]
287     index b@(m,_) i
288            | inRange b i = word8ToInt (i-m)
289            | otherwise   = indexError b i "Word8"
290     inRange (m,n) i      = m <= i && i <= n
291
292 instance Enum Word8 where
293     succ w          
294       | w == maxBound = succError "Word8"
295       | otherwise     = w+1
296     pred w          
297       | w == minBound = predError "Word8"
298       | otherwise     = w-1
299
300     toEnum   i@(I# i#)  
301       | i >= toInt (minBound::Word8) && i <= toInt (maxBound::Word8) 
302       = W8# (intToWord8# i#)
303       | otherwise
304       = toEnumError "Word8" i (minBound::Word8,maxBound::Word8)
305
306     fromEnum  (W8# w) = I# (word2Int# w)
307
308     enumFrom          = boundedEnumFrom
309     enumFromThen      = boundedEnumFromThen
310
311 instance Read Word8 where
312     readsPrec _ = readDec
313
314 instance Show Word8 where
315     showsPrec p w8 = showsPrec p (word8ToInt w8)
316
317 instance Bits Word8 where
318   (W8# x)  .&.  (W8# y)    = W8# (x `and#` y)
319   (W8# x)  .|.  (W8# y)    = W8# (x `or#` y)
320   (W8# x) `xor` (W8# y)    = W8# (x `xor#` y)
321   complement (W8# x)       = W8# (x `xor#` int2Word# 0xff#)
322   shift (W8# x#) i@(I# i#)
323         | i > 0     = W8# (wordToWord8# (shiftL# x# i#))
324         | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
325   w@(W8# x)  `rotate` (I# i)
326         | i ==# 0#    = w
327         | i ># 0#     = W8# ((wordToWord8# (shiftL# x i')) `or#`
328                              (shiftRL# (x `and#` 
329                                         (int2Word# (0x100# -# pow2# i2)))
330                                        i2))
331         | otherwise = rotate w (I# (8# +# i))
332           where
333            i' = word2Int# (int2Word# i `and#` int2Word# 7#)
334            i2 = 8# -# i'
335
336   bit (I# i#)
337         | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
338         | otherwise = 0 -- We'll be overbearing, for now..
339
340   testBit (W8# x#) (I# i#)
341     | i# <# 8# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
342     | otherwise             = False -- for now, this is really an error.
343
344   bitSize  _    = 8
345   isSigned _    = False
346
347 pow2# :: Int# -> Int#
348 pow2# x# = word2Int# (shiftL# (int2Word# 1#) x#)
349
350 pow2_64# :: Int# -> Int64#
351 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
352
353 -- ---------------------------------------------------------------------------
354 -- Word16
355 -- ---------------------------------------------------------------------------
356
357 -- The double byte type @Word16@ is represented in the Haskell
358 -- heap by boxing up a machine word, @Word#@. An invariant
359 -- for this representation is that only the lower 16 bits are
360 -- `active', any bits above are {\em always} zeroed out.
361 -- A consequence of this is that operations that could possibly
362 -- overflow have to mask out anything above the lower two bytes
363 -- before putting together the resulting @Word16@.
364
365 data Word16 = W16# Word#
366
367 instance CCallable Word16
368 instance CReturnable Word16
369
370 word16ToWord8  (W16# x) = W8#  (wordToWord8#  x)
371 word16ToWord32 (W16# x) = W32# x
372
373 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
374
375 -- mask out upper 16 bits.
376 intToWord16# :: Int# -> Word#
377 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
378
379 wordToWord16# :: Word# -> Word#
380 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
381
382 instance Eq  Word16    where 
383   (W16# x) == (W16# y) = x `eqWord#` y
384   (W16# x) /= (W16# y) = x `neWord#` y
385
386 instance Ord Word16     where
387   compare (W16# x#) (W16# y#) = compareWord# x# y#
388   (<)  (W16# x) (W16# y)      = x `ltWord#` y
389   (<=) (W16# x) (W16# y)      = x `leWord#` y
390   (>=) (W16# x) (W16# y)      = x `geWord#` y
391   (>)  (W16# x) (W16# y)      = x `gtWord#` y
392   max x@(W16# x#) y@(W16# y#) = 
393      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
394   min x@(W16# x#) y@(W16# y#) =
395      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
396
397
398
399 instance Num Word16 where
400   (W16# x) + (W16# y) = 
401        W16# (intToWord16# (word2Int# x +# word2Int# y))
402   (W16# x) - (W16# y) = 
403        W16# (intToWord16# (word2Int# x -# word2Int# y))
404   (W16# x) * (W16# y) = 
405        W16# (intToWord16# (word2Int# x *# word2Int# y))
406   negate w@(W16# x)  = 
407        if x' ==# 0# 
408         then w
409         else W16# (int2Word# (0x10000# -# x'))
410        where
411         x' = word2Int# x
412   abs x         = x
413   signum        = signumReal
414   fromInteger (S# i#)    = W16# (wordToWord16# (int2Word# i#))
415   fromInteger (J# s# d#) = W16# (wordToWord16# (integer2Word# s# d#))
416
417 instance Bounded Word16 where
418   minBound = 0
419   maxBound = 0xffff
420
421 instance Real Word16 where
422   toRational x = toInteger x % 1
423
424 instance Integral Word16 where
425   div  x@(W16# x#)  (W16# y#)
426    | y# `neWord#` (int2Word# 0#) = W16# (x# `quotWord#` y#)
427    | otherwise                   = divZeroError "div{Word16}" x
428
429   quot x@(W16# x#) (W16# y#)
430    | y# `neWord#`(int2Word# 0#)  = W16# (x# `quotWord#` y#)
431    | otherwise                   = divZeroError "quot{Word16}" x
432
433   rem  x@(W16# x#) (W16# y#)
434    | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
435    | otherwise                   = divZeroError "rem{Word16}" x
436
437   mod  x@(W16# x#)  (W16# y#)
438    | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
439    | otherwise                   = divZeroError "mod{Word16}" x
440
441   quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
442   divMod  (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
443
444   toInteger = toInteger . toInt
445   toInt     = word16ToInt
446
447 instance Ix Word16 where
448   range (m,n)          = [m..n]
449   index b@(m,_) i
450          | inRange b i = word16ToInt (i - m)
451          | otherwise   = indexError b i "Word16"
452   inRange (m,n) i      = m <= i && i <= n
453
454 instance Enum Word16 where
455     succ w          
456       | w == maxBound = succError "Word16"
457       | otherwise     = w+1
458     pred w          
459       | w == minBound = predError "Word16"
460       | otherwise     = w-1
461
462     toEnum   i@(I# i#)  
463       | i >= toInt (minBound::Word16) && i <= toInt (maxBound::Word16)
464       = W16# (intToWord16# i#)
465       | otherwise
466       = toEnumError "Word16" i (minBound::Word16,maxBound::Word16)
467
468     fromEnum  (W16# w) = I# (word2Int# w)
469     enumFrom     = boundedEnumFrom
470     enumFromThen = boundedEnumFromThen
471
472 instance Read Word16 where
473   readsPrec _ = readDec
474
475 instance Show Word16 where
476   showsPrec p w16 = showsPrec p (word16ToInt w16)
477
478 instance Bits Word16 where
479   (W16# x)  .&.  (W16# y)  = W16# (x `and#` y)
480   (W16# x)  .|.  (W16# y)  = W16# (x `or#` y)
481   (W16# x) `xor` (W16# y)  = W16# (x `xor#` y)
482   complement (W16# x)      = W16# (x `xor#` int2Word# 0xffff#)
483   shift (W16# x#) i@(I# i#)
484         | i > 0     = W16# (wordToWord16# (shiftL# x# i#))
485         | otherwise = W16# (shiftRL# x# (negateInt# i#))
486   w@(W16# x)  `rotate` (I# i)
487         | i ==# 0#    = w
488         | i ># 0#     = W16# ((wordToWord16# (shiftL# x i')) `or#`
489                               (shiftRL# (x `and#` 
490                                          (int2Word# (0x10000# -# pow2# i2)))
491                                         i2))
492         | otherwise = rotate w (I# (16# +# i'))
493           where
494            i' = word2Int# (int2Word# i `and#` int2Word# 15#)
495            i2 = 16# -# i'
496   bit (I# i#)
497         | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
498         | otherwise = 0 -- We'll be overbearing, for now..
499
500   testBit (W16# x#) (I# i#)
501     | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
502     | otherwise             = False -- for now, this is really an error.
503
504   bitSize  _    = 16
505   isSigned _    = False
506
507 -- ---------------------------------------------------------------------------
508 -- Word32
509 -- ---------------------------------------------------------------------------
510
511 -- The quad byte type @Word32@ is represented in the Haskell
512 -- heap by boxing up a machine word, @Word#@. An invariant
513 -- for this representation is that any bits above the lower
514 -- 32 are {\em always} zeroed out. A consequence of this is that
515 -- operations that could possibly overflow have to mask
516 -- the result before building the resulting @Word16@.
517
518 data Word32 = W32# Word#
519
520 instance CCallable Word32
521 instance CReturnable Word32
522
523 instance Eq  Word32    where 
524   (W32# x) == (W32# y) = x `eqWord#` y
525   (W32# x) /= (W32# y) = x `neWord#` y
526
527 instance Ord Word32    where
528   compare (W32# x#) (W32# y#) = compareWord# x# y#
529   (<)  (W32# x) (W32# y)      = x `ltWord#` y
530   (<=) (W32# x) (W32# y)      = x `leWord#` y
531   (>=) (W32# x) (W32# y)      = x `geWord#` y
532   (>)  (W32# x) (W32# y)      = x `gtWord#` y
533   max x@(W32# x#) y@(W32# y#) = 
534      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
535   min x@(W32# x#) y@(W32# y#) =
536      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
537
538 instance Num Word32 where
539   (W32# x) + (W32# y) = 
540        W32# (intToWord32# (word2Int# x +# word2Int# y))
541   (W32# x) - (W32# y) =
542        W32# (intToWord32# (word2Int# x -# word2Int# y))
543   (W32# x) * (W32# y) = 
544        W32# (intToWord32# (word2Int# x *# word2Int# y))
545 #if WORD_SIZE_IN_BYTES == 8
546   negate w@(W32# x)  = 
547       if x' ==# 0#
548        then w
549        else W32# (intToWord32# (0x100000000# -# x'))
550        where
551         x' = word2Int# x
552 #else
553   negate (W32# x)  = W32# (intToWord32# (negateInt# (word2Int# x)))
554 #endif
555   abs x           = x
556   signum          = signumReal
557   fromInteger (S# i#)    = W32# (intToWord32# i#)
558   fromInteger (J# s# d#) = W32# (wordToWord32# (integer2Word# s# d#))
559     -- ToDo: restrict fromInt{eger} range.
560
561 intToWord32#  :: Int#  -> Word#
562 wordToWord32# :: Word# -> Word#
563
564 #if WORD_SIZE_IN_BYTES == 8
565 intToWord32#  i#  = (int2Word# i#) `and#` (int2Word# 0xffffffff#)
566 wordToWord32# w#  = w# `and#` (int2Word# 0xffffffff#)
567 wordToWord64# w#  = w#
568 #else
569 intToWord32#  i# = int2Word# i#
570 wordToWord32# w# = w#
571 #endif
572
573 instance Bounded Word32 where
574     minBound = 0
575 #if WORD_SIZE_IN_BYTES == 8
576     maxBound = 0xffffffff
577 #else
578     maxBound = minBound - 1
579 #endif
580
581 instance Real Word32 where
582     toRational x = toInteger x % 1
583
584 instance Integral Word32 where
585     div  x y 
586       | y /= 0         = quotWord32 x y
587       | otherwise      = divZeroError "div{Word32}" x
588
589     quot x y
590       | y /= 0         = quotWord32 x y
591       | otherwise      = divZeroError "quot{Word32}" x
592
593     rem  x y
594       | y /= 0         = remWord32 x y
595       | otherwise      = divZeroError "rem{Word32}" x
596
597     mod  x y
598       | y /= 0         = remWord32 x y
599       | otherwise      = divZeroError "mod{Word32}" x
600
601     quotRem a b        = (a `quot` b, a `rem` b)
602     divMod x y         = quotRem x y
603
604     toInteger          = word32ToInteger 
605     toInt              = word32ToInt
606
607
608 {-# INLINE quotWord32 #-}
609 {-# INLINE remWord32  #-}
610 remWord32, quotWord32 :: Word32 -> Word32 -> Word32
611 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
612 (W32# x) `remWord32`  (W32# y) = W32# (x `remWord#`  y)
613
614
615 instance Ix Word32 where
616     range (m,n)          = [m..n]
617     index b@(m,_) i
618            | inRange b i = word32ToInt (i - m)
619            | otherwise   = indexError b i "Word32"
620     inRange (m,n) i      = m <= i && i <= n
621
622 instance Enum Word32 where
623     succ w          
624       | w == maxBound = succError "Word32"
625       | otherwise     = w+1
626     pred w          
627       | w == minBound = predError "Word32"
628       | otherwise     = w-1
629
630      -- the toEnum/fromEnum will fail if the mapping isn't legal,
631      -- use the intTo* & *ToInt coercion functions to 'bypass' these range checks.
632     toEnum   x
633       | x >= 0    = intToWord32 x
634       | otherwise
635       = toEnumError "Word32" x (minBound::Word32,maxBound::Word32)
636
637     fromEnum   x
638       | x <= intToWord32 (maxBound::Int)
639       = word32ToInt x
640       | otherwise
641       = fromEnumError "Word32" x 
642
643     enumFrom w           = [w .. maxBound]
644     enumFromTo   w1 w2
645        | w1 <= w2        = eftt32 True{-increasing-} w1 diff_f last
646        | otherwise       = []
647         where
648          last = (> w2)
649          diff_f x = x + 1 
650           
651     enumFromThen w1 w2   = [w1,w2 .. last]
652        where
653          last :: Word32
654          last
655           | w1 <=w2   = maxBound
656           | otherwise = minBound
657
658     enumFromThenTo w1 w2 wend  = eftt32 increasing w1 step_f last
659      where
660        increasing = w1 <= w2
661        diff1 = w2 - w1
662        diff2 = w1 - w2
663        
664        last
665         | increasing = (> wend)
666         | otherwise  = (< wend)
667
668        step_f 
669         | increasing = \ x -> x + diff1
670         | otherwise  = \ x -> x - diff2
671
672 eftt32 :: Bool -> Word32 -> (Word32 -> Word32) -> (Word32-> Bool) -> [Word32]
673 eftt32 increasing init stepper done = go init
674   where
675     go now
676      | done now                    = []
677      | increasing     && now > nxt = [now] -- oflow
678      | not increasing && now < nxt = [now] -- uflow
679      | otherwise                   = now : go nxt
680      where
681       nxt = stepper now 
682
683 instance Read Word32 where
684     readsPrec _ = readDec
685
686 instance Show Word32 where
687     showsPrec p w = showsPrec p (word32ToInteger w)
688
689 instance Bits Word32 where
690   (W32# x)  .&.  (W32# y)  = W32# (x `and#` y)
691   (W32# x)  .|.  (W32# y)  = W32# (x `or#` y)
692   (W32# x) `xor` (W32# y)  = W32# (x `xor#` y)
693   complement (W32# x)      = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
694   shift (W32# x) i@(I# i#)
695         | i > 0     = W32# (wordToWord32# (shiftL# x i#))
696         | otherwise = W32# (shiftRL# x (negateInt# i#))
697   w@(W32# x)  `rotate` (I# i)
698         | i ==# 0#    = w
699         | i ># 0#     = W32# ((wordToWord32# (shiftL# x i')) `or#`
700                               (shiftRL# (x `and#` 
701                                         (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
702                                      i2))
703         | otherwise = rotate w (I# (32# +# i))
704           where
705            i' = word2Int# (int2Word# i `and#` int2Word# 31#)
706            i2 = 32# -# i'
707            (W32# maxBound#) = maxBound
708
709   bit (I# i#)
710         | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
711         | otherwise = 0 -- We'll be overbearing, for now..
712
713   testBit (W32# x#) (I# i#)
714     | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
715     | otherwise             = False -- for now, this is really an error.
716   bitSize  _        = 32
717   isSigned _        = False
718
719 -- -----------------------------------------------------------------------------
720 -- Word64
721 -- -----------------------------------------------------------------------------
722
723 #if WORD_SIZE_IN_BYTES == 8
724 data Word64 = W64# Word#
725
726 word32ToWord64 (W32 w#) = W64# w#
727
728 word8ToWord64 (W8# w#) = W64# w#
729 word64ToWord8 (W64# w#) = W8# (w# `and#` (int2Word# 0xff#))
730
731 word16ToWord64 (W16# w#) = W64# w#
732 word64ToWord16 (W64# w#) = W16# (w# `and#` (int2Word# 0xffff#))
733
734 wordToWord32# :: Word# -> Word#
735 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
736
737 word64ToWord32 :: Word64 -> Word32
738 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
739
740 wordToWord64# w# = w#
741 word64ToWord# w# = w#
742
743 instance Eq  Word64     where 
744   (W64# x) == (W64# y) = x `eqWord#` y
745   (W64# x) /= (W64# y) = x `neWord#` y
746
747 instance Ord Word64     where 
748   compare (W64# x#) (W64# y#) = compareWord# x# y#
749   (<)  (W64# x) (W64# y)      = x `ltWord#` y
750   (<=) (W64# x) (W64# y)      = x `leWord#` y
751   (>=) (W64# x) (W64# y)      = x `geWord#` y
752   (>)  (W64# x) (W64# y)      = x `gtWord#` y
753   max x@(W64# x#) y@(W64# y#) = 
754      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
755   min x@(W64# x#) y@(W64# y#) =
756      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
757
758 instance Num Word64 where
759   (W64# x) + (W64# y) = 
760       W64# (intToWord64# (word2Int# x +# word2Int# y))
761   (W64# x) - (W64# y) = 
762       W64# (intToWord64# (word2Int# x -# word2Int# y))
763   (W64# x) * (W64# y) = 
764       W64# (intToWord64# (word2Int# x *# word2Int# y))
765   negate w@(W64# x)  = 
766      if x' ==# 0# 
767       then w
768       else W64# (int2Word# (0x100# -# x'))
769      where
770       x' = word2Int# x
771   abs x         = x
772   signum        = signumReal
773   fromInteger (S# i#)    = W64# (int2Word# i#)
774   fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
775
776 -- Note: no need to mask results here 
777 -- as they cannot overflow.
778 instance Integral Word64 where
779   div  x@(W64# x#)  (W64# y#)
780     | y# `neWord#` (int2Word# 0#)  = W64# (x# `quotWord#` y#)
781     | otherwise                    = divZeroError "div{Word64}" x
782
783   quot x@(W64# x#)  (W64# y#)
784     | y# `neWord#` (int2Word# 0#)  = W64# (x# `quotWord#` y#)
785     | otherwise                    = divZeroError "quot{Word64}" x
786
787   rem  x@(W64# x#)  (W64# y#)
788     | y# `neWord#` (int2Word# 0#)  = W64# (x# `remWord#` y#)
789     | otherwise                    = divZeroError "rem{Word64}" x
790
791   mod  (W64# x)  (W64# y)   
792     | y# `neWord#` (int2Word# 0#)  = W64# (x `remWord#` y)
793     | otherwise                    = divZeroError "mod{Word64}" x
794
795   quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
796   divMod  (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
797
798   toInteger (W64# x)        = word2Integer# x
799   toInt x                   = word64ToInt x
800
801 #else /* WORD_SIZE_IN_BYTES < 8 */
802
803 data Word64 = W64# Word64#
804
805 -- for completeness sake
806 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
807 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
808
809 word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
810 word64ToWord8 (W64# w#) = W8# ((word64ToWord# w#) `and#` (int2Word# 0xff#))
811
812 word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
813 word64ToWord16 (W64# w#) = W16# ((word64ToWord# w#) `and#` (int2Word# 0xffff#))
814
815 word64ToInteger (W64# w#) = 
816   case word64ToInteger# w# of
817     (# s#, p# #) -> J# s# p#
818 word64ToInt w = 
819    case w `quotRem` 0x100000000 of 
820      (_,l) -> toInt (word64ToWord32 l)
821
822 intToWord64# :: Int# -> Word64#
823 intToWord64# i# = wordToWord64# (int2Word# i#)
824
825 intToWord64 (I# i#) = W64# (intToWord64# i#)
826
827 integerToWord64 (S# i#)    = W64# (intToWord64# i#)
828 integerToWord64 (J# s# d#) = W64# (integerToWord64# s# d#)
829
830 instance Eq  Word64     where 
831   (W64# x) == (W64# y) = x `eqWord64#` y
832   (W64# x) /= (W64# y) = not (x `eqWord64#` y)
833
834 instance Ord Word64     where 
835   compare (W64# x#) (W64# y#) = compareWord64# x# y#
836   (<)  (W64# x) (W64# y)      = x `ltWord64#` y
837   (<=) (W64# x) (W64# y)      = x `leWord64#` y
838   (>=) (W64# x) (W64# y)      = x `geWord64#` y
839   (>)  (W64# x) (W64# y)      = x `gtWord64#` y
840   max x@(W64# x#) y@(W64# y#) = 
841      case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
842   min x@(W64# x#) y@(W64# y#) =
843      case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
844
845 instance Num Word64 where
846   (W64# x) + (W64# y) = 
847       W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
848   (W64# x) - (W64# y) = 
849       W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
850   (W64# x) * (W64# y) = 
851       W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
852   negate w
853      | w == 0     = w
854      | otherwise  = maxBound - w
855
856   abs x         = x
857   signum        = signumReal
858   fromInteger i = integerToWord64 i
859
860 -- Note: no need to mask results here  as they cannot overflow.
861 -- ToDo: protect against div by zero.
862 instance Integral Word64 where
863   div  (W64# x)  (W64# y)   = W64# (x `quotWord64#` y)
864   quot (W64# x)  (W64# y)   = W64# (x `quotWord64#` y)
865   rem  (W64# x)  (W64# y)   = W64# (x `remWord64#` y)
866   mod  (W64# x)  (W64# y)   = W64# (x `remWord64#` y)
867   quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
868   divMod  (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
869   toInteger w64             = word64ToInteger w64
870   toInt x                   = word64ToInt x
871
872 compareWord64# :: Word64# -> Word64# -> Ordering
873 compareWord64# i# j# 
874  | i# `ltWord64#` j# = LT
875  | i# `eqWord64#` j# = EQ
876  | otherwise         = GT
877
878 -- Word64# primop wrappers:
879
880 ltWord64# :: Word64# -> Word64# -> Bool
881 ltWord64# x# y# = stg_ltWord64 x# y# /=# 0#
882
883 leWord64# :: Word64# -> Word64# -> Bool
884 leWord64# x# y# = stg_leWord64 x# y# /=# 0#
885
886 eqWord64# :: Word64# -> Word64# -> Bool
887 eqWord64# x# y# = stg_eqWord64 x# y# /=# 0#
888       
889 neWord64# :: Word64# -> Word64# -> Bool
890 neWord64# x# y# = stg_neWord64 x# y# /=# 0#
891       
892 geWord64# :: Word64# -> Word64# -> Bool
893 geWord64# x# y# = stg_geWord64 x# y# /=# 0#
894       
895 gtWord64# :: Word64# -> Word64# -> Bool
896 gtWord64# x# y# = stg_gtWord64 x# y# /=# 0#
897
898 foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64#
899 foreign import "stg_int64ToWord64" unsafe int64ToWord64# :: Int64# -> Word64#
900 foreign import "stg_word64ToInt64" unsafe word64ToInt64# :: Word64# -> Int64#
901 foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64#
902 foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word#
903 foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64#
904 foreign import "stg_remWord64" unsafe remWord64# :: Word64# -> Word64# -> Word64#
905 foreign import "stg_quotWord64" unsafe quotWord64# :: Word64# -> Word64# -> Word64#
906 foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64#
907 foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64#
908 foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64#
909 foreign import "stg_gtWord64" unsafe stg_gtWord64 :: Word64# -> Word64# -> Int#
910 foreign import "stg_geWord64" unsafe stg_geWord64 :: Word64# -> Word64# -> Int#
911 foreign import "stg_neWord64" unsafe stg_neWord64 :: Word64# -> Word64# -> Int#
912 foreign import "stg_eqWord64" unsafe stg_eqWord64 :: Word64# -> Word64# -> Int#
913 foreign import "stg_leWord64" unsafe stg_leWord64 :: Word64# -> Word64# -> Int#
914 foreign import "stg_ltWord64" unsafe stg_ltWord64 :: Word64# -> Word64# -> Int#
915
916 #endif
917
918 instance CCallable   Word64
919 instance CReturnable Word64
920
921 instance Enum Word64 where
922     succ w          
923       | w == maxBound = succError "Word64"
924       | otherwise     = w+1
925     pred w          
926       | w == minBound = predError "Word64"
927       | otherwise     = w-1
928
929     toEnum i
930       | i >= 0    = intToWord64 i
931       | otherwise 
932       = toEnumError "Word64" i (minBound::Word64,maxBound::Word64)
933
934     fromEnum w
935       | w <= intToWord64 (maxBound::Int)
936       = word64ToInt w
937       | otherwise
938       = fromEnumError "Word64" w
939
940     enumFrom e1        = map integerToWord64 [word64ToInteger e1 .. word64ToInteger maxBound]
941     enumFromTo e1 e2   = map integerToWord64 [word64ToInteger e1 .. word64ToInteger e2]
942     enumFromThen e1 e2 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger last]
943                        where 
944                           last :: Word64
945                           last 
946                            | e2 < e1   = minBound
947                            | otherwise = maxBound
948
949     enumFromThenTo e1 e2 e3 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger e3]
950
951 instance Show Word64 where
952   showsPrec p x = showsPrec p (word64ToInteger x)
953
954 instance Read Word64 where
955   readsPrec _ s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
956
957 instance Ix Word64 where
958     range (m,n)          = [m..n]
959     index b@(m,_) i
960            | inRange b i = word64ToInt (i-m)
961            | otherwise   = indexError b i "Word64"
962     inRange (m,n) i      = m <= i && i <= n
963
964 instance Bounded Word64 where
965   minBound = 0
966   maxBound = minBound - 1
967
968 instance Real Word64 where
969   toRational x = toInteger x % 1
970
971 #if WORD_SIZE_IN_BYTES == 8
972
973 instance Bits Word64 where
974   (W64# x)  .&.  (W64# y)    = W64# (x `and#` y)
975   (W64# x)  .|.  (W64# y)    = W64# (x `or#` y)
976   (W64# x) `xor` (W64# y)    = W64# (x `xor#` y)
977   complement (W64# x)        = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
978   shift (W64# x#) i@(I# i#)
979         | i > 0     = W64# (shiftL# x# i#)
980         | otherwise = W64# (shiftRL# x# (negateInt# i#))
981
982   w@(W64# x)  `rotate` (I# i)
983         | i ==# 0#    = w
984         | i ># 0#     = W64# (shiftL# x i') `or#`
985                               (shiftRL# (x `and#` 
986                                         (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
987                                      i2))
988         | otherwise = rotate w (I# (64# +# i))
989           where
990            i' = word2Int# (int2Word# i `and#` int2Word# 63#)
991            i2 = 64# -# i'
992            (W64# maxBound#) = maxBound
993
994   bit (I# i#)
995         | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
996         | otherwise = 0 -- We'll be overbearing, for now..
997
998   testBit (W64# x#) (I# i#)
999     | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
1000     | otherwise              = False -- for now, this is really an error.
1001
1002   bitSize  _    = 64
1003   isSigned _    = False
1004
1005 #else /* WORD_SIZE_IN_BYTES < 8 */
1006
1007 instance Bits Word64 where
1008   (W64# x)  .&.  (W64# y)    = W64# (x `and64#` y)
1009   (W64# x)  .|.  (W64# y)    = W64# (x `or64#` y)
1010   (W64# x) `xor` (W64# y)    = W64# (x `xor64#` y)
1011   complement (W64# x)        = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
1012   shift (W64# x#) i@(I# i#)
1013         | i > 0     = W64# (shiftL64# x# i#)
1014         | otherwise = W64# (shiftRL64# x# (negateInt# i#))
1015
1016   w@(W64# x)  `rotate` (I# i)
1017         | i ==# 0#    = w
1018         | i ># 0#     = W64# ((shiftL64# x i') `or64#`
1019                               (shiftRL64# (x `and64#` 
1020                                            (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#` 
1021                                                            (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
1022                                      i2)
1023         | otherwise = rotate w (I# (64# +# i))
1024           where
1025            i' = word2Int# (int2Word# i `and#` int2Word# 63#)
1026            i2 = 64# -# i'
1027            (W64# maxBound#) = maxBound
1028
1029   bit (I# i#)
1030         | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
1031         | otherwise = 0 -- We'll be overbearing, for now..
1032
1033   testBit (W64# x#) (I# i#)
1034     | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
1035     | otherwise              = False -- for now, this is really an error.
1036
1037   bitSize  _    = 64
1038   isSigned _    = False
1039
1040 foreign import "stg_not64"     unsafe not64#    :: Word64# -> Word64#
1041 foreign import "stg_xor64"     unsafe xor64#    :: Word64# -> Word64# -> Word64#
1042 foreign import "stg_or64"      unsafe or64#     :: Word64# -> Word64# -> Word64#
1043 foreign import "stg_and64"     unsafe and64#    :: Word64# -> Word64# -> Word64#
1044 foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> Word64#
1045 foreign import "stg_shiftL64"  unsafe shiftL64#  :: Word64# -> Int# -> Word64#
1046
1047 #endif /* WORD_SIZE_IN_BYTES < 8 */
1048 \end{code}
1049
1050 Misc utils.
1051
1052 \begin{code}
1053 signumReal :: (Ord a, Num a) => a -> a
1054 signumReal x | x == 0    =  0
1055              | x > 0     =  1
1056              | otherwise = -1
1057 \end{code}
1058
1059 Utils for generating friendly error messages.
1060
1061 \begin{code}
1062 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1063 toEnumError inst_ty tag bnds
1064   = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1065            (showParen True (showsPrec 0 tag) $
1066              " is outside of bounds " ++
1067              show bnds))
1068
1069 fromEnumError :: (Show a,Show b) => String -> a -> b
1070 fromEnumError inst_ty tag
1071   = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1072            (showParen True (showsPrec 0 tag) $
1073              " is outside of Int's bounds " ++
1074              show (minBound::Int,maxBound::Int)))
1075
1076 succError :: String -> a
1077 succError inst_ty
1078   = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1079
1080 predError :: String -> a
1081 predError inst_ty
1082   = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1083
1084 divZeroError :: (Show a) => String -> a -> b
1085 divZeroError meth v 
1086   = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")
1087 \end{code}