1 {-# OPTIONS_GHC -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 -----------------------------------------------------------------------------
20 Int8(..), Int16(..), Int32(..), Int64(..))
25 import {-# SOURCE #-} GHC.Err
35 ------------------------------------------------------------------------
37 ------------------------------------------------------------------------
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.
42 data Int8 = I8# Int# deriving (Eq, Ord)
43 -- ^ 8-bit signed integer type
45 instance Show Int8 where
46 showsPrec p x = showsPrec p (fromIntegral x :: Int)
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#))
54 | otherwise = negate x
58 fromInteger (S# i#) = I8# (narrow8Int# i#)
59 fromInteger (J# s# d#) = I8# (narrow8Int# (integer2Int# s# d#))
61 instance Real Int8 where
62 toRational x = toInteger x % 1
64 instance Enum Int8 where
66 | x /= maxBound = x + 1
67 | otherwise = succError "Int8"
69 | x /= minBound = x - 1
70 | otherwise = predError "Int8"
72 | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
74 | otherwise = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
75 fromEnum (I8# x#) = I# x#
76 enumFrom = boundedEnumFrom
77 enumFromThen = boundedEnumFromThen
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#
102 instance Bounded Int8 where
106 instance Ix Int8 where
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
112 instance Read Int8 where
113 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
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#)
127 = I8# (narrow8Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
128 (x'# `shiftRL#` (8# -# i'#)))))
130 x'# = narrow8Word# (int2Word# x#)
131 i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
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#)
141 ------------------------------------------------------------------------
143 ------------------------------------------------------------------------
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.
148 data Int16 = I16# Int# deriving (Eq, Ord)
149 -- ^ 16-bit signed integer type
151 instance Show Int16 where
152 showsPrec p x = showsPrec p (fromIntegral x :: Int)
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#))
160 | otherwise = negate x
164 fromInteger (S# i#) = I16# (narrow16Int# i#)
165 fromInteger (J# s# d#) = I16# (narrow16Int# (integer2Int# s# d#))
167 instance Real Int16 where
168 toRational x = toInteger x % 1
170 instance Enum Int16 where
172 | x /= maxBound = x + 1
173 | otherwise = succError "Int16"
175 | x /= minBound = x - 1
176 | otherwise = predError "Int16"
178 | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
180 | otherwise = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
181 fromEnum (I16# x#) = I# x#
182 enumFrom = boundedEnumFrom
183 enumFromThen = boundedEnumFromThen
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#
208 instance Bounded Int16 where
212 instance Ix Int16 where
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
218 instance Read Int16 where
219 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
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#)
233 = I16# (narrow16Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
234 (x'# `shiftRL#` (16# -# i'#)))))
236 x'# = narrow16Word# (int2Word# x#)
237 i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
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#)
249 ------------------------------------------------------------------------
251 ------------------------------------------------------------------------
253 #if WORD_SIZE_IN_BITS < 32
255 data Int32 = I32# Int32#
256 -- ^ 32-bit signed integer type
258 instance Eq Int32 where
259 (I32# x#) == (I32# y#) = x# `eqInt32#` y#
260 (I32# x#) /= (I32# y#) = x# `neInt32#` y#
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#
268 instance Show Int32 where
269 showsPrec p x = showsPrec p (toInteger x)
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#)
277 | otherwise = negate x
281 fromInteger (S# i#) = I32# (intToInt32# i#)
282 fromInteger (J# s# d#) = I32# (integerToInt32# s# d#)
284 instance Enum Int32 where
286 | x /= maxBound = x + 1
287 | otherwise = succError "Int32"
289 | x /= minBound = x - 1
290 | otherwise = predError "Int32"
291 toEnum (I# i#) = I32# (intToInt32# i#)
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
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
325 divInt32#, modInt32# :: Int32# -> Int32# -> Int32#
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#
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#
338 r# = x# `remInt32#` y#
340 instance Read Int32 where
341 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
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#)
355 = I32# (word32ToInt32# ((x'# `shiftL32#` i'#) `or32#`
356 (x'# `shiftRL32#` (32# -# i'#))))
358 x'# = int32ToWord32# x#
359 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
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#
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
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.
407 data Int32 = I32# Int# deriving (Eq, Ord)
408 -- ^ 32-bit signed integer type
410 instance Show Int32 where
411 showsPrec p x = showsPrec p (fromIntegral x :: Int)
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#))
419 | otherwise = negate x
423 fromInteger (S# i#) = I32# (narrow32Int# i#)
424 fromInteger (J# s# d#) = I32# (narrow32Int# (integer2Int# s# d#))
426 instance Enum Int32 where
428 | x /= maxBound = x + 1
429 | otherwise = succError "Int32"
431 | x /= minBound = x - 1
432 | otherwise = predError "Int32"
433 #if WORD_SIZE_IN_BITS == 32
434 toEnum (I# i#) = I32# i#
437 | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
439 | otherwise = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
441 fromEnum (I32# x#) = I# x#
442 enumFrom = boundedEnumFrom
443 enumFromThen = boundedEnumFromThen
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#
468 instance Read Int32 where
469 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
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#)
483 = I32# (narrow32Int# (word2Int# ((x'# `shiftL#` i'#) `or#`
484 (x'# `shiftRL#` (32# -# i'#)))))
486 x'# = narrow32Word# (int2Word# x#)
487 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
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#)
503 instance Real Int32 where
504 toRational x = toInteger x % 1
506 instance Bounded Int32 where
507 minBound = -0x80000000
508 maxBound = 0x7FFFFFFF
510 instance Ix Int32 where
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
516 ------------------------------------------------------------------------
518 ------------------------------------------------------------------------
520 #if WORD_SIZE_IN_BITS < 64
522 data Int64 = I64# Int64#
523 -- ^ 64-bit signed integer type
525 instance Eq Int64 where
526 (I64# x#) == (I64# y#) = x# `eqInt64#` y#
527 (I64# x#) /= (I64# y#) = x# `neInt64#` y#
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#
535 instance Show Int64 where
536 showsPrec p x = showsPrec p (toInteger x)
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#)
544 | otherwise = negate x
548 fromInteger (S# i#) = I64# (intToInt64# i#)
549 fromInteger (J# s# d#) = I64# (integerToInt64# s# d#)
551 instance Enum Int64 where
553 | x /= maxBound = x + 1
554 | otherwise = succError "Int64"
556 | x /= minBound = x - 1
557 | otherwise = predError "Int64"
558 toEnum (I# i#) = I64# (intToInt64# i#)
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
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
593 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
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#
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#
606 r# = x# `remInt64#` y#
608 instance Read Int64 where
609 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
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#)
623 = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
624 (x'# `uncheckedShiftRL64#` (64# -# i'#))))
626 x'# = int64ToWord64# x#
627 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
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.
637 iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#
639 a `iShiftL64#` b | b >=# 64# = intToInt64# 0#
640 | otherwise = a `uncheckedIShiftL64#` b
642 a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#)
643 then intToInt64# (-1#)
645 | otherwise = a `uncheckedIShiftRA64#` b
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#
674 foreign import ccall unsafe "stg_integerToInt64" integerToInt64# :: Int# -> ByteArray# -> Int64#
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
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.
692 data Int64 = I64# Int# deriving (Eq, Ord)
693 -- ^ 64-bit signed integer type
695 instance Show Int64 where
696 showsPrec p x = showsPrec p (fromIntegral x :: Int)
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#)
704 | otherwise = negate x
708 fromInteger (S# i#) = I64# i#
709 fromInteger (J# s# d#) = I64# (integer2Int# s# d#)
711 instance Enum Int64 where
713 | x /= maxBound = x + 1
714 | otherwise = succError "Int64"
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
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#
744 instance Read Int64 where
745 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
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#)
759 = I64# (word2Int# ((x'# `shiftL#` i'#) `or#`
760 (x'# `shiftRL#` (64# -# i'#))))
763 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
768 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
769 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
774 instance Real Int64 where
775 toRational x = toInteger x % 1
777 instance Bounded Int64 where
778 minBound = -0x8000000000000000
779 maxBound = 0x7FFFFFFFFFFFFFFF
781 instance Ix Int64 where
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