Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplification to u256 digit counting in d128 div #886

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/boost/decimal/detail/div_impl.hpp
Original file line number Diff line number Diff line change
@@ -88,8 +88,9 @@ constexpr auto d128_generic_div_impl(const T& lhs, const T& rhs, T& q) noexcept
const auto digit_delta {sig_dig - std::numeric_limits<detail::uint128>::digits10};
res_sig /= detail::uint256_t(pow10(detail::uint128(digit_delta)));
res_exp += digit_delta;
BOOST_DECIMAL_ASSERT(res_sig.high == 0);
}
else if (res_sig == UINT64_C(0))
else if (res_sig.low == UINT64_C(0))
{
sign = false;
}
34 changes: 25 additions & 9 deletions include/boost/decimal/detail/integer_search_trees.hpp
Original file line number Diff line number Diff line change
@@ -203,20 +203,36 @@ constexpr int num_digits(const uint256_t& x) noexcept
return num_digits(x.low);
}

// 10^77
auto current_power_of_10 {uint256_t{uint128{UINT64_C(15930919111324522770), UINT64_C(5327493063679123134)}, uint128{UINT64_C(12292710897160462336), UINT64_C(0)}}};
// Use the most significant bit position to approximate log10
// log10(x) ≈ log2(x) / log2(10) ≈ log2(x) / 3.32

for (int i = 78; i > 0; --i)
int msb {};
// Check high part of high (most significant 64 bits of 256-bit number)
if (x.high.high != 0)
{
if (x >= current_power_of_10)
{
return i;
}
msb = 192 + (64 - countl_zero(x.high.high));
}
// Check low part of high (bits 128-191 of 256-bit number)
else if (x.high.low != 0)
{
msb = 128 + (64 - countl_zero(x.high.low));
}

current_power_of_10 /= UINT64_C(10);
// Approximate log10 using log2
const auto estimated_digits {(msb * 1000) / 3322 + 1}; // 1000/3322 approxeq 1/log2(10)

// Check if our estimate is correct or needs adjustment
if (x >= impl::emulated_256_pow10[estimated_digits])
{
// Check to make sure we don't exceed the maximum value
return estimated_digits != 78 ? estimated_digits + 1 : estimated_digits;
}
else if (estimated_digits > 1 && x < impl::emulated_256_pow10[estimated_digits - 1])
{
return estimated_digits - 1;
}

return 1;
return estimated_digits;
}

#ifdef _MSC_VER