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