[geos-commits] [SCM] GEOS branch master updated. 311b64a08768c3d4f2329ec4647070928058bcd2

git at osgeo.org git at osgeo.org
Wed Sep 18 08:38:36 PDT 2019


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GEOS".

The branch, master has been updated
       via  311b64a08768c3d4f2329ec4647070928058bcd2 (commit)
      from  d47ea8c428da1ed4957916c49268740b362db9fb (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 311b64a08768c3d4f2329ec4647070928058bcd2
Author: Paul Ramsey <pramsey at cleverelephant.ca>
Date:   Wed Sep 18 08:37:46 2019 -0700

    Improve the bit-shifting logic with good types and so on
    Even Roualt, Kurt Schwehr
    References #869

diff --git a/src/precision/CommonBits.cpp b/src/precision/CommonBits.cpp
index a960803..b2ba370 100644
--- a/src/precision/CommonBits.cpp
+++ b/src/precision/CommonBits.cpp
@@ -44,11 +44,12 @@ CommonBits::numCommonMostSigMantissaBits(int64 num1, int64 num2)
 int64
 CommonBits::zeroLowerBits(int64 bits, int nBits)
 {
-    if (nBits >= 64) return 0;
-    int64 invMask = (1 << nBits) - 1;
-    int64 mask = ~ invMask;
-    int64 zeroed = bits & mask;
-    return zeroed;
+    if (nBits >= 64 || nBits < 1) return 0;
+    const uint64_t bits_ = static_cast<uint64_t>(bits);
+    const uint64_t invMask = (1ull << nBits) - 1;
+    const uint64_t mask = ~ invMask;
+    const uint64_t zeroed = bits_ & mask;
+    return static_cast<int64>(zeroed);
 }
 
 /*static public*/
diff --git a/tests/unit/precision/CommonBitsTest.cpp b/tests/unit/precision/CommonBitsTest.cpp
index 02e36df..3ddea74 100644
--- a/tests/unit/precision/CommonBitsTest.cpp
+++ b/tests/unit/precision/CommonBitsTest.cpp
@@ -53,4 +53,28 @@ void object::test<3>
     ensure_equals(CommonBits::getBit(val, 63), 1);
 }
 
+// zeroLowerBits.
+template<>
+template<>
+void object::test<4>()
+{
+    constexpr int64 val = static_cast<int64>(0xffffffffffffffffull);
+    ensure_equals(sizeof(val), 8);
+
+    ensure_equals(CommonBits::zeroLowerBits(val, -1), 0);
+    ensure_equals(CommonBits::zeroLowerBits(val, 0), 0);
+    ensure_equals(CommonBits::zeroLowerBits(val, 1), -2);
+    ensure_equals(CommonBits::zeroLowerBits(val, 2), -4);
+    ensure_equals(CommonBits::zeroLowerBits(val, 16), -65536);
+    ensure_equals(CommonBits::zeroLowerBits(val, 31), -2147483648ll);
+    ensure_equals(CommonBits::zeroLowerBits(val, 32), -4294967296ll);
+    ensure_equals(CommonBits::zeroLowerBits(val, 62), -4611686018427387904ll);
+    ensure_equals(static_cast<uint64_t>(CommonBits::zeroLowerBits(val, 62)), 0xc000000000000000ull);
+    ensure_equals(static_cast<uint64_t>(CommonBits::zeroLowerBits(val, 63)), 9223372036854775808ull);
+    ensure_equals(static_cast<uint64_t>(CommonBits::zeroLowerBits(val, 63)), 0x8000000000000000ull);
+    ensure_equals(CommonBits::zeroLowerBits(val, 64), 0);
+    ensure_equals(CommonBits::zeroLowerBits(val, 10000), 0);
+}
+
+
 } // namespace tut

-----------------------------------------------------------------------

Summary of changes:
 src/precision/CommonBits.cpp            | 11 ++++++-----
 tests/unit/precision/CommonBitsTest.cpp | 24 ++++++++++++++++++++++++
 2 files changed, 30 insertions(+), 5 deletions(-)


hooks/post-receive
-- 
GEOS


More information about the geos-commits mailing list