Skip to content

Commit

Permalink
Merge pull request #5 from Ninds/DenormalBoundaryInParse
Browse files Browse the repository at this point in the history
FIX of Denormal boundary in parse #4
  • Loading branch information
Ninds committed Jul 1, 2020
2 parents 344bf9e + 5d98c2a commit 7eea3b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 14 additions & 1 deletion Ryu.Net.UnitTests/Test_s2d_n_small.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,25 @@ public void TestUTF16()

}

// https://github.com/ulfjack/ryu/issues/173
[Fact]
public void TestDenormalBoundaryInParse()
{
double result = 0;
var testCase = "2.2250738585072013e-308";
double refValue = 2.2250738585072013e-308;

var st = RyuDotNet.Internal.Ryu.s2d_n(testCase, out result);

Assert.Equal(Status.SUCCESS, st);
Assert.Equal(refValue, result);

}


}


}

public class SmallDataGenerator : IEnumerable<object[]>
{
Expand Down
11 changes: 7 additions & 4 deletions Ryu.Net/Internal/s2d.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,16 @@ internal static Status s2d_n(ReadOnlySpan<byte> buffer, out double result)
bool roundUp = (lastRemovedBit != 0) && (!trailingZeros || (((m2 >> shift) & 1) != 0));


uint64_t ieee_m2 = (m2 >> shift) + (roundUp ? 1U : 0);
if (ieee_m2 == (1ul << (DOUBLE_MANTISSA_BITS + 1)))
{
uint64_t ieee_m2 = (m2 >> shift) + (roundUp ? 1U : 0);
assert(ieee_m2 <= (1ul << (DOUBLE_MANTISSA_BITS + 1)));
ieee_m2 &= (1ul << 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 &= (1ul << DOUBLE_MANTISSA_BITS) - 1;

uint64_t ieee2 = (((((uint64_t)(signedM ? 1 : 0)) << DOUBLE_EXPONENT_BITS) | (uint64_t)ieee_e2) << DOUBLE_MANTISSA_BITS) | ieee_m2;
result = int64Bits2Double(ieee2);
return SUCCESS;
Expand Down

0 comments on commit 7eea3b9

Please sign in to comment.