Skip to content

Commit 323171f

Browse files
committed
Replace loop with single division
1 parent dc01f38 commit 323171f

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

include/boost/decimal/decimal128.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -742,11 +742,12 @@ constexpr decimal128::decimal128(T1 coeff, T2 exp, bool sign) noexcept
742742
// If the coeff is not in range make it so
743743
auto unsigned_coeff_digits {detail::num_digits(unsigned_coeff)};
744744
const bool reduced {unsigned_coeff_digits > detail::precision_v<decimal128>};
745-
while (unsigned_coeff_digits > detail::precision_v<decimal128> + 1)
745+
if (unsigned_coeff_digits > detail::precision_v<decimal128> + 1)
746746
{
747-
unsigned_coeff /= 10;
748-
++exp;
749-
--unsigned_coeff_digits;
747+
const auto digits_to_remove {unsigned_coeff_digits - (detail::precision_v<decimal128> + 1)};
748+
unsigned_coeff /= detail::pow10(static_cast<Unsigned_Integer>(digits_to_remove));
749+
exp += digits_to_remove;
750+
unsigned_coeff_digits -= digits_to_remove;
750751
}
751752

752753
// Round as required

include/boost/decimal/decimal32.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,12 @@ constexpr decimal32::decimal32(T coeff, T2 exp, bool sign) noexcept // NOLINT(re
622622
// If the coeff is not in range make it so
623623
auto unsigned_coeff_digits {detail::num_digits(unsigned_coeff)};
624624
const bool reduced {unsigned_coeff_digits > detail::precision};
625-
while (unsigned_coeff_digits > detail::precision + 1)
625+
if (unsigned_coeff_digits > detail::precision + 1)
626626
{
627-
unsigned_coeff /= 10;
628-
++exp;
629-
--unsigned_coeff_digits;
627+
const auto digits_to_remove {unsigned_coeff_digits - (detail::precision + 1)};
628+
unsigned_coeff /= detail::pow10(static_cast<Unsigned_Integer>(digits_to_remove));
629+
exp += digits_to_remove;
630+
unsigned_coeff_digits -= digits_to_remove;
630631
}
631632

632633
// Round as required

include/boost/decimal/decimal64.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,12 @@ constexpr decimal64::decimal64(T1 coeff, T2 exp, bool sign) noexcept
629629
// If the coeff is not in range make it so
630630
auto unsigned_coeff_digits {detail::num_digits(unsigned_coeff)};
631631
const bool reduced {unsigned_coeff_digits > detail::precision_v<decimal64>};
632-
while (unsigned_coeff_digits > detail::precision_v<decimal64> + 1)
632+
if (unsigned_coeff_digits > detail::precision_v<decimal64> + 1)
633633
{
634-
unsigned_coeff /= 10;
635-
++exp;
636-
--unsigned_coeff_digits;
634+
const auto digits_to_remove {unsigned_coeff_digits - (detail::precision_v<decimal64> + 1)};
635+
unsigned_coeff /= detail::pow10(static_cast<Unsigned_Integer>(digits_to_remove));
636+
exp += digits_to_remove;
637+
unsigned_coeff_digits -= digits_to_remove;
637638
}
638639

639640
// Round as required

0 commit comments

Comments
 (0)