Skip to content

Commit 3b81fed

Browse files
committed
Add exp_offset
1 parent ce01335 commit 3b81fed

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

test/double-check.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ const uint64_t pow10[] = {
4040

4141
using traits = float_traits<double>;
4242

43-
constexpr auto debias(int bin_exp_biased) -> int {
44-
return bin_exp_biased - (traits::num_sig_bits + traits::exp_bias);
43+
constexpr auto debias(int raw_exp) -> int {
44+
return raw_exp - traits::exp_offset;
4545
}
4646

4747
inline auto verify(uint64_t bits, uint64_t bin_sig, int bin_exp, int raw_exp,

zmij.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ template <typename Float> struct float_traits : std::numeric_limits<Float> {
282282
static constexpr int num_exp_bits = num_bits - num_sig_bits - 1;
283283
static constexpr int exp_mask = (1 << num_exp_bits) - 1;
284284
static constexpr int exp_bias = (1 << (num_exp_bits - 1)) - 1;
285+
static constexpr int exp_offset = exp_bias + num_sig_bits;
285286

286287
using sig_type = std::conditional_t<num_bits == 64, uint64_t, uint32_t>;
287288
static constexpr sig_type implicit_bit = sig_type(1) << num_sig_bits;
@@ -389,13 +390,12 @@ struct exp_shift_table {
389390
using traits = float_traits<double>;
390391
static constexpr bool enable = true;
391392
static constexpr int num_exps = traits::exp_mask + 1;
392-
static constexpr int offset = traits::num_sig_bits + traits::exp_bias;
393393
unsigned char data[enable ? num_exps : 1] = {};
394394

395395
constexpr exp_shift_table() {
396396
if (!enable) return;
397397
for (int raw_exp = 0; raw_exp < num_exps; ++raw_exp) {
398-
int bin_exp = raw_exp - offset;
398+
int bin_exp = raw_exp - traits::exp_offset;
399399
if (raw_exp == 0) ++bin_exp;
400400
int dec_exp = compute_dec_exp(bin_exp, true);
401401
data[raw_exp] = do_compute_exp_shift(bin_exp, dec_exp);
@@ -418,7 +418,7 @@ template <int num_bits, bool only_regular = false>
418418
constexpr ZMIJ_INLINE auto compute_exp_shift(int bin_exp, int dec_exp) noexcept
419419
-> unsigned char {
420420
if (num_bits == 64 && exp_shift_table::enable && only_regular)
421-
return exp_shifts.data[bin_exp + exp_shift_table::offset];
421+
return exp_shifts.data[bin_exp + float_traits<double>::exp_offset];
422422
return do_compute_exp_shift(bin_exp, dec_exp);
423423
}
424424

@@ -766,12 +766,12 @@ auto to_decimal_schubfach(UInt bin_sig, int64_t bin_exp, bool regular) noexcept
766766

767767
// Here be 🐉s.
768768
// Converts a binary FP number bin_sig * 2**bin_exp to the shortest decimal
769-
// representation, where bin_exp = raw_exp - num_sig_bits - exp_bias.
769+
// representation, where bin_exp = raw_exp - exp_offset.
770770
template <typename Float, typename UInt>
771771
ZMIJ_INLINE auto to_decimal_normal(UInt bin_sig, int64_t raw_exp,
772772
bool regular) noexcept -> zmij::dec_fp {
773773
using traits = float_traits<Float>;
774-
int64_t bin_exp = raw_exp - traits::num_sig_bits - traits::exp_bias;
774+
int64_t bin_exp = raw_exp - traits::exp_offset;
775775
constexpr int num_bits = std::numeric_limits<UInt>::digits;
776776
// An optimization from yy by Yaoyuan Guo:
777777
while (regular) [[ZMIJ_LIKELY]] {
@@ -881,8 +881,7 @@ inline auto to_decimal(double value) noexcept -> dec_fp {
881881
if (bin_exp == 0 || bin_exp == traits::exp_mask) [[ZMIJ_UNLIKELY]] {
882882
if (bin_exp != 0) return {0, int(~0u >> 1)};
883883
if (bin_sig == 0) return {0, 0};
884-
constexpr int exp_offset = traits::num_sig_bits + traits::exp_bias;
885-
dec = to_decimal_schubfach<true>(bin_sig, 1 - exp_offset, true);
884+
dec = to_decimal_schubfach<true>(bin_sig, 1 - traits::exp_offset, true);
886885
} else {
887886
dec = to_decimal_normal<double>(bin_sig | traits::implicit_bit, bin_exp,
888887
bin_sig != 0);
@@ -914,8 +913,7 @@ auto write(Float value, char* buffer) noexcept -> char* {
914913
memcpy(buffer, "0", 2);
915914
return buffer + 1;
916915
}
917-
constexpr int exp_offset = traits::num_sig_bits + traits::exp_bias;
918-
dec = to_decimal_schubfach<true>(bin_sig, 1 - exp_offset, true);
916+
dec = to_decimal_schubfach<true>(bin_sig, 1 - traits::exp_offset, true);
919917
} else {
920918
dec = to_decimal_normal<Float>(bin_sig | traits::implicit_bit, bin_exp,
921919
bin_sig != 0);

0 commit comments

Comments
 (0)