Int.hs 38.4 KB
Newer Older
1
{-# LANGUAGE Trustworthy #-}
Ian Lynagh's avatar
Ian Lynagh committed
2
{-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash, UnboxedTuples,
3
             StandaloneDeriving #-}
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

#include "MachDeps.h"

ross's avatar
ross committed
22
-- #hide
23
module GHC.Int (
24
25
        Int8(..), Int16(..), Int32(..), Int64(..),
        uncheckedIShiftL64#, uncheckedIShiftRA64#
26
    ) where
27
28

import Data.Bits
29
import Data.Maybe
30

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

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

46
47
48
49
50
51
52
53

------------------------------------------------------------------------
-- 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.

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

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

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

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

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

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

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

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

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

167
168
169
instance FiniteBits Int8 where
    finiteBitSize _ = 8

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

Daniel Fischer's avatar
Daniel Fischer committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
{-# RULES
"properFraction/Float->(Int8,Float)"
    forall x. properFraction (x :: Float) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
"truncate/Float->Int8"
    forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int8) (truncate x)
"floor/Float->Int8"
    forall x. floor    (x :: Float) = (fromIntegral :: Int -> Int8) (floor x)
"ceiling/Float->Int8"
    forall x. ceiling  (x :: Float) = (fromIntegral :: Int -> Int8) (ceiling x)
"round/Float->Int8"
    forall x. round    (x :: Float) = (fromIntegral :: Int -> Int8) (round x)
  #-}

{-# RULES
"properFraction/Double->(Int8,Double)"
    forall x. properFraction (x :: Double) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
"truncate/Double->Int8"
    forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int8) (truncate x)
"floor/Double->Int8"
    forall x. floor    (x :: Double) = (fromIntegral :: Int -> Int8) (floor x)
"ceiling/Double->Int8"
    forall x. ceiling  (x :: Double) = (fromIntegral :: Int -> Int8) (ceiling x)
"round/Double->Int8"
    forall x. round    (x :: Double) = (fromIntegral :: Int -> Int8) (round x)
  #-}

206
207
208
209
210
211
212
------------------------------------------------------------------------
-- 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.

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

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

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

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

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

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

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

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

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

326
327
328
instance FiniteBits Int16 where
    finiteBitSize _ = 16

329
330
331
332
{-# 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
333
"fromIntegral/a->Int16"      fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
334
335
336
"fromIntegral/Int16->a"      fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
  #-}

Daniel Fischer's avatar
Daniel Fischer committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
{-# RULES
"properFraction/Float->(Int16,Float)"
    forall x. properFraction (x :: Float) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
"truncate/Float->Int16"
    forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int16) (truncate x)
"floor/Float->Int16"
    forall x. floor    (x :: Float) = (fromIntegral :: Int -> Int16) (floor x)
"ceiling/Float->Int16"
    forall x. ceiling  (x :: Float) = (fromIntegral :: Int -> Int16) (ceiling x)
"round/Float->Int16"
    forall x. round    (x :: Float) = (fromIntegral :: Int -> Int16) (round x)
  #-}

{-# RULES
"properFraction/Double->(Int16,Double)"
    forall x. properFraction (x :: Double) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
"truncate/Double->Int16"
    forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int16) (truncate x)
"floor/Double->Int16"
    forall x. floor    (x :: Double) = (fromIntegral :: Int -> Int16) (floor x)
"ceiling/Double->Int16"
    forall x. ceiling  (x :: Double) = (fromIntegral :: Int -> Int16) (ceiling x)
"round/Double->Int16"
    forall x. round    (x :: Double) = (fromIntegral :: Int -> Int16) (round x)
  #-}

367
368
369
370
371
------------------------------------------------------------------------
-- type Int32
------------------------------------------------------------------------

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

Ian Lynagh's avatar
Ian Lynagh committed
377
data {-# CTYPE "HsInt32" #-} Int32 = I32# Int# deriving (Eq, Ord)
378
-- ^ 32-bit signed integer type
379
380
381
382
383

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

instance Num Int32 where
384
385
386
387
    (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#))
388
389
390
391
392
    abs x | x >= 0         = x
          | otherwise      = negate x
    signum x | x > 0       = 1
    signum 0               = 0
    signum _               = -1
393
    fromInteger i          = I32# (narrow32Int# (integerToInt i))
394
395
396
397
398
399
400
401

instance Enum Int32 where
    succ x
        | x /= maxBound = x + 1
        | otherwise     = succError "Int32"
    pred x
        | x /= minBound = x - 1
        | otherwise     = predError "Int32"
402
#if WORD_SIZE_IN_BITS == 32
403
404
405
406
407
408
409
410
411
412
413
414
415
    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#)
416
        | y == 0                     = divZeroError
417
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
418
        | otherwise                  = I32# (narrow32Int# (x# `quotInt#` y#))
419
    rem       (I32# x#) y@(I32# y#)
420
        | y == 0                     = divZeroError
421
422
423
424
          -- 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
425
        | otherwise                  = I32# (narrow32Int# (x# `remInt#` y#))
426
    div     x@(I32# x#) y@(I32# y#)
427
        | y == 0                     = divZeroError
428
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
429
        | otherwise                  = I32# (narrow32Int# (x# `divInt#` y#))
430
    mod       (I32# x#) y@(I32# y#)
431
        | y == 0                     = divZeroError
432
433
434
435
          -- 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
436
        | otherwise                  = I32# (narrow32Int# (x# `modInt#` y#))
437
    quotRem x@(I32# x#) y@(I32# y#)
438
        | y == 0                     = divZeroError
439
440
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
Ian Lynagh's avatar
Ian Lynagh committed
441
442
443
444
        | otherwise                  = case x# `quotRemInt#` y# of
                                       (# q, r #) ->
                                           (I32# (narrow32Int# q),
                                            I32# (narrow32Int# r))
445
    divMod  x@(I32# x#) y@(I32# y#)
446
        | y == 0                     = divZeroError
447
448
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
449
450
451
452
        | otherwise                  = case x# `divModInt#` y# of
                                       (# d, m #) ->
                                           (I32# (narrow32Int# d),
                                            I32# (narrow32Int# m))
453
    toInteger (I32# x#)              = smallInteger x#
454
455
456
457
458

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

instance Bits Int32 where
459
    {-# INLINE shift #-}
460
461
    {-# INLINE bit #-}
    {-# INLINE testBit #-}
462

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

491
492
493
instance FiniteBits Int32 where
    finiteBitSize _ = 32

494
495
496
497
498
499
{-# 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
500
"fromIntegral/a->Int32"      fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
501
502
503
"fromIntegral/Int32->a"      fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
  #-}

Daniel Fischer's avatar
Daniel Fischer committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
{-# RULES
"properFraction/Float->(Int32,Float)"
    forall x. properFraction (x :: Float) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
"truncate/Float->Int32"
    forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int32) (truncate x)
"floor/Float->Int32"
    forall x. floor    (x :: Float) = (fromIntegral :: Int -> Int32) (floor x)
"ceiling/Float->Int32"
    forall x. ceiling  (x :: Float) = (fromIntegral :: Int -> Int32) (ceiling x)
"round/Float->Int32"
    forall x. round    (x :: Float) = (fromIntegral :: Int -> Int32) (round x)
  #-}

{-# RULES
"properFraction/Double->(Int32,Double)"
    forall x. properFraction (x :: Double) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
"truncate/Double->Int32"
    forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int32) (truncate x)
"floor/Double->Int32"
    forall x. floor    (x :: Double) = (fromIntegral :: Int -> Int32) (floor x)
"ceiling/Double->Int32"
    forall x. ceiling  (x :: Double) = (fromIntegral :: Int -> Int32) (ceiling x)
"round/Double->Int32"
    forall x. round    (x :: Double) = (fromIntegral :: Int -> Int32) (round x)
  #-}

534
535
536
537
538
539
540
541
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
542
543
544
    range (m,n)         = [m..n]
    unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
    inRange (m,n) i     = m <= i && i <= n
545

546
547
548
549
------------------------------------------------------------------------
-- type Int64
------------------------------------------------------------------------

550
#if WORD_SIZE_IN_BITS < 64
551

Ian Lynagh's avatar
Ian Lynagh committed
552
data {-# CTYPE "HsInt64" #-} Int64 = I64# Int64#
553
-- ^ 64-bit signed integer type
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577

instance Eq Int64 where
    (I64# x#) == (I64# y#) = x# `eqInt64#` y#
    (I64# x#) /= (I64# y#) = x# `neInt64#` y#

instance Ord Int64 where
    (I64# x#) <  (I64# y#) = x# `ltInt64#` y#
    (I64# x#) <= (I64# y#) = x# `leInt64#` y#
    (I64# x#) >  (I64# y#) = x# `gtInt64#` y#
    (I64# x#) >= (I64# y#) = x# `geInt64#` y#

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
578
    fromInteger i          = I64# (integerToInt64 i)
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598

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#)
599
        | y == 0                     = divZeroError
600
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
601
        | otherwise                  = I64# (x# `quotInt64#` y#)
602
    rem       (I64# x#) y@(I64# y#)
603
        | y == 0                     = divZeroError
604
605
606
607
          -- 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
608
        | otherwise                  = I64# (x# `remInt64#` y#)
609
    div     x@(I64# x#) y@(I64# y#)
610
        | y == 0                     = divZeroError
611
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
612
        | otherwise                  = I64# (x# `divInt64#` y#)
613
    mod       (I64# x#) y@(I64# y#)
614
        | y == 0                     = divZeroError
615
616
617
618
          -- 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
619
        | otherwise                  = I64# (x# `modInt64#` y#)
620
    quotRem x@(I64# x#) y@(I64# y#)
621
        | y == 0                     = divZeroError
622
623
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
624
625
        | otherwise                  = (I64# (x# `quotInt64#` y#),
                                        I64# (x# `remInt64#` y#))
626
    divMod  x@(I64# x#) y@(I64# y#)
627
        | y == 0                     = divZeroError
628
629
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
630
631
        | otherwise                  = (I64# (x# `divInt64#` y#),
                                        I64# (x# `modInt64#` y#))
632
    toInteger (I64# x)               = int64ToInteger x
633

634

635
divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
636
637

-- Define div in terms of quot, being careful to avoid overflow (#7233)
638
x# `divInt64#` y#
639
640
641
642
643
644
645
646
647
648
    | (x# `gtInt64#` zero) && (y# `ltInt64#` zero)
        = ((x# `minusInt64#` one) `quotInt64#` y#) `minusInt64#` one
    | (x# `ltInt64#` zero) && (y# `gtInt64#` zero)
        = ((x# `plusInt64#` one)  `quotInt64#` y#) `minusInt64#` one
    | otherwise
        = x# `quotInt64#` y#
    where
    !zero = intToInt64# 0#
    !one  = intToInt64# 1#

649
x# `modInt64#` y#
650
651
652
    | (x# `gtInt64#` zero) && (y# `ltInt64#` zero) ||
      (x# `ltInt64#` zero) && (y# `gtInt64#` zero)
        = if r# `neInt64#` zero then r# `plusInt64#` y# else zero
653
654
    | otherwise = r#
    where
655
    !zero = intToInt64# 0#
656
    !r# = x# `remInt64#` y#
657
658
659
660
661

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

instance Bits Int64 where
662
    {-# INLINE shift #-}
663
664
    {-# INLINE bit #-}
    {-# INLINE testBit #-}
665

666
667
668
669
670
671
672
    (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#)
        | i# >=# 0#            = I64# (x# `iShiftL64#` i#)
        | otherwise            = I64# (x# `iShiftRA64#` negateInt# i#)
673
    (I64# x#) `shiftL` (I# i#) = I64# (x# `iShiftL64#` i#)
tibbe's avatar
tibbe committed
674
    (I64# x#) `unsafeShiftL` (I# i#) = I64# (x# `uncheckedIShiftL64#` i#)
675
    (I64# x#) `shiftR` (I# i#) = I64# (x# `iShiftRA64#` i#)
tibbe's avatar
tibbe committed
676
    (I64# x#) `unsafeShiftR` (I# i#) = I64# (x# `uncheckedIShiftRA64#` i#)
677
    (I64# x#) `rotate` (I# i#)
daniel.is.fischer's avatar
daniel.is.fischer committed
678
        | i'# ==# 0#
679
680
        = I64# x#
        | otherwise
681
682
        = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
                                (x'# `uncheckedShiftRL64#` (64# -# i'#))))
683
        where
684
        !x'# = int64ToWord64# x#
685
        !i'# = word2Int# (int2Word# i# `and#` 63##)
686
687
    bitSizeMaybe i             = Just (finiteBitSize i)
    bitSize i                  = finiteBitSize i
688
    isSigned _                 = True
tibbe's avatar
tibbe committed
689
    popCount (I64# x#)         =
daniel.is.fischer's avatar
daniel.is.fischer committed
690
        I# (word2Int# (popCnt64# (int64ToWord64# x#)))
691
692
    bit                        = bitDefault
    testBit                    = testBitDefault
693

694
695
696
697
698
699
700
701
702
703
-- 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#

a `iShiftL64#` b  | b >=# 64# = intToInt64# 0#
		  | otherwise = a `uncheckedIShiftL64#` b

daniel.is.fischer's avatar
daniel.is.fischer committed
704
705
a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#)
					then intToInt64# (-1#)
706
707
708
					else intToInt64# 0#
		  | otherwise = a `uncheckedIShiftRA64#` b

709
710
711
712
713
714
715
716
717
718
{-# 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
719
720
721
-- 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
722
723
724
725

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

Ian Lynagh's avatar
Ian Lynagh committed
727
data {-# CTYPE "HsInt64" #-} Int64 = I64# Int# deriving (Eq, Ord)
728
-- ^ 64-bit signed integer type
729
730
731
732
733
734
735
736
737
738
739
740
741
742

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
743
    fromInteger i          = I64# (integerToInt i)
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758

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#)
759
        | y == 0                     = divZeroError
760
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
761
        | otherwise                  = I64# (x# `quotInt#` y#)
762
    rem       (I64# x#) y@(I64# y#)
763
        | y == 0                     = divZeroError
764
765
766
767
          -- 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
768
        | otherwise                  = I64# (x# `remInt#` y#)
769
    div     x@(I64# x#) y@(I64# y#)
770
        | y == 0                     = divZeroError
771
        | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
772
        | otherwise                  = I64# (x# `divInt#` y#)
773
    mod       (I64# x#) y@(I64# y#)
774
        | y == 0                     = divZeroError
775
776
777
778
          -- 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
779
        | otherwise                  = I64# (x# `modInt#` y#)
780
    quotRem x@(I64# x#) y@(I64# y#)
781
        | y == 0                     = divZeroError
782
783
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
Ian Lynagh's avatar
Ian Lynagh committed
784
785
786
        | otherwise                  = case x# `quotRemInt#` y# of
                                       (# q, r #) ->
                                           (I64# q, I64# r)
787
    divMod  x@(I64# x#) y@(I64# y#)
788
        | y == 0                     = divZeroError
789
790
          -- Note [Order of tests]
        | y == (-1) && x == minBound = (overflowError, 0)
791
792
793
        | otherwise                  = case x# `divModInt#` y# of
                                       (# d, m #) ->
                                           (I64# d, I64# m)
794
    toInteger (I64# x#)              = smallInteger x#
795
796
797
798
799

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

instance Bits Int64 where
800
    {-# INLINE shift #-}
801
802
    {-# INLINE bit #-}
    {-# INLINE testBit #-}
803

804
805
806
807
808
809
810
    (I64# x#) .&.   (I64# y#)  = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
    (I64# x#) .|.   (I64# y#)  = I64# (word2Int# (int2Word# x# `or#`  int2Word# y#))
    (I64# x#) `xor` (I64# y#)  = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
    complement (I64# x#)       = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
    (I64# x#) `shift` (I# i#)
        | i# >=# 0#            = I64# (x# `iShiftL#` i#)
        | otherwise            = I64# (x# `iShiftRA#` negateInt# i#)
811
    (I64# x#) `shiftL` (I# i#) = I64# (x# `iShiftL#` i#)
tibbe's avatar
tibbe committed
812
    (I64# x#) `unsafeShiftL` (I# i#) = I64# (x# `uncheckedIShiftL#` i#)
813
    (I64# x#) `shiftR` (I# i#) = I64# (x# `iShiftRA#` i#)
tibbe's avatar
tibbe committed
814
    (I64# x#) `unsafeShiftR` (I# i#) = I64# (x# `uncheckedIShiftRA#` i#)
815
    (I64# x#) `rotate` (I# i#)
daniel.is.fischer's avatar
daniel.is.fischer committed
816
        | i'# ==# 0#
817
818
        = I64# x#
        | otherwise
819
820
        = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
                           (x'# `uncheckedShiftRL#` (64# -# i'#))))
821
        where
822
        !x'# = int2Word# x#
823
        !i'# = word2Int# (int2Word# i# `and#` 63##)
824
825
    bitSizeMaybe i             = Just (finiteBitSize i)
    bitSize i                  = finiteBitSize i
826
    isSigned _                 = True
tibbe's avatar
tibbe committed
827
    popCount (I64# x#)         = I# (word2Int# (popCnt64# (int2Word# x#)))
828
829
    bit                        = bitDefault
    testBit                    = testBitDefault
830
831

{-# RULES
832
"fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
833
834
835
"fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
  #-}

Daniel Fischer's avatar
Daniel Fischer committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
{-# RULES
"properFraction/Float->(Int64,Float)"
    forall x. properFraction (x :: Float) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
"truncate/Float->Int64"
    forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int64) (truncate x)
"floor/Float->Int64"
    forall x. floor    (x :: Float) = (fromIntegral :: Int -> Int64) (floor x)
"ceiling/Float->Int64"
    forall x. ceiling  (x :: Float) = (fromIntegral :: Int -> Int64) (ceiling x)
"round/Float->Int64"
    forall x. round    (x :: Float) = (fromIntegral :: Int -> Int64) (round x)
  #-}

{-# RULES
"properFraction/Double->(Int64,Double)"
    forall x. properFraction (x :: Double) =
                      case properFraction x of {
                        (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
"truncate/Double->Int64"
    forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int64) (truncate x)
"floor/Double->Int64"
    forall x. floor    (x :: Double) = (fromIntegral :: Int -> Int64) (floor x)
"ceiling/Double->Int64"
    forall x. ceiling  (x :: Double) = (fromIntegral :: Int -> Int64) (ceiling x)
"round/Double->Int64"
    forall x. round    (x :: Double) = (fromIntegral :: Int -> Int64) (round x)
  #-}

Ian Lynagh's avatar
Ian Lynagh committed
866
uncheckedIShiftL64# :: Int# -> Int# -> Int#
867
uncheckedIShiftL64#  = uncheckedIShiftL#
Ian Lynagh's avatar
Ian Lynagh committed
868
869

uncheckedIShiftRA64# :: Int# -> Int# -> Int#
870
uncheckedIShiftRA64# = uncheckedIShiftRA#
871
872
#endif

873
874
875
instance FiniteBits Int64 where
    finiteBitSize _ = 64

876
877
878
879
880
881
882
883
instance Real Int64 where
    toRational x = toInteger x % 1

instance Bounded Int64 where
    minBound = -0x8000000000000000
    maxBound =  0x7FFFFFFFFFFFFFFF

instance Ix Int64 where
Ian Lynagh's avatar
Ian Lynagh committed
884
885
886
    range (m,n)         = [m..n]
    unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
    inRange (m,n) i     = m <= i && i <= n
887

888

889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
{-
Note [Order of tests]

Suppose we had a definition like:

    quot x y
     | y == 0                     = divZeroError
     | x == minBound && y == (-1) = overflowError
     | otherwise                  = x `primQuot` y

Note in particular that the
    x == minBound
test comes before the
    y == (-1)
test.

this expands to something like:

    case y of
    0 -> divZeroError
    _ -> case x of
         -9223372036854775808 ->
             case y of
             -1 -> overflowError
             _ -> x `primQuot` y
         _ -> x `primQuot` y

Now if we have the call (x `quot` 2), and quot gets inlined, then we get:

    case 2 of
    0 -> divZeroError
    _ -> case x of
         -9223372036854775808 ->
             case 2 of
             -1 -> overflowError
             _ -> x `primQuot` 2
         _ -> x `primQuot` 2

which simplifies to:

    case x of
    -9223372036854775808 -> x `primQuot` 2
    _                    -> x `primQuot` 2

Now we have a case with two identical branches, which would be
eliminated (assuming it doesn't affect strictness, which it doesn't in
this case), leaving the desired:

    x `primQuot` 2

except in the minBound branch we know what x is, and GHC cleverly does
the division at compile time, giving:

    case x of
    -9223372036854775808 -> -4611686018427387904
    _                    -> x `primQuot` 2

So instead we use a definition like:

    quot x y
     | y == 0                     = divZeroError
     | y == (-1) && x == minBound = overflowError
     | otherwise                  = x `primQuot` y

which gives us:

    case y of
    0 -> divZeroError
    -1 ->
        case x of
        -9223372036854775808 -> overflowError
        _ -> x `primQuot` y
    _ -> x `primQuot` y

for which our call (x `quot` 2) expands to:

    case 2 of
    0 -> divZeroError
    -1 ->
        case x of
        -9223372036854775808 -> overflowError
        _ -> x `primQuot` 2
    _ -> x `primQuot` 2

which simplifies to:

    x `primQuot` 2

as required.



But we now have the same problem with a constant numerator: the call
(2 `quot` y) expands to

    case y of
    0 -> divZeroError
    -1 ->
        case 2 of
        -9223372036854775808 -> overflowError
        _ -> 2 `primQuot` y
    _ -> 2 `primQuot` y

which simplifies to:

    case y of
    0 -> divZeroError
    -1 -> 2 `primQuot` y
    _ -> 2 `primQuot` y

which simplifies to:

    case y of
    0 -> divZeroError
    -1 -> -2
    _ -> 2 `primQuot` y


However, constant denominators are more common than constant numerators,
so the
    y == (-1) && x == minBound
order gives us better code in the common case.
-}