Skip to content

Commit

Permalink
Simplify to_decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Feb 21, 2022
1 parent 70dc3de commit cf940ae
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2043,28 +2043,22 @@ template <typename T> decimal_fp<T> to_decimal(T x) noexcept {
// Add dist / 10^kappa to the significand.
ret_value.significand += dist;

if (divisible_by_small_divisor) {
// Check z^(f) >= epsilon^(f).
// We have either yi == zi - epsiloni or yi == (zi - epsiloni) - 1,
// where yi == zi - epsiloni if and only if z^(f) >= epsilon^(f)
// Since there are only 2 possibilities, we only need to care about the
// parity. Also, zi and r should have the same parity since the divisor
// is an even number.
const typename cache_accessor<T>::compute_mul_parity_result y_mul =
cache_accessor<T>::compute_mul_parity(two_fc, cache, beta);

if (y_mul.parity != approx_y_parity) {
--ret_value.significand;
} else {
// If z^(f) >= epsilon^(f), we might have a tie
// when z^(f) == epsilon^(f), or equivalently, when y is an integer
if (y_mul.is_integer) {
ret_value.significand = ret_value.significand % 2 == 0
? ret_value.significand
: ret_value.significand - 1;
}
}
}
if (!divisible_by_small_divisor) return ret_value;

// Check z^(f) >= epsilon^(f).
// We have either yi == zi - epsiloni or yi == (zi - epsiloni) - 1,
// where yi == zi - epsiloni if and only if z^(f) >= epsilon^(f).
// Since there are only 2 possibilities, we only need to care about the
// parity. Also, zi and r should have the same parity since the divisor
// is an even number.
const auto y_mul = cache_accessor<T>::compute_mul_parity(two_fc, cache, beta);

// If z^(f) >= epsilon^(f), we might have a tie when z^(f) == epsilon^(f),
// or equivalently, when y is an integer.
if (y_mul.parity != approx_y_parity)
--ret_value.significand;
else if (y_mul.is_integer && ret_value.significand % 2 != 0)
--ret_value.significand;
return ret_value;
}
} // namespace dragonbox
Expand Down

0 comments on commit cf940ae

Please sign in to comment.