[geos-commits] [SCM] GEOS branch 3.7 updated. 3e8be5b6f78961093ab7dbb5f404b20746618845
git at osgeo.org
git at osgeo.org
Wed Sep 18 08:45:59 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, 3.7 has been updated
via 3e8be5b6f78961093ab7dbb5f404b20746618845 (commit)
from 6c5b169568dc1e9556748e56bb83f3875018bccc (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 3e8be5b6f78961093ab7dbb5f404b20746618845
Author: Paul Ramsey <pramsey at cleverelephant.ca>
Date: Wed Sep 18 08:45:43 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 547ed5b..f54482d 100644
--- a/src/precision/CommonBits.cpp
+++ b/src/precision/CommonBits.cpp
@@ -43,11 +43,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 e3c0bf4..1114074 100644
--- a/tests/unit/precision/CommonBitsTest.cpp
+++ b/tests/unit/precision/CommonBitsTest.cpp
@@ -11,46 +11,69 @@ using geos::precision::CommonBits;
namespace tut
{
- // Common data used by tests
- struct test_commonbits_data {};
-
- typedef test_group<test_commonbits_data> group;
- typedef group::object object;
-
- group test_commonbits_group("geos::precision::CommonBits");
-
- // getBit from zero.
- template<>
- template<>
- void object::test<1>()
- {
- constexpr int64 val = 0ull;
- for (int i=0; i < 64; i++)
- {
- ensure_equals(CommonBits::getBit(val, i), 0);
- }
- }
-
- // getBit from all ones.
- template<>
- template<>
- void object::test<2>()
- {
- constexpr int64 val = 0xffffffffffffffffull;
- for (int i=0; i < 64; i++)
- {
- ensure_equals(CommonBits::getBit(val, i), 1);
- }
- }
-
- // getBit check high versus low bits.
- template<>
- template<>
- void object::test<3>()
- {
- constexpr int64 val = 0xffffffff00000000ull;
- ensure_equals(CommonBits::getBit(val, 0), 0);
- ensure_equals(CommonBits::getBit(val, 63), 1);
- }
+ // Common data used by tests
+ struct test_commonbits_data {};
+
+ typedef test_group<test_commonbits_data> group;
+ typedef group::object object;
+
+ group test_commonbits_group("geos::precision::CommonBits");
+
+ // getBit from zero.
+ template<>
+ template<>
+ void object::test<1>()
+ {
+ constexpr int64 val = 0ull;
+ for (int i=0; i < 64; i++)
+ {
+ ensure_equals(CommonBits::getBit(val, i), 0);
+ }
+ }
+
+ // getBit from all ones.
+ template<>
+ template<>
+ void object::test<2>()
+ {
+ constexpr int64 val = 0xffffffffffffffffull;
+ for (int i=0; i < 64; i++)
+ {
+ ensure_equals(CommonBits::getBit(val, i), 1);
+ }
+ }
+
+ // getBit check high versus low bits.
+ template<>
+ template<>
+ void object::test<3>()
+ {
+ constexpr int64 val = 0xffffffff00000000ull;
+ ensure_equals(CommonBits::getBit(val, 0), 0);
+ 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 | 105 +++++++++++++++++++-------------
2 files changed, 70 insertions(+), 46 deletions(-)
hooks/post-receive
--
GEOS
More information about the geos-commits
mailing list