Skip to content

Commit

Permalink
Proposed fix to avoid rounding mantissa down to zero for number on th…
Browse files Browse the repository at this point in the history
…e denormal boundary and add test cases.

Signed-off-by: Alan Bain <alan.bain@gmail.com>
  • Loading branch information
afrb2 authored and ulfjack committed Jun 30, 2020
1 parent b38c7cc commit abf76d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ryu/s2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,13 @@ enum Status s2d_n(const char * buffer, const int len, double * result) {
printf("ieee_m2 = %" PRIu64 "\n", (m2 >> shift) + roundUp);
#endif
uint64_t ieee_m2 = (m2 >> shift) + roundUp;
if (ieee_m2 == (1ull << (DOUBLE_MANTISSA_BITS + 1))) {
assert(ieee_m2 <= (1ull << (DOUBLE_MANTISSA_BITS + 1)));
ieee_m2 &= (1ull << DOUBLE_MANTISSA_BITS) - 1;
if (ieee_m2 == 0 && roundUp) {
// Due to how the IEEE represents +/-Infinity, we don't need to check for overflow here.
ieee_e2++;
}
ieee_m2 &= (1ull << DOUBLE_MANTISSA_BITS) - 1;

uint64_t ieee = (((((uint64_t) signedM) << DOUBLE_EXPONENT_BITS) | (uint64_t)ieee_e2) << DOUBLE_MANTISSA_BITS) | ieee_m2;
*result = int64Bits2Double(ieee);
return SUCCESS;
Expand Down
7 changes: 7 additions & 0 deletions ryu/tests/s2d_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,10 @@ TEST(S2dTest, TableSizeDenormal) {
TEST(S2dTest, Issue157) {
EXPECT_S2D(1.2999999999999999E+154, "1.2999999999999999E+154");
}

TEST(S2dTest, Issue173) {
// Denormal boundary
EXPECT_S2D(2.2250738585072012e-308, "2.2250738585072012e-308");
EXPECT_S2D(2.2250738585072013e-308, "2.2250738585072013e-308");
EXPECT_S2D(2.2250738585072014e-308, "2.2250738585072014e-308");
}

0 comments on commit abf76d2

Please sign in to comment.