480f28a982793b1ad9893d4df4e7d2bdf40639d4
[ghc-hetmet.git] / ghc / lib / exts / Word.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1997
3 %
4 \section[Word]{Module @Word@}
5
6 GHC implementation of the standard Hugs/GHC @Word@
7 interface, types and operations over unsigned, sized
8 quantities.
9
10 \begin{code}
11 #include "MachDeps.h"
12
13 module Word
14         ( Word8          -- all abstract.
15         , Word16         -- instances: Eq, Ord
16         , Word32         --  Num, Bounded, Real,
17         , Word64         --  Integral, Ix, Enum,
18                          --  Read, Show, Bits,
19                          --  CCallable, CReturnable
20                          --  (last two are GHC specific.)
21
22         , word8ToWord16   -- :: Word8  -> Word16
23         , word8ToWord32   -- :: Word8  -> Word32
24         , word8ToWord64   -- :: Word8  -> Word64
25
26         , word16ToWord8   -- :: Word16 -> Word32
27         , word16ToWord32  -- :: Word16 -> Word32
28         , word16ToWord64  -- :: Word8  -> Word64
29
30         , word32ToWord8   -- :: Word32 -> Word8
31         , word32ToWord16  -- :: Word32 -> Word16
32         , word32ToWord64  -- :: Word32 -> Word64
33
34         , word64ToWord8   -- :: Word64 -> Word8
35         , word64ToWord16  -- :: Word64 -> Word16
36         , word64ToWord32  -- :: Word64 -> Word32
37         
38         , word8ToInt      -- :: Word8  -> Int
39         , word16ToInt     -- :: Word16 -> Int
40         , word32ToInt     -- :: Word32 -> Int
41         , word64ToInt     -- :: Word64 -> Int
42
43         , intToWord8      -- :: Int    -> Word8
44         , intToWord16     -- :: Int    -> Word16
45         , intToWord32     -- :: Int    -> Word32
46         , intToWord64     -- :: Int    -> Word64
47
48         , word8ToInteger  -- :: Word8   -> Integer
49         , word16ToInteger -- :: Word16  -> Integer
50         , word32ToInteger -- :: Word32  -> Integer
51         , word64ToInteger -- :: Word64  -> Integer
52
53         , integerToWord8  -- :: Integer -> Word8
54         , integerToWord16 -- :: Integer -> Word16
55         , integerToWord32 -- :: Integer -> Word32
56         , integerToWord64 -- :: Integer -> Word64
57
58         -- NB! GHC SPECIFIC:
59         , wordToWord8     -- :: Word   -> Word8
60         , wordToWord16    -- :: Word   -> Word16
61         , wordToWord32    -- :: Word   -> Word32
62         , wordToWord64    -- :: Word   -> Word64
63
64         , word8ToWord     -- :: Word8  -> Word
65         , word16ToWord    -- :: Word16 -> Word
66         , word32ToWord    -- :: Word32 -> Word
67         , word64ToWord    -- :: Word64 -> Word
68
69         -- The "official" place to get these from is Addr.
70         , indexWord8OffAddr
71         , indexWord16OffAddr
72         , indexWord32OffAddr
73         , indexWord64OffAddr
74         
75         , readWord8OffAddr
76         , readWord16OffAddr
77         , readWord32OffAddr
78         , readWord64OffAddr
79         
80         , writeWord8OffAddr
81         , writeWord16OffAddr
82         , writeWord32OffAddr
83         , writeWord64OffAddr
84         
85         , sizeofWord8
86         , sizeofWord16
87         , sizeofWord32
88         , sizeofWord64
89
90         -- The "official" place to get these from is Foreign
91 #ifndef __PARALLEL_HASKELL__
92         , indexWord8OffForeignObj
93         , indexWord16OffForeignObj
94         , indexWord32OffForeignObj
95         , indexWord64OffForeignObj
96         
97         , readWord8OffForeignObj
98         , readWord16OffForeignObj
99         , readWord32OffForeignObj
100         , readWord64OffForeignObj
101         
102         , writeWord8OffForeignObj
103         , writeWord16OffForeignObj
104         , writeWord32OffForeignObj
105         , writeWord64OffForeignObj
106 #endif
107         
108         -- non-standard, GHC specific
109         , wordToInt
110
111         -- Internal, do not use.
112         , word8ToWord#
113         , word16ToWord#
114         , word32ToWord#
115
116         ) where
117
118 #ifdef __HUGS__
119 import PreludeBuiltin
120 #else
121 import PrelBase
122 import CCall
123 import PrelForeign
124 import PrelIOBase
125 import PrelAddr
126 #endif
127 import Ix
128 import PrelNum ( Num(..), Integral(..) )        -- To get fromInt/toInt
129 import Bits
130 import Ratio
131 import Numeric (readDec, showInt)
132
133 -----------------------------------------------------------------------------
134 -- The "official" coercion functions
135 -----------------------------------------------------------------------------
136
137 word8ToWord32  :: Word8  -> Word32
138 word16ToWord32 :: Word16 -> Word32
139 word32ToWord8  :: Word32 -> Word8
140 word32ToWord16 :: Word32 -> Word16
141
142 word8ToInt      :: Word8  -> Int
143 word16ToInt     :: Word16 -> Int
144 intToWord8      :: Int    -> Word8
145 intToWord16     :: Int    -> Word16
146
147 integerToWord8  :: Integer -> Word8
148 integerToWord16 :: Integer -> Word16
149
150 word8ToInt      = word32ToInt     . word8ToWord32
151 intToWord8      = word32ToWord8   . intToWord32
152 word16ToInt     = word32ToInt     . word16ToWord32
153 intToWord16     = word32ToWord16  . intToWord32
154 word8ToInteger  = word32ToInteger . word8ToWord32
155 word16ToInteger = word32ToInteger . word16ToWord32
156 integerToWord8  = fromInteger
157 integerToWord16 = fromInteger
158
159 intToWord32 :: Int -> Word32
160 intToWord32 (I# x)   = W32# ((int2Word# x) `and#` (case (maxBound::Word32) of W32# x# -> x#))
161 --intToWord32 (I# x)   = W32# (int2Word# x)
162
163 word32ToInt :: Word32 -> Int
164 word32ToInt (W32# x) = I#   (word2Int# x)
165
166 word32ToInteger :: Word32 -> Integer
167 word32ToInteger (W32# x) = word2Integer x
168
169 integerToWord32 :: Integer -> Word32
170 integerToWord32 = fromInteger
171
172 \end{code}
173
174 \subsection[Word8]{The @Word8@ interface}
175
176 The byte type @Word8@ is represented in the Haskell
177 heap by boxing up a 32-bit quantity, @Word#@. An invariant
178 for this representation is that the higher 24 bits are
179 *always* zeroed out. A consequence of this is that
180 operations that could possibly overflow have to mask
181 out the top three bytes before building the resulting @Word8@.
182
183 \begin{code}
184 data Word8  = W8# Word#
185
186 instance CCallable Word8
187 instance CReturnable Word8
188
189 word8ToWord32 (W8#  x) = W32# x
190 word8ToWord16 (W8#  x) = W16# x
191 word32ToWord8 (W32# x) = W8# (wordToWord8# x)
192
193 -- mask out upper three bytes.
194 intToWord8# :: Int# -> Word#
195 intToWord8# i# = (int2Word# i#) `and#` (int2Word# 0xff#)
196
197 wordToWord8# :: Word# -> Word#
198 wordToWord8# w# = w# `and#` (int2Word# 0xff#)
199
200 instance Eq  Word8     where 
201   (W8# x) == (W8# y) = x `eqWord#` y
202   (W8# x) /= (W8# y) = x `neWord#` y
203
204 instance Ord Word8     where 
205   compare (W8# x#) (W8# y#) = compareWord# x# y#
206   (<)  (W8# x) (W8# y)      = x `ltWord#` y
207   (<=) (W8# x) (W8# y)      = x `leWord#` y
208   (>=) (W8# x) (W8# y)      = x `geWord#` y
209   (>)  (W8# x) (W8# y)      = x `gtWord#` y
210   max x@(W8# x#) y@(W8# y#) = 
211      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
212   min x@(W8# x#) y@(W8# y#) =
213      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
214
215 -- Helper function, used by Ord Word* instances.
216 compareWord# :: Word# -> Word# -> Ordering
217 compareWord# x# y# 
218  | x# `ltWord#` y# = LT
219  | x# `eqWord#` y# = EQ
220  | otherwise       = GT
221
222 instance Num Word8 where
223   (W8# x) + (W8# y) = 
224       W8# (intToWord8# (word2Int# x +# word2Int# y))
225   (W8# x) - (W8# y) = 
226       W8# (intToWord8# (word2Int# x -# word2Int# y))
227   (W8# x) * (W8# y) = 
228       W8# (intToWord8# (word2Int# x *# word2Int# y))
229   negate w@(W8# x)  = 
230      if x' ==# 0# 
231       then w
232       else W8# (int2Word# (0x100# -# x'))
233      where
234       x' = word2Int# x
235   abs x         = x
236   signum        = signumReal
237   fromInteger (S# i#)    = W8# (wordToWord8# (int2Word# i#))
238   fromInteger (J# s# d#) = W8# (wordToWord8# (integer2Word# s# d#))
239   fromInt       = intToWord8
240
241 instance Bounded Word8 where
242   minBound = 0
243   maxBound = 0xff
244
245 instance Real Word8 where
246   toRational x = toInteger x % 1
247
248 -- Note: no need to mask results here 
249 -- as they cannot overflow.
250 instance Integral Word8 where
251   div  x@(W8# x#)  (W8# y#) 
252     | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
253     | otherwise                   = divZeroError "div{Word8}" x
254
255   quot x@(W8# x#)  (W8# y#)   
256     | y# `neWord#` (int2Word# 0#) = W8# (x# `quotWord#` y#)
257     | otherwise                   = divZeroError "quot{Word8}" x
258
259   rem  x@(W8# x#)  (W8# y#)
260     | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
261     | otherwise                   = divZeroError "rem{Word8}" x
262
263   mod  x@(W8# x#)  (W8# y#)
264     | y# `neWord#` (int2Word# 0#) = W8# (x# `remWord#` y#)
265     | otherwise                   = divZeroError "mod{Word8}" x
266
267   quotRem (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
268   divMod  (W8# x) (W8# y) = (W8# (x `quotWord#` y), W8# (x `remWord#` y))
269
270   toInteger (W8# x)       = word2Integer x
271   toInt x                 = word8ToInt x
272
273 instance Ix Word8 where
274     range (m,n)          = [m..n]
275     index b@(m,_) i
276            | inRange b i = word8ToInt (i-m)
277            | otherwise   = indexError i b "Word8"
278     inRange (m,n) i      = m <= i && i <= n
279
280 instance Enum Word8 where
281     succ w          
282       | w == maxBound = succError "Word8"
283       | otherwise     = w+1
284     pred w          
285       | w == minBound = predError "Word8"
286       | otherwise     = w-1
287
288     toEnum   i@(I# i#)  
289       | i >= toInt (minBound::Word8) && i <= toInt (maxBound::Word8) 
290       = W8# (intToWord8# i#)
291       | otherwise
292       = toEnumError "Word8" i (minBound::Word8,maxBound::Word8)
293
294     fromEnum  (W8# w) = I# (word2Int# w)
295     enumFrom c        = map toEnum [fromEnum c .. fromEnum (maxBound::Word8)]
296     enumFromThen c d  = map toEnum [fromEnum c, fromEnum d .. fromEnum last]
297                         where 
298                          last :: Word8
299                          last 
300                           | d < c     = minBound
301                           | otherwise = maxBound
302
303 instance Read Word8 where
304     readsPrec _ = readDec
305
306 instance Show Word8 where
307     showsPrec _ = showInt
308
309 --
310 -- Word8s are represented by an (unboxed) 32-bit Word.
311 -- The invariant is that the upper 24 bits are always zeroed out.
312 --
313 instance Bits Word8 where
314   (W8# x)  .&.  (W8# y)    = W8# (x `and#` y)
315   (W8# x)  .|.  (W8# y)    = W8# (x `or#` y)
316   (W8# x) `xor` (W8# y)    = W8# (x `xor#` y)
317   complement (W8# x)       = W8# (x `xor#` int2Word# 0xff#)
318   shift (W8# x#) i@(I# i#)
319         | i > 0     = W8# (wordToWord8# (shiftL# x# i#))
320         | otherwise = W8# (wordToWord8# (shiftRL# x# (negateInt# i#)))
321   w@(W8# x)  `rotate` (I# i)
322         | i ==# 0#    = w
323         | i ># 0#     = W8# ((wordToWord8# (shiftL# x i')) `or#`
324                              (shiftRL# (x `and#` 
325                                         (int2Word# (0x100# -# pow2# i2)))
326                                        i2))
327         | otherwise = rotate w (I# (8# +# i))
328           where
329            i' = word2Int# (int2Word# i `and#` int2Word# 7#)
330            i2 = 8# -# i'
331
332   bit (I# i#)
333         | i# >=# 0# && i# <=# 7# = W8# (wordToWord8# (shiftL# (int2Word# 1#) i#))
334         | otherwise = 0 -- We'll be overbearing, for now..
335
336   setBit x i    = x .|. bit i
337   clearBit x i  = x .&. complement (bit i)
338   complementBit x i = x `xor` bit i
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 word2Integer :: Word# -> Integer
351 word2Integer w = case word2Integer# w of
352                         (# s, d #) -> J# s d
353
354 pow2_64# :: Int# -> Int64#
355 pow2_64# x# = word64ToInt64# (shiftL64# (wordToWord64# (int2Word# 1#)) x#)
356
357 sizeofWord8 :: Word32
358 sizeofWord8 = 1
359
360 \end{code}
361
362 \subsection[Word16]{The @Word16@ interface}
363
364 The double byte type @Word16@ is represented in the Haskell
365 heap by boxing up a machine word, @Word#@. An invariant
366 for this representation is that only the lower 16 bits are
367 `active', any bits above are {\em always} zeroed out.
368 A consequence of this is that operations that could possibly
369 overflow have to mask out anything above the lower two bytes
370 before putting together the resulting @Word16@.
371
372 \begin{code}
373 data Word16 = W16# Word#
374 instance CCallable Word16
375 instance CReturnable Word16
376
377 word16ToWord32 (W16# x) = W32# x
378 word16ToWord8  (W16# x) = W8# (wordToWord8# x)
379 word32ToWord16 (W32# x) = W16# (wordToWord16# x)
380
381 -- mask out upper 16 bits.
382 intToWord16# :: Int# -> Word#
383 intToWord16# i# = ((int2Word# i#) `and#` (int2Word# 0xffff#))
384
385 wordToWord16# :: Word# -> Word#
386 wordToWord16# w# = w# `and#` (int2Word# 0xffff#)
387
388 instance Eq  Word16    where 
389   (W16# x) == (W16# y) = x `eqWord#` y
390   (W16# x) /= (W16# y) = x `neWord#` y
391
392 instance Ord Word16     where
393   compare (W16# x#) (W16# y#) = compareWord# x# y#
394   (<)  (W16# x) (W16# y)      = x `ltWord#` y
395   (<=) (W16# x) (W16# y)      = x `leWord#` y
396   (>=) (W16# x) (W16# y)      = x `geWord#` y
397   (>)  (W16# x) (W16# y)      = x `gtWord#` y
398   max x@(W16# x#) y@(W16# y#) = 
399      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
400   min x@(W16# x#) y@(W16# y#) =
401      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
402
403 instance Num Word16 where
404   (W16# x) + (W16# y) = 
405        W16# (intToWord16# (word2Int# x +# word2Int# y))
406   (W16# x) - (W16# y) = 
407        W16# (intToWord16# (word2Int# x -# word2Int# y))
408   (W16# x) * (W16# y) = 
409        W16# (intToWord16# (word2Int# x *# word2Int# y))
410   negate w@(W16# x)  = 
411        if x' ==# 0# 
412         then w
413         else W16# (int2Word# (0x10000# -# x'))
414        where
415         x' = word2Int# x
416   abs x         = x
417   signum        = signumReal
418   fromInteger (S# i#)    = W16# (wordToWord16# (int2Word# i#))
419   fromInteger (J# s# d#) = W16# (wordToWord16# (integer2Word# s# d#))
420   fromInt       = intToWord16
421
422 instance Bounded Word16 where
423   minBound = 0
424   maxBound = 0xffff
425
426 instance Real Word16 where
427   toRational x = toInteger x % 1
428
429 instance Integral Word16 where
430   div  x@(W16# x#)  (W16# y#)
431    | y# `neWord#` (int2Word# 0#) = W16# (x# `quotWord#` y#)
432    | otherwise                   = divZeroError "div{Word16}" x
433
434   quot x@(W16# x#) (W16# y#)
435    | y# `neWord#`(int2Word# 0#)  = W16# (x# `quotWord#` y#)
436    | otherwise                   = divZeroError "quot{Word16}" x
437
438   rem  x@(W16# x#) (W16# y#)
439    | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
440    | otherwise                   = divZeroError "rem{Word16}" x
441
442   mod  x@(W16# x#)  (W16# y#)
443    | y# `neWord#` (int2Word# 0#) = W16# (x# `remWord#` y#)
444    | otherwise                   = divZeroError "mod{Word16}" x
445
446   quotRem (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
447   divMod  (W16# x) (W16# y) = (W16# (x `quotWord#` y), W16# (x `remWord#` y))
448
449   toInteger (W16# x)        = word2Integer x
450   toInt x                   = word16ToInt x
451
452 instance Ix Word16 where
453   range (m,n)          = [m..n]
454   index b@(m,_) i
455          | inRange b i = word16ToInt (i - m)
456          | otherwise   = indexError i b "Word16"
457   inRange (m,n) i      = m <= i && i <= n
458
459 instance Enum Word16 where
460     succ w          
461       | w == maxBound = succError "Word16"
462       | otherwise     = w+1
463     pred w
464       | w == minBound = predError "Word16"
465       | otherwise     = w-1
466
467     toEnum   i@(I# i#)  
468       | i >= toInt (minBound::Word16) && i <= toInt (maxBound::Word16)
469       = W16# (intToWord16# i#)
470       | otherwise
471       = toEnumError "Word16" i (minBound::Word16,maxBound::Word16)
472
473     fromEnum  (W16# w) = I# (word2Int# w)
474     enumFrom c       = map toEnum [fromEnum c .. fromEnum (maxBound::Word16)]
475     enumFromThen c d = map toEnum [fromEnum c, fromEnum d .. fromEnum last]
476                        where 
477                          last :: Word16
478                          last 
479                           | d < c     = minBound
480                           | otherwise = maxBound
481
482 instance Read Word16 where
483   readsPrec _ = readDec
484
485 instance Show Word16 where
486   showsPrec _ = showInt
487
488 instance Bits Word16 where
489   (W16# x)  .&.  (W16# y)  = W16# (x `and#` y)
490   (W16# x)  .|.  (W16# y)  = W16# (x `or#` y)
491   (W16# x) `xor` (W16# y)  = W16# (x `xor#` y)
492   complement (W16# x)      = W16# (x `xor#` int2Word# 0xffff#)
493   shift (W16# x#) i@(I# i#)
494         | i > 0     = W16# (wordToWord16# (shiftL# x# i#))
495         | otherwise = W16# (shiftRL# x# (negateInt# i#))
496   w@(W16# x)  `rotate` (I# i)
497         | i ==# 0#    = w
498         | i ># 0#     = W16# ((wordToWord16# (shiftL# x i')) `or#`
499                               (shiftRL# (x `and#` 
500                                          (int2Word# (0x10000# -# pow2# i2)))
501                                         i2))
502         | otherwise = rotate w (I# (16# +# i'))
503           where
504            i' = word2Int# (int2Word# i `and#` int2Word# 15#)
505            i2 = 16# -# i'
506   bit (I# i#)
507         | i# >=# 0# && i# <=# 15# = W16# (shiftL# (int2Word# 1#) i#)
508         | otherwise = 0 -- We'll be overbearing, for now..
509
510   setBit x i    = x .|. bit i
511   clearBit x i  = x .&. complement (bit i)
512   complementBit x i = x `xor` bit i
513
514   testBit (W16# x#) (I# i#)
515     | i# <# 16# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
516     | otherwise             = False -- for now, this is really an error.
517
518   bitSize  _    = 16
519   isSigned _    = False
520
521
522 sizeofWord16 :: Word32
523 sizeofWord16 = 2
524
525 \end{code}
526
527 \subsection[Word32]{The @Word32@ interface}
528
529 The quad byte type @Word32@ is represented in the Haskell
530 heap by boxing up a machine word, @Word#@. An invariant
531 for this representation is that any bits above the lower
532 32 are {\em always} zeroed out. A consequence of this is that
533 operations that could possibly overflow have to mask
534 the result before building the resulting @Word16@.
535
536 \begin{code}
537 data Word32 = W32# Word#
538
539 instance CCallable Word32
540 instance CReturnable Word32
541
542 instance Eq  Word32    where 
543   (W32# x) == (W32# y) = x `eqWord#` y
544   (W32# x) /= (W32# y) = x `neWord#` y
545
546 instance Ord Word32    where
547   compare (W32# x#) (W32# y#) = compareWord# x# y#
548   (<)  (W32# x) (W32# y)      = x `ltWord#` y
549   (<=) (W32# x) (W32# y)      = x `leWord#` y
550   (>=) (W32# x) (W32# y)      = x `geWord#` y
551   (>)  (W32# x) (W32# y)      = x `gtWord#` y
552   max x@(W32# x#) y@(W32# y#) = 
553      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
554   min x@(W32# x#) y@(W32# y#) =
555      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
556
557 instance Num Word32 where
558   (W32# x) + (W32# y) = 
559        W32# (intToWord32# (word2Int# x +# word2Int# y))
560   (W32# x) - (W32# y) =
561        W32# (intToWord32# (word2Int# x -# word2Int# y))
562   (W32# x) * (W32# y) = 
563        W32# (intToWord32# (word2Int# x *# word2Int# y))
564 #if WORD_SIZE_IN_BYTES == 8
565   negate w@(W32# x)  = 
566       if x' ==# 0#
567        then w
568        else W32# (intToWord32# (0x100000000# -# x'))
569        where
570         x' = word2Int# x
571 #else
572   negate (W32# x)  = W32# (intToWord32# (negateInt# (word2Int# x)))
573 #endif
574   abs x           = x
575   signum          = signumReal
576   fromInteger (S# i#)    = W32# (intToWord32# i#)
577   fromInteger (J# s# d#) = W32# (wordToWord32# (integer2Word# s# d#))
578   fromInt (I# x)  = W32# (intToWord32# x)
579     -- ToDo: restrict fromInt{eger} range.
580
581 intToWord32#  :: Int#  -> Word#
582 wordToWord32# :: Word# -> Word#
583
584 #if WORD_SIZE_IN_BYTES == 8
585 intToWord32#  i#  = (int2Word# i#) `and#` (int2Word# 0xffffffff#)
586 wordToWord32# w#  = w# `and#` (int2Word# 0xffffffff#)
587 wordToWord64# w#  = w#
588 #else
589 intToWord32#  i# = int2Word# i#
590 wordToWord32# w# = w#
591
592 #endif
593
594 instance Bounded Word32 where
595     minBound = 0
596 #if WORD_SIZE_IN_BYTES == 8
597     maxBound = 0xffffffff
598 #else
599     maxBound = minBound - 1
600 #endif
601
602 instance Real Word32 where
603     toRational x = toInteger x % 1
604
605 instance Integral Word32 where
606     div  x y 
607       | y /= 0         = quotWord32 x y
608       | otherwise      = divZeroError "div{Word32}" x
609
610     quot x y
611       | y /= 0         = quotWord32 x y
612       | otherwise      = divZeroError "quot{Word32}" x
613
614     rem  x y
615       | y /= 0         = remWord32 x y
616       | otherwise      = divZeroError "rem{Word32}" x
617
618     mod  x y
619       | y /= 0         = remWord32 x y
620       | otherwise      = divZeroError "mod{Word32}" x
621
622     quotRem a b        = (a `quotWord32` b, a `remWord32` b)
623     divMod x y         = quotRem x y
624
625     toInteger (W32# x) = word2Integer x
626     toInt     (W32# x) = I# (word2Int# x)
627
628 {-# INLINE quotWord32 #-}
629 {-# INLINE remWord32  #-}
630 remWord32, quotWord32 :: Word32 -> Word32 -> Word32
631 (W32# x) `quotWord32` (W32# y) = W32# (x `quotWord#` y)
632 (W32# x) `remWord32`  (W32# y) = W32# (x `remWord#`  y)
633
634 instance Ix Word32 where
635     range (m,n)          = [m..n]
636     index b@(m,_) i
637            | inRange b i = word32ToInt (i - m)
638            | otherwise   = indexError i b "Word32"
639     inRange (m,n) i      = m <= i && i <= n
640
641 instance Enum Word32 where
642     succ w          
643       | w == maxBound = succError "Word32"
644       | otherwise     = w+1
645     pred w          
646       | w == minBound = predError "Word32"
647       | otherwise     = w-1
648
649      -- the toEnum/fromEnum will fail if the mapping isn't legal,
650      -- use the intTo* & *ToInt coercion functions to 'bypass' these range checks.
651     toEnum   x
652       | x >= 0    = intToWord32 x
653       | otherwise
654       = toEnumError "Word32" x (minBound::Word32,maxBound::Word32)
655
656     fromEnum   x
657       | x <= intToWord32 (maxBound::Int)
658       = word32ToInt x
659       | otherwise
660       = fromEnumError "Word32" x
661
662     enumFrom w           = [w .. maxBound]
663     enumFromTo   w1 w2
664        | w1 <= w2        = eftt32 True{-increasing-} w1 diff_f last
665        | otherwise       = []
666         where
667          last = (> w2)
668          diff_f x = x + 1 
669           
670     enumFromThen w1 w2   = [w1,w2 .. last]
671        where
672          last :: Word32
673          last
674           | w1 <=w2   = maxBound
675           | otherwise = minBound
676
677     enumFromThenTo w1 w2 wend  = eftt32 increasing w1 step_f last
678      where
679        increasing = w1 <= w2
680        diff1 = w2 - w1
681        diff2 = w1 - w2
682        
683        last
684         | increasing = (> wend)
685         | otherwise  = (< wend)
686
687        step_f 
688         | increasing = \ x -> x + diff1
689         | otherwise  = \ x -> x - diff2
690
691
692 eftt32 :: Bool -> Word32 -> (Word32 -> Word32) -> (Word32-> Bool) -> [Word32]
693 eftt32 increasing init stepper done = go init
694   where
695     go now
696      | done now                    = []
697      | increasing     && now > nxt = [now] -- oflow
698      | not increasing && now < nxt = [now] -- uflow
699      | otherwise                   = now : go nxt
700      where
701       nxt = stepper now 
702
703
704 instance Read Word32 where
705     readsPrec _ = readDec
706
707 instance Show Word32 where
708     showsPrec _ = showInt
709
710 instance Bits Word32 where
711   (W32# x)  .&.  (W32# y)  = W32# (x `and#` y)
712   (W32# x)  .|.  (W32# y)  = W32# (x `or#` y)
713   (W32# x) `xor` (W32# y)  = W32# (x `xor#` y)
714   complement (W32# x)      = W32# (x `xor#` mb#) where (W32# mb#) = maxBound
715   shift (W32# x) i@(I# i#)
716         | i > 0     = W32# (wordToWord32# (shiftL# x i#))
717         | otherwise = W32# (shiftRL# x (negateInt# i#))
718   w@(W32# x)  `rotate` (I# i)
719         | i ==# 0#    = w
720         | i ># 0#     = W32# ((wordToWord32# (shiftL# x i')) `or#`
721                               (shiftRL# (x `and#` 
722                                         (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
723                                      i2))
724         | otherwise = rotate w (I# (32# +# i))
725           where
726            i' = word2Int# (int2Word# i `and#` int2Word# 31#)
727            i2 = 32# -# i'
728            (W32# maxBound#) = maxBound
729
730   bit (I# i#)
731         | i# >=# 0# && i# <=# 31# = W32# (shiftL# (int2Word# 1#) i#)
732         | otherwise = 0 -- We'll be overbearing, for now..
733
734   setBit x i        = x .|. bit i
735   clearBit x i      = x .&. complement (bit i)
736   complementBit x i = x `xor` bit i
737
738   testBit (W32# x#) (I# i#)
739     | i# <# 32# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
740     | otherwise             = False -- for now, this is really an error.
741   bitSize  _        = 32
742   isSigned _        = False
743
744 sizeofWord32 :: Word32
745 sizeofWord32 = 4
746 \end{code}
747
748 \subsection[Word64]{The @Word64@ interface}
749
750 \begin{code}
751 #if WORD_SIZE_IN_BYTES == 8
752 --data Word64 = W64# Word#
753
754 word32ToWord64 :: Word32 -> Word64
755 word32ToWord64 (W32 w#) = W64# w#
756
757 word8ToWord64 :: Word8 -> Word64
758 word8ToWord64 (W8# w#) = W64# w#
759
760 word64ToWord8 :: Word64 -> Word8
761 word64ToWord8 (W64# w#) = W8# (w# `and#` (int2Word# 0xff#))
762
763 word16ToWord64 :: Word16 -> Word64
764 word16ToWord64 (W16# w#) = W64# w#
765
766 word64ToWord16 :: Word64 -> Word16
767 word64ToWord16 (W64# w#) = W16# (w# `and#` (int2Word# 0xffff#))
768
769 wordToWord32# :: Word# -> Word#
770 wordToWord32# w# = w# `and#` (case (maxBound::Word32) of W# x# -> x#)
771
772 word64ToWord32 :: Word64 -> Word32
773 word64ToWord32 (W64# w#) = W32# (wordToWord32# w#)
774
775 wordToWord64# w# = w#
776 word64ToWord# w# = w#
777
778 instance Eq  Word64     where 
779   (W64# x) == (W64# y) = x `eqWord#` y
780   (W64# x) /= (W64# y) = x `neWord#` y
781
782 instance Ord Word64     where 
783   compare (W64# x#) (W64# y#) = compareWord# x# y#
784   (<)  (W64# x) (W64# y)      = x `ltWord#` y
785   (<=) (W64# x) (W64# y)      = x `leWord#` y
786   (>=) (W64# x) (W64# y)      = x `geWord#` y
787   (>)  (W64# x) (W64# y)      = x `gtWord#` y
788   max x@(W64# x#) y@(W64# y#) = 
789      case (compareWord# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
790   min x@(W64# x#) y@(W64# y#) =
791      case (compareWord# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
792
793 instance Num Word64 where
794   (W64# x) + (W64# y) = 
795       W64# (intToWord64# (word2Int# x +# word2Int# y))
796   (W64# x) - (W64# y) = 
797       W64# (intToWord64# (word2Int# x -# word2Int# y))
798   (W64# x) * (W64# y) = 
799       W64# (intToWord64# (word2Int# x *# word2Int# y))
800   negate w@(W64# x)  = 
801      if x' ==# 0# 
802       then w
803       else W64# (int2Word# (0x100# -# x'))
804      where
805       x' = word2Int# x
806   abs x         = x
807   signum        = signumReal
808   fromInteger (S# i#)    = W64# (int2Word# i#)
809   fromInteger (J# s# d#) = W64# (integer2Word# s# d#)
810   fromInt       = intToWord64
811
812 -- Note: no need to mask results here 
813 -- as they cannot overflow.
814 instance Integral Word64 where
815   div  x@(W64# x#)  (W64# y#)
816     | y# `neWord#` (int2Word# 0#)  = W64# (x# `quotWord#` y#)
817     | otherwise                    = divZeroError "div{Word64}" x
818
819   quot x@(W64# x#)  (W64# y#)
820     | y# `neWord#` (int2Word# 0#)  = W64# (x# `quotWord#` y#)
821     | otherwise                    = divZeroError "quot{Word64}" x
822
823   rem  x@(W64# x#)  (W64# y#)
824     | y# `neWord#` (int2Word# 0#)  = W64# (x# `remWord#` y#)
825     | otherwise                    = divZeroError "rem{Word64}" x
826
827   mod  (W64# x)  (W64# y)   
828     | y# `neWord#` (int2Word# 0#)  = W64# (x `remWord#` y)
829     | otherwise                    = divZeroError "mod{Word64}" x
830
831   quotRem (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
832   divMod  (W64# x) (W64# y) = (W64# (x `quotWord#` y), W64# (x `remWord#` y))
833
834   toInteger (W64# x)        = word2Integer# x
835   toInt x                   = word64ToInt x
836
837
838 instance Bits Word64 where
839   (W64# x)  .&.  (W64# y)    = W64# (x `and#` y)
840   (W64# x)  .|.  (W64# y)    = W64# (x `or#` y)
841   (W64# x) `xor` (W64# y)    = W64# (x `xor#` y)
842   complement (W64# x)        = W64# (x `xor#` (case (maxBound::Word64) of W64# x# -> x#))
843   shift (W64# x#) i@(I# i#)
844         | i > 0     = W64# (shiftL# x# i#)
845         | otherwise = W64# (shiftRL# x# (negateInt# i#))
846
847   w@(W64# x)  `rotate` (I# i)
848         | i ==# 0#    = w
849         | i ># 0#     = W64# (shiftL# x i') `or#`
850                               (shiftRL# (x `and#` 
851                                         (int2Word# (word2Int# maxBound# -# pow2# i2 +# 1#)))
852                                      i2))
853         | otherwise = rotate w (I# (64# +# i))
854           where
855            i' = word2Int# (int2Word# i `and#` int2Word# 63#)
856            i2 = 64# -# i'
857            (W64# maxBound#) = maxBound
858
859   bit (I# i#)
860         | i# >=# 0# && i# <=# 63# = W64# (shiftL# (int2Word# 1#) i#)
861         | otherwise = 0 -- We'll be overbearing, for now..
862
863   setBit x i    = x .|. bit i
864   clearBit x i  = x .&. complement (bit i)
865   complementBit x i = x `xor` bit i
866
867   testBit (W64# x#) (I# i#)
868     | i# <# 64# && i# >=# 0# = (word2Int# (x# `and#` (shiftL# (int2Word# 1#) i#))) /=# 0#
869     | otherwise              = False -- for now, this is really an error.
870
871   bitSize  _    = 64
872   isSigned _    = False
873
874 #else
875 --defined in PrelCCall: data Word64 = W64 Word64# deriving (Eq, Ord, Bounded)
876
877 -- for completeness sake
878 word32ToWord64 :: Word32 -> Word64
879 word32ToWord64 (W32# w#) = W64# (wordToWord64# w#)
880
881 word64ToWord32 :: Word64 -> Word32
882 word64ToWord32 (W64# w#) = W32# (word64ToWord# w#)
883
884 word8ToWord64 :: Word8 -> Word64
885 word8ToWord64 (W8# w#) = W64# (wordToWord64# w#)
886
887 word64ToWord8 :: Word64 -> Word8
888 word64ToWord8 (W64# w#) = W8# ((word64ToWord# w#) `and#` (int2Word# 0xff#))
889
890 word16ToWord64 :: Word16 -> Word64
891 word16ToWord64 (W16# w#) = W64# (wordToWord64# w#)
892
893 word64ToWord16 :: Word64 -> Word16
894 word64ToWord16 (W64# w#) = W16# ((word64ToWord# w#) `and#` (int2Word# 0xffff#))
895
896
897 word64ToInteger :: Word64 -> Integer
898 word64ToInteger (W64# w#) = 
899   case word64ToInteger# w# of
900     (# s#, p# #) -> J# s# p#
901
902 word64ToInt :: Word64 -> Int
903 word64ToInt w = 
904    case w `quotRem` 0x100000000 of 
905      (_,l) -> toInt (word64ToWord32 l)
906
907 intToWord64# :: Int# -> Word64#
908 intToWord64# i# = wordToWord64# (int2Word# i#)
909
910 intToWord64 :: Int -> Word64
911 intToWord64 (I# i#) = W64# (intToWord64# i#)
912
913 integerToWord64 :: Integer -> Word64
914 integerToWord64 (J# s# d#) = W64# (integerToWord64# s# d#)
915
916 instance Eq  Word64     where 
917   (W64# x) == (W64# y) = x `eqWord64#` y
918   (W64# x) /= (W64# y) = not (x `eqWord64#` y)
919
920 instance Ord Word64     where 
921   compare (W64# x#) (W64# y#) = compareWord64# x# y#
922   (<)  (W64# x) (W64# y)      = x `ltWord64#` y
923   (<=) (W64# x) (W64# y)      = x `leWord64#` y
924   (>=) (W64# x) (W64# y)      = x `geWord64#` y
925   (>)  (W64# x) (W64# y)      = x `gtWord64#` y
926   max x@(W64# x#) y@(W64# y#) = 
927      case (compareWord64# x# y#) of { LT -> y ; EQ -> x ; GT -> x }
928   min x@(W64# x#) y@(W64# y#) =
929      case (compareWord64# x# y#) of { LT -> x ; EQ -> x ; GT -> y }
930
931 instance Num Word64 where
932   (W64# x) + (W64# y) = 
933       W64# (int64ToWord64# (word64ToInt64# x `plusInt64#` word64ToInt64# y))
934   (W64# x) - (W64# y) = 
935       W64# (int64ToWord64# (word64ToInt64# x `minusInt64#` word64ToInt64# y))
936   (W64# x) * (W64# y) = 
937       W64# (int64ToWord64# (word64ToInt64# x `timesInt64#` word64ToInt64# y))
938   negate w
939      | w == 0     = w
940      | otherwise  = maxBound - w
941
942   abs x         = x
943   signum        = signumReal
944   fromInteger i = integerToWord64 i
945   fromInt       = intToWord64
946
947 -- Note: no need to mask results here 
948 -- as they cannot overflow.
949 -- ToDo: protect against div by zero.
950 instance Integral Word64 where
951   div  (W64# x)  (W64# y)   = W64# (x `quotWord64#` y)
952   quot (W64# x)  (W64# y)   = W64# (x `quotWord64#` y)
953   rem  (W64# x)  (W64# y)   = W64# (x `remWord64#` y)
954   mod  (W64# x)  (W64# y)   = W64# (x `remWord64#` y)
955   quotRem (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
956   divMod  (W64# x) (W64# y) = (W64# (x `quotWord64#` y), W64# (x `remWord64#` y))
957   toInteger w64             = word64ToInteger w64
958   toInt x                   = word64ToInt x
959
960
961 instance Bits Word64 where
962   (W64# x)  .&.  (W64# y)    = W64# (x `and64#` y)
963   (W64# x)  .|.  (W64# y)    = W64# (x `or64#` y)
964   (W64# x) `xor` (W64# y)    = W64# (x `xor64#` y)
965   complement (W64# x)        = W64# (x `xor64#` (case (maxBound::Word64) of W64# x# -> x#))
966   shift (W64# x#) i@(I# i#)
967         | i > 0     = W64# (shiftL64# x# i#)
968         | otherwise = W64# (shiftRL64# x# (negateInt# i#))
969
970   w@(W64# x)  `rotate` (I# i)
971         | i ==# 0#    = w
972         | i ># 0#     = W64# ((shiftL64# x i') `or64#`
973                               (shiftRL64# (x `and64#` 
974                                            (int64ToWord64# ((word64ToInt64# maxBound#) `minusInt64#` 
975                                                            (pow2_64# i2 `plusInt64#` (intToInt64# 1#))))))
976                                      i2)
977         | otherwise = rotate w (I# (64# +# i))
978           where
979            i' = word2Int# (int2Word# i `and#` int2Word# 63#)
980            i2 = 64# -# i'
981            (W64# maxBound#) = maxBound
982
983   bit (I# i#)
984         | i# >=# 0# && i# <=# 63# = W64# (shiftL64# (wordToWord64# (int2Word# 1#)) i#)
985         | otherwise = 0 -- We'll be overbearing, for now..
986
987   setBit x i    = x .|. bit i
988   clearBit x i  = x .&. complement (bit i)
989   complementBit x i = x `xor` bit i
990
991   testBit (W64# x#) (I# i#)
992     | i# <# 64# && i# >=# 0# = (word2Int# (word64ToWord# (x# `and64#` (shiftL64# (wordToWord64# (int2Word# 1#)) i#)))) /=# 0#
993     | otherwise              = False -- for now, this is really an error.
994
995   bitSize  _    = 64
996   isSigned _    = False
997
998 compareWord64# :: Word64# -> Word64# -> Ordering
999 compareWord64# i# j# 
1000  | i# `ltWord64#` j# = LT
1001  | i# `eqWord64#` j# = EQ
1002  | otherwise         = GT
1003
1004 -- Word64# primop wrappers:
1005
1006 ltWord64# :: Word64# -> Word64# -> Bool
1007 ltWord64# x# y# =  unsafePerformIO $ do
1008         v <- _ccall_ stg_ltWord64 x# y# 
1009         case (v::Int) of
1010           0 -> return False
1011           _ -> return True
1012       
1013 leWord64# :: Word64# -> Word64# -> Bool
1014 leWord64# x# y# =  unsafePerformIO $ do
1015         v <- _ccall_ stg_leWord64 x# y# 
1016         case (v::Int) of
1017           0 -> return False
1018           _ -> return True
1019       
1020 eqWord64# :: Word64# -> Word64# -> Bool
1021 eqWord64# x# y# =  unsafePerformIO $ do
1022         v <- _ccall_ stg_eqWord64 x# y# 
1023         case (v::Int) of
1024           0 -> return False
1025           _ -> return True
1026       
1027 neWord64# :: Word64# -> Word64# -> Bool
1028 neWord64# x# y# =  unsafePerformIO $ do
1029         v <- _ccall_ stg_neWord64 x# y# 
1030         case (v::Int) of
1031           0 -> return False
1032           _ -> return True
1033       
1034 geWord64# :: Word64# -> Word64# -> Bool
1035 geWord64# x# y# =  unsafePerformIO $ do
1036         v <- _ccall_ stg_geWord64 x# y# 
1037         case (v::Int) of
1038           0 -> return False
1039           _ -> return True
1040       
1041 gtWord64# :: Word64# -> Word64# -> Bool
1042 gtWord64# x# y# =  unsafePerformIO $ do
1043         v <- _ccall_ stg_gtWord64 x# y# 
1044         case (v::Int) of
1045           0 -> return False
1046           _ -> return True
1047
1048 plusInt64# :: Int64# -> Int64# -> Int64#
1049 plusInt64# a# b# = 
1050   case (unsafePerformIO (_ccall_ stg_plusInt64 a# b#)) of
1051     I64# i# -> i#
1052
1053 minusInt64# :: Int64# -> Int64# -> Int64#
1054 minusInt64# a# b# =
1055   case (unsafePerformIO (_ccall_ stg_minusInt64 a# b#)) of
1056     I64# i# -> i#
1057
1058 timesInt64# :: Int64# -> Int64# -> Int64#
1059 timesInt64# a# b# =
1060   case (unsafePerformIO (_ccall_ stg_timesInt64 a# b#)) of
1061     I64# i# -> i#
1062
1063 quotWord64# :: Word64# -> Word64# -> Word64#
1064 quotWord64# a# b# =
1065   case (unsafePerformIO (_ccall_ stg_quotWord64 a# b#)) of
1066     W64# w# -> w#
1067
1068 remWord64# :: Word64# -> Word64# -> Word64#
1069 remWord64# a# b# =
1070   case (unsafePerformIO (_ccall_ stg_remWord64 a# b#)) of
1071     W64# w# -> w#
1072
1073 negateInt64# :: Int64# -> Int64#
1074 negateInt64# a# =
1075   case (unsafePerformIO (_ccall_ stg_negateInt64 a#)) of
1076     I64# i# -> i#
1077
1078 and64# :: Word64# -> Word64# -> Word64#
1079 and64# a# b# =
1080   case (unsafePerformIO (_ccall_ stg_and64 a# b#)) of
1081     W64# w# -> w#
1082
1083 or64# :: Word64# -> Word64# -> Word64#
1084 or64# a# b# =
1085   case (unsafePerformIO (_ccall_ stg_or64 a# b#)) of
1086     W64# w# -> w#
1087
1088 xor64# :: Word64# -> Word64# -> Word64#
1089 xor64# a# b# = 
1090   case (unsafePerformIO (_ccall_ stg_xor64 a# b#)) of
1091     W64# w# -> w#
1092
1093 not64# :: Word64# -> Word64#
1094 not64# a# = 
1095   case (unsafePerformIO (_ccall_ stg_not64 a#)) of
1096     W64# w# -> w#
1097
1098 shiftL64# :: Word64# -> Int# -> Word64#
1099 shiftL64# a# b# =
1100   case (unsafePerformIO (_ccall_ stg_shiftL64 a# b#)) of
1101     W64# w# -> w#
1102
1103 shiftRL64# :: Word64# -> Int# -> Word64#
1104 shiftRL64# a# b# =
1105   case (unsafePerformIO (_ccall_ stg_shiftRL64 a# b#)) of
1106     W64# w# -> w#
1107
1108 word64ToWord# :: Word64# -> Word#
1109 word64ToWord# w64# =
1110   case (unsafePerformIO (_ccall_ stg_word64ToWord w64#)) of
1111     W# w# -> w#
1112       
1113 wordToWord64# :: Word# -> Word64#
1114 wordToWord64# w# =
1115   case (unsafePerformIO (_ccall_ stg_wordToWord64 w#)) of
1116     W64# w64# -> w64#
1117
1118 word64ToInt64# :: Word64# -> Int64#
1119 word64ToInt64# w64# =
1120   case (unsafePerformIO (_ccall_ stg_word64ToInt64 w64#)) of
1121     I64# i# -> i#
1122
1123 int64ToWord64# :: Int64# -> Word64#
1124 int64ToWord64# i64# =
1125   case (unsafePerformIO (_ccall_ stg_int64ToWord64 i64#)) of
1126     W64# w# -> w#
1127
1128 intToInt64# :: Int# -> Int64#
1129 intToInt64# i# =
1130   case (unsafePerformIO (_ccall_ stg_intToInt64 i#)) of
1131     I64# i64# -> i64#
1132       
1133 #endif
1134
1135 instance Enum Word64 where
1136     succ w          
1137       | w == maxBound = succError "Word64"
1138       | otherwise     = w+1
1139     pred w          
1140       | w == minBound = predError "Word64"
1141       | otherwise     = w-1
1142
1143     toEnum i
1144       | i >= 0    = intToWord64 i
1145       | otherwise 
1146       = toEnumError "Word64" i (minBound::Word64,maxBound::Word64)
1147
1148     fromEnum w
1149       | w <= intToWord64 (maxBound::Int)
1150       = word64ToInt w
1151       | otherwise
1152       = fromEnumError "Word64" w
1153
1154     enumFrom e1        = map integerToWord64 [word64ToInteger e1 .. word64ToInteger maxBound]
1155     enumFromTo e1 e2   = map integerToWord64 [word64ToInteger e1 .. word64ToInteger e2]
1156     enumFromThen e1 e2 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger last]
1157                        where 
1158                           last :: Word64
1159                           last 
1160                            | e2 < e1   = minBound
1161                            | otherwise = maxBound
1162
1163     enumFromThenTo e1 e2 e3 = map integerToWord64 [word64ToInteger e1, word64ToInteger e2 .. word64ToInteger e3]
1164
1165 instance Show Word64 where
1166   showsPrec p x = showsPrec p (word64ToInteger x)
1167
1168 instance Read Word64 where
1169   readsPrec _ s = [ (integerToWord64 x,r) | (x,r) <- readDec s ]
1170
1171 instance Ix Word64 where
1172     range (m,n)          = [m..n]
1173     index b@(m,_) i
1174            | inRange b i = word64ToInt (i-m)
1175            | otherwise   = indexError i b "Word64"
1176     inRange (m,n) i      = m <= i && i <= n
1177
1178 instance Bounded Word64 where
1179   minBound = 0
1180   maxBound = minBound - 1
1181
1182 instance Real Word64 where
1183   toRational x = toInteger x % 1
1184
1185 sizeofWord64 :: Word32
1186 sizeofWord64 = 8
1187
1188 \end{code}
1189
1190
1191
1192 The Hugs-GHC extension libraries provide functions for going between
1193 Int and the various (un)signed ints. Here we provide the same for
1194 the GHC specific Word type:
1195
1196 \begin{code}
1197 wordToWord8  :: Word -> Word8
1198 wordToWord16 :: Word -> Word16
1199 wordToWord32 :: Word -> Word32
1200
1201 word8ToWord  :: Word8 -> Word
1202 word16ToWord :: Word16 -> Word
1203 word32ToWord :: Word32 -> Word
1204
1205 word8ToWord#   :: Word8 -> Word#
1206 word16ToWord#  :: Word16 -> Word#
1207 word32ToWord#  :: Word32 -> Word#
1208
1209 word8ToWord  (W8# w#)   = W# w#
1210 word8ToWord# (W8# w#)   = w#
1211
1212 wordToWord8 (W# w#)    = W8# (w# `and#` (case (maxBound::Word8) of W8# x# -> x#))
1213 word16ToWord  (W16# w#) = W# w#
1214 word16ToWord# (W16# w#) = w#
1215
1216 wordToWord16 (W# w#)   = W16# (w# `and#` (case (maxBound::Word16) of W16# x# -> x#))
1217 wordToWord32 (W# w#)   = W32# (w# `and#` (case (maxBound::Word32) of W32# x# -> x#))
1218
1219 word32ToWord  (W32# w#) = W# w#
1220 word32ToWord# (W32# w#) = w#
1221
1222 wordToWord64  :: Word -> Word64
1223 wordToWord64 (W# w#) = W64# (wordToWord64# w#)
1224
1225 -- lossy on 32-bit platforms, but provided nontheless.
1226 word64ToWord :: Word64 -> Word
1227 word64ToWord (W64# w#) = W# (word64ToWord# w#)
1228
1229 \end{code}
1230
1231
1232 --End of exported definitions
1233
1234 The remainder of this file consists of definitions which are only
1235 used in the implementation.
1236
1237 \begin{code}
1238 signumReal :: (Ord a, Num a) => a -> a
1239 signumReal x | x == 0    =  0
1240              | x > 0     =  1
1241              | otherwise = -1
1242
1243 \end{code}
1244
1245 NOTE: the index is in units of the size of the type, *not* bytes.
1246
1247 \begin{code}
1248 indexWord8OffAddr  :: Addr -> Int -> Word8
1249 indexWord8OffAddr (A# a#) (I# i#) = intToWord8 (I# (ord# (indexCharOffAddr# a# i#)))
1250
1251 indexWord16OffAddr :: Addr -> Int -> Word16
1252 indexWord16OffAddr a i =
1253 #ifdef WORDS_BIGENDIAN
1254   intToWord16 ( word8ToInt l + (word8ToInt maxBound) * word8ToInt h)
1255 #else
1256   intToWord16 ( word8ToInt h + (word8ToInt maxBound) * word8ToInt l)
1257 #endif
1258  where
1259    byte_idx = i * 2
1260    l = indexWord8OffAddr a byte_idx
1261    h = indexWord8OffAddr a (byte_idx+1)
1262
1263 indexWord32OffAddr :: Addr -> Int -> Word32
1264 indexWord32OffAddr (A# a#) i = wordToWord32 (W# (indexWordOffAddr# a# i'#))
1265  where
1266    -- adjust index to be in Word units, not Word32 ones.
1267   (I# i'#) 
1268 #if WORD_SIZE_IN_BYTES==8
1269    = i `div` 2
1270 #else
1271    = i
1272 #endif
1273
1274 indexWord64OffAddr :: Addr -> Int -> Word64
1275 indexWord64OffAddr (A# a#) (I# i#)
1276 #if WORD_SIZE_IN_BYTES==8
1277  = W64# (indexWordOffAddr# a# i#)
1278 #else
1279  = W64# (indexWord64OffAddr# a# i#)
1280 #endif
1281
1282 #ifndef __PARALLEL_HASKELL__
1283
1284 indexWord8OffForeignObj  :: ForeignObj -> Int -> Word8
1285 indexWord8OffForeignObj (ForeignObj fo#) (I# i#) = intToWord8 (I# (ord# (indexCharOffForeignObj# fo# i#)))
1286
1287 indexWord16OffForeignObj :: ForeignObj -> Int -> Word16
1288 indexWord16OffForeignObj fo i =
1289 #ifdef WORDS_BIGENDIAN
1290   intToWord16 ( word8ToInt l + (word8ToInt maxBound) * word8ToInt h)
1291 #else
1292   intToWord16 ( word8ToInt h + (word8ToInt maxBound) * word8ToInt l)
1293 #endif
1294  where
1295    byte_idx = i * 2
1296    l = indexWord8OffForeignObj fo byte_idx
1297    h = indexWord8OffForeignObj fo (byte_idx+1)
1298
1299 indexWord32OffForeignObj :: ForeignObj -> Int -> Word32
1300 indexWord32OffForeignObj (ForeignObj fo#) i = wordToWord32 (W# (indexWordOffForeignObj# fo# i'#))
1301  where
1302    -- adjust index to be in Word units, not Word32 ones.
1303   (I# i'#) 
1304 #if WORD_SIZE_IN_BYTES==8
1305    = i `div` 2
1306 #else
1307    = i
1308 #endif
1309
1310 indexWord64OffForeignObj :: ForeignObj -> Int -> Word64
1311 indexWord64OffForeignObj (ForeignObj fo#) (I# i#)
1312 #if WORD_SIZE_IN_BYTES==8
1313  = W64# (indexWordOffForeignObj# fo# i#)
1314 #else
1315  = W64# (indexWord64OffForeignObj# fo# i#)
1316 #endif
1317 #endif
1318
1319 \end{code}
1320
1321 Read words out of mutable memory:
1322
1323 \begin{code}
1324 readWord8OffAddr :: Addr -> Int -> IO Word8
1325 readWord8OffAddr a i = _casm_ `` %r=((StgWord8*)%0)[(StgInt)%1]; '' a i
1326
1327 readWord16OffAddr  :: Addr -> Int -> IO Word16
1328 readWord16OffAddr a i = _casm_ `` %r=((StgWord16*)%0)[(StgInt)%1]; '' a i
1329
1330 readWord32OffAddr  :: Addr -> Int -> IO Word32
1331 readWord32OffAddr a i = _casm_ `` %r=((StgWord32*)%0)[(StgInt)%1]; '' a i
1332
1333 readWord64OffAddr  :: Addr -> Int -> IO Word64
1334 #if WORD_SIZE_IN_BYTES==8
1335 readWord64OffAddr a i = _casm_ `` %r=((StgWord*)%0)[(StgInt)%1]; '' a i
1336 #else
1337 readWord64OffAddr a i = _casm_ `` %r=((StgWord64*)%0)[(StgInt)%1]; '' a i
1338 #endif
1339
1340 #ifndef __PARALLEL_HASKELL__
1341 readWord8OffForeignObj :: ForeignObj -> Int -> IO Word8
1342 readWord8OffForeignObj fo i = _casm_ `` %r=((StgWord8*)%0)[(StgInt)%1]; '' fo i
1343
1344 readWord16OffForeignObj  :: ForeignObj -> Int -> IO Word16
1345 readWord16OffForeignObj fo i = _casm_ `` %r=((StgWord16*)%0)[(StgInt)%1]; '' fo i
1346
1347 readWord32OffForeignObj  :: ForeignObj -> Int -> IO Word32
1348 readWord32OffForeignObj fo i = _casm_ `` %r=((StgWord32*)%0)[(StgInt)%1]; '' fo i
1349
1350 readWord64OffForeignObj  :: ForeignObj -> Int -> IO Word64
1351 #if WORD_SIZE_IN_BYTES==8
1352 readWord64OffForeignObj fo i = _casm_ `` %r=((StgWord*)%0)[(StgInt)%1]; '' fo i
1353 #else
1354 readWord64OffForeignObj fo i = _casm_ `` %r=((StgWord64*)%0)[(StgInt)%1]; '' fo i
1355 #endif
1356
1357 #endif 
1358
1359 \end{code}
1360
1361 Note: we provide primops for the writing via Addrs since that's used
1362 in the IO implementation (a place where we *really* do care about cycles.)
1363
1364 \begin{code}
1365 writeWord8OffAddr  :: Addr -> Int -> Word8  -> IO ()
1366 writeWord8OffAddr (A# a#) (I# i#) (W8# w#) = IO $ \ s# ->
1367       case (writeCharOffAddr# a# i# (chr# (word2Int# w#)) s#) of s2# -> (# s2#, () #)
1368
1369 writeWord16OffAddr :: Addr -> Int -> Word16 -> IO ()
1370 writeWord16OffAddr a i e = _casm_ `` (((StgWord16*)%0)[(StgInt)%1])=(StgWord16)%2; '' a i e
1371
1372 writeWord32OffAddr :: Addr -> Int -> Word32 -> IO ()
1373 writeWord32OffAddr (A# a#) i (W32# w#) = IO $ \ s# ->
1374       case (writeWordOffAddr#  a# i'# w# s#) of s2# -> (# s2#, () #)
1375  where
1376    -- adjust index to be in Word units, not Word32 ones.
1377   (I# i'#) 
1378 #if WORD_SIZE_IN_BYTES==8
1379    = i `div` 2
1380 #else
1381    = i
1382 #endif
1383
1384 writeWord64OffAddr :: Addr -> Int -> Word64 -> IO ()
1385 #if WORD_SIZE_IN_BYTES==8
1386 writeWord64OffAddr (A# a#) (I# i#) (W64# w#) = IO $ \ s# ->
1387       case (writeWordOffAddr#  a# i# w# s#) of s2# -> (# s2#, () #)
1388 #else
1389 writeWord64OffAddr (A# a#) (I# i#) (W64# w#) = IO $ \ s# ->
1390       case (writeWord64OffAddr#  a# i# w# s#) of s2# -> (# s2#, () #)
1391 #endif
1392
1393 #ifndef __PARALLEL_HASKELL__
1394
1395 writeWord8OffForeignObj  :: ForeignObj -> Int -> Word8  -> IO ()
1396 writeWord8OffForeignObj fo i w = _casm_ `` (((StgWord8*)%0)[(StgInt)%1])=(StgWord8)%2; '' fo i w
1397
1398 writeWord16OffForeignObj :: ForeignObj -> Int -> Word16 -> IO ()
1399 writeWord16OffForeignObj fo i w = _casm_ `` (((StgWord16*)%0)[(StgInt)%1])=(StgWord16)%2; '' fo i w
1400
1401 writeWord32OffForeignObj :: ForeignObj -> Int -> Word32 -> IO ()
1402 writeWord32OffForeignObj fo i w = _casm_ `` (((StgWord32*)%0)[(StgInt)%1])=(StgWord32)%2; '' fo i' w
1403  where
1404    -- adjust index to be in Word units, not Word32 ones.
1405   i' 
1406 #if WORD_SIZE_IN_BYTES==8
1407    = i `div` 2
1408 #else
1409    = i
1410 #endif
1411
1412 writeWord64OffForeignObj :: ForeignObj -> Int -> Word64 -> IO ()
1413 # if WORD_SIZE_IN_BYTES==8
1414 writeWord64OffForeignObj fo i e = _casm_ `` (((StgWord*)%0)[(StgInt)%1])=(StgWord)%2; '' fo i e
1415 # else
1416 writeWord64OffForeignObj fo i e = _casm_ `` (((StgWord64*)%0)[(StgInt)%1])=(StgWord64)%2; '' fo i e
1417 # endif
1418
1419 #endif
1420
1421 \end{code}
1422
1423 Utils for generating friendly error messages.
1424
1425 \begin{code}
1426 {-# NOINLINE indexError #-}
1427 indexError :: (Show a) => a -> (a,a) -> String -> b
1428 indexError i rng tp
1429   = error (showString "Ix{" . showString tp . showString "}.index: Index " .
1430            showParen True (showsPrec 0 i) .
1431            showString " out of range " $
1432            showParen True (showsPrec 0 rng) "")
1433
1434 toEnumError :: (Show a,Show b) => String -> a -> (b,b) -> c
1435 toEnumError inst_ty tag bnds
1436   = error ("Enum.toEnum{" ++ inst_ty ++ "}: tag " ++
1437            (showParen True (showsPrec 0 tag) $
1438              " is outside of bounds " ++
1439              show bnds))
1440
1441 fromEnumError :: (Show a,Show b) => String -> a -> b
1442 fromEnumError inst_ty tag
1443   = error ("Enum.fromEnum{" ++ inst_ty ++ "}: value " ++
1444            (showParen True (showsPrec 0 tag) $
1445              " is outside of Int's bounds " ++
1446              show (minBound::Int,maxBound::Int)))
1447
1448 succError :: String -> a
1449 succError inst_ty
1450   = error ("Enum.succ{" ++ inst_ty ++ "}: tried to take `succ' of maxBound")
1451
1452 predError :: String -> a
1453 predError inst_ty
1454   = error ("Enum.pred{" ++ inst_ty ++ "}: tried to take `pred' of minBound")
1455
1456 divZeroError :: (Show a) => String -> a -> b
1457 divZeroError meth v 
1458   = error ("Integral." ++ meth ++ ": divide by 0 (" ++ show v ++ " / 0)")
1459
1460 \end{code}