1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
5 -- Copyright : (c) The University of Glasgow 1997-2002
6 -- License : see libraries/base/LICENSE
8 -- Maintainer : cvs-ghc@haskell.org
9 -- Stability : internal
10 -- Portability : non-portable (GHC Extensions)
12 -- The sized integral datatypes, 'Int8', 'Int16', 'Int32', and 'Int64'.
14 -----------------------------------------------------------------------------
19 Int8(..), Int16(..), Int32(..), Int64(..))
33 ------------------------------------------------------------------------
35 ------------------------------------------------------------------------
37 -- Int8 is represented in the same way as Int. Operations may assume
38 -- and must ensure that it holds only values from its logical range.
40 data Int8 = I8# Int# deriving (Eq, Ord)
41 -- ^ 8-bit signed integer type
43 instance CCallable Int8
44 instance CReturnable Int8
46 instance Show Int8 where
47 showsPrec p x = showsPrec p (fromIntegral x :: Int)
49 instance Num Int8 where
50 (I8# x#) + (I8# y#) = I8# (narrow8Int# (x# +# y#))
51 (I8# x#) - (I8# y#) = I8# (narrow8Int# (x# -# y#))
52 (I8# x#) * (I8# y#) = I8# (narrow8Int# (x# *# y#))
53 negate (I8# x#) = I8# (narrow8Int# (negateInt# x#))
55 | otherwise = negate x
59 fromInteger (S# i#) = I8# (narrow8Int# i#)
60 fromInteger (J# s# d#) = I8# (narrow8Int# (integer2Int# s# d#))
62 instance Real Int8 where
63 toRational x = toInteger x % 1
65 instance Enum Int8 where
67 | x /= maxBound = x + 1
68 | otherwise = succError "Int8"
70 | x /= minBound = x - 1
71 | otherwise = predError "Int8"
73 | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
75 | otherwise = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
76 fromEnum (I8# x#) = I# x#
77 enumFrom = boundedEnumFrom
78 enumFromThen = boundedEnumFromThen
80 instance Integral Int8 where
81 quot x@(I8# x#) y@(I8# y#)
82 | y /= 0 = I8# (narrow8Int# (x# `quotInt#` y#))
83 | otherwise = divZeroError "quot{Int8}" x
84 rem x@(I8# x#) y@(I8# y#)
85 | y /= 0 = I8# (narrow8Int# (x# `remInt#` y#))
86 | otherwise = divZeroError "rem{Int8}" x
87 div x@(I8# x#) y@(I8# y#)
88 | y /= 0 = I8# (narrow8Int# (x# `divInt#` y#))
89 | otherwise = divZeroError "div{Int8}" x
90 mod x@(I8# x#) y@(I8# y#)
91 | y /= 0 = I8# (narrow8Int# (x# `modInt#` y#))
92 | otherwise = divZeroError "mod{Int8}" x
93 quotRem x@(I8# x#) y@(I8# y#)
94 | y /= 0 = (I8# (narrow8Int# (x# `quotInt#` y#)),
95 I8# (narrow8Int# (x# `remInt#` y#)))
96 | otherwise = divZeroError "quotRem{Int8}" x
97 divMod x@(I8# x#) y@(I8# y#)
98 | y /= 0 = (I8# (narrow8Int# (x# `divInt#` y#)),
99 I8# (narrow8Int# (x# `modInt#` y#)))
100 | otherwise = divZeroError "divMod{Int8}" x
101 toInteger (I8# x#) = S# x#
103 instance Bounded Int8 where
107 instance Ix Int8 where
109 unsafeIndex b@(m,_) i = fromIntegral (i - m)
110 inRange (m,n) i = m <= i && i <= n
111 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
113 instance Read Int8 where
114 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
116 instance Bits Int8 where
117 (I8# x#) .&. (I8# y#) = I8# (word2Int# (int2Word# x# `and#` int2Word# y#))
118 (I8# x#) .|. (I8# y#) = I8# (word2Int# (int2Word# x# `or#` int2Word# y#))
119 (I8# x#) `xor` (I8# y#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#))
120 complement (I8# x#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
121 (I8# x#) `shift` (I# i#)
122 | i# >=# 0# = I8# (narrow8Int# (x# `iShiftL#` i#))
123 | otherwise = I8# (x# `iShiftRA#` negateInt# i#)
124 (I8# x#) `rotate` (I# i#)
128 = I8# (narrow8Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
129 (x'# `shiftRL#` (8# -# i'#)))))
131 x'# = narrow8Word# (int2Word# x#)
132 i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
137 "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
138 "fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#)
139 "fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#)
142 ------------------------------------------------------------------------
144 ------------------------------------------------------------------------
146 -- Int16 is represented in the same way as Int. Operations may assume
147 -- and must ensure that it holds only values from its logical range.
149 data Int16 = I16# Int# deriving (Eq, Ord)
150 -- ^ 16-bit signed integer type
152 instance CCallable Int16
153 instance CReturnable Int16
155 instance Show Int16 where
156 showsPrec p x = showsPrec p (fromIntegral x :: Int)
158 instance Num Int16 where
159 (I16# x#) + (I16# y#) = I16# (narrow16Int# (x# +# y#))
160 (I16# x#) - (I16# y#) = I16# (narrow16Int# (x# -# y#))
161 (I16# x#) * (I16# y#) = I16# (narrow16Int# (x# *# y#))
162 negate (I16# x#) = I16# (narrow16Int# (negateInt# x#))
164 | otherwise = negate x
168 fromInteger (S# i#) = I16# (narrow16Int# i#)
169 fromInteger (J# s# d#) = I16# (narrow16Int# (integer2Int# s# d#))
171 instance Real Int16 where
172 toRational x = toInteger x % 1
174 instance Enum Int16 where
176 | x /= maxBound = x + 1
177 | otherwise = succError "Int16"
179 | x /= minBound = x - 1
180 | otherwise = predError "Int16"
182 | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
184 | otherwise = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
185 fromEnum (I16# x#) = I# x#
186 enumFrom = boundedEnumFrom
187 enumFromThen = boundedEnumFromThen
189 instance Integral Int16 where
190 quot x@(I16# x#) y@(I16# y#)
191 | y /= 0 = I16# (narrow16Int# (x# `quotInt#` y#))
192 | otherwise = divZeroError "quot{Int16}" x
193 rem x@(I16# x#) y@(I16# y#)
194 | y /= 0 = I16# (narrow16Int# (x# `remInt#` y#))
195 | otherwise = divZeroError "rem{Int16}" x
196 div x@(I16# x#) y@(I16# y#)
197 | y /= 0 = I16# (narrow16Int# (x# `divInt#` y#))
198 | otherwise = divZeroError "div{Int16}" x
199 mod x@(I16# x#) y@(I16# y#)
200 | y /= 0 = I16# (narrow16Int# (x# `modInt#` y#))
201 | otherwise = divZeroError "mod{Int16}" x
202 quotRem x@(I16# x#) y@(I16# y#)
203 | y /= 0 = (I16# (narrow16Int# (x# `quotInt#` y#)),
204 I16# (narrow16Int# (x# `remInt#` y#)))
205 | otherwise = divZeroError "quotRem{Int16}" x
206 divMod x@(I16# x#) y@(I16# y#)
207 | y /= 0 = (I16# (narrow16Int# (x# `divInt#` y#)),
208 I16# (narrow16Int# (x# `modInt#` y#)))
209 | otherwise = divZeroError "divMod{Int16}" x
210 toInteger (I16# x#) = S# x#
212 instance Bounded Int16 where
216 instance Ix Int16 where
218 unsafeIndex b@(m,_) i = fromIntegral (i - m)
219 inRange (m,n) i = m <= i && i <= n
220 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
222 instance Read Int16 where
223 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
225 instance Bits Int16 where
226 (I16# x#) .&. (I16# y#) = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
227 (I16# x#) .|. (I16# y#) = I16# (word2Int# (int2Word# x# `or#` int2Word# y#))
228 (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
229 complement (I16# x#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
230 (I16# x#) `shift` (I# i#)
231 | i# >=# 0# = I16# (narrow16Int# (x# `iShiftL#` i#))
232 | otherwise = I16# (x# `iShiftRA#` negateInt# i#)
233 (I16# x#) `rotate` (I# i#)
237 = I16# (narrow16Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
238 (x'# `shiftRL#` (16# -# i'#)))))
240 x'# = narrow16Word# (int2Word# x#)
241 i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
246 "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
247 "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x#
248 "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16
249 "fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
250 "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
253 ------------------------------------------------------------------------
255 ------------------------------------------------------------------------
257 #if WORD_SIZE_IN_BITS < 32
259 data Int32 = I32# Int32#
260 -- ^ 32-bit signed integer type
262 instance Eq Int32 where
263 (I32# x#) == (I32# y#) = x# `eqInt32#` y#
264 (I32# x#) /= (I32# y#) = x# `neInt32#` y#
266 instance Ord Int32 where
267 (I32# x#) < (I32# y#) = x# `ltInt32#` y#
268 (I32# x#) <= (I32# y#) = x# `leInt32#` y#
269 (I32# x#) > (I32# y#) = x# `gtInt32#` y#
270 (I32# x#) >= (I32# y#) = x# `geInt32#` y#
272 instance Show Int32 where
273 showsPrec p x = showsPrec p (toInteger x)
275 instance Num Int32 where
276 (I32# x#) + (I32# y#) = I32# (x# `plusInt32#` y#)
277 (I32# x#) - (I32# y#) = I32# (x# `minusInt32#` y#)
278 (I32# x#) * (I32# y#) = I32# (x# `timesInt32#` y#)
279 negate (I32# x#) = I32# (negateInt32# x#)
281 | otherwise = negate x
285 fromInteger (S# i#) = I32# (intToInt32# i#)
286 fromInteger (J# s# d#) = I32# (integerToInt32# s# d#)
288 instance Enum Int32 where
290 | x /= maxBound = x + 1
291 | otherwise = succError "Int32"
293 | x /= minBound = x - 1
294 | otherwise = predError "Int32"
295 toEnum (I# i#) = I32# (intToInt32# i#)
297 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
298 = I# (int32ToInt# x#)
299 | otherwise = fromEnumError "Int32" x
300 enumFrom = integralEnumFrom
301 enumFromThen = integralEnumFromThen
302 enumFromTo = integralEnumFromTo
303 enumFromThenTo = integralEnumFromThenTo
305 instance Integral Int32 where
306 quot x@(I32# x#) y@(I32# y#)
307 | y /= 0 = I32# (x# `quotInt32#` y#)
308 | otherwise = divZeroError "quot{Int32}" x
309 rem x@(I32# x#) y@(I32# y#)
310 | y /= 0 = I32# (x# `remInt32#` y#)
311 | otherwise = divZeroError "rem{Int32}" x
312 div x@(I32# x#) y@(I32# y#)
313 | y /= 0 = I32# (x# `divInt32#` y#)
314 | otherwise = divZeroError "div{Int32}" x
315 mod x@(I32# x#) y@(I32# y#)
316 | y /= 0 = I32# (x# `modInt32#` y#)
317 | otherwise = divZeroError "mod{Int32}" x
318 quotRem x@(I32# x#) y@(I32# y#)
319 | y /= 0 = (I32# (x# `quotInt32#` y#), I32# (x# `remInt32#` y#))
320 | otherwise = divZeroError "quotRem{Int32}" x
321 divMod x@(I32# x#) y@(I32# y#)
322 | y /= 0 = (I32# (x# `divInt32#` y#), I32# (x# `modInt32#` y#))
323 | otherwise = divZeroError "divMod{Int32}" x
324 toInteger x@(I32# x#)
325 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
326 = S# (int32ToInt# x#)
327 | otherwise = case int32ToInteger# x# of (# s, d #) -> J# s d
329 divInt32#, modInt32# :: Int32# -> Int32# -> Int32#
331 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#)
332 = ((x# `minusInt32#` y#) `minusInt32#` intToInt32# 1#) `quotInt32#` y#
333 | (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
334 = ((x# `minusInt32#` y#) `plusInt32#` intToInt32# 1#) `quotInt32#` y#
335 | otherwise = x# `quotInt32#` y#
337 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#) ||
338 (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
339 = if r# `neInt32#` intToInt32# 0# then r# `plusInt32#` y# else intToInt32# 0#
342 r# = x# `remInt32#` y#
344 instance Read Int32 where
345 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
347 instance Bits Int32 where
348 (I32# x#) .&. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `and32#` int32ToWord32# y#))
349 (I32# x#) .|. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `or32#` int32ToWord32# y#))
350 (I32# x#) `xor` (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `xor32#` int32ToWord32# y#))
351 complement (I32# x#) = I32# (word32ToInt32# (not32# (int32ToWord32# x#)))
352 (I32# x#) `shift` (I# i#)
353 | i# >=# 0# = I32# (x# `iShiftL32#` i#)
354 | otherwise = I32# (x# `iShiftRA32#` negateInt# i#)
355 (I32# x#) `rotate` (I# i#)
359 = I32# (word32ToInt32# ((x'# `shiftL32#` i'#) `or32#`
360 (x'# `shiftRL32#` (32# -# i'#))))
362 x'# = int32ToWord32# x#
363 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
367 foreign import "stg_eqInt32" unsafe eqInt32# :: Int32# -> Int32# -> Bool
368 foreign import "stg_neInt32" unsafe neInt32# :: Int32# -> Int32# -> Bool
369 foreign import "stg_ltInt32" unsafe ltInt32# :: Int32# -> Int32# -> Bool
370 foreign import "stg_leInt32" unsafe leInt32# :: Int32# -> Int32# -> Bool
371 foreign import "stg_gtInt32" unsafe gtInt32# :: Int32# -> Int32# -> Bool
372 foreign import "stg_geInt32" unsafe geInt32# :: Int32# -> Int32# -> Bool
373 foreign import "stg_plusInt32" unsafe plusInt32# :: Int32# -> Int32# -> Int32#
374 foreign import "stg_minusInt32" unsafe minusInt32# :: Int32# -> Int32# -> Int32#
375 foreign import "stg_timesInt32" unsafe timesInt32# :: Int32# -> Int32# -> Int32#
376 foreign import "stg_negateInt32" unsafe negateInt32# :: Int32# -> Int32#
377 foreign import "stg_quotInt32" unsafe quotInt32# :: Int32# -> Int32# -> Int32#
378 foreign import "stg_remInt32" unsafe remInt32# :: Int32# -> Int32# -> Int32#
379 foreign import "stg_intToInt32" unsafe intToInt32# :: Int# -> Int32#
380 foreign import "stg_int32ToInt" unsafe int32ToInt# :: Int32# -> Int#
381 foreign import "stg_wordToWord32" unsafe wordToWord32# :: Word# -> Word32#
382 foreign import "stg_int32ToWord32" unsafe int32ToWord32# :: Int32# -> Word32#
383 foreign import "stg_word32ToInt32" unsafe word32ToInt32# :: Word32# -> Int32#
384 foreign import "stg_and32" unsafe and32# :: Word32# -> Word32# -> Word32#
385 foreign import "stg_or32" unsafe or32# :: Word32# -> Word32# -> Word32#
386 foreign import "stg_xor32" unsafe xor32# :: Word32# -> Word32# -> Word32#
387 foreign import "stg_not32" unsafe not32# :: Word32# -> Word32#
388 foreign import "stg_iShiftL32" unsafe iShiftL32# :: Int32# -> Int# -> Int32#
389 foreign import "stg_iShiftRA32" unsafe iShiftRA32# :: Int32# -> Int# -> Int32#
390 foreign import "stg_shiftL32" unsafe shiftL32# :: Word32# -> Int# -> Word32#
391 foreign import "stg_shiftRL32" unsafe shiftRL32# :: Word32# -> Int# -> Word32#
394 "fromIntegral/Int->Int32" fromIntegral = \(I# x#) -> I32# (intToInt32# x#)
395 "fromIntegral/Word->Int32" fromIntegral = \(W# x#) -> I32# (word32ToInt32# (wordToWord32# x#))
396 "fromIntegral/Word32->Int32" fromIntegral = \(W32# x#) -> I32# (word32ToInt32# x#)
397 "fromIntegral/Int32->Int" fromIntegral = \(I32# x#) -> I# (int32ToInt# x#)
398 "fromIntegral/Int32->Word" fromIntegral = \(I32# x#) -> W# (int2Word# (int32ToInt# x#))
399 "fromIntegral/Int32->Word32" fromIntegral = \(I32# x#) -> W32# (int32ToWord32# x#)
400 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
405 -- Int32 is represented in the same way as Int.
406 #if WORD_SIZE_IN_BITS > 32
407 -- Operations may assume and must ensure that it holds only values
408 -- from its logical range.
411 data Int32 = I32# Int# deriving (Eq, Ord)
412 -- ^ 32-bit signed integer type
414 instance Show Int32 where
415 showsPrec p x = showsPrec p (fromIntegral x :: Int)
417 instance Num Int32 where
418 (I32# x#) + (I32# y#) = I32# (narrow32Int# (x# +# y#))
419 (I32# x#) - (I32# y#) = I32# (narrow32Int# (x# -# y#))
420 (I32# x#) * (I32# y#) = I32# (narrow32Int# (x# *# y#))
421 negate (I32# x#) = I32# (narrow32Int# (negateInt# x#))
423 | otherwise = negate x
427 fromInteger (S# i#) = I32# (narrow32Int# i#)
428 fromInteger (J# s# d#) = I32# (narrow32Int# (integer2Int# s# d#))
430 instance Enum Int32 where
432 | x /= maxBound = x + 1
433 | otherwise = succError "Int32"
435 | x /= minBound = x - 1
436 | otherwise = predError "Int32"
437 #if WORD_SIZE_IN_BITS == 32
438 toEnum (I# i#) = I32# i#
441 | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
443 | otherwise = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
445 fromEnum (I32# x#) = I# x#
446 enumFrom = boundedEnumFrom
447 enumFromThen = boundedEnumFromThen
449 instance Integral Int32 where
450 quot x@(I32# x#) y@(I32# y#)
451 | y /= 0 = I32# (narrow32Int# (x# `quotInt#` y#))
452 | otherwise = divZeroError "quot{Int32}" x
453 rem x@(I32# x#) y@(I32# y#)
454 | y /= 0 = I32# (narrow32Int# (x# `remInt#` y#))
455 | otherwise = divZeroError "rem{Int32}" x
456 div x@(I32# x#) y@(I32# y#)
457 | y /= 0 = I32# (narrow32Int# (x# `divInt#` y#))
458 | otherwise = divZeroError "div{Int32}" x
459 mod x@(I32# x#) y@(I32# y#)
460 | y /= 0 = I32# (narrow32Int# (x# `modInt#` y#))
461 | otherwise = divZeroError "mod{Int32}" x
462 quotRem x@(I32# x#) y@(I32# y#)
463 | y /= 0 = (I32# (narrow32Int# (x# `quotInt#` y#)),
464 I32# (narrow32Int# (x# `remInt#` y#)))
465 | otherwise = divZeroError "quotRem{Int32}" x
466 divMod x@(I32# x#) y@(I32# y#)
467 | y /= 0 = (I32# (narrow32Int# (x# `divInt#` y#)),
468 I32# (narrow32Int# (x# `modInt#` y#)))
469 | otherwise = divZeroError "divMod{Int32}" x
470 toInteger (I32# x#) = S# x#
472 instance Read Int32 where
473 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
475 instance Bits Int32 where
476 (I32# x#) .&. (I32# y#) = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
477 (I32# x#) .|. (I32# y#) = I32# (word2Int# (int2Word# x# `or#` int2Word# y#))
478 (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
479 complement (I32# x#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
480 (I32# x#) `shift` (I# i#)
481 | i# >=# 0# = I32# (narrow32Int# (x# `iShiftL#` i#))
482 | otherwise = I32# (x# `iShiftRA#` negateInt# i#)
483 (I32# x#) `rotate` (I# i#)
487 = I32# (narrow32Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
488 (x'# `shiftRL#` (32# -# i'#)))))
490 x'# = narrow32Word# (int2Word# x#)
491 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
496 "fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
497 "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
498 "fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x#
499 "fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x#
500 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
501 "fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
502 "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
507 instance CCallable Int32
508 instance CReturnable Int32
510 instance Real Int32 where
511 toRational x = toInteger x % 1
513 instance Bounded Int32 where
514 minBound = -0x80000000
515 maxBound = 0x7FFFFFFF
517 instance Ix Int32 where
519 unsafeIndex b@(m,_) i = fromIntegral (i - m)
520 inRange (m,n) i = m <= i && i <= n
521 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1
523 ------------------------------------------------------------------------
525 ------------------------------------------------------------------------
527 #if WORD_SIZE_IN_BITS < 64
529 data Int64 = I64# Int64#
530 -- ^ 64-bit signed integer type
532 instance Eq Int64 where
533 (I64# x#) == (I64# y#) = x# `eqInt64#` y#
534 (I64# x#) /= (I64# y#) = x# `neInt64#` y#
536 instance Ord Int64 where
537 (I64# x#) < (I64# y#) = x# `ltInt64#` y#
538 (I64# x#) <= (I64# y#) = x# `leInt64#` y#
539 (I64# x#) > (I64# y#) = x# `gtInt64#` y#
540 (I64# x#) >= (I64# y#) = x# `geInt64#` y#
542 instance Show Int64 where
543 showsPrec p x = showsPrec p (toInteger x)
545 instance Num Int64 where
546 (I64# x#) + (I64# y#) = I64# (x# `plusInt64#` y#)
547 (I64# x#) - (I64# y#) = I64# (x# `minusInt64#` y#)
548 (I64# x#) * (I64# y#) = I64# (x# `timesInt64#` y#)
549 negate (I64# x#) = I64# (negateInt64# x#)
551 | otherwise = negate x
555 fromInteger (S# i#) = I64# (intToInt64# i#)
556 fromInteger (J# s# d#) = I64# (integerToInt64# s# d#)
558 instance Enum Int64 where
560 | x /= maxBound = x + 1
561 | otherwise = succError "Int64"
563 | x /= minBound = x - 1
564 | otherwise = predError "Int64"
565 toEnum (I# i#) = I64# (intToInt64# i#)
567 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
568 = I# (int64ToInt# x#)
569 | otherwise = fromEnumError "Int64" x
570 enumFrom = integralEnumFrom
571 enumFromThen = integralEnumFromThen
572 enumFromTo = integralEnumFromTo
573 enumFromThenTo = integralEnumFromThenTo
575 instance Integral Int64 where
576 quot x@(I64# x#) y@(I64# y#)
577 | y /= 0 = I64# (x# `quotInt64#` y#)
578 | otherwise = divZeroError "quot{Int64}" x
579 rem x@(I64# x#) y@(I64# y#)
580 | y /= 0 = I64# (x# `remInt64#` y#)
581 | otherwise = divZeroError "rem{Int64}" x
582 div x@(I64# x#) y@(I64# y#)
583 | y /= 0 = I64# (x# `divInt64#` y#)
584 | otherwise = divZeroError "div{Int64}" x
585 mod x@(I64# x#) y@(I64# y#)
586 | y /= 0 = I64# (x# `modInt64#` y#)
587 | otherwise = divZeroError "mod{Int64}" x
588 quotRem x@(I64# x#) y@(I64# y#)
589 | y /= 0 = (I64# (x# `quotInt64#` y#), I64# (x# `remInt64#` y#))
590 | otherwise = divZeroError "quotRem{Int64}" x
591 divMod x@(I64# x#) y@(I64# y#)
592 | y /= 0 = (I64# (x# `divInt64#` y#), I64# (x# `modInt64#` y#))
593 | otherwise = divZeroError "divMod{Int64}" x
594 toInteger x@(I64# x#)
595 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
596 = S# (int64ToInt# x#)
597 | otherwise = case int64ToInteger# x# of (# s, d #) -> J# s d
600 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
602 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#)
603 = ((x# `minusInt64#` y#) `minusInt64#` intToInt64# 1#) `quotInt64#` y#
604 | (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
605 = ((x# `minusInt64#` y#) `plusInt64#` intToInt64# 1#) `quotInt64#` y#
606 | otherwise = x# `quotInt64#` y#
608 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) ||
609 (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
610 = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0#
613 r# = x# `remInt64#` y#
615 instance Read Int64 where
616 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
618 instance Bits Int64 where
619 (I64# x#) .&. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#))
620 (I64# x#) .|. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `or64#` int64ToWord64# y#))
621 (I64# x#) `xor` (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#))
622 complement (I64# x#) = I64# (word64ToInt64# (not64# (int64ToWord64# x#)))
623 (I64# x#) `shift` (I# i#)
624 | i# >=# 0# = I64# (x# `iShiftL64#` i#)
625 | otherwise = I64# (x# `iShiftRA64#` negateInt# i#)
626 (I64# x#) `rotate` (I# i#)
630 = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
631 (x'# `uncheckedShiftRL64#` (64# -# i'#))))
633 x'# = int64ToWord64# x#
634 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
639 -- give the 64-bit shift operations the same treatment as the 32-bit
640 -- ones (see GHC.Base), namely we wrap them in tests to catch the
641 -- cases when we're shifting more than 64 bits to avoid unspecified
642 -- behaviour in the C shift operations.
644 iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#
646 a `iShiftL64#` b | b >=# 64# = intToInt64# 0#
647 | otherwise = a `uncheckedIShiftL64#` b
649 a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#)
650 then intToInt64# (-1#)
652 | otherwise = a `uncheckedIShiftRA64#` b
655 foreign import ccall unsafe "stg_eqInt64" eqInt64# :: Int64# -> Int64# -> Bool
656 foreign import ccall unsafe "stg_neInt64" neInt64# :: Int64# -> Int64# -> Bool
657 foreign import ccall unsafe "stg_ltInt64" ltInt64# :: Int64# -> Int64# -> Bool
658 foreign import ccall unsafe "stg_leInt64" leInt64# :: Int64# -> Int64# -> Bool
659 foreign import ccall unsafe "stg_gtInt64" gtInt64# :: Int64# -> Int64# -> Bool
660 foreign import ccall unsafe "stg_geInt64" geInt64# :: Int64# -> Int64# -> Bool
661 foreign import ccall unsafe "stg_plusInt64" plusInt64# :: Int64# -> Int64# -> Int64#
662 foreign import ccall unsafe "stg_minusInt64" minusInt64# :: Int64# -> Int64# -> Int64#
663 foreign import ccall unsafe "stg_timesInt64" timesInt64# :: Int64# -> Int64# -> Int64#
664 foreign import ccall unsafe "stg_negateInt64" negateInt64# :: Int64# -> Int64#
665 foreign import ccall unsafe "stg_quotInt64" quotInt64# :: Int64# -> Int64# -> Int64#
666 foreign import ccall unsafe "stg_remInt64" remInt64# :: Int64# -> Int64# -> Int64#
667 foreign import ccall unsafe "stg_intToInt64" intToInt64# :: Int# -> Int64#
668 foreign import ccall unsafe "stg_int64ToInt" int64ToInt# :: Int64# -> Int#
669 foreign import ccall unsafe "stg_wordToWord64" wordToWord64# :: Word# -> Word64#
670 foreign import ccall unsafe "stg_int64ToWord64" int64ToWord64# :: Int64# -> Word64#
671 foreign import ccall unsafe "stg_word64ToInt64" word64ToInt64# :: Word64# -> Int64#
672 foreign import ccall unsafe "stg_and64" and64# :: Word64# -> Word64# -> Word64#
673 foreign import ccall unsafe "stg_or64" or64# :: Word64# -> Word64# -> Word64#
674 foreign import ccall unsafe "stg_xor64" xor64# :: Word64# -> Word64# -> Word64#
675 foreign import ccall unsafe "stg_not64" not64# :: Word64# -> Word64#
676 foreign import ccall unsafe "stg_uncheckedShiftL64" uncheckedShiftL64# :: Word64# -> Int# -> Word64#
677 foreign import ccall unsafe "stg_uncheckedShiftRL64" uncheckedShiftRL64# :: Word64# -> Int# -> Word64#
678 foreign import ccall unsafe "stg_uncheckedIShiftL64" uncheckedIShiftL64# :: Int64# -> Int# -> Int64#
679 foreign import ccall unsafe "stg_uncheckedIShiftRA64" uncheckedIShiftRA64# :: Int64# -> Int# -> Int64#
681 foreign import ccall unsafe "stg_integerToInt64" integerToInt64# :: Int# -> ByteArray# -> Int64#
684 "fromIntegral/Int->Int64" fromIntegral = \(I# x#) -> I64# (intToInt64# x#)
685 "fromIntegral/Word->Int64" fromIntegral = \(W# x#) -> I64# (word64ToInt64# (wordToWord64# x#))
686 "fromIntegral/Word64->Int64" fromIntegral = \(W64# x#) -> I64# (word64ToInt64# x#)
687 "fromIntegral/Int64->Int" fromIntegral = \(I64# x#) -> I# (int64ToInt# x#)
688 "fromIntegral/Int64->Word" fromIntegral = \(I64# x#) -> W# (int2Word# (int64ToInt# x#))
689 "fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#)
690 "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64
695 -- Int64 is represented in the same way as Int.
696 -- Operations may assume and must ensure that it holds only values
697 -- from its logical range.
699 data Int64 = I64# Int# deriving (Eq, Ord)
700 -- ^ 64-bit signed integer type
702 instance Show Int64 where
703 showsPrec p x = showsPrec p (fromIntegral x :: Int)
705 instance Num Int64 where
706 (I64# x#) + (I64# y#) = I64# (x# +# y#)
707 (I64# x#) - (I64# y#) = I64# (x# -# y#)
708 (I64# x#) * (I64# y#) = I64# (x# *# y#)
709 negate (I64# x#) = I64# (negateInt# x#)
711 | otherwise = negate x
715 fromInteger (S# i#) = I64# i#
716 fromInteger (J# s# d#) = I64# (integer2Int# s# d#)
718 instance Enum Int64 where
720 | x /= maxBound = x + 1
721 | otherwise = succError "Int64"
723 | x /= minBound = x - 1
724 | otherwise = predError "Int64"
725 toEnum (I# i#) = I64# i#
726 fromEnum (I64# x#) = I# x#
727 enumFrom = boundedEnumFrom
728 enumFromThen = boundedEnumFromThen
730 instance Integral Int64 where
731 quot x@(I64# x#) y@(I64# y#)
732 | y /= 0 = I64# (x# `quotInt#` y#)
733 | otherwise = divZeroError "quot{Int64}" x
734 rem x@(I64# x#) y@(I64# y#)
735 | y /= 0 = I64# (x# `remInt#` y#)
736 | otherwise = divZeroError "rem{Int64}" x
737 div x@(I64# x#) y@(I64# y#)
738 | y /= 0 = I64# (x# `divInt#` y#)
739 | otherwise = divZeroError "div{Int64}" x
740 mod x@(I64# x#) y@(I64# y#)
741 | y /= 0 = I64# (x# `modInt#` y#)
742 | otherwise = divZeroError "mod{Int64}" x
743 quotRem x@(I64# x#) y@(I64# y#)
744 | y /= 0 = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#))
745 | otherwise = divZeroError "quotRem{Int64}" x
746 divMod x@(I64# x#) y@(I64# y#)
747 | y /= 0 = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#))
748 | otherwise = divZeroError "divMod{Int64}" x
749 toInteger (I64# x#) = S# x#
751 instance Read Int64 where
752 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
754 instance Bits Int64 where
755 (I64# x#) .&. (I64# y#) = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
756 (I64# x#) .|. (I64# y#) = I64# (word2Int# (int2Word# x# `or#` int2Word# y#))
757 (I64# x#) `xor` (I64# y#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
758 complement (I64# x#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
759 (I64# x#) `shift` (I# i#)
760 | i# >=# 0# = I64# (x# `iShiftL#` i#)
761 | otherwise = I64# (x# `iShiftRA#` negateInt# i#)
762 (I64# x#) `rotate` (I# i#)
766 = I64# (word2Int# ((x'# `shiftL#` i'#) `or#`
767 (x'# `shiftRL#` (64# -# i'#))))
770 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
775 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
776 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
781 instance CCallable Int64
782 instance CReturnable Int64
784 instance Real Int64 where
785 toRational x = toInteger x % 1
787 instance Bounded Int64 where
788 minBound = -0x8000000000000000
789 maxBound = 0x7FFFFFFFFFFFFFFF
791 instance Ix Int64 where
793 unsafeIndex b@(m,_) i = fromIntegral (i - m)
794 inRange (m,n) i = m <= i && i <= n
795 unsafeRangeSize b@(_l,h) = unsafeIndex b h + 1