Skip to content

Commit

Permalink
Fix #157 (#158)
Browse files Browse the repository at this point in the history
multipleOfPowerOf2 only works for exponents < 64.

In this case here we have 0 < m10 < 2^64, so m10 can never
be a multiple of 2^p if p >= 64.
  • Loading branch information
abolz authored May 22, 2020
1 parent 4cd47db commit 27d3c55
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions ryu/d2s_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ static inline bool multipleOfPowerOf5(const uint64_t value, const uint32_t p) {
// Returns true if value is divisible by 2^p.
static inline bool multipleOfPowerOf2(const uint64_t value, const uint32_t p) {
assert(value != 0);
assert(p < 64);
// __builtin_ctzll doesn't appear to be faster here.
return (value & ((1ull << p) - 1)) == 0;
}
Expand Down
2 changes: 1 addition & 1 deletion ryu/s2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ enum Status s2d_n(const char * buffer, const int len, double * result) {
// This can only be the case if 2^e2 divides m10 * 10^e10, which in turn requires that the
// largest power of 2 that divides m10 + e10 is greater than e2. If e2 is less than e10, then
// the result must be exact. Otherwise we use the existing multipleOfPowerOf2 function.
trailingZeros = e2 < e10 || multipleOfPowerOf2(m10, e2 - e10);
trailingZeros = e2 < e10 || (e2 - e10 < 64 && multipleOfPowerOf2(m10, e2 - e10));
} else {
e2 = floor_log2(m10) + e10 - ceil_log2pow5(-e10) - (DOUBLE_MANTISSA_BITS + 1);
int j = e2 - e10 + ceil_log2pow5(-e10) - 1 + DOUBLE_POW5_INV_BITCOUNT;
Expand Down
4 changes: 4 additions & 0 deletions ryu/tests/s2d_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,7 @@ TEST(S2dTest, Overflow) {
TEST(S2dTest, TableSizeDenormal) {
EXPECT_S2D(5e-324, "4.9406564584124654e-324");
}

TEST(S2dTest, Issue157) {
EXPECT_S2D(1.2999999999999999E+154, "1.2999999999999999E+154");
}

0 comments on commit 27d3c55

Please sign in to comment.