Skip to content

Commit

Permalink
abtract logmod as MSB
Browse files Browse the repository at this point in the history
  • Loading branch information
GelilaSeifu authored and fboemer committed May 4, 2021
1 parent 3bf6ebe commit fde5149
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 1 addition & 3 deletions hexl/number-theory/number-theory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions hexl/util/msvc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions test/test-number-theory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fde5149

Please sign in to comment.