Skip to content

Commit

Permalink
Extract the round to even loigic so it can be reused for hex floats.
Browse files Browse the repository at this point in the history
  • Loading branch information
deadalnix committed Jul 14, 2023
1 parent 6a390f6 commit 0b8faa9
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions include/fast_float/decimal_to_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ namespace detail {
constexpr fastfloat_really_inline int32_t power(int32_t q) noexcept {
return (((152170 + 65536) * q) >> 16) + 63;
}


/**
* /!\ If the value is right in the middle of two float,
* we must round to even!
* We detect such occurence when m ends with 01 and then
* we have a continuous streak of 0s.
*/
constexpr fastfloat_really_inline bool shouldRoundUp(uint64_t product, int shift) noexcept {
uint64_t mantissa = product >> shift;
return ((mantissa << shift) != product) | ((mantissa & 3) == 3);
}
} // namespace detail

// create an adjusted mantissa, biased by the invalid power2
Expand Down Expand Up @@ -128,7 +140,8 @@ adjusted_mantissa compute_float(int64_t q, uint64_t w) noexcept {
// is easily predicted. Which is best is data specific.
int upperbit = int(product.high >> 63);

answer.mantissa = product.high >> (upperbit + 64 - binary::mantissa_explicit_bits() - 3);
int shift = upperbit + 64 - binary::mantissa_explicit_bits() - 3;
answer.mantissa = product.high >> shift;

answer.power2 = int32_t(detail::power(int32_t(q)) + upperbit - lz - binary::minimum_exponent());
if (answer.power2 <= 0) { // we have a subnormal?
Expand Down Expand Up @@ -156,20 +169,12 @@ adjusted_mantissa compute_float(int64_t q, uint64_t w) noexcept {
return answer;
}

// usually, we round *up*, but if we fall right in between and and we have an
// even basis, we need to round down
// We are only concerned with the cases where 5**q fits in single 64-bit word.
if ((product.low <= 1) && (q >= binary::min_exponent_round_to_even()) && (q <= binary::max_exponent_round_to_even()) &&
((answer.mantissa & 3) == 1) ) { // we may fall between two floats!
// To be in-between two floats we need that in doing
// answer.mantissa = product.high >> (upperbit + 64 - binary::mantissa_explicit_bits() - 3);
// ... we dropped out only zeroes. But if this happened, then we can go back!!!
if((answer.mantissa << (upperbit + 64 - binary::mantissa_explicit_bits() - 3)) == product.high) {
answer.mantissa &= ~uint64_t(1); // flip it so that we do not round up
}
// Usually, we round *up*, but if we fall right in between and and we have an
// even basis, we need to round to even.
if (product.low != 0 || detail::shouldRoundUp(product.high, shift)) {
answer.mantissa += 1;
}

answer.mantissa += (answer.mantissa & 1); // round up
answer.mantissa >>= 1;
if (answer.mantissa >= (uint64_t(2) << binary::mantissa_explicit_bits())) {
answer.mantissa = (uint64_t(1) << binary::mantissa_explicit_bits());
Expand Down

0 comments on commit 0b8faa9

Please sign in to comment.