Int.hs 39.3 KB
Newer Older
1
{-# LANGUAGE Trustworthy #-}
Ian Lynagh's avatar
Ian Lynagh committed
2
{-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash, UnboxedTuples,
3
             StandaloneDeriving, AutoDeriveTypeable, NegativeLiterals #-}
4
{-# OPTIONS_HADDOCK hide #-}
dterei's avatar
dterei committed
5

6 7 8 9 10
-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.Int
-- Copyright   :  (c) The University of Glasgow 1997-2002
-- License     :  see libraries/base/LICENSE
daniel.is.fischer's avatar
daniel.is.fischer committed
11
--
12 13 14 15 16 17 18
-- Maintainer  :  cvs-ghc@haskell.org
-- Stability   :  internal
-- Portability :  non-portable (GHC Extensions)
--
-- The sized integral datatypes, 'Int8', 'Int16', 'Int32', and 'Int64'.
--
-----------------------------------------------------------------------------
19 20 21 22

#include "MachDeps.h"

module GHC.Int (
23 24
        Int8(..), Int16(..), Int32(..), Int64(..),
        uncheckedIShiftL64#, uncheckedIShiftRA64#
25
    ) where
26 27

import Data.Bits
28
import Data.Maybe
29

30 31 32 33
#if WORD_SIZE_IN_BITS < 64
import GHC.IntWord64
#endif

34 35 36 37 38 39
import GHC.Base
import GHC.Enum
import GHC.Num
import GHC.Real
import GHC.Read
import GHC.Arr
40
import GHC.Word hiding (uncheckedShiftL64#, uncheckedShiftRL64#)
41
import GHC.Show
Daniel Fischer's avatar
Daniel Fischer committed
42
import GHC.Float ()     -- for RealFrac methods
Simon Peyton Jones's avatar
Simon Peyton Jones committed
43
import Data.Typeable
44

45 46 47 48 49 50 51 52

------------------------------------------------------------------------
-- type Int8
------------------------------------------------------------------------

-- Int8 is represented in the same way as Int. Operations may assume
-- and must ensure that it holds only values from its logical range.

Simon Peyton Jones's avatar
Simon Peyton Jones committed
53
data {-# CTYPE "HsInt8" #-} Int8 = I8# Int# deriving (Eq, Ord, Typeable)
54
-- ^ 8-bit signed integer type
55 56 57 58 59

instance Show Int8 where
    showsPrec p x = showsPrec p (fromIntegral x :: Int)

instance Num Int8 where
60 61 62 63
    (I8# x#) + (I8# y#)    = I8# (narrow8Int# (x# +# y#))
    (I8# x#) - (I8# y#)    = I8# (narrow8Int# (x# -# y#))
    (I8# x#) * (I8# y#)    = I8# (narrow8Int# (x# *# y#))
    negate (I8# x#)        = I8# (narrow8Int# (negateInt# x#))
64 65 66 67 68
    abs x | x >= 0         = x
          | otherwise      = negate x
    signum x | x > 0       = 1
    signum 0               = 0
    signum _               = -1
69
    fromInteger i          = I8# (narrow8Int# (integerToInt i))
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

instance Real Int8 where
    toRational x = toInteger x % 1

instance Enum Int8 where
    succ x
        | x /= maxBound = x + 1
        | otherwise     = succError "Int8"
    pred x
        | x /= minBound = x - 1
        | otherwise     = predError "Int8"
    toEnum i@(I# i#)
        | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
                        = I8# i#
        | otherwise     = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
    fromEnum (I8# x#)   = I# x#
    enumFrom            = boundedEnumFrom
    enumFromThen        = boundedEnumFromThen

instance Integral Int8 where
    quot    x@(I8# x#) y@(I8# y#)
91
        | y == 0                     = divZeroError
92
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
93
        | otherwise                  = I8# (narrow8Int# (x# `quotInt#` y#))
94
    rem     (I8# x#) y@(I8# y#)
95 96
        | y == 0                     = divZeroError
        | otherwise                  = I8# (narrow8Int# (x# `remInt#` y#))
97
    div     x@(I8# x#) y@(I8# y#)
98
        | y == 0                     = divZeroError
99
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
100
        | otherwise                  = I8# (narrow8Int# (x# `divInt#` y#))
101
    mod       (I8# x#) y@(I8# y#)
102 103
        | y == 0                     = divZeroError
        | otherwise                  = I8# (narrow8Int# (x# `modInt#` y#))
104
    quotRem x@(I8# x#) y@(I8# y#)
105
        | y == 0                     = divZeroError
106 107
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
Ian Lynagh's avatar
Ian Lynagh committed
108 109 110 111
        | otherwise                  = case x# `quotRemInt#` y# of
                                       (# q, r #) ->
                                           (I8# (narrow8Int# q),
                                            I8# (narrow8Int# r))
112
    divMod  x@(I8# x#) y@(I8# y#)
113
        | y == 0                     = divZeroError
114 115
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
116 117 118 119
        | otherwise                  = case x# `divModInt#` y# of
                                       (# d, m #) ->
                                           (I8# (narrow8Int# d),
                                            I8# (narrow8Int# m))
120
    toInteger (I8# x#)               = smallInteger x#
121 122 123 124 125 126

instance Bounded Int8 where
    minBound = -0x80
    maxBound =  0x7F

instance Ix Int8 where
Ian Lynagh's avatar
Ian Lynagh committed
127 128 129
    range (m,n)         = [m..n]
    unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
    inRange (m,n) i     = m <= i && i <= n
130 131 132 133 134

instance Read Int8 where
    readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]

instance Bits Int8 where
135
    {-# INLINE shift #-}
136 137
    {-# INLINE bit #-}
    {-# INLINE testBit #-}
138

139 140 141
    (I8# x#) .&.   (I8# y#)   = I8# (word2Int# (int2Word# x# `and#` int2Word# y#))
    (I8# x#) .|.   (I8# y#)   = I8# (word2Int# (int2Word# x# `or#`  int2Word# y#))
    (I8# x#) `xor` (I8# y#)   = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#))
142
    complement (I8# x#)       = I8# (word2Int# (not# (int2Word# x#)))
143
    (I8# x#) `shift` (I# i#)
144
        | isTrue# (i# >=# 0#) = I8# (narrow8Int# (x# `iShiftL#` i#))
145
        | otherwise           = I8# (x# `iShiftRA#` negateInt# i#)
146
    (I8# x#) `shiftL`       (I# i#) = I8# (narrow8Int# (x# `iShiftL#` i#))
tibbe's avatar
tibbe committed
147
    (I8# x#) `unsafeShiftL` (I# i#) = I8# (narrow8Int# (x# `uncheckedIShiftL#` i#))
148
    (I8# x#) `shiftR`       (I# i#) = I8# (x# `iShiftRA#` i#)
tibbe's avatar
tibbe committed
149
    (I8# x#) `unsafeShiftR` (I# i#) = I8# (x# `uncheckedIShiftRA#` i#)
150
    (I8# x#) `rotate` (I# i#)
151
        | isTrue# (i'# ==# 0#)
152 153
        = I8# x#
        | otherwise
154 155
        = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
                                       (x'# `uncheckedShiftRL#` (8# -# i'#)))))
156
        where
157
        !x'# = narrow8Word# (int2Word# x#)
158
        !i'# = word2Int# (int2Word# i# `and#` 7##)
159 160
    bitSizeMaybe i            = Just (finiteBitSize i)
    bitSize i                 = finiteBitSize i
161
    isSigned _                = True
tibbe's avatar
tibbe committed
162
    popCount (I8# x#)         = I# (word2Int# (popCnt8# (int2Word# x#)))
163 164
    bit                       = bitDefault
    testBit                   = testBitDefault
165

166 167
instance FiniteBits Int8 where
    finiteBitSize _ = 8
168 169
    countLeadingZeros  (I8# x#) = I# (word2Int# (clz8# (int2Word# x#)))
    countTrailingZeros (I8# x#) = I# (word2Int# (ctz8# (int2Word# x#)))
170

171 172
{-# RULES
"fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
173
"fromIntegral/a->Int8"    fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#)
174 175 176
"fromIntegral/Int8->a"    fromIntegral = \(I8# x#) -> fromIntegral (I# x#)
  #-}

Daniel Fischer's avatar
Daniel Fischer committed
177 178
{-# RULES
"properFraction/Float->(Int8,Float)"
179
    properFraction = \x ->
Daniel Fischer's avatar
Daniel Fischer committed
180
                      case properFraction x of {
181
                        (n, y) -> ((fromIntegral :: Int -> Int8) n, y :: Float) }
Daniel Fischer's avatar
Daniel Fischer committed
182
"truncate/Float->Int8"
183
    truncate = (fromIntegral :: Int -> Int8) . (truncate :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
184
"floor/Float->Int8"
185
    floor    = (fromIntegral :: Int -> Int8) . (floor :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
186
"ceiling/Float->Int8"
187
    ceiling  = (fromIntegral :: Int -> Int8) . (ceiling :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
188
"round/Float->Int8"
189
    round    = (fromIntegral :: Int -> Int8) . (round  :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
190 191 192 193
  #-}

{-# RULES
"properFraction/Double->(Int8,Double)"
194
    properFraction = \x ->
Daniel Fischer's avatar
Daniel Fischer committed
195
                      case properFraction x of {
196
                        (n, y) -> ((fromIntegral :: Int -> Int8) n, y :: Double) }
Daniel Fischer's avatar
Daniel Fischer committed
197
"truncate/Double->Int8"
198
    truncate = (fromIntegral :: Int -> Int8) . (truncate :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
199
"floor/Double->Int8"
200
    floor    = (fromIntegral :: Int -> Int8) . (floor :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
201
"ceiling/Double->Int8"
202
    ceiling  = (fromIntegral :: Int -> Int8) . (ceiling :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
203
"round/Double->Int8"
204
    round    = (fromIntegral :: Int -> Int8) . (round  :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
205 206
  #-}

207 208 209 210 211 212 213
------------------------------------------------------------------------
-- type Int16
------------------------------------------------------------------------

-- Int16 is represented in the same way as Int. Operations may assume
-- and must ensure that it holds only values from its logical range.

Simon Peyton Jones's avatar
Simon Peyton Jones committed
214
data {-# CTYPE "HsInt16" #-} Int16 = I16# Int# deriving (Eq, Ord, Typeable)
215
-- ^ 16-bit signed integer type
216 217 218 219 220

instance Show Int16 where
    showsPrec p x = showsPrec p (fromIntegral x :: Int)

instance Num Int16 where
221 222 223 224
    (I16# x#) + (I16# y#)  = I16# (narrow16Int# (x# +# y#))
    (I16# x#) - (I16# y#)  = I16# (narrow16Int# (x# -# y#))
    (I16# x#) * (I16# y#)  = I16# (narrow16Int# (x# *# y#))
    negate (I16# x#)       = I16# (narrow16Int# (negateInt# x#))
225 226 227 228 229
    abs x | x >= 0         = x
          | otherwise      = negate x
    signum x | x > 0       = 1
    signum 0               = 0
    signum _               = -1
230
    fromInteger i          = I16# (narrow16Int# (integerToInt i))
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

instance Real Int16 where
    toRational x = toInteger x % 1

instance Enum Int16 where
    succ x
        | x /= maxBound = x + 1
        | otherwise     = succError "Int16"
    pred x
        | x /= minBound = x - 1
        | otherwise     = predError "Int16"
    toEnum i@(I# i#)
        | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
                        = I16# i#
        | otherwise     = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
    fromEnum (I16# x#)  = I# x#
    enumFrom            = boundedEnumFrom
    enumFromThen        = boundedEnumFromThen

instance Integral Int16 where
    quot    x@(I16# x#) y@(I16# y#)
252
        | y == 0                     = divZeroError
253
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
254
        | otherwise                  = I16# (narrow16Int# (x# `quotInt#` y#))
255
    rem       (I16# x#) y@(I16# y#)
256 257
        | y == 0                     = divZeroError
        | otherwise                  = I16# (narrow16Int# (x# `remInt#` y#))
258
    div     x@(I16# x#) y@(I16# y#)
259
        | y == 0                     = divZeroError
260
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
261
        | otherwise                  = I16# (narrow16Int# (x# `divInt#` y#))
262
    mod       (I16# x#) y@(I16# y#)
263 264
        | y == 0                     = divZeroError
        | otherwise                  = I16# (narrow16Int# (x# `modInt#` y#))
265
    quotRem x@(I16# x#) y@(I16# y#)
266
        | y == 0                     = divZeroError
267 268
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
Ian Lynagh's avatar
Ian Lynagh committed
269 270 271 272
        | otherwise                  = case x# `quotRemInt#` y# of
                                       (# q, r #) ->
                                           (I16# (narrow16Int# q),
                                            I16# (narrow16Int# r))
273
    divMod  x@(I16# x#) y@(I16# y#)
274
        | y == 0                     = divZeroError
275 276
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
277 278 279 280
        | otherwise                  = case x# `divModInt#` y# of
                                       (# d, m #) ->
                                           (I16# (narrow16Int# d),
                                            I16# (narrow16Int# m))
281
    toInteger (I16# x#)              = smallInteger x#
282 283 284 285 286 287

instance Bounded Int16 where
    minBound = -0x8000
    maxBound =  0x7FFF

instance Ix Int16 where
Ian Lynagh's avatar
Ian Lynagh committed
288 289 290
    range (m,n)         = [m..n]
    unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
    inRange (m,n) i     = m <= i && i <= n
291 292 293 294 295

instance Read Int16 where
    readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]

instance Bits Int16 where
296
    {-# INLINE shift #-}
297 298
    {-# INLINE bit #-}
    {-# INLINE testBit #-}
299

300 301 302
    (I16# x#) .&.   (I16# y#)  = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
    (I16# x#) .|.   (I16# y#)  = I16# (word2Int# (int2Word# x# `or#`  int2Word# y#))
    (I16# x#) `xor` (I16# y#)  = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
303
    complement (I16# x#)       = I16# (word2Int# (not# (int2Word# x#)))
304
    (I16# x#) `shift` (I# i#)
305
        | isTrue# (i# >=# 0#)  = I16# (narrow16Int# (x# `iShiftL#` i#))
306
        | otherwise            = I16# (x# `iShiftRA#` negateInt# i#)
307
    (I16# x#) `shiftL`       (I# i#) = I16# (narrow16Int# (x# `iShiftL#` i#))
tibbe's avatar
tibbe committed
308
    (I16# x#) `unsafeShiftL` (I# i#) = I16# (narrow16Int# (x# `uncheckedIShiftL#` i#))
309
    (I16# x#) `shiftR`       (I# i#) = I16# (x# `iShiftRA#` i#)
tibbe's avatar
tibbe committed
310
    (I16# x#) `unsafeShiftR` (I# i#) = I16# (x# `uncheckedIShiftRA#` i#)
311
    (I16# x#) `rotate` (I# i#)
312
        | isTrue# (i'# ==# 0#)
313 314
        = I16# x#
        | otherwise
315 316
        = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
                                         (x'# `uncheckedShiftRL#` (16# -# i'#)))))
317
        where
318
        !x'# = narrow16Word# (int2Word# x#)
319
        !i'# = word2Int# (int2Word# i# `and#` 15##)
320 321
    bitSizeMaybe i             = Just (finiteBitSize i)
    bitSize i                  = finiteBitSize i
322
    isSigned _                 = True
tibbe's avatar
tibbe committed
323
    popCount (I16# x#)         = I# (word2Int# (popCnt16# (int2Word# x#)))
324 325
    bit                        = bitDefault
    testBit                    = testBitDefault
Simon Marlow's avatar
Simon Marlow committed
326

327 328
instance FiniteBits Int16 where
    finiteBitSize _ = 16
329 330
    countLeadingZeros  (I16# x#) = I# (word2Int# (clz16# (int2Word# x#)))
    countTrailingZeros (I16# x#) = I# (word2Int# (ctz16# (int2Word# x#)))
331

332 333 334 335
{-# RULES
"fromIntegral/Word8->Int16"  fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
"fromIntegral/Int8->Int16"   fromIntegral = \(I8# x#) -> I16# x#
"fromIntegral/Int16->Int16"  fromIntegral = id :: Int16 -> Int16
336
"fromIntegral/a->Int16"      fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
337 338 339
"fromIntegral/Int16->a"      fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
  #-}

Daniel Fischer's avatar
Daniel Fischer committed
340 341
{-# RULES
"properFraction/Float->(Int16,Float)"
342
    properFraction = \x ->
Daniel Fischer's avatar
Daniel Fischer committed
343
                      case properFraction x of {
344
                        (n, y) -> ((fromIntegral :: Int -> Int16) n, y :: Float) }
Daniel Fischer's avatar
Daniel Fischer committed
345
"truncate/Float->Int16"
346
    truncate = (fromIntegral :: Int -> Int16) . (truncate :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
347
"floor/Float->Int16"
348
    floor    = (fromIntegral :: Int -> Int16) . (floor :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
349
"ceiling/Float->Int16"
350
    ceiling  = (fromIntegral :: Int -> Int16) . (ceiling :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
351
"round/Float->Int16"
352
    round    = (fromIntegral :: Int -> Int16) . (round  :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
353 354 355 356
  #-}

{-# RULES
"properFraction/Double->(Int16,Double)"
357
    properFraction = \x ->
Daniel Fischer's avatar
Daniel Fischer committed
358
                      case properFraction x of {
359
                        (n, y) -> ((fromIntegral :: Int -> Int16) n, y :: Double) }
Daniel Fischer's avatar
Daniel Fischer committed
360
"truncate/Double->Int16"
361
    truncate = (fromIntegral :: Int -> Int16) . (truncate :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
362
"floor/Double->Int16"
363
    floor    = (fromIntegral :: Int -> Int16) . (floor :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
364
"ceiling/Double->Int16"
365
    ceiling  = (fromIntegral :: Int -> Int16) . (ceiling :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
366
"round/Double->Int16"
367
    round    = (fromIntegral :: Int -> Int16) . (round  :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
368 369
  #-}

370 371 372 373 374
------------------------------------------------------------------------
-- type Int32
------------------------------------------------------------------------

-- Int32 is represented in the same way as Int.
375
#if WORD_SIZE_IN_BITS > 32
376 377 378 379
-- Operations may assume and must ensure that it holds only values
-- from its logical range.
#endif

Simon Peyton Jones's avatar
Simon Peyton Jones committed
380
data {-# CTYPE "HsInt32" #-} Int32 = I32# Int# deriving (Eq, Ord, Typeable)
381
-- ^ 32-bit signed integer type
382 383 384 385 386

instance Show Int32 where
    showsPrec p x = showsPrec p (fromIntegral x :: Int)

instance Num Int32 where
387 388 389 390
    (I32# x#) + (I32# y#)  = I32# (narrow32Int# (x# +# y#))
    (I32# x#) - (I32# y#)  = I32# (narrow32Int# (x# -# y#))
    (I32# x#) * (I32# y#)  = I32# (narrow32Int# (x# *# y#))
    negate (I32# x#)       = I32# (narrow32Int# (negateInt# x#))
391 392 393 394 395
    abs x | x >= 0         = x
          | otherwise      = negate x
    signum x | x > 0       = 1
    signum 0               = 0
    signum _               = -1
396
    fromInteger i          = I32# (narrow32Int# (integerToInt i))
397 398 399 400 401 402 403 404

instance Enum Int32 where
    succ x
        | x /= maxBound = x + 1
        | otherwise     = succError "Int32"
    pred x
        | x /= minBound = x - 1
        | otherwise     = predError "Int32"
405
#if WORD_SIZE_IN_BITS == 32
406 407 408 409 410 411 412 413 414 415 416 417 418
    toEnum (I# i#)      = I32# i#
#else
    toEnum i@(I# i#)
        | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
                        = I32# i#
        | otherwise     = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
#endif
    fromEnum (I32# x#)  = I# x#
    enumFrom            = boundedEnumFrom
    enumFromThen        = boundedEnumFromThen

instance Integral Int32 where
    quot    x@(I32# x#) y@(I32# y#)
419
        | y == 0                     = divZeroError
420
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
421
        | otherwise                  = I32# (narrow32Int# (x# `quotInt#` y#))
422
    rem       (I32# x#) y@(I32# y#)
423
        | y == 0                     = divZeroError
424 425 426 427
          -- The quotRem CPU instruction fails for minBound `quotRem` -1,
          -- but minBound `rem` -1 is well-defined (0). We therefore
          -- special-case it.
        | y == (-1)                  = 0
428
        | otherwise                  = I32# (narrow32Int# (x# `remInt#` y#))
429
    div     x@(I32# x#) y@(I32# y#)
430
        | y == 0                     = divZeroError
431
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
432
        | otherwise                  = I32# (narrow32Int# (x# `divInt#` y#))
433
    mod       (I32# x#) y@(I32# y#)
434
        | y == 0                     = divZeroError
435 436 437 438
          -- The divMod CPU instruction fails for minBound `divMod` -1,
          -- but minBound `mod` -1 is well-defined (0). We therefore
          -- special-case it.
        | y == (-1)                  = 0
439
        | otherwise                  = I32# (narrow32Int# (x# `modInt#` y#))
440
    quotRem x@(I32# x#) y@(I32# y#)
441
        | y == 0                     = divZeroError
442 443
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
Ian Lynagh's avatar
Ian Lynagh committed
444 445 446 447
        | otherwise                  = case x# `quotRemInt#` y# of
                                       (# q, r #) ->
                                           (I32# (narrow32Int# q),
                                            I32# (narrow32Int# r))
448
    divMod  x@(I32# x#) y@(I32# y#)
449
        | y == 0                     = divZeroError
450 451
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
452 453 454 455
        | otherwise                  = case x# `divModInt#` y# of
                                       (# d, m #) ->
                                           (I32# (narrow32Int# d),
                                            I32# (narrow32Int# m))
456
    toInteger (I32# x#)              = smallInteger x#
457 458 459 460 461

instance Read Int32 where
    readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]

instance Bits Int32 where
462
    {-# INLINE shift #-}
463 464
    {-# INLINE bit #-}
    {-# INLINE testBit #-}
465

466 467 468
    (I32# x#) .&.   (I32# y#)  = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
    (I32# x#) .|.   (I32# y#)  = I32# (word2Int# (int2Word# x# `or#`  int2Word# y#))
    (I32# x#) `xor` (I32# y#)  = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
469
    complement (I32# x#)       = I32# (word2Int# (not# (int2Word# x#)))
470
    (I32# x#) `shift` (I# i#)
471
        | isTrue# (i# >=# 0#)  = I32# (narrow32Int# (x# `iShiftL#` i#))
472
        | otherwise            = I32# (x# `iShiftRA#` negateInt# i#)
473
    (I32# x#) `shiftL`       (I# i#) = I32# (narrow32Int# (x# `iShiftL#` i#))
tibbe's avatar
tibbe committed
474 475
    (I32# x#) `unsafeShiftL` (I# i#) =
        I32# (narrow32Int# (x# `uncheckedIShiftL#` i#))
476
    (I32# x#) `shiftR`       (I# i#) = I32# (x# `iShiftRA#` i#)
tibbe's avatar
tibbe committed
477
    (I32# x#) `unsafeShiftR` (I# i#) = I32# (x# `uncheckedIShiftRA#` i#)
478
    (I32# x#) `rotate` (I# i#)
479
        | isTrue# (i'# ==# 0#)
480 481
        = I32# x#
        | otherwise
482 483
        = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
                                         (x'# `uncheckedShiftRL#` (32# -# i'#)))))
484
        where
485
        !x'# = narrow32Word# (int2Word# x#)
486
        !i'# = word2Int# (int2Word# i# `and#` 31##)
487 488
    bitSizeMaybe i             = Just (finiteBitSize i)
    bitSize i                  = finiteBitSize i
489
    isSigned _                 = True
tibbe's avatar
tibbe committed
490
    popCount (I32# x#)         = I# (word2Int# (popCnt32# (int2Word# x#)))
491 492
    bit                        = bitDefault
    testBit                    = testBitDefault
493

494 495
instance FiniteBits Int32 where
    finiteBitSize _ = 32
496 497
    countLeadingZeros  (I32# x#) = I# (word2Int# (clz32# (int2Word# x#)))
    countTrailingZeros (I32# x#) = I# (word2Int# (ctz32# (int2Word# x#)))
498

499 500 501 502 503 504
{-# RULES
"fromIntegral/Word8->Int32"  fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
"fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
"fromIntegral/Int8->Int32"   fromIntegral = \(I8# x#) -> I32# x#
"fromIntegral/Int16->Int32"  fromIntegral = \(I16# x#) -> I32# x#
"fromIntegral/Int32->Int32"  fromIntegral = id :: Int32 -> Int32
505
"fromIntegral/a->Int32"      fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
506 507 508
"fromIntegral/Int32->a"      fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
  #-}

Daniel Fischer's avatar
Daniel Fischer committed
509 510
{-# RULES
"properFraction/Float->(Int32,Float)"
511
    properFraction = \x ->
Daniel Fischer's avatar
Daniel Fischer committed
512
                      case properFraction x of {
513
                        (n, y) -> ((fromIntegral :: Int -> Int32) n, y :: Float) }
Daniel Fischer's avatar
Daniel Fischer committed
514
"truncate/Float->Int32"
515
    truncate = (fromIntegral :: Int -> Int32) . (truncate :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
516
"floor/Float->Int32"
517
    floor    = (fromIntegral :: Int -> Int32) . (floor :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
518
"ceiling/Float->Int32"
519
    ceiling  = (fromIntegral :: Int -> Int32) . (ceiling :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
520
"round/Float->Int32"
521
    round    = (fromIntegral :: Int -> Int32) . (round  :: Float -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
522 523 524 525
  #-}

{-# RULES
"properFraction/Double->(Int32,Double)"
526
    properFraction = \x ->
Daniel Fischer's avatar
Daniel Fischer committed
527
                      case properFraction x of {
528
                        (n, y) -> ((fromIntegral :: Int -> Int32) n, y :: Double) }
Daniel Fischer's avatar
Daniel Fischer committed
529
"truncate/Double->Int32"
530
    truncate = (fromIntegral :: Int -> Int32) . (truncate :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
531
"floor/Double->Int32"
532
    floor    = (fromIntegral :: Int -> Int32) . (floor :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
533
"ceiling/Double->Int32"
534
    ceiling  = (fromIntegral :: Int -> Int32) . (ceiling :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
535
"round/Double->Int32"
536
    round    = (fromIntegral :: Int -> Int32) . (round  :: Double -> Int)
Daniel Fischer's avatar
Daniel Fischer committed
537 538
  #-}

539 540 541 542 543 544 545 546
instance Real Int32 where
    toRational x = toInteger x % 1

instance Bounded Int32 where
    minBound = -0x80000000
    maxBound =  0x7FFFFFFF

instance Ix Int32 where
Ian Lynagh's avatar
Ian Lynagh committed
547 548 549
    range (m,n)         = [m..n]
    unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
    inRange (m,n) i     = m <= i && i <= n
550

551 552 553 554
------------------------------------------------------------------------
-- type Int64
------------------------------------------------------------------------

555
#if WORD_SIZE_IN_BITS < 64
556

Simon Peyton Jones's avatar
Simon Peyton Jones committed
557
data {-# CTYPE "HsInt64" #-} Int64 = I64# Int64# deriving( Typeable )
558
-- ^ 64-bit signed integer type
559 560

instance Eq Int64 where
561 562
    (I64# x#) == (I64# y#) = isTrue# (x# `eqInt64#` y#)
    (I64# x#) /= (I64# y#) = isTrue# (x# `neInt64#` y#)
563 564

instance Ord Int64 where
565 566 567 568
    (I64# x#) <  (I64# y#) = isTrue# (x# `ltInt64#` y#)
    (I64# x#) <= (I64# y#) = isTrue# (x# `leInt64#` y#)
    (I64# x#) >  (I64# y#) = isTrue# (x# `gtInt64#` y#)
    (I64# x#) >= (I64# y#) = isTrue# (x# `geInt64#` y#)
569 570 571 572 573 574 575 576 577 578 579 580 581 582

instance Show Int64 where
    showsPrec p x = showsPrec p (toInteger x)

instance Num Int64 where
    (I64# x#) + (I64# y#)  = I64# (x# `plusInt64#`  y#)
    (I64# x#) - (I64# y#)  = I64# (x# `minusInt64#` y#)
    (I64# x#) * (I64# y#)  = I64# (x# `timesInt64#` y#)
    negate (I64# x#)       = I64# (negateInt64# x#)
    abs x | x >= 0         = x
          | otherwise      = negate x
    signum x | x > 0       = 1
    signum 0               = 0
    signum _               = -1
583
    fromInteger i          = I64# (integerToInt64 i)
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

instance Enum Int64 where
    succ x
        | x /= maxBound = x + 1
        | otherwise     = succError "Int64"
    pred x
        | x /= minBound = x - 1
        | otherwise     = predError "Int64"
    toEnum (I# i#)      = I64# (intToInt64# i#)
    fromEnum x@(I64# x#)
        | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
                        = I# (int64ToInt# x#)
        | otherwise     = fromEnumError "Int64" x
    enumFrom            = integralEnumFrom
    enumFromThen        = integralEnumFromThen
    enumFromTo          = integralEnumFromTo
    enumFromThenTo      = integralEnumFromThenTo

instance Integral Int64 where
    quot    x@(I64# x#) y@(I64# y#)
604
        | y == 0                     = divZeroError
605
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
606
        | otherwise                  = I64# (x# `quotInt64#` y#)
607
    rem       (I64# x#) y@(I64# y#)
608
        | y == 0                     = divZeroError
609 610 611 612
          -- The quotRem CPU instruction fails for minBound `quotRem` -1,
          -- but minBound `rem` -1 is well-defined (0). We therefore
          -- special-case it.
        | y == (-1)                  = 0
613
        | otherwise                  = I64# (x# `remInt64#` y#)
614
    div     x@(I64# x#) y@(I64# y#)
615
        | y == 0                     = divZeroError
616
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
617
        | otherwise                  = I64# (x# `divInt64#` y#)
618
    mod       (I64# x#) y@(I64# y#)
619
        | y == 0                     = divZeroError
620 621 622 623
          -- The divMod CPU instruction fails for minBound `divMod` -1,
          -- but minBound `mod` -1 is well-defined (0). We therefore
          -- special-case it.
        | y == (-1)                  = 0
624
        | otherwise                  = I64# (x# `modInt64#` y#)
625
    quotRem x@(I64# x#) y@(I64# y#)
626
        | y == 0                     = divZeroError
627 628
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
629 630
        | otherwise                  = (I64# (x# `quotInt64#` y#),
                                        I64# (x# `remInt64#` y#))
631
    divMod  x@(I64# x#) y@(I64# y#)
632
        | y == 0                     = divZeroError
633 634
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
635 636
        | otherwise                  = (I64# (x# `divInt64#` y#),
                                        I64# (x# `modInt64#` y#))
637
    toInteger (I64# x)               = int64ToInteger x
638

639

640
divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
641 642

-- Define div in terms of quot, being careful to avoid overflow (#7233)
643
x# `divInt64#` y#
644
    | isTrue# (x# `gtInt64#` zero) && isTrue# (y# `ltInt64#` zero)
645
        = ((x# `minusInt64#` one) `quotInt64#` y#) `minusInt64#` one
646
    | isTrue# (x# `ltInt64#` zero) && isTrue# (y# `gtInt64#` zero)
647 648 649 650 651 652 653
        = ((x# `plusInt64#` one)  `quotInt64#` y#) `minusInt64#` one
    | otherwise
        = x# `quotInt64#` y#
    where
    !zero = intToInt64# 0#
    !one  = intToInt64# 1#

654
x# `modInt64#` y#
655 656 657
    | isTrue# (x# `gtInt64#` zero) && isTrue# (y# `ltInt64#` zero) ||
      isTrue# (x# `ltInt64#` zero) && isTrue# (y# `gtInt64#` zero)
        = if isTrue# (r# `neInt64#` zero) then r# `plusInt64#` y# else zero
658 659
    | otherwise = r#
    where
660
    !zero = intToInt64# 0#
661
    !r# = x# `remInt64#` y#
662 663 664 665 666

instance Read Int64 where
    readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]

instance Bits Int64 where
667
    {-# INLINE shift #-}
668 669
    {-# INLINE bit #-}
    {-# INLINE testBit #-}
670

671 672 673 674 675
    (I64# x#) .&.   (I64# y#)  = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#))
    (I64# x#) .|.   (I64# y#)  = I64# (word64ToInt64# (int64ToWord64# x# `or64#`  int64ToWord64# y#))
    (I64# x#) `xor` (I64# y#)  = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#))
    complement (I64# x#)       = I64# (word64ToInt64# (not64# (int64ToWord64# x#)))
    (I64# x#) `shift` (I# i#)
676
        | isTrue# (i# >=# 0#)  = I64# (x# `iShiftL64#` i#)
677
        | otherwise            = I64# (x# `iShiftRA64#` negateInt# i#)
678
    (I64# x#) `shiftL` (I# i#) = I64# (x# `iShiftL64#` i#)
tibbe's avatar
tibbe committed
679
    (I64# x#) `unsafeShiftL` (I# i#) = I64# (x# `uncheckedIShiftL64#` i#)
680
    (I64# x#) `shiftR` (I# i#) = I64# (x# `iShiftRA64#` i#)
tibbe's avatar
tibbe committed
681
    (I64# x#) `unsafeShiftR` (I# i#) = I64# (x# `uncheckedIShiftRA64#` i#)
682
    (I64# x#) `rotate` (I# i#)
683
        | isTrue# (i'# ==# 0#)
684 685
        = I64# x#
        | otherwise
686 687
        = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
                                (x'# `uncheckedShiftRL64#` (64# -# i'#))))
688
        where
689
        !x'# = int64ToWord64# x#
690
        !i'# = word2Int# (int2Word# i# `and#` 63##)
691 692
    bitSizeMaybe i             = Just (finiteBitSize i)
    bitSize i                  = finiteBitSize i
693
    isSigned _                 = True
tibbe's avatar
tibbe committed
694
    popCount (I64# x#)         =
daniel.is.fischer's avatar
daniel.is.fischer committed
695
        I# (word2Int# (popCnt64# (int64ToWord64# x#)))
696 697
    bit                        = bitDefault
    testBit                    = testBitDefault
698

699 700 701 702 703 704 705
-- give the 64-bit shift operations the same treatment as the 32-bit
-- ones (see GHC.Base), namely we wrap them in tests to catch the
-- cases when we're shifting more than 64 bits to avoid unspecified
-- behaviour in the C shift operations.

iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#

706 707
a `iShiftL64#` b  | isTrue# (b >=# 64#) = intToInt64# 0#
		  | otherwise           = a `uncheckedIShiftL64#` b
708

709 710 711
a `iShiftRA64#` b | isTrue# (b >=# 64#) = if isTrue# (a `ltInt64#` (intToInt64# 0#))
				          then intToInt64# (-1#)
					  else intToInt64# 0#
712 713
		  | otherwise = a `uncheckedIShiftRA64#` b

714 715 716 717 718 719 720 721 722 723
{-# RULES
"fromIntegral/Int->Int64"    fromIntegral = \(I#   x#) -> I64# (intToInt64# x#)
"fromIntegral/Word->Int64"   fromIntegral = \(W#   x#) -> I64# (word64ToInt64# (wordToWord64# x#))
"fromIntegral/Word64->Int64" fromIntegral = \(W64# x#) -> I64# (word64ToInt64# x#)
"fromIntegral/Int64->Int"    fromIntegral = \(I64# x#) -> I#   (int64ToInt# x#)
"fromIntegral/Int64->Word"   fromIntegral = \(I64# x#) -> W#   (int2Word# (int64ToInt# x#))
"fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#)
"fromIntegral/Int64->Int64"  fromIntegral = id :: Int64 -> Int64
  #-}

Daniel Fischer's avatar
Daniel Fischer committed
724 725 726
-- No RULES for RealFrac methods if Int is smaller than Int64, we can't
-- go through Int and whether going through Integer is faster is uncertain.
#else
727 728 729 730

-- Int64 is represented in the same way as Int.
-- Operations may assume and must ensure that it holds only values
-- from its logical range.
731

Simon Peyton Jones's avatar
Simon Peyton Jones committed
732
data {-# CTYPE "HsInt64" #-} Int64 = I64# Int# deriving (Eq, Ord, Typeable)
733
-- ^ 64-bit signed integer type
734 735 736 737 738 739 740 741 742 743 744 745 746 747

instance Show Int64 where
    showsPrec p x = showsPrec p (fromIntegral x :: Int)

instance Num Int64 where
    (I64# x#) + (I64# y#)  = I64# (x# +# y#)
    (I64# x#) - (I64# y#)  = I64# (x# -# y#)
    (I64# x#) * (I64# y#)  = I64# (x# *# y#)
    negate (I64# x#)       = I64# (negateInt# x#)
    abs x | x >= 0         = x
          | otherwise      = negate x
    signum x | x > 0       = 1
    signum 0               = 0
    signum _               = -1
748
    fromInteger i          = I64# (integerToInt i)
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

instance Enum Int64 where
    succ x
        | x /= maxBound = x + 1
        | otherwise     = succError "Int64"
    pred x
        | x /= minBound = x - 1
        | otherwise     = predError "Int64"
    toEnum (I# i#)      = I64# i#
    fromEnum (I64# x#)  = I# x#
    enumFrom            = boundedEnumFrom
    enumFromThen        = boundedEnumFromThen

instance Integral Int64 where
    quot    x@(I64# x#) y@(I64# y#)
764
        | y == 0                     = divZeroError
765
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
766
        | otherwise                  = I64# (x# `quotInt#` y#)
767
    rem       (I64# x#) y@(I64# y#)
768
        | y == 0                     = divZeroError
769 770 771 772
          -- The quotRem CPU instruction fails for minBound `quotRem` -1,
          -- but minBound `rem` -1 is well-defined (0). We therefore
          -- special-case it.
        | y == (-1)                  = 0
773
        | otherwise                  = I64# (x# `remInt#` y#)
774
    div     x@(I64# x#) y@(I64# y#)
775
        | y == 0                     = divZeroError
776
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
777
        | otherwise                  = I64# (x# `divInt#` y#)
778
    mod       (I64# x#) y@(I64# y#)
779
        | y == 0                     = divZeroError
780 781 782 783
          -- The divMod CPU instruction fails for minBound `divMod` -1,
          -- but minBound `mod` -1 is well-defined (0). We therefore
          -- special-case it.
        | y == (-1)                  = 0
784
        | otherwise                  = I64# (x# `modInt#`