[project @ 2005-02-03 10:32:11 by ross]
[haskell-directory.git] / GHC / Int.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  GHC.Int
5 -- Copyright   :  (c) The University of Glasgow 1997-2002
6 -- License     :  see libraries/base/LICENSE
7 -- 
8 -- Maintainer  :  cvs-ghc@haskell.org
9 -- Stability   :  internal
10 -- Portability :  non-portable (GHC Extensions)
11 --
12 -- The sized integral datatypes, 'Int8', 'Int16', 'Int32', and 'Int64'.
13 --
14 -----------------------------------------------------------------------------
15
16 #include "MachDeps.h"
17
18 -- #hide
19 module GHC.Int (
20     Int8(..), Int16(..), Int32(..), Int64(..))
21     where
22
23 import Data.Bits
24
25 import {-# SOURCE #-} GHC.Err
26 import GHC.Base
27 import GHC.Enum
28 import GHC.Num
29 import GHC.Real
30 import GHC.Read
31 import GHC.Arr
32 import GHC.Word
33 import GHC.Show
34
35 ------------------------------------------------------------------------
36 -- type Int8
37 ------------------------------------------------------------------------
38
39 -- Int8 is represented in the same way as Int. Operations may assume
40 -- and must ensure that it holds only values from its logical range.
41
42 data Int8 = I8# Int# deriving (Eq, Ord)
43 -- ^ 8-bit signed integer type
44
45 instance Show Int8 where
46     showsPrec p x = showsPrec p (fromIntegral x :: Int)
47
48 instance Num Int8 where
49     (I8# x#) + (I8# y#)    = I8# (narrow8Int# (x# +# y#))
50     (I8# x#) - (I8# y#)    = I8# (narrow8Int# (x# -# y#))
51     (I8# x#) * (I8# y#)    = I8# (narrow8Int# (x# *# y#))
52     negate (I8# x#)        = I8# (narrow8Int# (negateInt# x#))
53     abs x | x >= 0         = x
54           | otherwise      = negate x
55     signum x | x > 0       = 1
56     signum 0               = 0
57     signum _               = -1
58     fromInteger (S# i#)    = I8# (narrow8Int# i#)
59     fromInteger (J# s# d#) = I8# (narrow8Int# (integer2Int# s# d#))
60
61 instance Real Int8 where
62     toRational x = toInteger x % 1
63
64 instance Enum Int8 where
65     succ x
66         | x /= maxBound = x + 1
67         | otherwise     = succError "Int8"
68     pred x
69         | x /= minBound = x - 1
70         | otherwise     = predError "Int8"
71     toEnum i@(I# i#)
72         | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
73                         = I8# i#
74         | otherwise     = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
75     fromEnum (I8# x#)   = I# x#
76     enumFrom            = boundedEnumFrom
77     enumFromThen        = boundedEnumFromThen
78
79 instance Integral Int8 where
80     quot    x@(I8# x#) y@(I8# y#)
81         | y /= 0                  = I8# (narrow8Int# (x# `quotInt#` y#))
82         | otherwise               = divZeroError
83     rem     x@(I8# x#) y@(I8# y#)
84         | y /= 0                  = I8# (narrow8Int# (x# `remInt#` y#))
85         | otherwise               = divZeroError
86     div     x@(I8# x#) y@(I8# y#)
87         | y /= 0                  = I8# (narrow8Int# (x# `divInt#` y#))
88         | otherwise               = divZeroError
89     mod     x@(I8# x#) y@(I8# y#)
90         | y /= 0                  = I8# (narrow8Int# (x# `modInt#` y#))
91         | otherwise               = divZeroError
92     quotRem x@(I8# x#) y@(I8# y#)
93         | y /= 0                  = (I8# (narrow8Int# (x# `quotInt#` y#)),
94                                     I8# (narrow8Int# (x# `remInt#` y#)))
95         | otherwise               = divZeroError
96     divMod  x@(I8# x#) y@(I8# y#)
97         | y /= 0                  = (I8# (narrow8Int# (x# `divInt#` y#)),
98                                     I8# (narrow8Int# (x# `modInt#` y#)))
99         | otherwise               = divZeroError
100     toInteger (I8# x#)            = S# x#
101
102 instance Bounded Int8 where
103     minBound = -0x80
104     maxBound =  0x7F
105
106 instance Ix Int8 where
107     range (m,n)              = [m..n]
108     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
109     inRange (m,n) i          = m <= i && i <= n
110     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
111
112 instance Read Int8 where
113     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
114
115 instance Bits Int8 where
116     (I8# x#) .&.   (I8# y#)   = I8# (word2Int# (int2Word# x# `and#` int2Word# y#))
117     (I8# x#) .|.   (I8# y#)   = I8# (word2Int# (int2Word# x# `or#`  int2Word# y#))
118     (I8# x#) `xor` (I8# y#)   = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#))
119     complement (I8# x#)       = I8# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
120     (I8# x#) `shift` (I# i#)
121         | i# >=# 0#           = I8# (narrow8Int# (x# `iShiftL#` i#))
122         | otherwise           = I8# (x# `iShiftRA#` negateInt# i#)
123     (I8# x#) `rotate` (I# i#)
124         | i'# ==# 0# 
125         = I8# x#
126         | otherwise
127         = I8# (narrow8Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
128                                        (x'# `shiftRL#` (8# -# i'#)))))
129         where
130         x'# = narrow8Word# (int2Word# x#)
131         i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
132     bitSize  _                = 8
133     isSigned _                = True
134
135 {-# RULES
136 "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
137 "fromIntegral/a->Int8"    fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#)
138 "fromIntegral/Int8->a"    fromIntegral = \(I8# x#) -> fromIntegral (I# x#)
139   #-}
140
141 ------------------------------------------------------------------------
142 -- type Int16
143 ------------------------------------------------------------------------
144
145 -- Int16 is represented in the same way as Int. Operations may assume
146 -- and must ensure that it holds only values from its logical range.
147
148 data Int16 = I16# Int# deriving (Eq, Ord)
149 -- ^ 16-bit signed integer type
150
151 instance Show Int16 where
152     showsPrec p x = showsPrec p (fromIntegral x :: Int)
153
154 instance Num Int16 where
155     (I16# x#) + (I16# y#)  = I16# (narrow16Int# (x# +# y#))
156     (I16# x#) - (I16# y#)  = I16# (narrow16Int# (x# -# y#))
157     (I16# x#) * (I16# y#)  = I16# (narrow16Int# (x# *# y#))
158     negate (I16# x#)       = I16# (narrow16Int# (negateInt# x#))
159     abs x | x >= 0         = x
160           | otherwise      = negate x
161     signum x | x > 0       = 1
162     signum 0               = 0
163     signum _               = -1
164     fromInteger (S# i#)    = I16# (narrow16Int# i#)
165     fromInteger (J# s# d#) = I16# (narrow16Int# (integer2Int# s# d#))
166
167 instance Real Int16 where
168     toRational x = toInteger x % 1
169
170 instance Enum Int16 where
171     succ x
172         | x /= maxBound = x + 1
173         | otherwise     = succError "Int16"
174     pred x
175         | x /= minBound = x - 1
176         | otherwise     = predError "Int16"
177     toEnum i@(I# i#)
178         | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
179                         = I16# i#
180         | otherwise     = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
181     fromEnum (I16# x#)  = I# x#
182     enumFrom            = boundedEnumFrom
183     enumFromThen        = boundedEnumFromThen
184
185 instance Integral Int16 where
186     quot    x@(I16# x#) y@(I16# y#)
187         | y /= 0                  = I16# (narrow16Int# (x# `quotInt#` y#))
188         | otherwise               = divZeroError
189     rem     x@(I16# x#) y@(I16# y#)
190         | y /= 0                  = I16# (narrow16Int# (x# `remInt#` y#))
191         | otherwise               = divZeroError
192     div     x@(I16# x#) y@(I16# y#)
193         | y /= 0                  = I16# (narrow16Int# (x# `divInt#` y#))
194         | otherwise               = divZeroError
195     mod     x@(I16# x#) y@(I16# y#)
196         | y /= 0                  = I16# (narrow16Int# (x# `modInt#` y#))
197         | otherwise               = divZeroError
198     quotRem x@(I16# x#) y@(I16# y#)
199         | y /= 0                  = (I16# (narrow16Int# (x# `quotInt#` y#)),
200                                     I16# (narrow16Int# (x# `remInt#` y#)))
201         | otherwise               = divZeroError
202     divMod  x@(I16# x#) y@(I16# y#)
203         | y /= 0                  = (I16# (narrow16Int# (x# `divInt#` y#)),
204                                     I16# (narrow16Int# (x# `modInt#` y#)))
205         | otherwise               = divZeroError
206     toInteger (I16# x#)           = S# x#
207
208 instance Bounded Int16 where
209     minBound = -0x8000
210     maxBound =  0x7FFF
211
212 instance Ix Int16 where
213     range (m,n)              = [m..n]
214     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
215     inRange (m,n) i          = m <= i && i <= n
216     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
217
218 instance Read Int16 where
219     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
220
221 instance Bits Int16 where
222     (I16# x#) .&.   (I16# y#)  = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
223     (I16# x#) .|.   (I16# y#)  = I16# (word2Int# (int2Word# x# `or#`  int2Word# y#))
224     (I16# x#) `xor` (I16# y#)  = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
225     complement (I16# x#)       = I16# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
226     (I16# x#) `shift` (I# i#)
227         | i# >=# 0#            = I16# (narrow16Int# (x# `iShiftL#` i#))
228         | otherwise            = I16# (x# `iShiftRA#` negateInt# i#)
229     (I16# x#) `rotate` (I# i#)
230         | i'# ==# 0# 
231         = I16# x#
232         | otherwise
233         = I16# (narrow16Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
234                                          (x'# `shiftRL#` (16# -# i'#)))))
235         where
236         x'# = narrow16Word# (int2Word# x#)
237         i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
238     bitSize  _                 = 16
239     isSigned _                 = True
240
241 {-# RULES
242 "fromIntegral/Word8->Int16"  fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
243 "fromIntegral/Int8->Int16"   fromIntegral = \(I8# x#) -> I16# x#
244 "fromIntegral/Int16->Int16"  fromIntegral = id :: Int16 -> Int16
245 "fromIntegral/a->Int16"      fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
246 "fromIntegral/Int16->a"      fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
247   #-}
248
249 ------------------------------------------------------------------------
250 -- type Int32
251 ------------------------------------------------------------------------
252
253 #if WORD_SIZE_IN_BITS < 32
254
255 data Int32 = I32# Int32#
256 -- ^ 32-bit signed integer type
257
258 instance Eq Int32 where
259     (I32# x#) == (I32# y#) = x# `eqInt32#` y#
260     (I32# x#) /= (I32# y#) = x# `neInt32#` y#
261
262 instance Ord Int32 where
263     (I32# x#) <  (I32# y#) = x# `ltInt32#` y#
264     (I32# x#) <= (I32# y#) = x# `leInt32#` y#
265     (I32# x#) >  (I32# y#) = x# `gtInt32#` y#
266     (I32# x#) >= (I32# y#) = x# `geInt32#` y#
267
268 instance Show Int32 where
269     showsPrec p x = showsPrec p (toInteger x)
270
271 instance Num Int32 where
272     (I32# x#) + (I32# y#)  = I32# (x# `plusInt32#`  y#)
273     (I32# x#) - (I32# y#)  = I32# (x# `minusInt32#` y#)
274     (I32# x#) * (I32# y#)  = I32# (x# `timesInt32#` y#)
275     negate (I32# x#)       = I32# (negateInt32# x#)
276     abs x | x >= 0         = x
277           | otherwise      = negate x
278     signum x | x > 0       = 1
279     signum 0               = 0
280     signum _               = -1
281     fromInteger (S# i#)    = I32# (intToInt32# i#)
282     fromInteger (J# s# d#) = I32# (integerToInt32# s# d#)
283
284 instance Enum Int32 where
285     succ x
286         | x /= maxBound = x + 1
287         | otherwise     = succError "Int32"
288     pred x
289         | x /= minBound = x - 1
290         | otherwise     = predError "Int32"
291     toEnum (I# i#)      = I32# (intToInt32# i#)
292     fromEnum x@(I32# x#)
293         | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
294                         = I# (int32ToInt# x#)
295         | otherwise     = fromEnumError "Int32" x
296     enumFrom            = integralEnumFrom
297     enumFromThen        = integralEnumFromThen
298     enumFromTo          = integralEnumFromTo
299     enumFromThenTo      = integralEnumFromThenTo
300
301 instance Integral Int32 where
302     quot    x@(I32# x#) y@(I32# y#)
303         | y /= 0                  = I32# (x# `quotInt32#` y#)
304         | otherwise               = divZeroError
305     rem     x@(I32# x#) y@(I32# y#)
306         | y /= 0                  = I32# (x# `remInt32#` y#)
307         | otherwise               = divZeroError
308     div     x@(I32# x#) y@(I32# y#)
309         | y /= 0                  = I32# (x# `divInt32#` y#)
310         | otherwise               = divZeroError
311     mod     x@(I32# x#) y@(I32# y#)
312         | y /= 0                  = I32# (x# `modInt32#` y#)
313         | otherwise               = divZeroError
314     quotRem x@(I32# x#) y@(I32# y#)
315         | y /= 0                  = (I32# (x# `quotInt32#` y#), I32# (x# `remInt32#` y#))
316         | otherwise               = divZeroError
317     divMod  x@(I32# x#) y@(I32# y#)
318         | y /= 0                  = (I32# (x# `divInt32#` y#), I32# (x# `modInt32#` y#))
319         | otherwise               = divZeroError
320     toInteger x@(I32# x#)
321         | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
322                                   = S# (int32ToInt# x#)
323         | otherwise               = case int32ToInteger# x# of (# s, d #) -> J# s d
324
325 divInt32#, modInt32# :: Int32# -> Int32# -> Int32#
326 x# `divInt32#` y#
327     | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#)
328         = ((x# `minusInt32#` y#) `minusInt32#` intToInt32# 1#) `quotInt32#` y#
329     | (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
330         = ((x# `minusInt32#` y#) `plusInt32#` intToInt32# 1#) `quotInt32#` y#
331     | otherwise                = x# `quotInt32#` y#
332 x# `modInt32#` y#
333     | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#) ||
334       (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
335         = if r# `neInt32#` intToInt32# 0# then r# `plusInt32#` y# else intToInt32# 0#
336     | otherwise = r#
337     where
338     r# = x# `remInt32#` y#
339
340 instance Read Int32 where
341     readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
342
343 instance Bits Int32 where
344     (I32# x#) .&.   (I32# y#)  = I32# (word32ToInt32# (int32ToWord32# x# `and32#` int32ToWord32# y#))
345     (I32# x#) .|.   (I32# y#)  = I32# (word32ToInt32# (int32ToWord32# x# `or32#`  int32ToWord32# y#))
346     (I32# x#) `xor` (I32# y#)  = I32# (word32ToInt32# (int32ToWord32# x# `xor32#` int32ToWord32# y#))
347     complement (I32# x#)       = I32# (word32ToInt32# (not32# (int32ToWord32# x#)))
348     (I32# x#) `shift` (I# i#)
349         | i# >=# 0#            = I32# (x# `iShiftL32#` i#)
350         | otherwise            = I32# (x# `iShiftRA32#` negateInt# i#)
351     (I32# x#) `rotate` (I# i#)
352         | i'# ==# 0# 
353         = I32# x#
354         | otherwise
355         = I32# (word32ToInt32# ((x'# `shiftL32#` i'#) `or32#`
356                                 (x'# `shiftRL32#` (32# -# i'#))))
357         where
358         x'# = int32ToWord32# x#
359         i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
360     bitSize  _                 = 32
361     isSigned _                 = True
362
363 foreign import "stg_eqInt32"       unsafe eqInt32#       :: Int32# -> Int32# -> Bool
364 foreign import "stg_neInt32"       unsafe neInt32#       :: Int32# -> Int32# -> Bool
365 foreign import "stg_ltInt32"       unsafe ltInt32#       :: Int32# -> Int32# -> Bool
366 foreign import "stg_leInt32"       unsafe leInt32#       :: Int32# -> Int32# -> Bool
367 foreign import "stg_gtInt32"       unsafe gtInt32#       :: Int32# -> Int32# -> Bool
368 foreign import "stg_geInt32"       unsafe geInt32#       :: Int32# -> Int32# -> Bool
369 foreign import "stg_plusInt32"     unsafe plusInt32#     :: Int32# -> Int32# -> Int32#
370 foreign import "stg_minusInt32"    unsafe minusInt32#    :: Int32# -> Int32# -> Int32#
371 foreign import "stg_timesInt32"    unsafe timesInt32#    :: Int32# -> Int32# -> Int32#
372 foreign import "stg_negateInt32"   unsafe negateInt32#   :: Int32# -> Int32#
373 foreign import "stg_quotInt32"     unsafe quotInt32#     :: Int32# -> Int32# -> Int32#
374 foreign import "stg_remInt32"      unsafe remInt32#      :: Int32# -> Int32# -> Int32#
375 foreign import "stg_intToInt32"    unsafe intToInt32#    :: Int# -> Int32#
376 foreign import "stg_int32ToInt"    unsafe int32ToInt#    :: Int32# -> Int#
377 foreign import "stg_wordToWord32"  unsafe wordToWord32#  :: Word# -> Word32#
378 foreign import "stg_int32ToWord32" unsafe int32ToWord32# :: Int32# -> Word32#
379 foreign import "stg_word32ToInt32" unsafe word32ToInt32# :: Word32# -> Int32#
380 foreign import "stg_and32"         unsafe and32#         :: Word32# -> Word32# -> Word32#
381 foreign import "stg_or32"          unsafe or32#          :: Word32# -> Word32# -> Word32#
382 foreign import "stg_xor32"         unsafe xor32#         :: Word32# -> Word32# -> Word32#
383 foreign import "stg_not32"         unsafe not32#         :: Word32# -> Word32#
384 foreign import "stg_iShiftL32"     unsafe iShiftL32#     :: Int32# -> Int# -> Int32#
385 foreign import "stg_iShiftRA32"    unsafe iShiftRA32#    :: Int32# -> Int# -> Int32#
386 foreign import "stg_shiftL32"      unsafe shiftL32#      :: Word32# -> Int# -> Word32#
387 foreign import "stg_shiftRL32"     unsafe shiftRL32#     :: Word32# -> Int# -> Word32#
388
389 {-# RULES
390 "fromIntegral/Int->Int32"    fromIntegral = \(I#   x#) -> I32# (intToInt32# x#)
391 "fromIntegral/Word->Int32"   fromIntegral = \(W#   x#) -> I32# (word32ToInt32# (wordToWord32# x#))
392 "fromIntegral/Word32->Int32" fromIntegral = \(W32# x#) -> I32# (word32ToInt32# x#)
393 "fromIntegral/Int32->Int"    fromIntegral = \(I32# x#) -> I#   (int32ToInt# x#)
394 "fromIntegral/Int32->Word"   fromIntegral = \(I32# x#) -> W#   (int2Word# (int32ToInt# x#))
395 "fromIntegral/Int32->Word32" fromIntegral = \(I32# x#) -> W32# (int32ToWord32# x#)
396 "fromIntegral/Int32->Int32"  fromIntegral = id :: Int32 -> Int32
397   #-}
398
399 #else 
400
401 -- Int32 is represented in the same way as Int.
402 #if WORD_SIZE_IN_BITS > 32
403 -- Operations may assume and must ensure that it holds only values
404 -- from its logical range.
405 #endif
406
407 data Int32 = I32# Int# deriving (Eq, Ord)
408 -- ^ 32-bit signed integer type
409
410 instance Show Int32 where
411     showsPrec p x = showsPrec p (fromIntegral x :: Int)
412
413 instance Num Int32 where
414     (I32# x#) + (I32# y#)  = I32# (narrow32Int# (x# +# y#))
415     (I32# x#) - (I32# y#)  = I32# (narrow32Int# (x# -# y#))
416     (I32# x#) * (I32# y#)  = I32# (narrow32Int# (x# *# y#))
417     negate (I32# x#)       = I32# (narrow32Int# (negateInt# x#))
418     abs x | x >= 0         = x
419           | otherwise      = negate x
420     signum x | x > 0       = 1
421     signum 0               = 0
422     signum _               = -1
423     fromInteger (S# i#)    = I32# (narrow32Int# i#)
424     fromInteger (J# s# d#) = I32# (narrow32Int# (integer2Int# s# d#))
425
426 instance Enum Int32 where
427     succ x
428         | x /= maxBound = x + 1
429         | otherwise     = succError "Int32"
430     pred x
431         | x /= minBound = x - 1
432         | otherwise     = predError "Int32"
433 #if WORD_SIZE_IN_BITS == 32
434     toEnum (I# i#)      = I32# i#
435 #else
436     toEnum i@(I# i#)
437         | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
438                         = I32# i#
439         | otherwise     = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
440 #endif
441     fromEnum (I32# x#)  = I# x#
442     enumFrom            = boundedEnumFrom
443     enumFromThen        = boundedEnumFromThen
444
445 instance Integral Int32 where
446     quot    x@(I32# x#) y@(I32# y#)
447         | y /= 0                  = I32# (narrow32Int# (x# `quotInt#` y#))
448         | otherwise               = divZeroError
449     rem     x@(I32# x#) y@(I32# y#)
450         | y /= 0                  = I32# (narrow32Int# (x# `remInt#` y#))
451         | otherwise               = divZeroError
452     div     x@(I32# x#) y@(I32# y#)
453         | y /= 0                  = I32# (narrow32Int# (x# `divInt#` y#))
454         | otherwise               = divZeroError
455     mod     x@(I32# x#) y@(I32# y#)
456         | y /= 0                  = I32# (narrow32Int# (x# `modInt#` y#))
457         | otherwise               = divZeroError
458     quotRem x@(I32# x#) y@(I32# y#)
459         | y /= 0                  = (I32# (narrow32Int# (x# `quotInt#` y#)),
460                                     I32# (narrow32Int# (x# `remInt#` y#)))
461         | otherwise               = divZeroError
462     divMod  x@(I32# x#) y@(I32# y#)
463         | y /= 0                  = (I32# (narrow32Int# (x# `divInt#` y#)),
464                                     I32# (narrow32Int# (x# `modInt#` y#)))
465         | otherwise               = divZeroError
466     toInteger (I32# x#)           = S# x#
467
468 instance Read Int32 where
469     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
470
471 instance Bits Int32 where
472     (I32# x#) .&.   (I32# y#)  = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
473     (I32# x#) .|.   (I32# y#)  = I32# (word2Int# (int2Word# x# `or#`  int2Word# y#))
474     (I32# x#) `xor` (I32# y#)  = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
475     complement (I32# x#)       = I32# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
476     (I32# x#) `shift` (I# i#)
477         | i# >=# 0#            = I32# (narrow32Int# (x# `iShiftL#` i#))
478         | otherwise            = I32# (x# `iShiftRA#` negateInt# i#)
479     (I32# x#) `rotate` (I# i#)
480         | i'# ==# 0# 
481         = I32# x#
482         | otherwise
483         = I32# (narrow32Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
484                                         (x'# `shiftRL#` (32# -# i'#)))))
485         where
486         x'# = narrow32Word# (int2Word# x#)
487         i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
488     bitSize  _                 = 32
489     isSigned _                 = True
490
491 {-# RULES
492 "fromIntegral/Word8->Int32"  fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
493 "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
494 "fromIntegral/Int8->Int32"   fromIntegral = \(I8# x#) -> I32# x#
495 "fromIntegral/Int16->Int32"  fromIntegral = \(I16# x#) -> I32# x#
496 "fromIntegral/Int32->Int32"  fromIntegral = id :: Int32 -> Int32
497 "fromIntegral/a->Int32"      fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
498 "fromIntegral/Int32->a"      fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
499   #-}
500
501 #endif 
502
503 instance Real Int32 where
504     toRational x = toInteger x % 1
505
506 instance Bounded Int32 where
507     minBound = -0x80000000
508     maxBound =  0x7FFFFFFF
509
510 instance Ix Int32 where
511     range (m,n)              = [m..n]
512     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
513     inRange (m,n) i          = m <= i && i <= n
514     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
515
516 ------------------------------------------------------------------------
517 -- type Int64
518 ------------------------------------------------------------------------
519
520 #if WORD_SIZE_IN_BITS < 64
521
522 data Int64 = I64# Int64#
523 -- ^ 64-bit signed integer type
524
525 instance Eq Int64 where
526     (I64# x#) == (I64# y#) = x# `eqInt64#` y#
527     (I64# x#) /= (I64# y#) = x# `neInt64#` y#
528
529 instance Ord Int64 where
530     (I64# x#) <  (I64# y#) = x# `ltInt64#` y#
531     (I64# x#) <= (I64# y#) = x# `leInt64#` y#
532     (I64# x#) >  (I64# y#) = x# `gtInt64#` y#
533     (I64# x#) >= (I64# y#) = x# `geInt64#` y#
534
535 instance Show Int64 where
536     showsPrec p x = showsPrec p (toInteger x)
537
538 instance Num Int64 where
539     (I64# x#) + (I64# y#)  = I64# (x# `plusInt64#`  y#)
540     (I64# x#) - (I64# y#)  = I64# (x# `minusInt64#` y#)
541     (I64# x#) * (I64# y#)  = I64# (x# `timesInt64#` y#)
542     negate (I64# x#)       = I64# (negateInt64# x#)
543     abs x | x >= 0         = x
544           | otherwise      = negate x
545     signum x | x > 0       = 1
546     signum 0               = 0
547     signum _               = -1
548     fromInteger (S# i#)    = I64# (intToInt64# i#)
549     fromInteger (J# s# d#) = I64# (integerToInt64# s# d#)
550
551 instance Enum Int64 where
552     succ x
553         | x /= maxBound = x + 1
554         | otherwise     = succError "Int64"
555     pred x
556         | x /= minBound = x - 1
557         | otherwise     = predError "Int64"
558     toEnum (I# i#)      = I64# (intToInt64# i#)
559     fromEnum x@(I64# x#)
560         | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
561                         = I# (int64ToInt# x#)
562         | otherwise     = fromEnumError "Int64" x
563     enumFrom            = integralEnumFrom
564     enumFromThen        = integralEnumFromThen
565     enumFromTo          = integralEnumFromTo
566     enumFromThenTo      = integralEnumFromThenTo
567
568 instance Integral Int64 where
569     quot    x@(I64# x#) y@(I64# y#)
570         | y /= 0                  = I64# (x# `quotInt64#` y#)
571         | otherwise               = divZeroError
572     rem     x@(I64# x#) y@(I64# y#)
573         | y /= 0                  = I64# (x# `remInt64#` y#)
574         | otherwise               = divZeroError
575     div     x@(I64# x#) y@(I64# y#)
576         | y /= 0                  = I64# (x# `divInt64#` y#)
577         | otherwise               = divZeroError
578     mod     x@(I64# x#) y@(I64# y#)
579         | y /= 0                  = I64# (x# `modInt64#` y#)
580         | otherwise               = divZeroError
581     quotRem x@(I64# x#) y@(I64# y#)
582         | y /= 0                  = (I64# (x# `quotInt64#` y#), I64# (x# `remInt64#` y#))
583         | otherwise               = divZeroError
584     divMod  x@(I64# x#) y@(I64# y#)
585         | y /= 0                  = (I64# (x# `divInt64#` y#), I64# (x# `modInt64#` y#))
586         | otherwise               = divZeroError
587     toInteger x@(I64# x#)
588         | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
589                                   = S# (int64ToInt# x#)
590         | otherwise               = case int64ToInteger# x# of (# s, d #) -> J# s d
591
592
593 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
594 x# `divInt64#` y#
595     | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#)
596         = ((x# `minusInt64#` y#) `minusInt64#` intToInt64# 1#) `quotInt64#` y#
597     | (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
598         = ((x# `minusInt64#` y#) `plusInt64#` intToInt64# 1#) `quotInt64#` y#
599     | otherwise                = x# `quotInt64#` y#
600 x# `modInt64#` y#
601     | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) ||
602       (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
603         = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0#
604     | otherwise = r#
605     where
606     r# = x# `remInt64#` y#
607
608 instance Read Int64 where
609     readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
610
611 instance Bits Int64 where
612     (I64# x#) .&.   (I64# y#)  = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#))
613     (I64# x#) .|.   (I64# y#)  = I64# (word64ToInt64# (int64ToWord64# x# `or64#`  int64ToWord64# y#))
614     (I64# x#) `xor` (I64# y#)  = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#))
615     complement (I64# x#)       = I64# (word64ToInt64# (not64# (int64ToWord64# x#)))
616     (I64# x#) `shift` (I# i#)
617         | i# >=# 0#            = I64# (x# `iShiftL64#` i#)
618         | otherwise            = I64# (x# `iShiftRA64#` negateInt# i#)
619     (I64# x#) `rotate` (I# i#)
620         | i'# ==# 0# 
621         = I64# x#
622         | otherwise
623         = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
624                                 (x'# `uncheckedShiftRL64#` (64# -# i'#))))
625         where
626         x'# = int64ToWord64# x#
627         i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
628     bitSize  _                 = 64
629     isSigned _                 = True
630
631
632 -- give the 64-bit shift operations the same treatment as the 32-bit
633 -- ones (see GHC.Base), namely we wrap them in tests to catch the
634 -- cases when we're shifting more than 64 bits to avoid unspecified
635 -- behaviour in the C shift operations.
636
637 iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#
638
639 a `iShiftL64#` b  | b >=# 64# = intToInt64# 0#
640                   | otherwise = a `uncheckedIShiftL64#` b
641
642 a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#) 
643                                         then intToInt64# (-1#) 
644                                         else intToInt64# 0#
645                   | otherwise = a `uncheckedIShiftRA64#` b
646
647
648 foreign import ccall unsafe "stg_eqInt64"       eqInt64#       :: Int64# -> Int64# -> Bool
649 foreign import ccall unsafe "stg_neInt64"       neInt64#       :: Int64# -> Int64# -> Bool
650 foreign import ccall unsafe "stg_ltInt64"       ltInt64#       :: Int64# -> Int64# -> Bool
651 foreign import ccall unsafe "stg_leInt64"       leInt64#       :: Int64# -> Int64# -> Bool
652 foreign import ccall unsafe "stg_gtInt64"       gtInt64#       :: Int64# -> Int64# -> Bool
653 foreign import ccall unsafe "stg_geInt64"       geInt64#       :: Int64# -> Int64# -> Bool
654 foreign import ccall unsafe "stg_plusInt64"     plusInt64#     :: Int64# -> Int64# -> Int64#
655 foreign import ccall unsafe "stg_minusInt64"    minusInt64#    :: Int64# -> Int64# -> Int64#
656 foreign import ccall unsafe "stg_timesInt64"    timesInt64#    :: Int64# -> Int64# -> Int64#
657 foreign import ccall unsafe "stg_negateInt64"   negateInt64#   :: Int64# -> Int64#
658 foreign import ccall unsafe "stg_quotInt64"     quotInt64#     :: Int64# -> Int64# -> Int64#
659 foreign import ccall unsafe "stg_remInt64"      remInt64#      :: Int64# -> Int64# -> Int64#
660 foreign import ccall unsafe "stg_intToInt64"    intToInt64#    :: Int# -> Int64#
661 foreign import ccall unsafe "stg_int64ToInt"    int64ToInt#    :: Int64# -> Int#
662 foreign import ccall unsafe "stg_wordToWord64"  wordToWord64#  :: Word# -> Word64#
663 foreign import ccall unsafe "stg_int64ToWord64" int64ToWord64# :: Int64# -> Word64#
664 foreign import ccall unsafe "stg_word64ToInt64" word64ToInt64# :: Word64# -> Int64#
665 foreign import ccall unsafe "stg_and64"         and64#         :: Word64# -> Word64# -> Word64#
666 foreign import ccall unsafe "stg_or64"          or64#          :: Word64# -> Word64# -> Word64#
667 foreign import ccall unsafe "stg_xor64"         xor64#         :: Word64# -> Word64# -> Word64#
668 foreign import ccall unsafe "stg_not64"         not64#         :: Word64# -> Word64#
669 foreign import ccall unsafe "stg_uncheckedShiftL64"      uncheckedShiftL64#      :: Word64# -> Int# -> Word64#
670 foreign import ccall unsafe "stg_uncheckedShiftRL64"     uncheckedShiftRL64#     :: Word64# -> Int# -> Word64#
671 foreign import ccall unsafe "stg_uncheckedIShiftL64"     uncheckedIShiftL64#     :: Int64# -> Int# -> Int64#
672 foreign import ccall unsafe "stg_uncheckedIShiftRA64"    uncheckedIShiftRA64#    :: Int64# -> Int# -> Int64#
673
674 foreign import ccall unsafe "stg_integerToInt64"  integerToInt64#  :: Int# -> ByteArray# -> Int64#
675
676 {-# RULES
677 "fromIntegral/Int->Int64"    fromIntegral = \(I#   x#) -> I64# (intToInt64# x#)
678 "fromIntegral/Word->Int64"   fromIntegral = \(W#   x#) -> I64# (word64ToInt64# (wordToWord64# x#))
679 "fromIntegral/Word64->Int64" fromIntegral = \(W64# x#) -> I64# (word64ToInt64# x#)
680 "fromIntegral/Int64->Int"    fromIntegral = \(I64# x#) -> I#   (int64ToInt# x#)
681 "fromIntegral/Int64->Word"   fromIntegral = \(I64# x#) -> W#   (int2Word# (int64ToInt# x#))
682 "fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#)
683 "fromIntegral/Int64->Int64"  fromIntegral = id :: Int64 -> Int64
684   #-}
685
686 #else 
687
688 -- Int64 is represented in the same way as Int.
689 -- Operations may assume and must ensure that it holds only values
690 -- from its logical range.
691
692 data Int64 = I64# Int# deriving (Eq, Ord)
693 -- ^ 64-bit signed integer type
694
695 instance Show Int64 where
696     showsPrec p x = showsPrec p (fromIntegral x :: Int)
697
698 instance Num Int64 where
699     (I64# x#) + (I64# y#)  = I64# (x# +# y#)
700     (I64# x#) - (I64# y#)  = I64# (x# -# y#)
701     (I64# x#) * (I64# y#)  = I64# (x# *# y#)
702     negate (I64# x#)       = I64# (negateInt# x#)
703     abs x | x >= 0         = x
704           | otherwise      = negate x
705     signum x | x > 0       = 1
706     signum 0               = 0
707     signum _               = -1
708     fromInteger (S# i#)    = I64# i#
709     fromInteger (J# s# d#) = I64# (integer2Int# s# d#)
710
711 instance Enum Int64 where
712     succ x
713         | x /= maxBound = x + 1
714         | otherwise     = succError "Int64"
715     pred x
716         | x /= minBound = x - 1
717         | otherwise     = predError "Int64"
718     toEnum (I# i#)      = I64# i#
719     fromEnum (I64# x#)  = I# x#
720     enumFrom            = boundedEnumFrom
721     enumFromThen        = boundedEnumFromThen
722
723 instance Integral Int64 where
724     quot    x@(I64# x#) y@(I64# y#)
725         | y /= 0                  = I64# (x# `quotInt#` y#)
726         | otherwise               = divZeroError
727     rem     x@(I64# x#) y@(I64# y#)
728         | y /= 0                  = I64# (x# `remInt#` y#)
729         | otherwise               = divZeroError
730     div     x@(I64# x#) y@(I64# y#)
731         | y /= 0                  = I64# (x# `divInt#` y#)
732         | otherwise               = divZeroError
733     mod     x@(I64# x#) y@(I64# y#)
734         | y /= 0                  = I64# (x# `modInt#` y#)
735         | otherwise               = divZeroError
736     quotRem x@(I64# x#) y@(I64# y#)
737         | y /= 0                  = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#))
738         | otherwise               = divZeroError
739     divMod  x@(I64# x#) y@(I64# y#)
740         | y /= 0                  = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#))
741         | otherwise               = divZeroError
742     toInteger (I64# x#)           = S# x#
743
744 instance Read Int64 where
745     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
746
747 instance Bits Int64 where
748     (I64# x#) .&.   (I64# y#)  = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
749     (I64# x#) .|.   (I64# y#)  = I64# (word2Int# (int2Word# x# `or#`  int2Word# y#))
750     (I64# x#) `xor` (I64# y#)  = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
751     complement (I64# x#)       = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
752     (I64# x#) `shift` (I# i#)
753         | i# >=# 0#            = I64# (x# `iShiftL#` i#)
754         | otherwise            = I64# (x# `iShiftRA#` negateInt# i#)
755     (I64# x#) `rotate` (I# i#)
756         | i'# ==# 0# 
757         = I64# x#
758         | otherwise
759         = I64# (word2Int# ((x'# `shiftL#` i'#) `or#`
760                            (x'# `shiftRL#` (64# -# i'#))))
761         where
762         x'# = int2Word# x#
763         i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
764     bitSize  _                 = 64
765     isSigned _                 = True
766
767 {-# RULES
768 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
769 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
770   #-}
771
772 #endif
773
774 instance Real Int64 where
775     toRational x = toInteger x % 1
776
777 instance Bounded Int64 where
778     minBound = -0x8000000000000000
779     maxBound =  0x7FFFFFFFFFFFFFFF
780
781 instance Ix Int64 where
782     range (m,n)              = [m..n]
783     unsafeIndex b@(m,_) i    = fromIntegral (i - m)
784     inRange (m,n) i          = m <= i && i <= n
785     unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1