diff options
| author | John Ericson <git@JohnEricson.me> | 2019-10-19 18:59:48 -0400 |
|---|---|---|
| committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-05-06 02:30:54 -0400 |
| commit | c4f4193a13f751380e4cedbc2688a339f69325c9 (patch) | |
| tree | e9cf1fb7f05d99dbbe1dcec7c1e526932220d9c5 /libraries/base | |
| parent | a5e9e5b601fecef421ec4bfa28e135404986ded0 (diff) | |
| download | haskell-c4f4193a13f751380e4cedbc2688a339f69325c9.tar.gz | |
Use fix-sized arithmetic primops for fixed size boxed types
We think the compiler is ready, so we can do this for all over the 8-,
16-, and 32-bit boxed types.
We are holding off on doing all the primops at once so things are easier
to investigate.
Metric Decrease:
T12545
Diffstat (limited to 'libraries/base')
| -rw-r--r-- | libraries/base/GHC/Int.hs | 84 | ||||
| -rw-r--r-- | libraries/base/GHC/Word.hs | 61 |
2 files changed, 65 insertions, 80 deletions
diff --git a/libraries/base/GHC/Int.hs b/libraries/base/GHC/Int.hs index a9feb3d890..fa5aa3f8e5 100644 --- a/libraries/base/GHC/Int.hs +++ b/libraries/base/GHC/Int.hs @@ -100,10 +100,10 @@ instance Show Int8 where -- | @since 2.01 instance Num Int8 where - (I8# x#) + (I8# y#) = I8# (intToInt8# ((int8ToInt# x#) +# (int8ToInt# y#))) - (I8# x#) - (I8# y#) = I8# (intToInt8# ((int8ToInt# x#) -# (int8ToInt# y#))) - (I8# x#) * (I8# y#) = I8# (intToInt8# ((int8ToInt# x#) *# (int8ToInt# y#))) - negate (I8# x#) = I8# (intToInt8# (negateInt# (int8ToInt# x#))) + (I8# x#) + (I8# y#) = I8# (x# `plusInt8#` y#) + (I8# x#) - (I8# y#) = I8# (x# `subInt8#` y#) + (I8# x#) * (I8# y#) = I8# (x# `timesInt8#` y#) + negate (I8# x#) = I8# (negateInt8# x#) abs x | x >= 0 = x | otherwise = negate x signum x | x > 0 = 1 @@ -136,7 +136,7 @@ instance Integral Int8 where quot x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError | y == (-1) && x == minBound = overflowError -- Note [Order of tests] - | otherwise = I8# (intToInt8# ((int8ToInt# x#) `quotInt#` (int8ToInt# y#))) + | otherwise = I8# (x# `quotInt8#` y#) rem (I8# x#) y@(I8# y#) | y == 0 = divZeroError -- The quotRem CPU instruction might fail for 'minBound @@ -144,11 +144,11 @@ instance Integral Int8 where -- width of signed integer. But, 'minBound `rem` -1' is -- well-defined (0). We therefore special-case it. | y == (-1) = 0 - | otherwise = I8# (intToInt8# ((int8ToInt# x#) `remInt#` (int8ToInt# y#))) + | otherwise = I8# (x# `remInt8#` y#) div x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError | y == (-1) && x == minBound = overflowError -- Note [Order of tests] - | otherwise = I8# (intToInt8# ((int8ToInt# x#) `divInt#` (int8ToInt# y#))) + | otherwise = I8# (x# `divInt8#` y#) mod (I8# x#) y@(I8# y#) | y == 0 = divZeroError -- The divMod CPU instruction might fail for 'minBound @@ -156,23 +156,19 @@ instance Integral Int8 where -- width of signed integer. But, 'minBound `mod` -1' is -- well-defined (0). We therefore special-case it. | y == (-1) = 0 - | otherwise = I8# (intToInt8# ((int8ToInt# x#) `modInt#` (int8ToInt# y#))) + | otherwise = I8# (x# `modInt8#` y#) quotRem x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError -- Note [Order of tests] | y == (-1) && x == minBound = (overflowError, 0) - | otherwise = case (int8ToInt# x#) `quotRemInt#` (int8ToInt# y#) of - (# q, r #) -> - (I8# (intToInt8# q), - I8# (intToInt8# r)) + | otherwise = case x# `quotRemInt8#` y# of + (# q, r #) -> (I8# q, I8# r) divMod x@(I8# x#) y@(I8# y#) | y == 0 = divZeroError -- Note [Order of tests] | y == (-1) && x == minBound = (overflowError, 0) - | otherwise = case (int8ToInt# x#) `divModInt#` (int8ToInt# y#) of - (# d, m #) -> - (I8# (intToInt8# d), - I8# (intToInt8# m)) + | otherwise = case x# `divModInt8#` y# of + (# d, m #) -> (I8# d, I8# m) toInteger (I8# x#) = IS (int8ToInt# x#) -- | @since 2.01 @@ -317,10 +313,10 @@ instance Show Int16 where -- | @since 2.01 instance Num Int16 where - (I16# x#) + (I16# y#) = I16# (intToInt16# ((int16ToInt# x#) +# (int16ToInt# y#))) - (I16# x#) - (I16# y#) = I16# (intToInt16# ((int16ToInt# x#) -# (int16ToInt# y#))) - (I16# x#) * (I16# y#) = I16# (intToInt16# ((int16ToInt# x#) *# (int16ToInt# y#))) - negate (I16# x#) = I16# (intToInt16# (negateInt# (int16ToInt# x#))) + (I16# x#) + (I16# y#) = I16# (x# `plusInt16#` y#) + (I16# x#) - (I16# y#) = I16# (x# `subInt16#` y#) + (I16# x#) * (I16# y#) = I16# (x# `timesInt16#` y#) + negate (I16# x#) = I16# (negateInt16# x#) abs x | x >= 0 = x | otherwise = negate x signum x | x > 0 = 1 @@ -353,7 +349,7 @@ instance Integral Int16 where quot x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError | y == (-1) && x == minBound = overflowError -- Note [Order of tests] - | otherwise = I16# (intToInt16# ((int16ToInt# x#) `quotInt#` (int16ToInt# y#))) + | otherwise = I16# (x# `quotInt16#` y#) rem (I16# x#) y@(I16# y#) | y == 0 = divZeroError -- The quotRem CPU instruction might fail for 'minBound @@ -361,11 +357,11 @@ instance Integral Int16 where -- width of signed integer. But, 'minBound `rem` -1' is -- well-defined (0). We therefore special-case it. | y == (-1) = 0 - | otherwise = I16# (intToInt16# ((int16ToInt# x#) `remInt#` (int16ToInt# y#))) + | otherwise = I16# (x# `remInt16#` y#) div x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError | y == (-1) && x == minBound = overflowError -- Note [Order of tests] - | otherwise = I16# (intToInt16# ((int16ToInt# x#) `divInt#` (int16ToInt# y#))) + | otherwise = I16# (x# `divInt16#` y#) mod (I16# x#) y@(I16# y#) | y == 0 = divZeroError -- The divMod CPU instruction might fail for 'minBound @@ -373,23 +369,19 @@ instance Integral Int16 where -- width of signed integer. But, 'minBound `mod` -1' is -- well-defined (0). We therefore special-case it. | y == (-1) = 0 - | otherwise = I16# (intToInt16# ((int16ToInt# x#) `modInt#` (int16ToInt# y#))) + | otherwise = I16# (x# `modInt16#` y#) quotRem x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError -- Note [Order of tests] | y == (-1) && x == minBound = (overflowError, 0) - | otherwise = case (int16ToInt# x#) `quotRemInt#` (int16ToInt# y#) of - (# q, r #) -> - (I16# (intToInt16# q), - I16# (intToInt16# r)) + | otherwise = case x# `quotRemInt16#` y# of + (# q, r #) -> (I16# q, I16# r) divMod x@(I16# x#) y@(I16# y#) | y == 0 = divZeroError -- Note [Order of tests] | y == (-1) && x == minBound = (overflowError, 0) - | otherwise = case (int16ToInt# x#) `divModInt#` (int16ToInt# y#) of - (# d, m #) -> - (I16# (intToInt16# d), - I16# (intToInt16# m)) + | otherwise = case x# `divModInt16#` y# of + (# d, m #) -> (I16# d, I16# m) toInteger (I16# x#) = IS (int16ToInt# x#) -- | @since 2.01 @@ -539,10 +531,10 @@ instance Show Int32 where -- | @since 2.01 instance Num Int32 where - (I32# x#) + (I32# y#) = I32# (intToInt32# ((int32ToInt# x#) +# (int32ToInt# y#))) - (I32# x#) - (I32# y#) = I32# (intToInt32# ((int32ToInt# x#) -# (int32ToInt# y#))) - (I32# x#) * (I32# y#) = I32# (intToInt32# ((int32ToInt# x#) *# (int32ToInt# y#))) - negate (I32# x#) = I32# (intToInt32# (negateInt# (int32ToInt# x#))) + (I32# x#) + (I32# y#) = I32# (x# `plusInt32#` y#) + (I32# x#) - (I32# y#) = I32# (x# `subInt32#` y#) + (I32# x#) * (I32# y#) = I32# (x# `timesInt32#` y#) + negate (I32# x#) = I32# (negateInt32# x#) abs x | x >= 0 = x | otherwise = negate x signum x | x > 0 = 1 @@ -575,7 +567,7 @@ instance Integral Int32 where quot x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError | y == (-1) && x == minBound = overflowError -- Note [Order of tests] - | otherwise = I32# (intToInt32# ((int32ToInt# x#) `quotInt#` (int32ToInt# y#))) + | otherwise = I32# (x# `quotInt32#` y#) rem (I32# x#) y@(I32# y#) | y == 0 = divZeroError -- The quotRem CPU instruction might fail for 'minBound @@ -583,11 +575,11 @@ instance Integral Int32 where -- width of signed integer. But, 'minBound `rem` -1' is -- well-defined (0). We therefore special-case it. | y == (-1) = 0 - | otherwise = I32# (intToInt32# ((int32ToInt# x#) `remInt#` (int32ToInt# y#))) + | otherwise = I32# (x# `remInt32#` y#) div x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError | y == (-1) && x == minBound = overflowError -- Note [Order of tests] - | otherwise = I32# (intToInt32# ((int32ToInt# x#) `divInt#` (int32ToInt# y#))) + | otherwise = I32# (x# `divInt32#` y#) mod (I32# x#) y@(I32# y#) | y == 0 = divZeroError -- The divMod CPU instruction might fail for 'minBound @@ -595,23 +587,19 @@ instance Integral Int32 where -- width of signed integer. But, 'minBound `mod` -1' is -- well-defined (0). We therefore special-case it. | y == (-1) = 0 - | otherwise = I32# (intToInt32# ((int32ToInt# x#) `modInt#` (int32ToInt# y#))) + | otherwise = I32# (x# `modInt32#` y#) quotRem x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError -- Note [Order of tests] | y == (-1) && x == minBound = (overflowError, 0) - | otherwise = case (int32ToInt# x#) `quotRemInt#` (int32ToInt# y#) of - (# q, r #) -> - (I32# (intToInt32# q), - I32# (intToInt32# r)) + | otherwise = case x# `quotRemInt32#` y# of + (# q, r #) -> (I32# q, I32# r) divMod x@(I32# x#) y@(I32# y#) | y == 0 = divZeroError -- Note [Order of tests] | y == (-1) && x == minBound = (overflowError, 0) - | otherwise = case (int32ToInt# x#) `divModInt#` (int32ToInt# y#) of - (# d, m #) -> - (I32# (intToInt32# d), - I32# (intToInt32# m)) + | otherwise = case x# `divModInt32#` y# of + (# d, m #) -> (I32# d, I32# m) toInteger (I32# x#) = IS (int32ToInt# x#) -- | @since 2.01 diff --git a/libraries/base/GHC/Word.hs b/libraries/base/GHC/Word.hs index c704f3afc7..b75d24359b 100644 --- a/libraries/base/GHC/Word.hs +++ b/libraries/base/GHC/Word.hs @@ -108,10 +108,10 @@ instance Show Word8 where -- | @since 2.01 instance Num Word8 where - (W8# x#) + (W8# y#) = W8# (wordToWord8# ((word8ToWord# x#) `plusWord#` (word8ToWord# y#))) - (W8# x#) - (W8# y#) = W8# (wordToWord8# ((word8ToWord# x#) `minusWord#` (word8ToWord# y#))) - (W8# x#) * (W8# y#) = W8# (wordToWord8# ((word8ToWord# x#) `timesWord#` (word8ToWord# y#))) - negate (W8# x#) = W8# (wordToWord8# (int2Word# (negateInt# (word2Int# ((word8ToWord# x#)))))) + (W8# x#) + (W8# y#) = W8# (x# `plusWord8#` y#) + (W8# x#) - (W8# y#) = W8# (x# `subWord8#` y#) + (W8# x#) * (W8# y#) = W8# (x# `timesWord8#` y#) + negate (W8# x#) = W8# (int8ToWord8# (negateInt8# (word8ToInt8# x#))) abs x = x signum 0 = 0 signum _ = 1 @@ -140,25 +140,24 @@ instance Enum Word8 where -- | @since 2.01 instance Integral Word8 where quot (W8# x#) y@(W8# y#) - | y /= 0 = W8# (wordToWord8# ((word8ToWord# x#) `quotWord#` (word8ToWord# y#))) + | y /= 0 = W8# (x# `quotWord8#` y#) | otherwise = divZeroError rem (W8# x#) y@(W8# y#) - | y /= 0 = W8# (wordToWord8# ((word8ToWord# x#) `remWord#` (word8ToWord# y#))) + | y /= 0 = W8# (x# `remWord8#` y#) | otherwise = divZeroError div (W8# x#) y@(W8# y#) - | y /= 0 = W8# (wordToWord8# ((word8ToWord# x#) `quotWord#` (word8ToWord# y#))) + | y /= 0 = W8# (x# `quotWord8#` y#) | otherwise = divZeroError mod (W8# x#) y@(W8# y#) - | y /= 0 = W8# (wordToWord8# ((word8ToWord# x#) `remWord#` (word8ToWord# y#))) + | y /= 0 = W8# (x# `remWord8#` y#) | otherwise = divZeroError quotRem (W8# x#) y@(W8# y#) - | y /= 0 = case (word8ToWord# x#) `quotRemWord#` (word8ToWord# y#) of - (# q, r #) -> - (W8# (wordToWord8# q), W8# (wordToWord8# r)) + | y /= 0 = case x# `quotRemWord8#` y# of + (# q, r #) -> (W8# q, W8# r) | otherwise = divZeroError divMod (W8# x#) y@(W8# y#) - | y /= 0 = (W8# (wordToWord8# ((word8ToWord# x#) `quotWord#` (word8ToWord# y#))) - ,W8# (wordToWord8# ((word8ToWord# x#) `remWord#` (word8ToWord# y#)))) + | y /= 0 = (W8# (x# `quotWord8#` y#) + ,W8# (x# `remWord8#` y#)) | otherwise = divZeroError toInteger (W8# x#) = IS (word2Int# (word8ToWord# x#)) @@ -299,10 +298,10 @@ instance Show Word16 where -- | @since 2.01 instance Num Word16 where - (W16# x#) + (W16# y#) = W16# (wordToWord16# ((word16ToWord# x#) `plusWord#` (word16ToWord# y#))) - (W16# x#) - (W16# y#) = W16# (wordToWord16# ((word16ToWord# x#) `minusWord#` (word16ToWord# y#))) - (W16# x#) * (W16# y#) = W16# (wordToWord16# ((word16ToWord# x#) `timesWord#` (word16ToWord# y#))) - negate (W16# x#) = W16# (wordToWord16# (int2Word# (negateInt# (word2Int# (word16ToWord# x#))))) + (W16# x#) + (W16# y#) = W16# (x# `plusWord16#` y#) + (W16# x#) - (W16# y#) = W16# (x# `subWord16#` y#) + (W16# x#) * (W16# y#) = W16# (x# `timesWord16#` y#) + negate (W16# x#) = W16# (int16ToWord16# (negateInt16# (word16ToInt16# x#))) abs x = x signum 0 = 0 signum _ = 1 @@ -528,10 +527,10 @@ gtWord32, geWord32, ltWord32, leWord32 :: Word32 -> Word32 -> Bool -- | @since 2.01 instance Num Word32 where - (W32# x#) + (W32# y#) = W32# (wordToWord32# ((word32ToWord# x#) `plusWord#` (word32ToWord# y#))) - (W32# x#) - (W32# y#) = W32# (wordToWord32# ((word32ToWord# x#) `minusWord#` (word32ToWord# y#))) - (W32# x#) * (W32# y#) = W32# (wordToWord32# ((word32ToWord# x#) `timesWord#` (word32ToWord# y#))) - negate (W32# x#) = W32# (wordToWord32# (int2Word# (negateInt# (word2Int# (word32ToWord# x#))))) + (W32# x#) + (W32# y#) = W32# (x# `plusWord32#` y#) + (W32# x#) - (W32# y#) = W32# (x# `subWord32#` y#) + (W32# x#) * (W32# y#) = W32# (x# `timesWord32#` y#) + negate (W32# x#) = W32# (int32ToWord32# (negateInt32# (word32ToInt32# x#))) abs x = x signum 0 = 0 signum _ = 1 @@ -570,25 +569,24 @@ instance Enum Word32 where -- | @since 2.01 instance Integral Word32 where quot (W32# x#) y@(W32# y#) - | y /= 0 = W32# (wordToWord32# ((word32ToWord# x#) `quotWord#` (word32ToWord# y#))) + | y /= 0 = W32# (x# `quotWord32#` y#) | otherwise = divZeroError rem (W32# x#) y@(W32# y#) - | y /= 0 = W32# (wordToWord32# ((word32ToWord# x#) `remWord#` (word32ToWord# y#))) + | y /= 0 = W32# (x# `remWord32#` y#) | otherwise = divZeroError div (W32# x#) y@(W32# y#) - | y /= 0 = W32# (wordToWord32# ((word32ToWord# x#) `quotWord#` (word32ToWord# y#))) + | y /= 0 = W32# (x# `quotWord32#` y#) | otherwise = divZeroError mod (W32# x#) y@(W32# y#) - | y /= 0 = W32# (wordToWord32# ((word32ToWord# x#) `remWord#` (word32ToWord# y#))) + | y /= 0 = W32# (x# `remWord32#` y#) | otherwise = divZeroError quotRem (W32# x#) y@(W32# y#) - | y /= 0 = case (word32ToWord# x#) `quotRemWord#` (word32ToWord# y#) of - (# q, r #) -> - (W32# (wordToWord32# q), W32# (wordToWord32# r)) + | y /= 0 = case x# `quotRemWord32#` y# of + (# q, r #) -> (W32# q, W32# r) | otherwise = divZeroError divMod (W32# x#) y@(W32# y#) - | y /= 0 = (W32# (wordToWord32# ((word32ToWord# x#) `quotWord#` (word32ToWord# y#))) - ,W32# (wordToWord32# ((word32ToWord# x#) `remWord#` (word32ToWord# y#)))) + | y /= 0 = (W32# (x# `quotWord32#` y#) + ,W32# (x# `remWord32#` y#)) | otherwise = divZeroError toInteger (W32# x#) #if WORD_SIZE_IN_BITS == 32 @@ -949,8 +947,7 @@ instance Integral Word64 where | otherwise = divZeroError quotRem (W64# x#) y@(W64# y#) | y /= 0 = case x# `quotRemWord#` y# of - (# q, r #) -> - (W64# q, W64# r) + (# q, r #) -> (W64# q, W64# r) | otherwise = divZeroError divMod (W64# x#) y@(W64# y#) | y /= 0 = (W64# (x# `quotWord#` y#), W64# (x# `remWord#` y#)) |
