Follow toInt# -> integerToInt renaming
[packages/base.git] / GHC / Int.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP, NoImplicitPrelude, BangPatterns, MagicHash,
3 StandaloneDeriving #-}
4 {-# OPTIONS_HADDOCK hide #-}
5 -----------------------------------------------------------------------------
6 -- |
7 -- Module : GHC.Int
8 -- Copyright : (c) The University of Glasgow 1997-2002
9 -- License : see libraries/base/LICENSE
10 --
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (GHC Extensions)
14 --
15 -- The sized integral datatypes, 'Int8', 'Int16', 'Int32', and 'Int64'.
16 --
17 -----------------------------------------------------------------------------
18
19 #include "MachDeps.h"
20
21 -- #hide
22 module GHC.Int (
23 Int8(..), Int16(..), Int32(..), Int64(..),
24 uncheckedIShiftL64#, uncheckedIShiftRA64#
25 ) where
26
27 import Data.Bits
28
29 #if WORD_SIZE_IN_BITS < 32
30 import GHC.IntWord32
31 #endif
32 #if WORD_SIZE_IN_BITS < 64
33 import GHC.IntWord64
34 #endif
35
36 import GHC.Base
37 import GHC.Enum
38 import GHC.Num
39 import GHC.Real
40 import GHC.Read
41 import GHC.Arr
42 import GHC.Err
43 import GHC.Word hiding (uncheckedShiftL64#, uncheckedShiftRL64#)
44 import GHC.Show
45 import GHC.Float () -- for RealFrac methods
46
47
48 ------------------------------------------------------------------------
49 -- type Int8
50 ------------------------------------------------------------------------
51
52 -- Int8 is represented in the same way as Int. Operations may assume
53 -- and must ensure that it holds only values from its logical range.
54
55 data Int8 = I8# Int# deriving (Eq, Ord)
56 -- ^ 8-bit signed integer type
57
58 instance Show Int8 where
59 showsPrec p x = showsPrec p (fromIntegral x :: Int)
60
61 instance Num Int8 where
62 (I8# x#) + (I8# y#) = I8# (narrow8Int# (x# +# y#))
63 (I8# x#) - (I8# y#) = I8# (narrow8Int# (x# -# y#))
64 (I8# x#) * (I8# y#) = I8# (narrow8Int# (x# *# y#))
65 negate (I8# x#) = I8# (narrow8Int# (negateInt# x#))
66 abs x | x >= 0 = x
67 | otherwise = negate x
68 signum x | x > 0 = 1
69 signum 0 = 0
70 signum _ = -1
71 fromInteger i = I8# (narrow8Int# (integerToInt i))
72
73 instance Real Int8 where
74 toRational x = toInteger x % 1
75
76 instance Enum Int8 where
77 succ x
78 | x /= maxBound = x + 1
79 | otherwise = succError "Int8"
80 pred x
81 | x /= minBound = x - 1
82 | otherwise = predError "Int8"
83 toEnum i@(I# i#)
84 | i >= fromIntegral (minBound::Int8) && i <= fromIntegral (maxBound::Int8)
85 = I8# i#
86 | otherwise = toEnumError "Int8" i (minBound::Int8, maxBound::Int8)
87 fromEnum (I8# x#) = I# x#
88 enumFrom = boundedEnumFrom
89 enumFromThen = boundedEnumFromThen
90
91 instance Integral Int8 where
92 quot x@(I8# x#) y@(I8# y#)
93 | y == 0 = divZeroError
94 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
95 | otherwise = I8# (narrow8Int# (x# `quotInt#` y#))
96 rem (I8# x#) y@(I8# y#)
97 | y == 0 = divZeroError
98 | otherwise = I8# (narrow8Int# (x# `remInt#` y#))
99 div x@(I8# x#) y@(I8# y#)
100 | y == 0 = divZeroError
101 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
102 | otherwise = I8# (narrow8Int# (x# `divInt#` y#))
103 mod (I8# x#) y@(I8# y#)
104 | y == 0 = divZeroError
105 | otherwise = I8# (narrow8Int# (x# `modInt#` y#))
106 quotRem x@(I8# x#) y@(I8# y#)
107 | y == 0 = divZeroError
108 -- Note [Order of tests]
109 | y == (-1) && x == minBound = (overflowError, 0)
110 | otherwise = (I8# (narrow8Int# (x# `quotInt#` y#)),
111 I8# (narrow8Int# (x# `remInt#` y#)))
112 divMod x@(I8# x#) y@(I8# y#)
113 | y == 0 = divZeroError
114 -- Note [Order of tests]
115 | y == (-1) && x == minBound = (overflowError, 0)
116 | otherwise = (I8# (narrow8Int# (x# `divInt#` y#)),
117 I8# (narrow8Int# (x# `modInt#` y#)))
118 toInteger (I8# x#) = smallInteger x#
119
120 instance Bounded Int8 where
121 minBound = -0x80
122 maxBound = 0x7F
123
124 instance Ix Int8 where
125 range (m,n) = [m..n]
126 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
127 inRange (m,n) i = m <= i && i <= n
128
129 instance Read Int8 where
130 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
131
132 instance Bits Int8 where
133 {-# INLINE shift #-}
134
135 (I8# x#) .&. (I8# y#) = I8# (word2Int# (int2Word# x# `and#` int2Word# y#))
136 (I8# x#) .|. (I8# y#) = I8# (word2Int# (int2Word# x# `or#` int2Word# y#))
137 (I8# x#) `xor` (I8# y#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# y#))
138 complement (I8# x#) = I8# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
139 (I8# x#) `shift` (I# i#)
140 | i# >=# 0# = I8# (narrow8Int# (x# `iShiftL#` i#))
141 | otherwise = I8# (x# `iShiftRA#` negateInt# i#)
142 (I8# x#) `rotate` (I# i#)
143 | i'# ==# 0#
144 = I8# x#
145 | otherwise
146 = I8# (narrow8Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
147 (x'# `uncheckedShiftRL#` (8# -# i'#)))))
148 where
149 !x'# = narrow8Word# (int2Word# x#)
150 !i'# = word2Int# (int2Word# i# `and#` int2Word# 7#)
151 bitSize _ = 8
152 isSigned _ = True
153
154 {-# RULES
155 "fromIntegral/Int8->Int8" fromIntegral = id :: Int8 -> Int8
156 "fromIntegral/a->Int8" fromIntegral = \x -> case fromIntegral x of I# x# -> I8# (narrow8Int# x#)
157 "fromIntegral/Int8->a" fromIntegral = \(I8# x#) -> fromIntegral (I# x#)
158 #-}
159
160 {-# RULES
161 "properFraction/Float->(Int8,Float)"
162 forall x. properFraction (x :: Float) =
163 case properFraction x of {
164 (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
165 "truncate/Float->Int8"
166 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int8) (truncate x)
167 "floor/Float->Int8"
168 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int8) (floor x)
169 "ceiling/Float->Int8"
170 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int8) (ceiling x)
171 "round/Float->Int8"
172 forall x. round (x :: Float) = (fromIntegral :: Int -> Int8) (round x)
173 #-}
174
175 {-# RULES
176 "properFraction/Double->(Int8,Double)"
177 forall x. properFraction (x :: Double) =
178 case properFraction x of {
179 (n, y) -> ((fromIntegral :: Int -> Int8) n, y) }
180 "truncate/Double->Int8"
181 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int8) (truncate x)
182 "floor/Double->Int8"
183 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int8) (floor x)
184 "ceiling/Double->Int8"
185 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int8) (ceiling x)
186 "round/Double->Int8"
187 forall x. round (x :: Double) = (fromIntegral :: Int -> Int8) (round x)
188 #-}
189
190 ------------------------------------------------------------------------
191 -- type Int16
192 ------------------------------------------------------------------------
193
194 -- Int16 is represented in the same way as Int. Operations may assume
195 -- and must ensure that it holds only values from its logical range.
196
197 data Int16 = I16# Int# deriving (Eq, Ord)
198 -- ^ 16-bit signed integer type
199
200 instance Show Int16 where
201 showsPrec p x = showsPrec p (fromIntegral x :: Int)
202
203 instance Num Int16 where
204 (I16# x#) + (I16# y#) = I16# (narrow16Int# (x# +# y#))
205 (I16# x#) - (I16# y#) = I16# (narrow16Int# (x# -# y#))
206 (I16# x#) * (I16# y#) = I16# (narrow16Int# (x# *# y#))
207 negate (I16# x#) = I16# (narrow16Int# (negateInt# x#))
208 abs x | x >= 0 = x
209 | otherwise = negate x
210 signum x | x > 0 = 1
211 signum 0 = 0
212 signum _ = -1
213 fromInteger i = I16# (narrow16Int# (integerToInt i))
214
215 instance Real Int16 where
216 toRational x = toInteger x % 1
217
218 instance Enum Int16 where
219 succ x
220 | x /= maxBound = x + 1
221 | otherwise = succError "Int16"
222 pred x
223 | x /= minBound = x - 1
224 | otherwise = predError "Int16"
225 toEnum i@(I# i#)
226 | i >= fromIntegral (minBound::Int16) && i <= fromIntegral (maxBound::Int16)
227 = I16# i#
228 | otherwise = toEnumError "Int16" i (minBound::Int16, maxBound::Int16)
229 fromEnum (I16# x#) = I# x#
230 enumFrom = boundedEnumFrom
231 enumFromThen = boundedEnumFromThen
232
233 instance Integral Int16 where
234 quot x@(I16# x#) y@(I16# y#)
235 | y == 0 = divZeroError
236 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
237 | otherwise = I16# (narrow16Int# (x# `quotInt#` y#))
238 rem (I16# x#) y@(I16# y#)
239 | y == 0 = divZeroError
240 | otherwise = I16# (narrow16Int# (x# `remInt#` y#))
241 div x@(I16# x#) y@(I16# y#)
242 | y == 0 = divZeroError
243 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
244 | otherwise = I16# (narrow16Int# (x# `divInt#` y#))
245 mod (I16# x#) y@(I16# y#)
246 | y == 0 = divZeroError
247 | otherwise = I16# (narrow16Int# (x# `modInt#` y#))
248 quotRem x@(I16# x#) y@(I16# y#)
249 | y == 0 = divZeroError
250 -- Note [Order of tests]
251 | y == (-1) && x == minBound = (overflowError, 0)
252 | otherwise = (I16# (narrow16Int# (x# `quotInt#` y#)),
253 I16# (narrow16Int# (x# `remInt#` y#)))
254 divMod x@(I16# x#) y@(I16# y#)
255 | y == 0 = divZeroError
256 -- Note [Order of tests]
257 | y == (-1) && x == minBound = (overflowError, 0)
258 | otherwise = (I16# (narrow16Int# (x# `divInt#` y#)),
259 I16# (narrow16Int# (x# `modInt#` y#)))
260 toInteger (I16# x#) = smallInteger x#
261
262 instance Bounded Int16 where
263 minBound = -0x8000
264 maxBound = 0x7FFF
265
266 instance Ix Int16 where
267 range (m,n) = [m..n]
268 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
269 inRange (m,n) i = m <= i && i <= n
270
271 instance Read Int16 where
272 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
273
274 instance Bits Int16 where
275 {-# INLINE shift #-}
276
277 (I16# x#) .&. (I16# y#) = I16# (word2Int# (int2Word# x# `and#` int2Word# y#))
278 (I16# x#) .|. (I16# y#) = I16# (word2Int# (int2Word# x# `or#` int2Word# y#))
279 (I16# x#) `xor` (I16# y#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# y#))
280 complement (I16# x#) = I16# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
281 (I16# x#) `shift` (I# i#)
282 | i# >=# 0# = I16# (narrow16Int# (x# `iShiftL#` i#))
283 | otherwise = I16# (x# `iShiftRA#` negateInt# i#)
284 (I16# x#) `rotate` (I# i#)
285 | i'# ==# 0#
286 = I16# x#
287 | otherwise
288 = I16# (narrow16Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
289 (x'# `uncheckedShiftRL#` (16# -# i'#)))))
290 where
291 !x'# = narrow16Word# (int2Word# x#)
292 !i'# = word2Int# (int2Word# i# `and#` int2Word# 15#)
293 bitSize _ = 16
294 isSigned _ = True
295
296
297 {-# RULES
298 "fromIntegral/Word8->Int16" fromIntegral = \(W8# x#) -> I16# (word2Int# x#)
299 "fromIntegral/Int8->Int16" fromIntegral = \(I8# x#) -> I16# x#
300 "fromIntegral/Int16->Int16" fromIntegral = id :: Int16 -> Int16
301 "fromIntegral/a->Int16" fromIntegral = \x -> case fromIntegral x of I# x# -> I16# (narrow16Int# x#)
302 "fromIntegral/Int16->a" fromIntegral = \(I16# x#) -> fromIntegral (I# x#)
303 #-}
304
305 {-# RULES
306 "properFraction/Float->(Int16,Float)"
307 forall x. properFraction (x :: Float) =
308 case properFraction x of {
309 (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
310 "truncate/Float->Int16"
311 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int16) (truncate x)
312 "floor/Float->Int16"
313 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int16) (floor x)
314 "ceiling/Float->Int16"
315 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int16) (ceiling x)
316 "round/Float->Int16"
317 forall x. round (x :: Float) = (fromIntegral :: Int -> Int16) (round x)
318 #-}
319
320 {-# RULES
321 "properFraction/Double->(Int16,Double)"
322 forall x. properFraction (x :: Double) =
323 case properFraction x of {
324 (n, y) -> ((fromIntegral :: Int -> Int16) n, y) }
325 "truncate/Double->Int16"
326 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int16) (truncate x)
327 "floor/Double->Int16"
328 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int16) (floor x)
329 "ceiling/Double->Int16"
330 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int16) (ceiling x)
331 "round/Double->Int16"
332 forall x. round (x :: Double) = (fromIntegral :: Int -> Int16) (round x)
333 #-}
334
335 ------------------------------------------------------------------------
336 -- type Int32
337 ------------------------------------------------------------------------
338
339 #if WORD_SIZE_IN_BITS < 32
340
341 data Int32 = I32# Int32#
342 -- ^ 32-bit signed integer type
343
344 instance Eq Int32 where
345 (I32# x#) == (I32# y#) = x# `eqInt32#` y#
346 (I32# x#) /= (I32# y#) = x# `neInt32#` y#
347
348 instance Ord Int32 where
349 (I32# x#) < (I32# y#) = x# `ltInt32#` y#
350 (I32# x#) <= (I32# y#) = x# `leInt32#` y#
351 (I32# x#) > (I32# y#) = x# `gtInt32#` y#
352 (I32# x#) >= (I32# y#) = x# `geInt32#` y#
353
354 instance Show Int32 where
355 showsPrec p x = showsPrec p (toInteger x)
356
357 instance Num Int32 where
358 (I32# x#) + (I32# y#) = I32# (x# `plusInt32#` y#)
359 (I32# x#) - (I32# y#) = I32# (x# `minusInt32#` y#)
360 (I32# x#) * (I32# y#) = I32# (x# `timesInt32#` y#)
361 negate (I32# x#) = I32# (negateInt32# x#)
362 abs x | x >= 0 = x
363 | otherwise = negate x
364 signum x | x > 0 = 1
365 signum 0 = 0
366 signum _ = -1
367 fromInteger (S# i#) = I32# (intToInt32# i#)
368 fromInteger (J# s# d#) = I32# (integerToInt32# s# d#)
369
370 instance Enum Int32 where
371 succ x
372 | x /= maxBound = x + 1
373 | otherwise = succError "Int32"
374 pred x
375 | x /= minBound = x - 1
376 | otherwise = predError "Int32"
377 toEnum (I# i#) = I32# (intToInt32# i#)
378 fromEnum x@(I32# x#)
379 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
380 = I# (int32ToInt# x#)
381 | otherwise = fromEnumError "Int32" x
382 enumFrom = integralEnumFrom
383 enumFromThen = integralEnumFromThen
384 enumFromTo = integralEnumFromTo
385 enumFromThenTo = integralEnumFromThenTo
386
387 instance Integral Int32 where
388 quot x@(I32# x#) y@(I32# y#)
389 | y == 0 = divZeroError
390 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
391 | otherwise = I32# (x# `quotInt32#` y#)
392 rem (I32# x#) y@(I32# y#)
393 | y == 0 = divZeroError
394 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
395 -- but minBound `rem` -1 is well-defined (0). We therefore
396 -- special-case it.
397 | y == (-1) = 0
398 | otherwise = I32# (x# `remInt32#` y#)
399 div x@(I32# x#) y@(I32# y#)
400 | y == 0 = divZeroError
401 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
402 | otherwise = I32# (x# `divInt32#` y#)
403 mod (I32# x#) y@(I32# y#)
404 | y == 0 = divZeroError
405 -- The divMod CPU instruction fails for minBound `divMod` -1,
406 -- but minBound `mod` -1 is well-defined (0). We therefore
407 -- special-case it.
408 | y == (-1) = 0
409 | otherwise = I32# (x# `modInt32#` y#)
410 quotRem x@(I32# x#) y@(I32# y#)
411 | y == 0 = divZeroError
412 -- Note [Order of tests]
413 | y == (-1) && x == minBound = (overflowError, 0)
414 | otherwise = (I32# (x# `quotInt32#` y#),
415 I32# (x# `remInt32#` y#))
416 divMod x@(I32# x#) y@(I32# y#)
417 | y == 0 = divZeroError
418 -- Note [Order of tests]
419 | y == (-1) && x == minBound = (overflowError, 0)
420 | otherwise = (I32# (x# `divInt32#` y#),
421 I32# (x# `modInt32#` y#))
422 toInteger x@(I32# x#)
423 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
424 = smallInteger (int32ToInt# x#)
425 | otherwise = case int32ToInteger# x# of (# s, d #) -> J# s d
426
427 divInt32#, modInt32# :: Int32# -> Int32# -> Int32#
428 x# `divInt32#` y#
429 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#)
430 = ((x# `minusInt32#` y#) `minusInt32#` intToInt32# 1#) `quotInt32#` y#
431 | (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
432 = ((x# `minusInt32#` y#) `plusInt32#` intToInt32# 1#) `quotInt32#` y#
433 | otherwise = x# `quotInt32#` y#
434 x# `modInt32#` y#
435 | (x# `gtInt32#` intToInt32# 0#) && (y# `ltInt32#` intToInt32# 0#) ||
436 (x# `ltInt32#` intToInt32# 0#) && (y# `gtInt32#` intToInt32# 0#)
437 = if r# `neInt32#` intToInt32# 0# then r# `plusInt32#` y# else intToInt32# 0#
438 | otherwise = r#
439 where
440 r# = x# `remInt32#` y#
441
442 instance Read Int32 where
443 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
444
445 instance Bits Int32 where
446 {-# INLINE shift #-}
447
448 (I32# x#) .&. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `and32#` int32ToWord32# y#))
449 (I32# x#) .|. (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `or32#` int32ToWord32# y#))
450 (I32# x#) `xor` (I32# y#) = I32# (word32ToInt32# (int32ToWord32# x# `xor32#` int32ToWord32# y#))
451 complement (I32# x#) = I32# (word32ToInt32# (not32# (int32ToWord32# x#)))
452 (I32# x#) `shift` (I# i#)
453 | i# >=# 0# = I32# (x# `iShiftL32#` i#)
454 | otherwise = I32# (x# `iShiftRA32#` negateInt# i#)
455 (I32# x#) `rotate` (I# i#)
456 | i'# ==# 0#
457 = I32# x#
458 | otherwise
459 = I32# (word32ToInt32# ((x'# `shiftL32#` i'#) `or32#`
460 (x'# `shiftRL32#` (32# -# i'#))))
461 where
462 x'# = int32ToWord32# x#
463 i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
464 bitSize _ = 32
465 isSigned _ = True
466
467
468 {-# RULES
469 "fromIntegral/Int->Int32" fromIntegral = \(I# x#) -> I32# (intToInt32# x#)
470 "fromIntegral/Word->Int32" fromIntegral = \(W# x#) -> I32# (word32ToInt32# (wordToWord32# x#))
471 "fromIntegral/Word32->Int32" fromIntegral = \(W32# x#) -> I32# (word32ToInt32# x#)
472 "fromIntegral/Int32->Int" fromIntegral = \(I32# x#) -> I# (int32ToInt# x#)
473 "fromIntegral/Int32->Word" fromIntegral = \(I32# x#) -> W# (int2Word# (int32ToInt# x#))
474 "fromIntegral/Int32->Word32" fromIntegral = \(I32# x#) -> W32# (int32ToWord32# x#)
475 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
476 #-}
477
478 -- No rules for RealFrac methods if Int32 is larger than Int
479 #else
480
481 -- Int32 is represented in the same way as Int.
482 #if WORD_SIZE_IN_BITS > 32
483 -- Operations may assume and must ensure that it holds only values
484 -- from its logical range.
485 #endif
486
487 data Int32 = I32# Int# deriving (Eq, Ord)
488 -- ^ 32-bit signed integer type
489
490 instance Show Int32 where
491 showsPrec p x = showsPrec p (fromIntegral x :: Int)
492
493 instance Num Int32 where
494 (I32# x#) + (I32# y#) = I32# (narrow32Int# (x# +# y#))
495 (I32# x#) - (I32# y#) = I32# (narrow32Int# (x# -# y#))
496 (I32# x#) * (I32# y#) = I32# (narrow32Int# (x# *# y#))
497 negate (I32# x#) = I32# (narrow32Int# (negateInt# x#))
498 abs x | x >= 0 = x
499 | otherwise = negate x
500 signum x | x > 0 = 1
501 signum 0 = 0
502 signum _ = -1
503 fromInteger i = I32# (narrow32Int# (integerToInt i))
504
505 instance Enum Int32 where
506 succ x
507 | x /= maxBound = x + 1
508 | otherwise = succError "Int32"
509 pred x
510 | x /= minBound = x - 1
511 | otherwise = predError "Int32"
512 #if WORD_SIZE_IN_BITS == 32
513 toEnum (I# i#) = I32# i#
514 #else
515 toEnum i@(I# i#)
516 | i >= fromIntegral (minBound::Int32) && i <= fromIntegral (maxBound::Int32)
517 = I32# i#
518 | otherwise = toEnumError "Int32" i (minBound::Int32, maxBound::Int32)
519 #endif
520 fromEnum (I32# x#) = I# x#
521 enumFrom = boundedEnumFrom
522 enumFromThen = boundedEnumFromThen
523
524 instance Integral Int32 where
525 quot x@(I32# x#) y@(I32# y#)
526 | y == 0 = divZeroError
527 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
528 | otherwise = I32# (narrow32Int# (x# `quotInt#` y#))
529 rem (I32# x#) y@(I32# y#)
530 | y == 0 = divZeroError
531 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
532 -- but minBound `rem` -1 is well-defined (0). We therefore
533 -- special-case it.
534 | y == (-1) = 0
535 | otherwise = I32# (narrow32Int# (x# `remInt#` y#))
536 div x@(I32# x#) y@(I32# y#)
537 | y == 0 = divZeroError
538 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
539 | otherwise = I32# (narrow32Int# (x# `divInt#` y#))
540 mod (I32# x#) y@(I32# y#)
541 | y == 0 = divZeroError
542 -- The divMod CPU instruction fails for minBound `divMod` -1,
543 -- but minBound `mod` -1 is well-defined (0). We therefore
544 -- special-case it.
545 | y == (-1) = 0
546 | otherwise = I32# (narrow32Int# (x# `modInt#` y#))
547 quotRem x@(I32# x#) y@(I32# y#)
548 | y == 0 = divZeroError
549 -- Note [Order of tests]
550 | y == (-1) && x == minBound = (overflowError, 0)
551 | otherwise = (I32# (narrow32Int# (x# `quotInt#` y#)),
552 I32# (narrow32Int# (x# `remInt#` y#)))
553 divMod x@(I32# x#) y@(I32# y#)
554 | y == 0 = divZeroError
555 -- Note [Order of tests]
556 | y == (-1) && x == minBound = (overflowError, 0)
557 | otherwise = (I32# (narrow32Int# (x# `divInt#` y#)),
558 I32# (narrow32Int# (x# `modInt#` y#)))
559 toInteger (I32# x#) = smallInteger x#
560
561 instance Read Int32 where
562 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
563
564 instance Bits Int32 where
565 {-# INLINE shift #-}
566
567 (I32# x#) .&. (I32# y#) = I32# (word2Int# (int2Word# x# `and#` int2Word# y#))
568 (I32# x#) .|. (I32# y#) = I32# (word2Int# (int2Word# x# `or#` int2Word# y#))
569 (I32# x#) `xor` (I32# y#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# y#))
570 complement (I32# x#) = I32# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
571 (I32# x#) `shift` (I# i#)
572 | i# >=# 0# = I32# (narrow32Int# (x# `iShiftL#` i#))
573 | otherwise = I32# (x# `iShiftRA#` negateInt# i#)
574 (I32# x#) `rotate` (I# i#)
575 | i'# ==# 0#
576 = I32# x#
577 | otherwise
578 = I32# (narrow32Int# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
579 (x'# `uncheckedShiftRL#` (32# -# i'#)))))
580 where
581 !x'# = narrow32Word# (int2Word# x#)
582 !i'# = word2Int# (int2Word# i# `and#` int2Word# 31#)
583 bitSize _ = 32
584 isSigned _ = True
585
586 {-# RULES
587 "fromIntegral/Word8->Int32" fromIntegral = \(W8# x#) -> I32# (word2Int# x#)
588 "fromIntegral/Word16->Int32" fromIntegral = \(W16# x#) -> I32# (word2Int# x#)
589 "fromIntegral/Int8->Int32" fromIntegral = \(I8# x#) -> I32# x#
590 "fromIntegral/Int16->Int32" fromIntegral = \(I16# x#) -> I32# x#
591 "fromIntegral/Int32->Int32" fromIntegral = id :: Int32 -> Int32
592 "fromIntegral/a->Int32" fromIntegral = \x -> case fromIntegral x of I# x# -> I32# (narrow32Int# x#)
593 "fromIntegral/Int32->a" fromIntegral = \(I32# x#) -> fromIntegral (I# x#)
594 #-}
595
596 {-# RULES
597 "properFraction/Float->(Int32,Float)"
598 forall x. properFraction (x :: Float) =
599 case properFraction x of {
600 (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
601 "truncate/Float->Int32"
602 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int32) (truncate x)
603 "floor/Float->Int32"
604 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int32) (floor x)
605 "ceiling/Float->Int32"
606 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int32) (ceiling x)
607 "round/Float->Int32"
608 forall x. round (x :: Float) = (fromIntegral :: Int -> Int32) (round x)
609 #-}
610
611 {-# RULES
612 "properFraction/Double->(Int32,Double)"
613 forall x. properFraction (x :: Double) =
614 case properFraction x of {
615 (n, y) -> ((fromIntegral :: Int -> Int32) n, y) }
616 "truncate/Double->Int32"
617 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int32) (truncate x)
618 "floor/Double->Int32"
619 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int32) (floor x)
620 "ceiling/Double->Int32"
621 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int32) (ceiling x)
622 "round/Double->Int32"
623 forall x. round (x :: Double) = (fromIntegral :: Int -> Int32) (round x)
624 #-}
625
626 #endif
627
628 instance Real Int32 where
629 toRational x = toInteger x % 1
630
631 instance Bounded Int32 where
632 minBound = -0x80000000
633 maxBound = 0x7FFFFFFF
634
635 instance Ix Int32 where
636 range (m,n) = [m..n]
637 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
638 inRange (m,n) i = m <= i && i <= n
639
640 ------------------------------------------------------------------------
641 -- type Int64
642 ------------------------------------------------------------------------
643
644 #if WORD_SIZE_IN_BITS < 64
645
646 data Int64 = I64# Int64#
647 -- ^ 64-bit signed integer type
648
649 instance Eq Int64 where
650 (I64# x#) == (I64# y#) = x# `eqInt64#` y#
651 (I64# x#) /= (I64# y#) = x# `neInt64#` y#
652
653 instance Ord Int64 where
654 (I64# x#) < (I64# y#) = x# `ltInt64#` y#
655 (I64# x#) <= (I64# y#) = x# `leInt64#` y#
656 (I64# x#) > (I64# y#) = x# `gtInt64#` y#
657 (I64# x#) >= (I64# y#) = x# `geInt64#` y#
658
659 instance Show Int64 where
660 showsPrec p x = showsPrec p (toInteger x)
661
662 instance Num Int64 where
663 (I64# x#) + (I64# y#) = I64# (x# `plusInt64#` y#)
664 (I64# x#) - (I64# y#) = I64# (x# `minusInt64#` y#)
665 (I64# x#) * (I64# y#) = I64# (x# `timesInt64#` y#)
666 negate (I64# x#) = I64# (negateInt64# x#)
667 abs x | x >= 0 = x
668 | otherwise = negate x
669 signum x | x > 0 = 1
670 signum 0 = 0
671 signum _ = -1
672 fromInteger i = I64# (integerToInt64 i)
673
674 instance Enum Int64 where
675 succ x
676 | x /= maxBound = x + 1
677 | otherwise = succError "Int64"
678 pred x
679 | x /= minBound = x - 1
680 | otherwise = predError "Int64"
681 toEnum (I# i#) = I64# (intToInt64# i#)
682 fromEnum x@(I64# x#)
683 | x >= fromIntegral (minBound::Int) && x <= fromIntegral (maxBound::Int)
684 = I# (int64ToInt# x#)
685 | otherwise = fromEnumError "Int64" x
686 enumFrom = integralEnumFrom
687 enumFromThen = integralEnumFromThen
688 enumFromTo = integralEnumFromTo
689 enumFromThenTo = integralEnumFromThenTo
690
691 instance Integral Int64 where
692 quot x@(I64# x#) y@(I64# y#)
693 | y == 0 = divZeroError
694 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
695 | otherwise = I64# (x# `quotInt64#` y#)
696 rem (I64# x#) y@(I64# y#)
697 | y == 0 = divZeroError
698 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
699 -- but minBound `rem` -1 is well-defined (0). We therefore
700 -- special-case it.
701 | y == (-1) = 0
702 | otherwise = I64# (x# `remInt64#` y#)
703 div x@(I64# x#) y@(I64# y#)
704 | y == 0 = divZeroError
705 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
706 | otherwise = I64# (x# `divInt64#` y#)
707 mod (I64# x#) y@(I64# y#)
708 | y == 0 = divZeroError
709 -- The divMod CPU instruction fails for minBound `divMod` -1,
710 -- but minBound `mod` -1 is well-defined (0). We therefore
711 -- special-case it.
712 | y == (-1) = 0
713 | otherwise = I64# (x# `modInt64#` y#)
714 quotRem x@(I64# x#) y@(I64# y#)
715 | y == 0 = divZeroError
716 -- Note [Order of tests]
717 | y == (-1) && x == minBound = (overflowError, 0)
718 | otherwise = (I64# (x# `quotInt64#` y#),
719 I64# (x# `remInt64#` y#))
720 divMod x@(I64# x#) y@(I64# y#)
721 | y == 0 = divZeroError
722 -- Note [Order of tests]
723 | y == (-1) && x == minBound = (overflowError, 0)
724 | otherwise = (I64# (x# `divInt64#` y#),
725 I64# (x# `modInt64#` y#))
726 toInteger (I64# x) = int64ToInteger x
727
728
729 divInt64#, modInt64# :: Int64# -> Int64# -> Int64#
730 x# `divInt64#` y#
731 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#)
732 = ((x# `minusInt64#` y#) `minusInt64#` intToInt64# 1#) `quotInt64#` y#
733 | (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
734 = ((x# `minusInt64#` y#) `plusInt64#` intToInt64# 1#) `quotInt64#` y#
735 | otherwise = x# `quotInt64#` y#
736 x# `modInt64#` y#
737 | (x# `gtInt64#` intToInt64# 0#) && (y# `ltInt64#` intToInt64# 0#) ||
738 (x# `ltInt64#` intToInt64# 0#) && (y# `gtInt64#` intToInt64# 0#)
739 = if r# `neInt64#` intToInt64# 0# then r# `plusInt64#` y# else intToInt64# 0#
740 | otherwise = r#
741 where
742 !r# = x# `remInt64#` y#
743
744 instance Read Int64 where
745 readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
746
747 instance Bits Int64 where
748 {-# INLINE shift #-}
749
750 (I64# x#) .&. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `and64#` int64ToWord64# y#))
751 (I64# x#) .|. (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `or64#` int64ToWord64# y#))
752 (I64# x#) `xor` (I64# y#) = I64# (word64ToInt64# (int64ToWord64# x# `xor64#` int64ToWord64# y#))
753 complement (I64# x#) = I64# (word64ToInt64# (not64# (int64ToWord64# x#)))
754 (I64# x#) `shift` (I# i#)
755 | i# >=# 0# = I64# (x# `iShiftL64#` i#)
756 | otherwise = I64# (x# `iShiftRA64#` negateInt# i#)
757 (I64# x#) `rotate` (I# i#)
758 | i'# ==# 0#
759 = I64# x#
760 | otherwise
761 = I64# (word64ToInt64# ((x'# `uncheckedShiftL64#` i'#) `or64#`
762 (x'# `uncheckedShiftRL64#` (64# -# i'#))))
763 where
764 !x'# = int64ToWord64# x#
765 !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
766 bitSize _ = 64
767 isSigned _ = True
768
769 -- give the 64-bit shift operations the same treatment as the 32-bit
770 -- ones (see GHC.Base), namely we wrap them in tests to catch the
771 -- cases when we're shifting more than 64 bits to avoid unspecified
772 -- behaviour in the C shift operations.
773
774 iShiftL64#, iShiftRA64# :: Int64# -> Int# -> Int64#
775
776 a `iShiftL64#` b | b >=# 64# = intToInt64# 0#
777 | otherwise = a `uncheckedIShiftL64#` b
778
779 a `iShiftRA64#` b | b >=# 64# = if a `ltInt64#` (intToInt64# 0#)
780 then intToInt64# (-1#)
781 else intToInt64# 0#
782 | otherwise = a `uncheckedIShiftRA64#` b
783
784 {-# RULES
785 "fromIntegral/Int->Int64" fromIntegral = \(I# x#) -> I64# (intToInt64# x#)
786 "fromIntegral/Word->Int64" fromIntegral = \(W# x#) -> I64# (word64ToInt64# (wordToWord64# x#))
787 "fromIntegral/Word64->Int64" fromIntegral = \(W64# x#) -> I64# (word64ToInt64# x#)
788 "fromIntegral/Int64->Int" fromIntegral = \(I64# x#) -> I# (int64ToInt# x#)
789 "fromIntegral/Int64->Word" fromIntegral = \(I64# x#) -> W# (int2Word# (int64ToInt# x#))
790 "fromIntegral/Int64->Word64" fromIntegral = \(I64# x#) -> W64# (int64ToWord64# x#)
791 "fromIntegral/Int64->Int64" fromIntegral = id :: Int64 -> Int64
792 #-}
793
794 -- No RULES for RealFrac methods if Int is smaller than Int64, we can't
795 -- go through Int and whether going through Integer is faster is uncertain.
796 #else
797
798 -- Int64 is represented in the same way as Int.
799 -- Operations may assume and must ensure that it holds only values
800 -- from its logical range.
801
802 data Int64 = I64# Int# deriving (Eq, Ord)
803 -- ^ 64-bit signed integer type
804
805 instance Show Int64 where
806 showsPrec p x = showsPrec p (fromIntegral x :: Int)
807
808 instance Num Int64 where
809 (I64# x#) + (I64# y#) = I64# (x# +# y#)
810 (I64# x#) - (I64# y#) = I64# (x# -# y#)
811 (I64# x#) * (I64# y#) = I64# (x# *# y#)
812 negate (I64# x#) = I64# (negateInt# x#)
813 abs x | x >= 0 = x
814 | otherwise = negate x
815 signum x | x > 0 = 1
816 signum 0 = 0
817 signum _ = -1
818 fromInteger i = I64# (integerToInt i)
819
820 instance Enum Int64 where
821 succ x
822 | x /= maxBound = x + 1
823 | otherwise = succError "Int64"
824 pred x
825 | x /= minBound = x - 1
826 | otherwise = predError "Int64"
827 toEnum (I# i#) = I64# i#
828 fromEnum (I64# x#) = I# x#
829 enumFrom = boundedEnumFrom
830 enumFromThen = boundedEnumFromThen
831
832 instance Integral Int64 where
833 quot x@(I64# x#) y@(I64# y#)
834 | y == 0 = divZeroError
835 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
836 | otherwise = I64# (x# `quotInt#` y#)
837 rem (I64# x#) y@(I64# y#)
838 | y == 0 = divZeroError
839 -- The quotRem CPU instruction fails for minBound `quotRem` -1,
840 -- but minBound `rem` -1 is well-defined (0). We therefore
841 -- special-case it.
842 | y == (-1) = 0
843 | otherwise = I64# (x# `remInt#` y#)
844 div x@(I64# x#) y@(I64# y#)
845 | y == 0 = divZeroError
846 | y == (-1) && x == minBound = overflowError -- Note [Order of tests]
847 | otherwise = I64# (x# `divInt#` y#)
848 mod (I64# x#) y@(I64# y#)
849 | y == 0 = divZeroError
850 -- The divMod CPU instruction fails for minBound `divMod` -1,
851 -- but minBound `mod` -1 is well-defined (0). We therefore
852 -- special-case it.
853 | y == (-1) = 0
854 | otherwise = I64# (x# `modInt#` y#)
855 quotRem x@(I64# x#) y@(I64# y#)
856 | y == 0 = divZeroError
857 -- Note [Order of tests]
858 | y == (-1) && x == minBound = (overflowError, 0)
859 | otherwise = (I64# (x# `quotInt#` y#), I64# (x# `remInt#` y#))
860 divMod x@(I64# x#) y@(I64# y#)
861 | y == 0 = divZeroError
862 -- Note [Order of tests]
863 | y == (-1) && x == minBound = (overflowError, 0)
864 | otherwise = (I64# (x# `divInt#` y#), I64# (x# `modInt#` y#))
865 toInteger (I64# x#) = smallInteger x#
866
867 instance Read Int64 where
868 readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
869
870 instance Bits Int64 where
871 {-# INLINE shift #-}
872
873 (I64# x#) .&. (I64# y#) = I64# (word2Int# (int2Word# x# `and#` int2Word# y#))
874 (I64# x#) .|. (I64# y#) = I64# (word2Int# (int2Word# x# `or#` int2Word# y#))
875 (I64# x#) `xor` (I64# y#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# y#))
876 complement (I64# x#) = I64# (word2Int# (int2Word# x# `xor#` int2Word# (-1#)))
877 (I64# x#) `shift` (I# i#)
878 | i# >=# 0# = I64# (x# `iShiftL#` i#)
879 | otherwise = I64# (x# `iShiftRA#` negateInt# i#)
880 (I64# x#) `rotate` (I# i#)
881 | i'# ==# 0#
882 = I64# x#
883 | otherwise
884 = I64# (word2Int# ((x'# `uncheckedShiftL#` i'#) `or#`
885 (x'# `uncheckedShiftRL#` (64# -# i'#))))
886 where
887 !x'# = int2Word# x#
888 !i'# = word2Int# (int2Word# i# `and#` int2Word# 63#)
889 bitSize _ = 64
890 isSigned _ = True
891
892 {-# RULES
893 "fromIntegral/a->Int64" fromIntegral = \x -> case fromIntegral x of I# x# -> I64# x#
894 "fromIntegral/Int64->a" fromIntegral = \(I64# x#) -> fromIntegral (I# x#)
895 #-}
896
897 {-# RULES
898 "properFraction/Float->(Int64,Float)"
899 forall x. properFraction (x :: Float) =
900 case properFraction x of {
901 (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
902 "truncate/Float->Int64"
903 forall x. truncate (x :: Float) = (fromIntegral :: Int -> Int64) (truncate x)
904 "floor/Float->Int64"
905 forall x. floor (x :: Float) = (fromIntegral :: Int -> Int64) (floor x)
906 "ceiling/Float->Int64"
907 forall x. ceiling (x :: Float) = (fromIntegral :: Int -> Int64) (ceiling x)
908 "round/Float->Int64"
909 forall x. round (x :: Float) = (fromIntegral :: Int -> Int64) (round x)
910 #-}
911
912 {-# RULES
913 "properFraction/Double->(Int64,Double)"
914 forall x. properFraction (x :: Double) =
915 case properFraction x of {
916 (n, y) -> ((fromIntegral :: Int -> Int64) n, y) }
917 "truncate/Double->Int64"
918 forall x. truncate (x :: Double) = (fromIntegral :: Int -> Int64) (truncate x)
919 "floor/Double->Int64"
920 forall x. floor (x :: Double) = (fromIntegral :: Int -> Int64) (floor x)
921 "ceiling/Double->Int64"
922 forall x. ceiling (x :: Double) = (fromIntegral :: Int -> Int64) (ceiling x)
923 "round/Double->Int64"
924 forall x. round (x :: Double) = (fromIntegral :: Int -> Int64) (round x)
925 #-}
926
927 uncheckedIShiftL64# :: Int# -> Int# -> Int#
928 uncheckedIShiftL64# = uncheckedIShiftL#
929
930 uncheckedIShiftRA64# :: Int# -> Int# -> Int#
931 uncheckedIShiftRA64# = uncheckedIShiftRA#
932 #endif
933
934 instance Real Int64 where
935 toRational x = toInteger x % 1
936
937 instance Bounded Int64 where
938 minBound = -0x8000000000000000
939 maxBound = 0x7FFFFFFFFFFFFFFF
940
941 instance Ix Int64 where
942 range (m,n) = [m..n]
943 unsafeIndex (m,_) i = fromIntegral i - fromIntegral m
944 inRange (m,n) i = m <= i && i <= n
945
946
947 {-
948 Note [Order of tests]
949
950 Suppose we had a definition like:
951
952 quot x y
953 | y == 0 = divZeroError
954 | x == minBound && y == (-1) = overflowError
955 | otherwise = x `primQuot` y
956
957 Note in particular that the
958 x == minBound
959 test comes before the
960 y == (-1)
961 test.
962
963 this expands to something like:
964
965 case y of
966 0 -> divZeroError
967 _ -> case x of
968 -9223372036854775808 ->
969 case y of
970 -1 -> overflowError
971 _ -> x `primQuot` y
972 _ -> x `primQuot` y
973
974 Now if we have the call (x `quot` 2), and quot gets inlined, then we get:
975
976 case 2 of
977 0 -> divZeroError
978 _ -> case x of
979 -9223372036854775808 ->
980 case 2 of
981 -1 -> overflowError
982 _ -> x `primQuot` 2
983 _ -> x `primQuot` 2
984
985 which simplifies to:
986
987 case x of
988 -9223372036854775808 -> x `primQuot` 2
989 _ -> x `primQuot` 2
990
991 Now we have a case with two identical branches, which would be
992 eliminated (assuming it doesn't affect strictness, which it doesn't in
993 this case), leaving the desired:
994
995 x `primQuot` 2
996
997 except in the minBound branch we know what x is, and GHC cleverly does
998 the division at compile time, giving:
999
1000 case x of
1001 -9223372036854775808 -> -4611686018427387904
1002 _ -> x `primQuot` 2
1003
1004 So instead we use a definition like:
1005
1006 quot x y
1007 | y == 0 = divZeroError
1008 | y == (-1) && x == minBound = overflowError
1009 | otherwise = x `primQuot` y
1010
1011 which gives us:
1012
1013 case y of
1014 0 -> divZeroError
1015 -1 ->
1016 case x of
1017 -9223372036854775808 -> overflowError
1018 _ -> x `primQuot` y
1019 _ -> x `primQuot` y
1020
1021 for which our call (x `quot` 2) expands to:
1022
1023 case 2 of
1024 0 -> divZeroError
1025 -1 ->
1026 case x of
1027 -9223372036854775808 -> overflowError
1028 _ -> x `primQuot` 2
1029 _ -> x `primQuot` 2
1030
1031 which simplifies to:
1032
1033 x `primQuot` 2
1034
1035 as required.
1036
1037
1038
1039 But we now have the same problem with a constant numerator: the call
1040 (2 `quot` y) expands to
1041
1042 case y of
1043 0 -> divZeroError
1044 -1 ->
1045 case 2 of
1046 -9223372036854775808 -> overflowError
1047 _ -> 2 `primQuot` y
1048 _ -> 2 `primQuot` y
1049
1050 which simplifies to:
1051
1052 case y of
1053 0 -> divZeroError
1054 -1 -> 2 `primQuot` y
1055 _ -> 2 `primQuot` y
1056
1057 which simplifies to:
1058
1059 case y of
1060 0 -> divZeroError
1061 -1 -> -2
1062 _ -> 2 `primQuot` y
1063
1064
1065 However, constant denominators are more common than constant numerators,
1066 so the
1067 y == (-1) && x == minBound
1068 order gives us better code in the common case.
1069 -}