[Git][ghc/ghc][master] Fix isValidNatural: The BigNat in NatJ# must have at least 2 limbs
Marge Bot
gitlab at gitlab.haskell.org
Fri Jun 7 14:25:21 UTC 2019
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
be63d299 by Simon Jakobi at 2019-06-07T14:25:16Z
Fix isValidNatural: The BigNat in NatJ# must have at least 2 limbs
Previously the `integer-gmp` variant of `isValidNatural` would fail to
detect values `<= maxBound::Word` that were incorrectly encoded using
the `NatJ#` constructor.
- - - - -
6 changed files:
- libraries/base/GHC/Natural.hs
- libraries/base/changelog.md
- libraries/base/tests/all.T
- + libraries/base/tests/isValidNatural.hs
- + libraries/base/tests/isValidNatural.stdout
- libraries/integer-gmp/src/GHC/Integer/Type.hs
Changes:
=====================================
libraries/base/GHC/Natural.hs
=====================================
@@ -157,7 +157,9 @@ data Natural = NatS# GmpLimb# -- ^ in @[0, maxBound::Word]@
isValidNatural :: Natural -> Bool
isValidNatural (NatS# _) = True
isValidNatural (NatJ# bn) = isTrue# (isValidBigNat# bn)
- && isTrue# (sizeofBigNat# bn ># 0#)
+ -- A 1-limb BigNat could fit into a NatS#, so we
+ -- require at least 2 limbs.
+ && isTrue# (sizeofBigNat# bn ># 1#)
signumNatural :: Natural -> Natural
signumNatural (NatS# 0##) = NatS# 0##
=====================================
libraries/base/changelog.md
=====================================
@@ -5,6 +5,10 @@
* Add a `TestEquality` instance for the `Compose` newtype.
+ * Fix the `integer-gmp` variant of `isValidNatural`: Previously it would fail
+ to detect values `<= maxBound::Word` that were incorrectly encoded using
+ the `NatJ#` constructor.
+
## 4.13.0.0 *TBA*
* Bundled with GHC *TBA*
=====================================
libraries/base/tests/all.T
=====================================
@@ -40,6 +40,7 @@ test('take001', extra_run_opts('1'), compile_and_run, [''])
test('inits', normal, compile_and_run, [''])
test('genericNegative001', extra_run_opts('-1'), compile_and_run, [''])
test('ix001', normal, compile_and_run, [''])
+test('isValidNatural', reqlib('integer-gmp'), compile_and_run, [''])
# need to add -K64m to the compiler opts, so that GHCi gets it too
test('ioref001',
=====================================
libraries/base/tests/isValidNatural.hs
=====================================
@@ -0,0 +1,9 @@
+{-# language MagicHash #-}
+
+import GHC.Integer.GMP.Internals
+import GHC.Natural
+
+main = print $ map isValidNatural [0, 1, maxWord, maxWord + 1, invalid]
+ where
+ maxWord = fromIntegral (maxBound :: Word)
+ invalid = NatJ# oneBigNat -- 1 would fit into the NatS# constructor.
=====================================
libraries/base/tests/isValidNatural.stdout
=====================================
@@ -0,0 +1 @@
+[True,True,True,True,False]
=====================================
libraries/integer-gmp/src/GHC/Integer/Type.hs
=====================================
@@ -1778,6 +1778,8 @@ foreign import ccall unsafe "gmp.h __gmpn_popcount"
-- BigNat-wrapped ByteArray#-primops
-- | Return number of limbs contained in 'BigNat'.
+--
+-- The result is always @>= 1@ since even zero is encoded with 1 limb.
sizeofBigNat# :: BigNat -> GmpSize#
sizeofBigNat# (BN# x#)
= sizeofByteArray# x# `uncheckedIShiftRL#` GMP_LIMB_SHIFT#
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/commit/be63d2996308c77f8a0a44592074c98f66a80e93
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/commit/be63d2996308c77f8a0a44592074c98f66a80e93
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20190607/722eb350/attachment-0001.html>
More information about the ghc-commits
mailing list