Skip to content

Commit 627d376

Browse files
committed
Add special handling for u256 divided by a single word
1 parent 7237e34 commit 627d376

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

include/boost/decimal/detail/u256.hpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,25 @@ BOOST_DECIMAL_FORCE_INLINE constexpr auto div_mod(const u256& lhs, const Unsigne
10771077

10781078
BOOST_DECIMAL_DETAIL_INT128_ASSERT(m >= n);
10791079

1080-
int128::detail::impl::knuth_divide<true>(u, m, v, n, q);
1080+
// Simplify handling of single word division
1081+
// We run into this case with dividing by powers of 10 while rounding u256
1082+
if (n == 1U)
1083+
{
1084+
std::uint64_t remainder {};
1085+
1086+
for (std::size_t j = m; j-- > 0;)
1087+
{
1088+
const auto dividend {(remainder << 32) | u[j]};
1089+
q[j] = static_cast<std::uint32_t>(dividend / v[0]);
1090+
remainder = dividend % v[0];
1091+
}
1092+
1093+
u[0] = static_cast<std::uint32_t>(remainder);
1094+
}
1095+
else
1096+
{
1097+
int128::detail::impl::knuth_divide<true>(u, m, v, n, q);
1098+
}
10811099

10821100
return {from_words(q), from_words(u)};
10831101
}

0 commit comments

Comments
 (0)