Title: [249597] trunk
Revision
249597
Author
[email protected]
Date
2019-09-06 15:49:56 -0700 (Fri, 06 Sep 2019)

Log Message

Math.round() produces wrong result for value prior to 0.5
https://bugs.webkit.org/show_bug.cgi?id=185115

Reviewed by Saam Barati.

JSTests:

* stress/math-round-basics.js:
Add positive/negative test cases.

* test262/expectations.yaml:
Mark test passing.

Source/_javascript_Core:

Our Math.round implementation goes in the wrong direction for double values like 0.49999999999999994.
This requires just a subtle adjustment for three of our four versions; only baseline JIT needed a full rewrite.

Specifically:
  - While 0.49999999999999994 is representable, 1 - 0.49999999999999994 is not (it turns into 0.5),
    so taking the difference between ceil(value)` and `value` is problematic.
  - The baseline implementation was doing `floor(x + 0.5)` for positive doubles and slowpathing negative ones
    (by falling back to jsRound). This patch gives baseline a legitimate implementation too.

* dfg/DFGSpeculativeJIT.cpp:
(JSC::DFG::SpeculativeJIT::compileArithRounding):
* ftl/FTLLowerDFGToB3.cpp:
(JSC::FTL::DFG::LowerDFGToB3::compileArithRound):
* jit/ThunkGenerators.cpp:
(JSC::roundThunkGenerator):
* runtime/MathCommon.cpp:

Modified Paths

Diff

Modified: trunk/JSTests/ChangeLog (249596 => 249597)


--- trunk/JSTests/ChangeLog	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/JSTests/ChangeLog	2019-09-06 22:49:56 UTC (rev 249597)
@@ -1,3 +1,16 @@
+2019-09-06  Ross Kirsling  <[email protected]>
+
+        Math.round() produces wrong result for value prior to 0.5
+        https://bugs.webkit.org/show_bug.cgi?id=185115
+
+        Reviewed by Saam Barati.
+
+        * stress/math-round-basics.js:
+        Add positive/negative test cases.
+
+        * test262/expectations.yaml:
+        Mark test passing.
+
 2019-09-06  Mark Lam  <[email protected]>
 
         Move web-assembly-constructors-should-not-override-global-object-property.js below JSTests/wasm/stress.

Modified: trunk/JSTests/stress/math-round-basics.js (249596 => 249597)


--- trunk/JSTests/stress/math-round-basics.js	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/JSTests/stress/math-round-basics.js	2019-09-06 22:49:56 UTC (rev 249597)
@@ -81,6 +81,14 @@
     if (roundedValue !== Number.NEGATIVE_INFINITY)
         throw "mathRoundOnDoubles(Number.NEGATIVE_INFINITY) = " + roundedValue;
 
+    var roundedValue = mathRoundOnDoubles(0.5 - Number.EPSILON / 4);
+    if (roundedValue !== 0 || (1 / roundedValue) !== Infinity)
+        throw "mathRoundOnDoubles(0.5 - Number.EPSILON / 4) = " + roundedValue;
+
+    var roundedValue = mathRoundOnDoubles(-0.5 + Number.EPSILON / 4);
+    if (roundedValue !== 0 || (1 / roundedValue) !== -Infinity)
+        throw "mathRoundOnDoubles(-0.5 + Number.EPSILON / 4) = " + roundedValue;
+
     var boolean = !!(i % 2);
     var roundedBoolean = mathRoundOnBooleans(boolean);
     if (roundedBoolean != boolean)

Modified: trunk/JSTests/test262/expectations.yaml (249596 => 249597)


--- trunk/JSTests/test262/expectations.yaml	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/JSTests/test262/expectations.yaml	2019-09-06 22:49:56 UTC (rev 249597)
@@ -1212,9 +1212,6 @@
 test/built-ins/Map/proto-from-ctor-realm.js:
   default: 'Test262Error: Expected SameValue(«[object Map]», «[object Map]») to be true'
   strict mode: 'Test262Error: Expected SameValue(«[object Map]», «[object Map]») to be true'
-test/built-ins/Math/round/S15.8.2.15_A7.js:
-  default: "Test262Error: #4: '1 / Math.round(0.49999999999999994) !== 1 / 0'"
-  strict mode: "Test262Error: #4: '1 / Math.round(0.49999999999999994) !== 1 / 0'"
 test/built-ins/Number/proto-from-ctor-realm.js:
   default: 'Test262Error: Expected SameValue(«0», «0») to be true'
   strict mode: 'Test262Error: Expected SameValue(«0», «0») to be true'

Modified: trunk/Source/_javascript_Core/ChangeLog (249596 => 249597)


--- trunk/Source/_javascript_Core/ChangeLog	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-09-06 22:49:56 UTC (rev 249597)
@@ -1,3 +1,27 @@
+2019-09-06  Ross Kirsling  <[email protected]>
+
+        Math.round() produces wrong result for value prior to 0.5
+        https://bugs.webkit.org/show_bug.cgi?id=185115
+
+        Reviewed by Saam Barati.
+
+        Our Math.round implementation goes in the wrong direction for double values like 0.49999999999999994.
+        This requires just a subtle adjustment for three of our four versions; only baseline JIT needed a full rewrite.
+
+        Specifically:
+          - While 0.49999999999999994 is representable, 1 - 0.49999999999999994 is not (it turns into 0.5),
+            so taking the difference between ceil(value)` and `value` is problematic.
+          - The baseline implementation was doing `floor(x + 0.5)` for positive doubles and slowpathing negative ones
+            (by falling back to jsRound). This patch gives baseline a legitimate implementation too.
+
+        * dfg/DFGSpeculativeJIT.cpp:
+        (JSC::DFG::SpeculativeJIT::compileArithRounding):
+        * ftl/FTLLowerDFGToB3.cpp:
+        (JSC::FTL::DFG::LowerDFGToB3::compileArithRound):
+        * jit/ThunkGenerators.cpp:
+        (JSC::roundThunkGenerator):
+        * runtime/MathCommon.cpp:
+
 2019-09-05  Joseph Pecoraro  <[email protected]>
 
         Tail Deleted Frames shown in Web Inspector are sometimes incorrect (Shadow Chicken)

Modified: trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp (249596 => 249597)


--- trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/Source/_javascript_Core/dfg/DFGSpeculativeJIT.cpp	2019-09-06 22:49:56 UTC (rev 249597)
@@ -5604,16 +5604,14 @@
                     m_jit.floorDouble(resultFPR, resultFPR);
                 } else {
                     m_jit.ceilDouble(valueFPR, resultFPR);
-                    FPRTemporary realPart(this);
-                    FPRReg realPartFPR = realPart.fpr();
-                    m_jit.subDouble(resultFPR, valueFPR, realPartFPR);
 
                     FPRTemporary scratch(this);
                     FPRReg scratchFPR = scratch.fpr();
-                    static const double halfConstant = 0.5;
+                    static const double halfConstant = -0.5;
                     m_jit.loadDouble(TrustedImmPtr(&halfConstant), scratchFPR);
+                    m_jit.addDouble(resultFPR, scratchFPR);
 
-                    JITCompiler::Jump shouldUseCeiled = m_jit.branchDouble(JITCompiler::DoubleLessThanOrEqual, realPartFPR, scratchFPR);
+                    JITCompiler::Jump shouldUseCeiled = m_jit.branchDouble(JITCompiler::DoubleLessThanOrEqual, scratchFPR, valueFPR);
                     static const double _oneConstant_ = -1.0;
                     m_jit.loadDouble(TrustedImmPtr(&oneConstant), scratchFPR);
                     m_jit.addDouble(scratchFPR, resultFPR);

Modified: trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp (249596 => 249597)


--- trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/Source/_javascript_Core/ftl/FTLLowerDFGToB3.cpp	2019-09-06 22:49:56 UTC (rev 249597)
@@ -2945,7 +2945,7 @@
                 LValue value = lowDouble(m_node->child1());
                 result = m_out.doubleFloor(m_out.doubleAdd(value, m_out.constDouble(0.5)));
             } else {
-                LBasicBlock realPartIsMoreThanHalf = m_out.newBlock();
+                LBasicBlock shouldRoundDown = m_out.newBlock();
                 LBasicBlock continuation = m_out.newBlock();
 
                 LValue value = lowDouble(m_node->child1());
@@ -2952,11 +2952,10 @@
                 LValue integerValue = m_out.doubleCeil(value);
                 ValueFromBlock integerValueResult = m_out.anchor(integerValue);
 
-                LValue realPart = m_out.doubleSub(integerValue, value);
+                LValue ceilMinusHalf = m_out.doubleSub(integerValue, m_out.constDouble(0.5));
+                m_out.branch(m_out.doubleGreaterThanOrUnordered(ceilMinusHalf, value), unsure(shouldRoundDown), unsure(continuation));
 
-                m_out.branch(m_out.doubleGreaterThanOrUnordered(realPart, m_out.constDouble(0.5)), unsure(realPartIsMoreThanHalf), unsure(continuation));
-
-                LBasicBlock lastNext = m_out.appendTo(realPartIsMoreThanHalf, continuation);
+                LBasicBlock lastNext = m_out.appendTo(shouldRoundDown, continuation);
                 LValue integerValueRoundedDown = m_out.doubleSub(integerValue, m_out.constDouble(1));
                 ValueFromBlock integerValueRoundedDownResult = m_out.anchor(integerValueRoundedDown);
                 m_out.jump(continuation);

Modified: trunk/Source/_javascript_Core/jit/ThunkGenerators.cpp (249596 => 249597)


--- trunk/Source/_javascript_Core/jit/ThunkGenerators.cpp	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/Source/_javascript_Core/jit/ThunkGenerators.cpp	2019-09-06 22:49:56 UTC (rev 249597)
@@ -895,8 +895,6 @@
 defineUnaryDoubleOpWrapper(floor);
 defineUnaryDoubleOpWrapper(ceil);
 defineUnaryDoubleOpWrapper(trunc);
-
-static const double halfConstant = 0.5;
     
 MacroAssemblerCodeRef<JITThunkPtrTag> floorThunkGenerator(VM& vm)
 {
@@ -997,24 +995,29 @@
     jit.returnInt32(SpecializedThunkJIT::regT0);
     nonIntJump.link(&jit);
     jit.loadDoubleArgument(0, SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::regT0);
-    SpecializedThunkJIT::Jump intResult;
     SpecializedThunkJIT::JumpList doubleResult;
-    if (jit.supportsFloatingPointTruncate()) {
+    if (jit.supportsFloatingPointRounding()) {
         jit.moveZeroToDouble(SpecializedThunkJIT::fpRegT1);
         doubleResult.append(jit.branchDouble(MacroAssembler::DoubleEqual, SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT1));
-        SpecializedThunkJIT::JumpList slowPath;
-        // Handle the negative doubles in the slow path for now.
-        slowPath.append(jit.branchDouble(MacroAssembler::DoubleLessThanOrUnordered, SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT1));
-        jit.loadDouble(MacroAssembler::TrustedImmPtr(&halfConstant), SpecializedThunkJIT::fpRegT1);
-        jit.addDouble(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT1);
-        slowPath.append(jit.branchTruncateDoubleToInt32(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::regT0));
-        intResult = jit.jump();
-        slowPath.link(&jit);
-    }
-    jit.callDoubleToDoublePreservingReturn(UnaryDoubleOpWrapper(jsRound));
+
+        jit.ceilDouble(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::fpRegT1);
+        static const double halfConstant = -0.5;
+        jit.loadDouble(MacroAssembler::TrustedImmPtr(&halfConstant), SpecializedThunkJIT::fpRegT2);
+        jit.addDouble(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::fpRegT2);
+        MacroAssembler::Jump shouldRoundDown = jit.branchDouble(MacroAssembler::DoubleGreaterThan, SpecializedThunkJIT::fpRegT2, SpecializedThunkJIT::fpRegT0);
+
+        jit.moveDouble(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::fpRegT0);
+        MacroAssembler::Jump continuation = jit.jump();
+
+        shouldRoundDown.link(&jit);
+        static const double _oneConstant_ = 1.0;
+        jit.loadDouble(MacroAssembler::TrustedImmPtr(&oneConstant), SpecializedThunkJIT::fpRegT2);
+        jit.subDouble(SpecializedThunkJIT::fpRegT1, SpecializedThunkJIT::fpRegT2, SpecializedThunkJIT::fpRegT0);
+
+        continuation.link(&jit);
+    } else
+        jit.callDoubleToDoublePreservingReturn(UnaryDoubleOpWrapper(jsRound));
     jit.branchConvertDoubleToInt32(SpecializedThunkJIT::fpRegT0, SpecializedThunkJIT::regT0, doubleResult, SpecializedThunkJIT::fpRegT1);
-    if (jit.supportsFloatingPointTruncate())
-        intResult.link(&jit);
     jit.returnInt32(SpecializedThunkJIT::regT0);
     doubleResult.link(&jit);
     jit.returnDouble(SpecializedThunkJIT::fpRegT0);

Modified: trunk/Source/_javascript_Core/runtime/MathCommon.cpp (249596 => 249597)


--- trunk/Source/_javascript_Core/runtime/MathCommon.cpp	2019-09-06 22:34:03 UTC (rev 249596)
+++ trunk/Source/_javascript_Core/runtime/MathCommon.cpp	2019-09-06 22:49:56 UTC (rev 249597)
@@ -479,7 +479,7 @@
 double jsRound(double value)
 {
     double integer = ceil(value);
-    return integer - (integer - value > 0.5);
+    return integer - (integer - 0.5 > value);
 }
 
 #if CALLING_CONVENTION_IS_STDCALL || CPU(ARM_THUMB2)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to