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