Skip to content

Commit 8ee560d

Browse files
Gumichocopengin8Constellation
authored andcommitted
[JSC] Fix Math.sumPrecise for -Number.MAX_VALUE
https://bugs.webkit.org/show_bug.cgi?id=315450 Reviewed by Yusuke Suzuki. Discovered a bug in `Math.sumPrecise`. For exmaple,, `Math.sumPrecise([-Number.MAX_VALUE])` returns `-infinity`. `Math.sumPrecise([-1])` returns `-1.0000000000000002`. This patch fixes the bug. Test: Tools/TestWebKitAPI/Tests/WTF/PreciseSum.cpp * JSTests/stress/math-sum-precise.js: * Source/WTF/wtf/PreciseSum.cpp: (WTF::Xsum::XsumSmall::compute): * Tools/TestWebKitAPI/Tests/WTF/PreciseSum.cpp: Canonical link: https://commits.webkit.org/324530@main
1 parent db8364a commit 8ee560d

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎JSTests/stress/math-sum-precise.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ for (var i = 0; i < testLoopCount; i++) {
1111
shouldBe(Math.sumPrecise([1e308]), 1e308);
1212
shouldBe(Math.sumPrecise([1e308, -1e308]), 0);
1313
shouldBe(Math.sumPrecise([0.1]), 0.1);
14+
shouldBe(Math.sumPrecise([-1]), -1);
15+
shouldBe(Math.sumPrecise([-0.1]), -0.1);
16+
shouldBe(Math.sumPrecise([1, -1, -1]), -1);
17+
shouldBe(Math.sumPrecise([-8.8]), -8.8);
1418
shouldBe(Math.sumPrecise([0.1, 0.1]), 0.2);
1519
shouldBe(Math.sumPrecise([0.1, -0.1]), 0);
1620
shouldBe(Math.sumPrecise([1e308, 1e308, 0.1, 0.1, 1e30, 0.1, -1e30, -1e308, -1e308]), 0.30000000000000004);
@@ -45,6 +49,9 @@ for (var i = 0; i < testLoopCount; i++) {
4549
shouldBe(Math.sumPrecise([-Infinity]), -Infinity);
4650
shouldBe(Math.sumPrecise([-Infinity, -Infinity]), -Infinity);
4751

52+
shouldBe(Math.sumPrecise([Number.MAX_VALUE]), Number.MAX_VALUE);
53+
shouldBe(Math.sumPrecise([-Number.MAX_VALUE]), -Number.MAX_VALUE);
54+
4855
shouldBe(Math.sumPrecise([]), -0);
4956
shouldBe(Math.sumPrecise([-0]), -0);
5057
shouldBe(Math.sumPrecise([-0, -0]), -0);

‎Source/WTF/wtf/PreciseSum.cpp‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -549,17 +549,19 @@ double XsumSmall::compute()
549549
ivalue = -ivalue;
550550
if ((ivalue & 3) == 3)
551551
shouldRoundAwayFromZero = true;
552-
if (!lower) {
553-
while (j > 0) {
554-
j -= 1;
555-
if (m_smallAccumulator.chunk[j]) {
556-
lower = 1;
557-
break;
552+
else if ((ivalue & 3) > 1 && (ivalue & 4)) {
553+
if (!lower) {
554+
while (j > 0) {
555+
j -= 1;
556+
if (m_smallAccumulator.chunk[j]) {
557+
lower = 1;
558+
break;
559+
}
558560
}
559561
}
562+
if (!lower)
563+
shouldRoundAwayFromZero = true;
560564
}
561-
if (!lower)
562-
shouldRoundAwayFromZero = true;
563565
}
564566

565567
if (shouldRoundAwayFromZero) {

‎Tools/TestWebKitAPI/Tests/WTF/PreciseSum.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,17 @@ namespace TestWebKitAPI {
3232

3333
static constexpr double Infinity = std::numeric_limits<double>::infinity();
3434
static constexpr double NaN = std::numeric_limits<double>::quiet_NaN();
35+
static constexpr double MAX_VALUE = std::numeric_limits<double>::max();
3536

36-
static const std::array<std::pair<std::vector<double>, double>, 38> TEST_CASES = { {
37+
static const std::array<std::pair<std::vector<double>, double>, 44> TEST_CASES = { {
3738
{ { 1, 2, 3 }, 6 },
3839
{ { 1e308 }, 1e308 },
3940
{ { 1e308, -1e308 }, 0 },
4041
{ { 0.1 }, 0.1 },
42+
{ { -1 }, -1 },
43+
{ { -0.1 }, -0.1 },
44+
{ { 1, -1, -1 }, -1 },
45+
{ { -1.8 }, -1.8 },
4146
{ { 0.1, 0.1 }, 0.2 },
4247
{ { 0.1, -0.1 }, 0 },
4348
{ { 1e308, 1e308, 0.1, 0.1, 1e30, 0.1, -1e30, -1e308, -1e308 }, 0.30000000000000004 },
@@ -69,6 +74,8 @@ static const std::array<std::pair<std::vector<double>, double>, 38> TEST_CASES =
6974
{ { Infinity, Infinity }, Infinity },
7075
{ { -Infinity }, -Infinity },
7176
{ { -Infinity, -Infinity }, -Infinity },
77+
{ { MAX_VALUE }, MAX_VALUE },
78+
{ { -MAX_VALUE }, -MAX_VALUE },
7279

7380
{ { }, -0 },
7481
{ { 0 }, 0 },

0 commit comments

Comments
 (0)