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 - fromIntegral m
109 inRange (m,n) i = m <= i && i <= n
111 instance Read Int8 where
112 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
114 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'# `uncheckedShiftL#` i'#) `or#`
129 (x'# `uncheckedShiftRL#` (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 Show Int16 where
153 showsPrec p x = showsPrec p (fromIntegral x :: Int)
155 instance Num Int16 where
156 (I16# x#) + (I16# y#) = I16# (narrow16Int# (x# +# y#))
157 (I16# x#) - (I16# y#) = I16# (narrow16Int# (x# -# y#))
158 (I16# x#) * (I16# y#) = I16# (narrow16Int# (x# *# y#))
159 negate (I16# x#) = I16# (narrow16Int# (negateInt# x#))
161 | otherwise = negate x
165 fromInteger (S# i#) = I16# (narrow16Int# i#)
166 fromInteger (J# s# d#) = I16# (narrow16Int# (integer2Int# s# d#))
168 instance Real Int16 where
169 toRational x = toInteger x % 1
171 instance Enum Int16 where
173 | x /= maxBound = x + 1
174 | otherwise = succError "Int16"
176 | x /= minBound = x - 1
177 | otherwise = predError "Int16"
179 | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
181 | otherwise = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
182 fromEnum (I16# x#) = I# x#
183 enumFrom = boundedEnumFrom
184 enumFromThen = boundedEnumFromThen
186 instance Integral Int16 where
187 quot x@(I16# x#) y@(I16# y#)
188 | y /= 0 = I16# (narrow16Int# (x# `quotInt#` y#))
189 | otherwise = divZeroError
190 rem x@(I16# x#) y@(I16# y#)
191 | y /= 0 = I16# (narrow16Int# (x# `remInt#` y#))
192 | otherwise = divZeroError
193 div x@(I16# x#) y@(I16# y#)
194 | y /= 0 = I16# (narrow16Int# (x# `divInt#` y#))
195 | otherwise = divZeroError
196 mod x@(I16# x#) y@(I16# y#)
197 | y /= 0 = I16# (narrow16Int# (x# `modInt#` y#))
198 | otherwise = divZeroError
199 quotRem x@(I16# x#) y@(I16# y#)
200 | y /= 0 = (I16# (narrow16Int# (x# `quotInt#` y#)),
201 I16# (narrow16Int# (x# `remInt#` y#)))
202 | otherwise = divZeroError
203 divMod x@(I16# x#) y@(I16# y#)
204 | y /= 0 = (I16# (narrow16Int# (x# `divInt#` y#)),
205 I16# (narrow16Int# (x# `modInt#` y#)))
206 | otherwise = divZeroError
207 toInteger (I16# x#) = S# x#
209 instance Bounded Int16 where
213 instance Ix Int16 where
215 unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m
216 inRange (m,n) i = m <= i && i <= n
218 instance Read Int16 where
219 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
221 instance Bits Int16 where
224 (I16# x#) .&. (I16# y#) = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
225 (I16# x#) .|. (I16# y#) = I16# (word2Int# (int2Word# x# `or#` int2Word# y#))
226 (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
227 complement (I16# x#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
228 (I16# x#) `shift` (I# i#)
229 | i# >=# 0# = I16# (narrow16Int# (x# `iShiftL#` i#))
230 | otherwise = I16# (x# `iShiftRA#` negateInt# i#)
231 (I16# x#) `rotate` (I# i#)
235 = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
236 (x'# `uncheckedShiftRL#` (16# -# i'#)))))
238 x'# = narrow16Word# (int2Word# x#)
239 i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
244 "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
245 "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x#
246 "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16
247 "fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
248 "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
251 ------------------------------------------------------------------------
253 ------------------------------------------------------------------------
255 #if WORD_SIZE_IN_BITS < 32
257 data Int32 = I32# Int32#
258 -- ^ 32-bit signed integer type
260 instance Eq Int32 where
261 (I32# x#) == (I32# y#) = x# `eqInt32#` y#
262 (I32# x#) /= (I32# y#) = x# `neInt32#` y#
264 instance Ord Int32 where
265 (I32# x#) < (I32# y#) = x# `ltInt32#` y#
266 (I32# x#) <= (I32# y#) = x# `leInt32#` y#
267 (I32# x#) > (I32# y#) = x# `gtInt32#` y#
268 (I32# x#) >= (I32# y#) = x# `geInt32#` y#
270 instance Show Int32 where
271 showsPrec p x = showsPrec p (toInteger x)
273 instance Num Int32 where
274 (I32# x#) + (I32# y#) = I32# (x# `plusInt32#` y#)
275 (I32# x#) - (I32# y#) = I32# (x# `minusInt32#` y#)
276 (I32# x#) * (I32# y#) = I32# (x# `timesInt32#` y#)
277 negate (I32# x#) = I32# (negateInt32# x#)
279 | otherwise = negate x
283 fromInteger (S# i#) = I32# (intToInt32# i#)
284 fromInteger (J# s# d#) = I32# (integerToInt32# s# d#)
286 instance Enum Int32 where
288 | x /= maxBound = x + 1
289 | otherwise = succError "Int32"
291 | x /= minBound = x - 1
292 | otherwise = predError "Int32"
293 toEnum (I# i#) = I32# (intToInt32# i#)
295 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
296 = I# (int32ToInt# x#)
297 | otherwise = fromEnumError "Int32" x
298 enumFrom = integralEnumFrom
299 enumFromThen = integralEnumFromThen
300 enumFromTo = integralEnumFromTo
301 enumFromThenTo = integralEnumFromThenTo
303 instance Integral Int32 where
304 quot x@(I32# x#) y@(I32# y#)
305 | y /= 0 = I32# (x# `quotInt32#` y#)
306 | otherwise = divZeroError
307 rem x@(I32# x#) y@(I32# y#)
308 | y /= 0 = I32# (x# `remInt32#` y#)
309 | otherwise = divZeroError
310 div x@(I32# x#) y@(I32# y#)
311 | y /= 0 = I32# (x# `divInt32#` y#)
312 | otherwise = divZeroError
313 mod x@(I32# x#) y@(I32# y#)
314 | y /= 0 = I32# (x# `modInt32#` y#)
315 | otherwise = divZeroError
316 quotRem x@(I32# x#) y@(I32# y#)
317 | y /= 0 = (I32# (x# `quotInt32#` y#), I32# (x# `remInt32#` y#))
318 | otherwise = divZeroError
319 divMod x@(I32# x#) y@(I32# y#)
320 | y /= 0 = (I32# (x# `divInt32#` y#), I32# (x# `modInt32#` y#))
321 | otherwise = divZeroError
322 toInteger x@(I32# x#)
323 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
324 = S# (int32ToInt# x#)
325 | otherwise = case int32ToInteger# x# of (# s, d #) -> J# s d
327 divInt32#, modInt32# :: Int32# -> Int32# -> Int32#
329 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#)
330 = ((x# `minusInt32#` y#) `minusInt32#` intToInt32# 1#) `quotInt32#` y#
331 | (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
332 = ((x# `minusInt32#` y#) `plusInt32#` intToInt32# 1#) `quotInt32#` y#
333 | otherwise = x# `quotInt32#` y#
335 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#) ||
336 (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
337 = if r# `neInt32#` intToInt32# 0# then r# `plusInt32#` y# else intToInt32# 0#
340 r# = x# `remInt32#` y#
342 instance Read Int32 where
343 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
345 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
453 rem x@(I32# x#) y@(I32# y#)
454 | y /= 0 = I32# (narrow32Int# (x# `remInt#` y#))
455 | otherwise = divZeroError
456 div x@(I32# x#) y@(I32# y#)
457 | y /= 0 = I32# (narrow32Int# (x# `divInt#` y#))
458 | otherwise = divZeroError
459 mod x@(I32# x#) y@(I32# y#)
460 | y /= 0 = I32# (narrow32Int# (x# `modInt#` y#))
461 | otherwise = divZeroError
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
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
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
478 (I32# x#) .&. (I32# y#) = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
479 (I32# x#) .|. (I32# y#) = I32# (word2Int# (int2Word# x# `or#` int2Word# y#))
480 (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
481 complement (I32# x#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
482 (I32# x#) `shift` (I# i#)
483 | i# >=# 0# = I32# (narrow32Int# (x# `iShiftL#` i#))
484 | otherwise = I32# (x# `iShiftRA#` negateInt# i#)
485 (I32# x#) `rotate` (I# i#)
489 = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
490 (x'# `uncheckedShiftRL#` (32# -# i'#)))))
492 x'# = narrow32Word# (int2Word# x#)
493 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
498 "fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
499 "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
500 "fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x#
501 "fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x#
502 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
503 "fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
504 "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
509 instance Real Int32 where
510 toRational x = toInteger x % 1
512 instance Bounded Int32 where
513 minBound = -0x80000000
514 maxBound = 0x7FFFFFFF
516 instance Ix Int32 where
518 unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m
519 inRange (m,n) i = m <= i && i <= n
521 ------------------------------------------------------------------------
523 ------------------------------------------------------------------------
525 #if WORD_SIZE_IN_BITS < 64
527 data Int64 = I64# Int64#
528 -- ^ 64-bit signed integer type
530 instance Eq Int64 where
531 (I64# x#) == (I64# y#) = x# `eqInt64#` y#
532 (I64# x#) /= (I64# y#) = x# `neInt64#` y#
534 instance Ord Int64 where
535 (I64# x#) < (I64# y#) = x# `ltInt64#` y#
536 (I64# x#) <= (I64# y#) = x# `leInt64#` y#
537 (I64# x#) > (I64# y#) = x# `gtInt64#` y#
538 (I64# x#) >= (I64# y#) = x# `geInt64#` y#
540 instance Show Int64 where
541 showsPrec p x = showsPrec p (toInteger x)
543 instance Num Int64 where
544 (I64# x#) + (I64# y#) = I64# (x# `plusInt64#` y#)
545 (I64# x#) - (I64# y#) = I64# (x# `minusInt64#` y#)
546 (I64# x#) * (I64# y#) = I64# (x# `timesInt64#` y#)
547 negate (I64# x#) = I64# (negateInt64# x#)
549 | otherwise = negate x
553 fromInteger (S# i#) = I64# (intToInt64# i#)
554 fromInteger (J# s# d#) = I64# (integerToInt64# s# d#)
556 instance Enum Int64 where
558 | x /= maxBound = x + 1
559 | otherwise = succError "Int64"
561 | x /= minBound = x - 1
562 | otherwise = predError "Int64"
563 toEnum (I# i#) = I64# (intToInt64# i#)
565 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
566 = I# (int64ToInt# x#)
567 | otherwise = fromEnumError "Int64" x
568 enumFrom = integralEnumFrom
569 enumFromThen = integralEnumFromThen
570 enumFromTo = integralEnumFromTo
571 enumFromThenTo = integralEnumFromThenTo
573 instance Integral Int64 where
574 quot x@(I64# x#) y@(I64# y#)
575 | y /= 0 = I64# (x# `quotInt64#` y#)
576 | otherwise = divZeroError
577 rem x@(I64# x#) y@(I64# y#)
578 | y /= 0 = I64# (x# `remInt64#` y#)
579 | otherwise = divZeroError
580 div x@(I64# x#) y@(I64# y#)
581 | y /= 0 = I64# (x# `divInt64#` y#)
582 | otherwise = divZeroError
583 mod x@(I64# x#) y@(I64# y#)
584 | y /= 0 = I64# (x# `modInt64#` y#)
585 | otherwise = divZeroError
586 quotRem x@(I64# x#) y@(I64# y#)
587 | y /= 0 = (I64# (x# `quotInt64#` y#), I64# (x# `remInt64#` y#))
588 | otherwise = divZeroError
589 divMod x@(I64# x#) y@(I64# y#)
590 | y /= 0 = (I64# (x# `divInt64#` y#), I64# (x# `modInt64#` y#))
591 | otherwise = divZeroError
592 toInteger x@(I64# x#)
593 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
594 = S# (int64ToInt# x#)
595 | otherwise = case int64ToInteger# x# of (# s, d #) -> J# s d
598 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
600 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#)
601 = ((x# `minusInt64#` y#) `minusInt64#` intToInt64# 1#) `quotInt64#` y#
602 | (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
603 = ((x# `minusInt64#` y#) `plusInt64#` intToInt64# 1#) `quotInt64#` y#
604 | otherwise = x# `quotInt64#` y#
606 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) ||
607 (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
608 = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0#
611 r# = x# `remInt64#` y#
613 instance Read Int64 where
614 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
616 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
734 rem x@(I64# x#) y@(I64# y#)
735 | y /= 0 = I64# (x# `remInt#` y#)
736 | otherwise = divZeroError
737 div x@(I64# x#) y@(I64# y#)
738 | y /= 0 = I64# (x# `divInt#` y#)
739 | otherwise = divZeroError
740 mod x@(I64# x#) y@(I64# y#)
741 | y /= 0 = I64# (x# `modInt#` y#)
742 | otherwise = divZeroError
743 quotRem x@(I64# x#) y@(I64# y#)
744 | y /= 0 = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#))
745 | otherwise = divZeroError
746 divMod x@(I64# x#) y@(I64# y#)
747 | y /= 0 = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#))
748 | otherwise = divZeroError
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
757 (I64# x#) .&. (I64# y#) = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
758 (I64# x#) .|. (I64# y#) = I64# (word2Int# (int2Word# x# `or#` int2Word# y#))
759 (I64# x#) `xor` (I64# y#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
760 complement (I64# x#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
761 (I64# x#) `shift` (I# i#)
762 | i# >=# 0# = I64# (x# `iShiftL#` i#)
763 | otherwise = I64# (x# `iShiftRA#` negateInt# i#)
764 (I64# x#) `rotate` (I# i#)
768 = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
769 (x'# `uncheckedShiftRL#` (64# -# i'#))))
772 i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
777 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
778 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
783 instance Real Int64 where
784 toRational x = toInteger x % 1
786 instance Bounded Int64 where
787 minBound = -0x8000000000000000
788 maxBound = 0x7FFFFFFFFFFFFFFF
790 instance Ix Int64 where
792 unsafeIndex b@(m,_) i = fromIntegral i - fromIntegral m
793 inRange (m,n) i = m <= i && i <= n