Skip to content

Commit f087a34

Browse files
committed
Rename to_decimal_normal to to_decimal_fast since it can handle subnormals
1 parent 243fec9 commit f087a34

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

test/double-check.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ constexpr auto debias(int raw_exp) -> int {
4646

4747
inline auto verify(uint64_t bits, uint64_t bin_sig, int bin_exp, int raw_exp,
4848
bool& has_errors) -> bool {
49-
to_decimal_result actual = to_decimal_normal<double>(bin_sig, raw_exp, true);
49+
to_decimal_result actual = to_decimal_fast<double>(bin_sig, raw_exp, true);
5050

5151
double value;
5252
memcpy(&value, &bits, sizeof(double));

zmij.cc

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,8 @@ ZMIJ_INLINE auto to_decimal_schubfach(UInt bin_sig, int64_t bin_exp,
774774
// Converts a binary FP number bin_sig * 2**bin_exp to the shortest decimal
775775
// representation, where bin_exp = raw_exp - exp_offset.
776776
template <typename Float, typename UInt>
777-
ZMIJ_INLINE auto to_decimal_normal(UInt bin_sig, int64_t raw_exp,
778-
bool regular) noexcept -> to_decimal_result {
777+
ZMIJ_INLINE auto to_decimal_fast(UInt bin_sig, int64_t raw_exp,
778+
bool regular) noexcept -> to_decimal_result {
779779
using traits = float_traits<Float>;
780780
int64_t bin_exp = raw_exp - traits::exp_offset;
781781
constexpr int num_bits = std::numeric_limits<UInt>::digits;
@@ -934,8 +934,8 @@ inline auto to_decimal(double value) noexcept -> dec_fp {
934934
bin_exp = 1;
935935
bin_sig |= traits::implicit_bit;
936936
}
937-
auto dec = to_decimal_normal<double>(bin_sig ^ traits::implicit_bit, bin_exp,
938-
bin_sig != 0);
937+
auto dec = to_decimal_fast<double>(bin_sig ^ traits::implicit_bit, bin_exp,
938+
bin_sig != 0);
939939
return {dec.sig, dec.exp, negative};
940940
}
941941

@@ -954,6 +954,7 @@ auto write(Float value, char* buffer) noexcept -> char* {
954954
buffer += traits::is_negative(bits);
955955

956956
to_decimal_result dec;
957+
constexpr uint64_t threshold = uint64_t(traits::num_bits == 64 ? 1e16 : 1e8);
957958
if (bin_exp == 0 || bin_exp == traits::exp_mask) [[ZMIJ_UNLIKELY]] {
958959
if (bin_exp != 0) {
959960
memcpy(buffer, bin_sig == 0 ? "inf" : "nan", 4);
@@ -964,17 +965,17 @@ auto write(Float value, char* buffer) noexcept -> char* {
964965
return buffer + 1;
965966
}
966967
dec = to_decimal_schubfach(bin_sig, 1 - traits::exp_offset, true);
967-
while (dec.sig < uint64_t(traits::num_bits == 64 ? 1e16 : 1e8)) {
968+
while (dec.sig < threshold) {
968969
dec.sig *= 10;
969970
--dec.exp;
970971
}
971972
dec.set_div10(dec.sig / 10);
972973
} else {
973-
dec = to_decimal_normal<Float>(bin_sig | traits::implicit_bit, bin_exp,
974-
bin_sig != 0);
974+
dec = to_decimal_fast<Float>(bin_sig | traits::implicit_bit, bin_exp,
975+
bin_sig != 0);
975976
}
976977
int dec_exp = dec.exp;
977-
bool extra_digit = dec.sig >= uint64_t(traits::num_bits == 64 ? 1e16 : 1e8);
978+
bool extra_digit = dec.sig >= threshold;
978979
dec_exp += traits::max_digits10 - 2 + extra_digit;
979980
if (traits::num_bits == 32 && dec.sig < uint32_t(1e7)) [[ZMIJ_UNLIKELY]] {
980981
dec.sig *= 10;

0 commit comments

Comments
 (0)