diff options
author | Sylvain Henry <sylvain@haskus.fr> | 2021-08-11 16:20:53 +0200 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-09-07 08:02:28 -0400 |
commit | f72aa31d36f4fbab0258cae1c94ac0cc24682ab9 (patch) | |
tree | bfdd65d170c425272f066a851b26bec3bdf34d96 /libraries/ghc-bignum/src/GHC/Num/Natural.hs | |
parent | 3fb1afea019422292954785575902c62473e93e3 (diff) | |
download | haskell-f72aa31d36f4fbab0258cae1c94ac0cc24682ab9.tar.gz |
Bignum: refactor conversion rules
* make "passthrough" rules non built-in: they don't need to
* enhance note about efficient conversions between numeric types
* make integerFromNatural a little more efficient
* fix noinline pragma for naturalToWordClamp# (at least with non
built-in rules, we get warnings in cases like this)
Diffstat (limited to 'libraries/ghc-bignum/src/GHC/Num/Natural.hs')
-rw-r--r-- | libraries/ghc-bignum/src/GHC/Num/Natural.hs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/libraries/ghc-bignum/src/GHC/Num/Natural.hs b/libraries/ghc-bignum/src/GHC/Num/Natural.hs index 38a20f5169..9f950a843c 100644 --- a/libraries/ghc-bignum/src/GHC/Num/Natural.hs +++ b/libraries/ghc-bignum/src/GHC/Num/Natural.hs @@ -72,6 +72,7 @@ naturalIsPowerOf2# (NB w) = bigNatIsPowerOf2# w -- | Create a Natural from a BigNat# (respect the invariants) naturalFromBigNat# :: BigNat# -> Natural +{-# NOINLINE naturalFromBigNat# #-} naturalFromBigNat# x = case bigNatSize# x of 0# -> naturalZero 1# -> NS (bigNatIndex# x 0#) @@ -79,6 +80,7 @@ naturalFromBigNat# x = case bigNatSize# x of -- | Convert a Natural into a BigNat# naturalToBigNat# :: Natural -> BigNat# +{-# NOINLINE naturalToBigNat# #-} naturalToBigNat# (NS w) = bigNatFromWord# w naturalToBigNat# (NB bn) = bn @@ -112,7 +114,7 @@ naturalToWord !n = W# (naturalToWord# n) -- | Convert a Natural into a Word# clamping to (maxBound :: Word#). naturalToWordClamp# :: Natural -> Word# -{-# NOINLINE naturalToWordClamp #-} +{-# NOINLINE naturalToWordClamp# #-} naturalToWordClamp# (NS x) = x naturalToWordClamp# (NB _) = WORD_MAXBOUND## @@ -585,3 +587,18 @@ naturalFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# {-# NOINLINE naturalFromByteArray# #-} naturalFromByteArray# sz ba off e s = case bigNatFromByteArray# sz ba off e s of (# s', a #) -> (# s', naturalFromBigNat# a #) + + + +-- See Note [Optimising conversions between numeric types] +-- in GHC.Num.Integer +{-# RULES +"Word# -> Natural -> Word#" + forall x. naturalToWord# (NS x) = x + +"Word# -> Natural -> Word# (clamp)" + forall x. naturalToWordClamp# (NS x) = x + +"BigNat# -> Natural -> BigNat#" + forall x. naturalToBigNat# (naturalFromBigNat# x) = x +#-} |