diff --git a/hexl/number-theory/number-theory.hpp b/hexl/number-theory/number-theory.hpp index 06fd1265..be2d9956 100644 --- a/hexl/number-theory/number-theory.hpp +++ b/hexl/number-theory/number-theory.hpp @@ -56,9 +56,7 @@ inline bool IsPowerOfTwo(uint64_t num) { return num && !(num & (num - 1)); } // Returns log2(x) for x a power of 2 inline uint64_t Log2(uint64_t x) { HEXL_CHECK(IsPowerOfTwo(x), x << " not a power of 2"); - uint64_t ret = 0; - while (x >>= 1) ++ret; - return ret; + return MSB(x); } // Returns the maximum value that can be represented using bits bits diff --git a/hexl/util/msvc.hpp b/hexl/util/msvc.hpp index 1c676153..b98ec394 100644 --- a/hexl/util/msvc.hpp +++ b/hexl/util/msvc.hpp @@ -151,7 +151,7 @@ inline void SubWithCarry128(uint64_t* result_hi, uint64_t* result_lo, } /// @brief Computes and returns significant bit count -/// @param[in] value Input element +/// @param[in] value Input element at most 128 bits long inline uint64_t SignificantBitLength(const uint64_t* value) { HEXL_CHECK(value != nullptr, "Require value != nullptr"); @@ -191,7 +191,7 @@ inline void DivideUInt128UInt64(uint64_t* quotient, const uint64_t* numerator, HEXL_CHECK(numerator != nullptr, "Require numerator != nullptr"); HEXL_CHECK(denominator == 128, "denominator cannot be 0 " << denominator); - // get bit count of Divisor + // get bit count of divisor uint64_t numerator_bits = SignificantBitLength(numerator); const uint64_t numerator_bits_const = numerator_bits; const uint64_t uint_128_bit = 128ULL; diff --git a/test/test-number-theory.cpp b/test/test-number-theory.cpp index cbe219c4..9a63c596 100644 --- a/test/test-number-theory.cpp +++ b/test/test-number-theory.cpp @@ -425,9 +425,11 @@ TEST(NumberTheory, DivideUInt128UInt64Lo) { } TEST(NumberTheory, MSB) { - EXPECT_EQ(60ULL, MSB(2305843009213689601)); - EXPECT_EQ(59ULL, MSB(1152921504606844417)); - EXPECT_EQ(59ULL, MSB(1152921504606844289)); + EXPECT_EQ(60ULL, MSB(2305843009213689601)); // 2**61 - 4351 + EXPECT_EQ(59ULL, MSB(1152921504606844417)); // 2**60 - 2559 + EXPECT_EQ(59ULL, MSB(1152921504606844289)); // 2**60 - 2687 + EXPECT_EQ(8ULL, MSB(256)); + EXPECT_EQ(0ULL, MSB(1)); } } // namespace hexl