Skip to content

Commit 6f0247e

Browse files
committed
Move normalization to subnormal path
1 parent b622f29 commit 6f0247e

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

zmij.cc

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -708,21 +708,9 @@ struct to_decimal_result {
708708
#endif
709709
};
710710

711-
template <int num_bits>
712-
auto normalize(to_decimal_result dec, bool subnormal) noexcept
713-
-> to_decimal_result {
714-
if (!subnormal) [[ZMIJ_LIKELY]]
715-
return dec;
716-
while (dec.sig < (num_bits == 64 ? uint64_t(1e16) : uint64_t(1e8))) {
717-
dec.sig *= 10;
718-
--dec.exp;
719-
}
720-
dec.set_div10(dec.sig / 10);
721-
return dec;
722-
}
723-
724-
template <bool subnormal = false, typename UInt>
725-
auto to_decimal_schubfach(UInt bin_sig, int64_t bin_exp, bool regular) noexcept
711+
template <typename UInt>
712+
ZMIJ_INLINE auto to_decimal_schubfach(UInt bin_sig, int64_t bin_exp,
713+
bool regular) noexcept
726714
-> to_decimal_result {
727715
constexpr int num_bits = std::numeric_limits<UInt>::digits;
728716
int dec_exp = compute_dec_exp(bin_exp, regular);
@@ -752,7 +740,7 @@ auto to_decimal_schubfach(UInt bin_sig, int64_t bin_exp, bool regular) noexcept
752740
if ((shorter << bound_shift) >= lower) {
753741
to_decimal_result result = {int64_t(shorter), dec_exp};
754742
result.set_div10(div10);
755-
return normalize<num_bits>(result, subnormal);
743+
return result;
756744
}
757745

758746
UInt scaled_sig =
@@ -769,7 +757,7 @@ auto to_decimal_schubfach(UInt bin_sig, int64_t bin_exp, bool regular) noexcept
769757
UInt dec_sig = (below_closer & below_in) ? longer_below : longer_above;
770758
to_decimal_result result = {int64_t(dec_sig), dec_exp};
771759
result.set_div10(dec_sig / 10);
772-
return normalize<num_bits>(result, subnormal);
760+
return result;
773761
}
774762

775763
// Here be 🐉s.
@@ -890,7 +878,7 @@ inline auto to_decimal(double value) noexcept -> dec_fp {
890878
if (bin_exp == 0 || bin_exp == traits::exp_mask) [[ZMIJ_UNLIKELY]] {
891879
if (bin_exp != 0) return {0, int(~0u >> 1)};
892880
if (bin_sig == 0) return {0, 0};
893-
dec = to_decimal_schubfach<true>(bin_sig, 1 - traits::exp_offset, true);
881+
dec = to_decimal_schubfach(bin_sig, 1 - traits::exp_offset, true);
894882
} else {
895883
dec = to_decimal_normal<double>(bin_sig | traits::implicit_bit, bin_exp,
896884
bin_sig != 0);
@@ -922,7 +910,12 @@ auto write(Float value, char* buffer) noexcept -> char* {
922910
memcpy(buffer, "0", 2);
923911
return buffer + 1;
924912
}
925-
dec = to_decimal_schubfach<true>(bin_sig, 1 - traits::exp_offset, true);
913+
dec = to_decimal_schubfach(bin_sig, 1 - traits::exp_offset, true);
914+
while (dec.sig < uint64_t(traits::num_bits == 64 ? 1e16 : 1e8)) {
915+
dec.sig *= 10;
916+
--dec.exp;
917+
}
918+
dec.set_div10(dec.sig / 10);
926919
} else {
927920
dec = to_decimal_normal<Float>(bin_sig | traits::implicit_bit, bin_exp,
928921
bin_sig != 0);

0 commit comments

Comments
 (0)