Skip to content

Commit de632a2

Browse files
authored
Merge pull request #577 from cppalliance/567
Remove loop from constructors
2 parents f049c57 + fe6cdfe commit de632a2

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

include/boost/decimal/decimal128.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -742,11 +742,23 @@ 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+
749+
#if defined(__GNUC__) && !defined(__clang__)
750+
# pragma GCC diagnostic push
751+
# pragma GCC diagnostic ignored "-Wconversion"
752+
#endif
753+
754+
unsigned_coeff /= detail::pow10(static_cast<Unsigned_Integer>(digits_to_remove));
755+
756+
#if defined(__GNUC__) && !defined(__clang__)
757+
# pragma GCC diagnostic pop
758+
#endif
759+
760+
exp += digits_to_remove;
761+
unsigned_coeff_digits -= digits_to_remove;
750762
}
751763

752764
// Round as required

include/boost/decimal/decimal32.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,23 @@ 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+
629+
#if defined(__GNUC__) && !defined(__clang__)
630+
# pragma GCC diagnostic push
631+
# pragma GCC diagnostic ignored "-Wconversion"
632+
#endif
633+
634+
unsigned_coeff /= detail::pow10(static_cast<Unsigned_Integer>(digits_to_remove));
635+
636+
#if defined(__GNUC__) && !defined(__clang__)
637+
# pragma GCC diagnostic pop
638+
#endif
639+
640+
exp += digits_to_remove;
641+
unsigned_coeff_digits -= digits_to_remove;
630642
}
631643

632644
// Round as required

include/boost/decimal/decimal64.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,23 @@ 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+
636+
#if defined(__GNUC__) && !defined(__clang__)
637+
# pragma GCC diagnostic push
638+
# pragma GCC diagnostic ignored "-Wconversion"
639+
#endif
640+
641+
unsigned_coeff /= detail::pow10(static_cast<Unsigned_Integer>(digits_to_remove));
642+
643+
#if defined(__GNUC__) && !defined(__clang__)
644+
# pragma GCC diagnostic pop
645+
#endif
646+
647+
exp += digits_to_remove;
648+
unsigned_coeff_digits -= digits_to_remove;
637649
}
638650

639651
// Round as required

0 commit comments

Comments
 (0)