[project @ 2001-02-22 16:48:24 by qrczak]
[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
284 instance Ix Word8 where
285     range (m,n)          = [m..n]
286     index b@(m,_) i
287            | inRange b i = word8ToInt (i-m)
288            | otherwise   = indexError b i "Word8"
289     inRange (m,n) i      = m <= i && i <= n
290
291 instance Enum Word8 where
292     succ w          
293       | w == maxBound = succError "Word8"
294       | otherwise     = w+1
295     pred w          
296       | w == minBound = predError "Word8"
297       | otherwise     = w-1
298
299     toEnum   i@(I# i#)  
300       | i >= fromIntegral (minBound::Word8) && i <= fromIntegral (maxBound::Word8) 
301       = W8# (intToWord8# i#)
302       | otherwise
303       = toEnumError "Word8" i (minBound::Word8,maxBound::Word8)
304
305     fromEnum  (W8# w) = I# (word2Int# w)
306
307     enumFrom          = boundedEnumFrom
308     enumFromThen      = boundedEnumFromThen
309
310 instance Read Word8 where
311     readsPrec _ = readDec
312
313 instance Show Word8 where
314     showsPrec p w8 = showsPrec p (word8ToInt w8)
315
316 instance Bits Word8 where
317   (W8# x)  .&.  (W8# y)    = W8# (x `and#` y)
318   (W8# x)  .|.  (W8# y)    = W8# (x `or#` y)
319   (W8# x) `xor` (W8# y)    = W8# (x `xor#` y)
320   complement (W8# x)       = W8# (x `xor#` int2Word# 0xff#)
321   shift (W8# x#) i@(I# i#)
322         | i > 0     = W8# (wordToWord8# (shiftL# x# i#))
323         | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
324   w@(W8# x)  `rotate` (I# i)
325         | i ==# 0#    = w
326         | i ># 0#     = W8# ((wordToWord8# (shiftL# x i')) `or#`
327                              (shiftRL# (x `and#` 
328                                         (int2Word# (0x100# -# pow2# i2)))
329                                        i2))
330         | otherwise = rotate w (I# (8# +# i))
331           where
332            i' = word2Int# (int2Word# i `and#` int2Word# 7#)
333            i2 = 8# -# i'
334
335   bit (I# i#)
336         | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
337         | otherwise = 0 -- We'll be overbearing, for now..
338
339   testBit (W8# x#) (I# i#)
340     | i# <# 8# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
341     | otherwise             = False -- for now, this is really an error.
342
343   bitSize  _    = 8
344   isSigned _    = False
345
346 pow2# :: Int# -> Int#
347 pow2# x# = word2Int# (shiftL# (int2Word# 1#) x#)
348
349 pow2_64# :: Int# -> Int64#
350 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
351
352 -- ---------------------------------------------------------------------------
353 -- Word16
354 -- ---------------------------------------------------------------------------
355
356 -- The double byte type @Word16@ is represented in the Haskell
357 -- heap by boxing up a machine word, @Word#@. An invariant
358 -- for this representation is that only the lower 16 bits are
359 -- `active', any bits above are {\em always} zeroed out.
360 -- A consequence of this is that operations that could possibly
361 -- overflow have to mask out anything above the lower two bytes
362 -- before putting together the resulting @Word16@.
363
364 data Word16 = W16# Word#
365
366 instance CCallable Word16
367 instance CReturnable Word16
368
369 word16ToWord8  (W16# x) = W8#  (wordToWord8#  x)
370 word16ToWord32 (W16# x) = W32# x
371
372 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
373
374 -- mask out upper 16 bits.
375 intToWord16# :: Int# -> Word#
376 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
377
378 wordToWord16# :: Word# -> Word#
379 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
380
381 instance Eq  Word16    where 
382   (W16# x) == (W16# y) = x `eqWord#` y
383   (W16# x) /= (W16# y) = x `neWord#` y
384
385 instance Ord Word16     where
386   compare (W16# x#) (W16# y#) = compareWord# x# y#
387   (<)  (W16# x) (W16# y)      = x `ltWord#` y
388   (<=) (W16# x) (W16# y)      = x `leWord#` y
389   (>=) (W16# x) (W16# y)      = x `geWord#` y
390   (>)  (W16# x) (W16# y)      = x `gtWord#` y
391   max x@(W16# x#) y@(W16# y#) = 
392      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
393   min x@(W16# x#) y@(W16# y#) =
394      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
395
396
397
398 instance Num Word16 where
399   (W16# x) + (W16# y) = 
400        W16# (intToWord16# (word2Int# x +# word2Int# y))
401   (W16# x) - (W16# y) = 
402        W16# (intToWord16# (word2Int# x -# word2Int# y))
403   (W16# x) * (W16# y) = 
404        W16# (intToWord16# (word2Int# x *# word2Int# y))
405   negate w@(W16# x)  = 
406        if x' ==# 0# 
407         then w
408         else W16# (int2Word# (0x10000# -# x'))
409        where
410         x' = word2Int# x
411   abs x         = x
412   signum        = signumReal
413   fromInteger (S# i#)    = W16# (wordToWord16# (int2Word# i#))
414   fromInteger (J# s# d#) = W16# (wordToWord16# (integer2Word# s# d#))
415
416 instance Bounded Word16 where
417   minBound = 0
418   maxBound = 0xffff
419
420 instance Real Word16 where
421   toRational x = toInteger x % 1
422
423 instance Integral Word16 where
424   div  x@(W16# x#)  (W16# y#)
425    | y# `neWord#` (int2Word# 0#) = W16# (x# `quotWord#` y#)
426    | otherwise                   = divZeroError "div{Word16}" x
427
428   quot x@(W16# x#) (W16# y#)
429    | y# `neWord#`(int2Word# 0#)  = W16# (x# `quotWord#` y#)
430    | otherwise                   = divZeroError "quot{Word16}" x
431
432   rem  x@(W16# x#) (W16# y#)
433    | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
434    | otherwise                   = divZeroError "rem{Word16}" x
435
436   mod  x@(W16# x#)  (W16# y#)
437    | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
438    | otherwise                   = divZeroError "mod{Word16}" x
439
440   quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
441   divMod  (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
442
443   toInteger = toInteger . word16ToInt
444
445 instance Ix Word16 where
446   range (m,n)          = [m..n]
447   index b@(m,_) i
448          | inRange b i = word16ToInt (i - m)
449          | otherwise   = indexError b i "Word16"
450   inRange (m,n) i      = m <= i && i <= n
451
452 instance Enum Word16 where
453     succ w          
454       | w == maxBound = succError "Word16"
455       | otherwise     = w+1
456     pred w          
457       | w == minBound = predError "Word16"
458       | otherwise     = w-1
459
460     toEnum   i@(I# i#)  
461       | i >= fromIntegral (minBound::Word16) && i <= fromIntegral (maxBound::Word16)
462       = W16# (intToWord16# i#)
463       | otherwise
464       = toEnumError "Word16" i (minBound::Word16,maxBound::Word16)
465
466     fromEnum  (W16# w) = I# (word2Int# w)
467     enumFrom     = boundedEnumFrom
468     enumFromThen = boundedEnumFromThen
469
470 instance Read Word16 where
471   readsPrec _ = readDec
472
473 instance Show Word16 where
474   showsPrec p w16 = showsPrec p (word16ToInt w16)
475
476 instance Bits Word16 where
477   (W16# x)  .&.  (W16# y)  = W16# (x `and#` y)
478   (W16# x)  .|.  (W16# y)  = W16# (x `or#` y)
479   (W16# x) `xor` (W16# y)  = W16# (x `xor#` y)
480   complement (W16# x)      = W16# (x `xor#` int2Word# 0xffff#)
481   shift (W16# x#) i@(I# i#)
482         | i > 0     = W16# (wordToWord16# (shiftL# x# i#))
483         | otherwise = W16# (shiftRL# x# (negateInt# i#))
484   w@(W16# x)  `rotate` (I# i)
485         | i ==# 0#    = w
486         | i ># 0#     = W16# ((wordToWord16# (shiftL# x i')) `or#`
487                               (shiftRL# (x `and#` 
488                                          (int2Word# (0x10000# -# pow2# i2)))
489                                         i2))
490         | otherwise = rotate w (I# (16# +# i'))
491           where
492            i' = word2Int# (int2Word# i `and#` int2Word# 15#)
493            i2 = 16# -# i'
494   bit (I# i#)
495         | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
496         | otherwise = 0 -- We'll be overbearing, for now..
497
498   testBit (W16# x#) (I# i#)
499     | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
500     | otherwise             = False -- for now, this is really an error.
501
502   bitSize  _    = 16
503   isSigned _    = False
504
505 -- ---------------------------------------------------------------------------
506 -- Word32
507 -- ---------------------------------------------------------------------------
508
509 -- The quad byte type @Word32@ is represented in the Haskell
510 -- heap by boxing up a machine word, @Word#@. An invariant
511 -- for this representation is that any bits above the lower
512 -- 32 are {\em always} zeroed out. A consequence of this is that
513 -- operations that could possibly overflow have to mask
514 -- the result before building the resulting @Word16@.
515
516 data Word32 = W32# Word#
517
518 instance CCallable Word32
519 instance CReturnable Word32
520
521 instance Eq  Word32    where 
522   (W32# x) == (W32# y) = x `eqWord#` y
523   (W32# x) /= (W32# y) = x `neWord#` y
524
525 instance Ord Word32    where
526   compare (W32# x#) (W32# y#) = compareWord# x# y#
527   (<)  (W32# x) (W32# y)      = x `ltWord#` y
528   (<=) (W32# x) (W32# y)      = x `leWord#` y
529   (>=) (W32# x) (W32# y)      = x `geWord#` y
530   (>)  (W32# x) (W32# y)      = x `gtWord#` y
531   max x@(W32# x#) y@(W32# y#) = 
532      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
533   min x@(W32# x#) y@(W32# y#) =
534      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
535
536 instance Num Word32 where
537   (W32# x) + (W32# y) = 
538        W32# (intToWord32# (word2Int# x +# word2Int# y))
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 #if WORD_SIZE_IN_BYTES == 8
544   negate w@(W32# x)  = 
545       if x' ==# 0#
546        then w
547        else W32# (intToWord32# (0x100000000# -# x'))
548        where
549         x' = word2Int# x
550 #else
551   negate (W32# x)  = W32# (intToWord32# (negateInt# (word2Int# x)))
552 #endif
553   abs x           = x
554   signum          = signumReal
555   fromInteger (S# i#)    = W32# (intToWord32# i#)
556   fromInteger (J# s# d#) = W32# (wordToWord32# (integer2Word# s# d#))
557     -- ToDo: restrict fromInt{eger} range.
558
559 intToWord32#  :: Int#  -> Word#
560 wordToWord32# :: Word# -> Word#
561
562 #if WORD_SIZE_IN_BYTES == 8
563 intToWord32#  i#  = (int2Word# i#) `and#` (int2Word# 0xffffffff#)
564 wordToWord32# w#  = w# `and#` (int2Word# 0xffffffff#)
565 wordToWord64# w#  = w#
566 #else
567 intToWord32#  i# = int2Word# i#
568 wordToWord32# w# = w#
569 #endif
570
571 instance Bounded Word32 where
572     minBound = 0
573 #if WORD_SIZE_IN_BYTES == 8
574     maxBound = 0xffffffff
575 #else
576     maxBound = minBound - 1
577 #endif
578
579 instance Real Word32 where
580     toRational x = toInteger x % 1
581
582 instance Integral Word32 where
583     div  x y 
584       | y /= 0         = quotWord32 x y
585       | otherwise      = divZeroError "div{Word32}" x
586
587     quot x y
588       | y /= 0         = quotWord32 x y
589       | otherwise      = divZeroError "quot{Word32}" x
590
591     rem  x y
592       | y /= 0         = remWord32 x y
593       | otherwise      = divZeroError "rem{Word32}" x
594
595     mod  x y
596       | y /= 0         = remWord32 x y
597       | otherwise      = divZeroError "mod{Word32}" x
598
599     quotRem a b        = (a `quot` b, a `rem` b)
600     divMod x y         = quotRem x y
601
602     toInteger          = word32ToInteger 
603
604
605 {-# INLINE quotWord32 #-}
606 {-# INLINE remWord32  #-}
607 remWord32, quotWord32 :: Word32 -> Word32 -> Word32
608 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
609 (W32# x) `remWord32`  (W32# y) = W32# (x `remWord#`  y)
610
611
612 instance Ix Word32 where
613     range (m,n)          = [m..n]
614     index b@(m,_) i
615            | inRange b i = word32ToInt (i - m)
616            | otherwise   = indexError b i "Word32"
617     inRange (m,n) i      = m <= i && i <= n
618
619 instance Enum Word32 where
620     succ w          
621       | w == maxBound = succError "Word32"
622       | otherwise     = w+1
623     pred w          
624       | w == minBound = predError "Word32"
625       | otherwise     = w-1
626
627      -- the toEnum/fromEnum will fail if the mapping isn't legal,
628      -- use the intTo* & *ToInt coercion functions to 'bypass' these range checks.
629     toEnum   x
630       | x >= 0    = intToWord32 x
631       | otherwise
632       = toEnumError "Word32" x (minBound::Word32,maxBound::Word32)
633
634     fromEnum   x
635       | x <= intToWord32 (maxBound::Int)
636       = word32ToInt x
637       | otherwise
638       = fromEnumError "Word32" x 
639
640     enumFrom w           = [w .. maxBound]
641     enumFromTo   w1 w2
642        | w1 <= w2        = eftt32 True{-increasing-} w1 diff_f last
643        | otherwise       = []
644         where
645          last = (> w2)
646          diff_f x = x + 1 
647           
648     enumFromThen w1 w2   = [w1,w2 .. last]
649        where
650          last :: Word32
651          last
652           | w1 <=w2   = maxBound
653           | otherwise = minBound
654
655     enumFromThenTo w1 w2 wend  = eftt32 increasing w1 step_f last
656      where
657        increasing = w1 <= w2
658        diff1 = w2 - w1
659        diff2 = w1 - w2
660        
661        last
662         | increasing = (> wend)
663         | otherwise  = (< wend)
664
665        step_f 
666         | increasing = \ x -> x + diff1
667         | otherwise  = \ x -> x - diff2
668
669 eftt32 :: Bool -> Word32 -> (Word32 -> Word32) -> (Word32-> Bool) -> [Word32]
670 eftt32 increasing init stepper done = go init
671   where
672     go now
673      | done now                    = []
674      | increasing     && now > nxt = [now] -- oflow
675      | not increasing && now < nxt = [now] -- uflow
676      | otherwise                   = now : go nxt
677      where
678       nxt = stepper now 
679
680 instance Read Word32 where
681     readsPrec _ = readDec
682
683 instance Show Word32 where
684     showsPrec p w = showsPrec p (word32ToInteger w)
685
686 instance Bits Word32 where
687   (W32# x)  .&.  (W32# y)  = W32# (x `and#` y)
688   (W32# x)  .|.  (W32# y)  = W32# (x `or#` y)
689   (W32# x) `xor` (W32# y)  = W32# (x `xor#` y)
690   complement (W32# x)      = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
691   shift (W32# x) i@(I# i#)
692         | i > 0     = W32# (wordToWord32# (shiftL# x i#))
693         | otherwise = W32# (shiftRL# x (negateInt# i#))
694   w@(W32# x)  `rotate` (I# i)
695         | i ==# 0#    = w
696         | i ># 0#     = W32# ((wordToWord32# (shiftL# x i')) `or#`
697                               (shiftRL# (x `and#` 
698                                         (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
699                                      i2))
700         | otherwise = rotate w (I# (32# +# i))
701           where
702            i' = word2Int# (int2Word# i `and#` int2Word# 31#)
703            i2 = 32# -# i'
704            (W32# maxBound#) = maxBound
705
706   bit (I# i#)
707         | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
708         | otherwise = 0 -- We'll be overbearing, for now..
709
710   testBit (W32# x#) (I# i#)
711     | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
712     | otherwise             = False -- for now, this is really an error.
713   bitSize  _        = 32
714   isSigned _        = False
715
716 -- -----------------------------------------------------------------------------
717 -- Word64
718 -- -----------------------------------------------------------------------------
719
720 #if WORD_SIZE_IN_BYTES == 8
721 data Word64 = W64# Word#
722
723 word32ToWord64 (W32 w#) = W64# w#
724
725 word8ToWord64 (W8# w#) = W64# w#
726 word64ToWord8 (W64# w#) = W8# (w# `and#` (int2Word# 0xff#))
727
728 word16ToWord64 (W16# w#) = W64# w#
729 word64ToWord16 (W64# w#) = W16# (w# `and#` (int2Word# 0xffff#))
730
731 wordToWord32# :: Word# -> Word#
732 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
733
734 word64ToWord32 :: Word64 -> Word32
735 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
736
737 wordToWord64# w# = w#
738 word64ToWord# w# = w#
739
740 instance Eq  Word64     where 
741   (W64# x) == (W64# y) = x `eqWord#` y
742   (W64# x) /= (W64# y) = x `neWord#` y
743
744 instance Ord Word64     where 
745   compare (W64# x#) (W64# y#) = compareWord# x# y#
746   (<)  (W64# x) (W64# y)      = x `ltWord#` y
747   (<=) (W64# x) (W64# y)      = x `leWord#` y
748   (>=) (W64# x) (W64# y)      = x `geWord#` y
749   (>)  (W64# x) (W64# y)      = x `gtWord#` y
750   max x@(W64# x#) y@(W64# y#) = 
751      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
752   min x@(W64# x#) y@(W64# y#) =
753      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
754
755 instance Num Word64 where
756   (W64# x) + (W64# y) = 
757       W64# (intToWord64# (word2Int# x +# word2Int# y))
758   (W64# x) - (W64# y) = 
759       W64# (intToWord64# (word2Int# x -# word2Int# y))
760   (W64# x) * (W64# y) = 
761       W64# (intToWord64# (word2Int# x *# word2Int# y))
762   negate w@(W64# x)  = 
763      if x' ==# 0# 
764       then w
765       else W64# (int2Word# (0x100# -# x'))
766      where
767       x' = word2Int# x
768   abs x         = x
769   signum        = signumReal
770   fromInteger (S# i#)    = W64# (int2Word# i#)
771   fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
772
773 -- Note: no need to mask results here 
774 -- as they cannot overflow.
775 instance Integral Word64 where
776   div  x@(W64# x#)  (W64# y#)
777     | y# `neWord#` (int2Word# 0#)  = W64# (x# `quotWord#` y#)
778     | otherwise                    = divZeroError "div{Word64}" x
779
780   quot x@(W64# x#)  (W64# y#)
781     | y# `neWord#` (int2Word# 0#)  = W64# (x# `quotWord#` y#)
782     | otherwise                    = divZeroError "quot{Word64}" x
783
784   rem  x@(W64# x#)  (W64# y#)
785     | y# `neWord#` (int2Word# 0#)  = W64# (x# `remWord#` y#)
786     | otherwise                    = divZeroError "rem{Word64}" x
787
788   mod  (W64# x)  (W64# y)   
789     | y# `neWord#` (int2Word# 0#)  = W64# (x `remWord#` y)
790     | otherwise                    = divZeroError "mod{Word64}" x
791
792   quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
793   divMod  (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
794
795   toInteger (W64# x)        = word2Integer# x
796
797 #else /* WORD_SIZE_IN_BYTES < 8 */
798
799 data Word64 = W64# Word64#
800
801 -- for completeness sake
802 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
803 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
804
805 word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
806 word64ToWord8 (W64# w#) = W8# ((word64ToWord# w#) `and#` (int2Word# 0xff#))
807
808 word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
809 word64ToWord16 (W64# w#) = W16# ((word64ToWord# w#) `and#` (int2Word# 0xffff#))
810
811 word64ToInteger (W64# w#) = 
812   case word64ToInteger# w# of
813     (# s#, p# #) -> J# s# p#
814 word64ToInt (W64# w#) = I# (word2Int# (word64ToWord# w#))
815
816 intToWord64# :: Int# -> Word64#
817 intToWord64# i# = wordToWord64# (int2Word# i#)
818
819 intToWord64 (I# i#) = W64# (intToWord64# i#)
820
821 integerToWord64 (S# i#)    = W64# (intToWord64# i#)
822 integerToWord64 (J# s# d#) = W64# (integerToWord64# s# d#)
823
824 instance Eq  Word64     where 
825   (W64# x) == (W64# y) = x `eqWord64#` y
826   (W64# x) /= (W64# y) = not (x `eqWord64#` y)
827
828 instance Ord Word64     where 
829   compare (W64# x#) (W64# y#) = compareWord64# x# y#
830   (<)  (W64# x) (W64# y)      = x `ltWord64#` y
831   (<=) (W64# x) (W64# y)      = x `leWord64#` y
832   (>=) (W64# x) (W64# y)      = x `geWord64#` y
833   (>)  (W64# x) (W64# y)      = x `gtWord64#` y
834   max x@(W64# x#) y@(W64# y#) = 
835      case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
836   min x@(W64# x#) y@(W64# y#) =
837      case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
838
839 instance Num Word64 where
840   (W64# x) + (W64# y) = 
841       W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
842   (W64# x) - (W64# y) = 
843       W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
844   (W64# x) * (W64# y) = 
845       W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
846   negate w
847      | w == 0     = w
848      | otherwise  = maxBound - w
849
850   abs x         = x
851   signum        = signumReal
852   fromInteger i = integerToWord64 i
853
854 -- Note: no need to mask results here  as they cannot overflow.
855 -- ToDo: protect against div by zero.
856 instance Integral Word64 where
857   div  (W64# x)  (W64# y)   = W64# (x `quotWord64#` y)
858   quot (W64# x)  (W64# y)   = W64# (x `quotWord64#` y)
859   rem  (W64# x)  (W64# y)   = W64# (x `remWord64#` y)
860   mod  (W64# x)  (W64# y)   = W64# (x `remWord64#` y)
861   quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
862   divMod  (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
863   toInteger w64             = word64ToInteger w64
864
865 compareWord64# :: Word64# -> Word64# -> Ordering
866 compareWord64# i# j# 
867  | i# `ltWord64#` j# = LT
868  | i# `eqWord64#` j# = EQ
869  | otherwise         = GT
870
871 -- Word64# primop wrappers:
872
873 ltWord64# :: Word64# -> Word64# -> Bool
874 ltWord64# x# y# = stg_ltWord64 x# y# /=# 0#
875
876 leWord64# :: Word64# -> Word64# -> Bool
877 leWord64# x# y# = stg_leWord64 x# y# /=# 0#
878
879 eqWord64# :: Word64# -> Word64# -> Bool
880 eqWord64# x# y# = stg_eqWord64 x# y# /=# 0#
881       
882 neWord64# :: Word64# -> Word64# -> Bool
883 neWord64# x# y# = stg_neWord64 x# y# /=# 0#
884       
885 geWord64# :: Word64# -> Word64# -> Bool
886 geWord64# x# y# = stg_geWord64 x# y# /=# 0#
887       
888 gtWord64# :: Word64# -> Word64# -> Bool
889 gtWord64# x# y# = stg_gtWord64 x# y# /=# 0#
890
891 foreign import "stg_intToInt64" unsafe intToInt64# :: Int# -> Int64#
892 foreign import "stg_int64ToWord64" unsafe int64ToWord64# :: Int64# -> Word64#
893 foreign import "stg_word64ToInt64" unsafe word64ToInt64# :: Word64# -> Int64#
894 foreign import "stg_wordToWord64" unsafe wordToWord64# :: Word# -> Word64#
895 foreign import "stg_word64ToWord" unsafe word64ToWord# :: Word64# -> Word#
896 foreign import "stg_negateInt64" unsafe negateInt64# :: Int64# -> Int64#
897 foreign import "stg_remWord64" unsafe remWord64# :: Word64# -> Word64# -> Word64#
898 foreign import "stg_quotWord64" unsafe quotWord64# :: Word64# -> Word64# -> Word64#
899 foreign import "stg_timesInt64" unsafe timesInt64# :: Int64# -> Int64# -> Int64#
900 foreign import "stg_minusInt64" unsafe minusInt64# :: Int64# -> Int64# -> Int64#
901 foreign import "stg_plusInt64" unsafe plusInt64# :: Int64# -> Int64# -> Int64#
902 foreign import "stg_gtWord64" unsafe stg_gtWord64 :: Word64# -> Word64# -> Int#
903 foreign import "stg_geWord64" unsafe stg_geWord64 :: Word64# -> Word64# -> Int#
904 foreign import "stg_neWord64" unsafe stg_neWord64 :: Word64# -> Word64# -> Int#
905 foreign import "stg_eqWord64" unsafe stg_eqWord64 :: Word64# -> Word64# -> Int#
906 foreign import "stg_leWord64" unsafe stg_leWord64 :: Word64# -> Word64# -> Int#
907 foreign import "stg_ltWord64" unsafe stg_ltWord64 :: Word64# -> Word64# -> Int#
908
909 #endif
910
911 instance CCallable   Word64
912 instance CReturnable Word64
913
914 instance Enum Word64 where
915     succ w          
916       | w == maxBound = succError "Word64"
917       | otherwise     = w+1
918     pred w          
919       | w == minBound = predError "Word64"
920       | otherwise     = w-1
921
922     toEnum i
923       | i >= 0    = intToWord64 i
924       | otherwise 
925       = toEnumError "Word64" i (minBound::Word64,maxBound::Word64)
926
927     fromEnum w
928       | w <= intToWord64 (maxBound::Int)
929       = word64ToInt w
930       | otherwise
931       = fromEnumError "Word64" w
932
933     enumFrom e1        = map integerToWord64 [word64ToInteger e1 .. word64ToInteger maxBound]
934     enumFromTo e1 e2   = map integerToWord64 [word64ToInteger e1 .. word64ToInteger e2]
935     enumFromThen e1 e2 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger last]
936                        where 
937                           last :: Word64
938                           last 
939                            | e2 < e1   = minBound
940                            | otherwise = maxBound
941
942     enumFromThenTo e1 e2 e3 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger e3]
943
944 instance Show Word64 where
945   showsPrec p x = showsPrec p (word64ToInteger x)
946
947 instance Read Word64 where
948   readsPrec _ s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
949
950 instance Ix Word64 where
951     range (m,n)          = [m..n]
952     index b@(m,_) i
953            | inRange b i = word64ToInt (i-m)
954            | otherwise   = indexError b i "Word64"
955     inRange (m,n) i      = m <= i && i <= n
956
957 instance Bounded Word64 where
958   minBound = 0
959   maxBound = minBound - 1
960
961 instance Real Word64 where
962   toRational x = toInteger x % 1
963
964 #if WORD_SIZE_IN_BYTES == 8
965
966 instance Bits Word64 where
967   (W64# x)  .&.  (W64# y)    = W64# (x `and#` y)
968   (W64# x)  .|.  (W64# y)    = W64# (x `or#` y)
969   (W64# x) `xor` (W64# y)    = W64# (x `xor#` y)
970   complement (W64# x)        = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
971   shift (W64# x#) i@(I# i#)
972         | i > 0     = W64# (shiftL# x# i#)
973         | otherwise = W64# (shiftRL# x# (negateInt# i#))
974
975   w@(W64# x)  `rotate` (I# i)
976         | i ==# 0#    = w
977         | i ># 0#     = W64# (shiftL# x i') `or#`
978                               (shiftRL# (x `and#` 
979                                         (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
980                                      i2))
981         | otherwise = rotate w (I# (64# +# i))
982           where
983            i' = word2Int# (int2Word# i `and#` int2Word# 63#)
984            i2 = 64# -# i'
985            (W64# maxBound#) = maxBound
986
987   bit (I# i#)
988         | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
989         | otherwise = 0 -- We'll be overbearing, for now..
990
991   testBit (W64# x#) (I# i#)
992     | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
993     | otherwise              = False -- for now, this is really an error.
994
995   bitSize  _    = 64
996   isSigned _    = False
997
998 #else /* WORD_SIZE_IN_BYTES < 8 */
999
1000 instance Bits Word64 where
1001   (W64# x)  .&.  (W64# y)    = W64# (x `and64#` y)
1002   (W64# x)  .|.  (W64# y)    = W64# (x `or64#` y)
1003   (W64# x) `xor` (W64# y)    = W64# (x `xor64#` y)
1004   complement (W64# x)        = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
1005   shift (W64# x#) i@(I# i#)
1006         | i > 0     = W64# (shiftL64# x# i#)
1007         | otherwise = W64# (shiftRL64# x# (negateInt# i#))
1008
1009   w@(W64# x)  `rotate` (I# i)
1010         | i ==# 0#    = w
1011         | i ># 0#     = W64# ((shiftL64# x i') `or64#`
1012                               (shiftRL64# (x `and64#` 
1013                                            (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#` 
1014                                                            (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
1015                                      i2)
1016         | otherwise = rotate w (I# (64# +# i))
1017           where
1018            i' = word2Int# (int2Word# i `and#` int2Word# 63#)
1019            i2 = 64# -# i'
1020            (W64# maxBound#) = maxBound
1021
1022   bit (I# i#)
1023         | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
1024         | otherwise = 0 -- We'll be overbearing, for now..
1025
1026   testBit (W64# x#) (I# i#)
1027     | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
1028     | otherwise              = False -- for now, this is really an error.
1029
1030   bitSize  _    = 64
1031   isSigned _    = False
1032
1033 foreign import "stg_not64"     unsafe not64#    :: Word64# -> Word64#
1034 foreign import "stg_xor64"     unsafe xor64#    :: Word64# -> Word64# -> Word64#
1035 foreign import "stg_or64"      unsafe or64#     :: Word64# -> Word64# -> Word64#
1036 foreign import "stg_and64"     unsafe and64#    :: Word64# -> Word64# -> Word64#
1037 foreign import "stg_shiftRL64" unsafe shiftRL64# :: Word64# -> Int# -> Word64#
1038 foreign import "stg_shiftL64"  unsafe shiftL64#  :: Word64# -> Int# -> Word64#
1039
1040 #endif /* WORD_SIZE_IN_BYTES < 8 */
1041 \end{code}
1042
1043 Misc utils.
1044
1045 \begin{code}
1046 signumReal :: (Ord a, Num a) => a -> a
1047 signumReal x | x == 0    =  0
1048              | x > 0     =  1
1049              | otherwise = -1
1050 \end{code}
1051
1052 Utils for generating friendly error messages.
1053
1054 \begin{code}
1055 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1056 toEnumError inst_ty tag bnds
1057   = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1058            (showParen True (showsPrec 0 tag) $
1059              " is outside of bounds " ++
1060              show bnds))
1061
1062 fromEnumError :: (Show a,Show b) => String -> a -> b
1063 fromEnumError inst_ty tag
1064   = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1065            (showParen True (showsPrec 0 tag) $
1066              " is outside of Int's bounds " ++
1067              show (minBound::Int,maxBound::Int)))
1068
1069 succError :: String -> a
1070 succError inst_ty
1071   = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1072
1073 predError :: String -> a
1074 predError inst_ty
1075   = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1076
1077 divZeroError :: (Show a) => String -> a -> b
1078 divZeroError meth v 
1079   = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")
1080 \end{code}