Reviewers: Jakob,

Message:
Based on https://chromiumcodereview.appspot.com/10168001.

PTAL.

Description:
Port r11517 (not deopt Math.floor on negative input) to x64, sse2.


BUG=v8:873
TEST=math-floor-negative.js


Please review this at http://codereview.chromium.org/10636057/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/ia32/lithium-codegen-ia32.cc
  M src/x64/assembler-x64.h
  M src/x64/lithium-codegen-x64.cc
  A + test/mjsunit/math-floor-negative.js


Index: src/ia32/lithium-codegen-ia32.cc
diff --git a/src/ia32/lithium-codegen-ia32.cc b/src/ia32/lithium-codegen-ia32.cc index 5225763a3868219684ec8f454d39c0ab17fc7785..e27a32628ea69ed015cb984d276b056847dfb9af 100644
--- a/src/ia32/lithium-codegen-ia32.cc
+++ b/src/ia32/lithium-codegen-ia32.cc
@@ -3169,8 +3169,7 @@ void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
     __ cmp(output_reg, 0x80000000u);
     DeoptimizeIf(equal, instr->environment());
   } else {
-    Label negative_sign;
-    Label done;
+    Label negative_sign, done;
     // Deoptimize on unordered.
     __ xorps(xmm_scratch, xmm_scratch);  // Zero the register.
     __ ucomisd(input_reg, xmm_scratch);
@@ -3196,9 +3195,9 @@ void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
     DeoptimizeIf(equal, instr->environment());
     __ jmp(&done, Label::kNear);

-    // Non-zero negative reaches here
+    // Non-zero negative reaches here.
     __ bind(&negative_sign);
-    // Truncate, then compare and compensate
+    // Truncate, then compare and compensate.
     __ cvttsd2si(output_reg, Operand(input_reg));
     __ cvtsi2sd(xmm_scratch, output_reg);
     __ ucomisd(input_reg, xmm_scratch);
Index: src/x64/assembler-x64.h
diff --git a/src/x64/assembler-x64.h b/src/x64/assembler-x64.h
index 9f5f850294141dd852544b26e098b40367c4fda8..3c1b9614b9c0ec382af74245b6787021271b00e0 100644
--- a/src/x64/assembler-x64.h
+++ b/src/x64/assembler-x64.h
@@ -455,6 +455,7 @@ class CpuFeatures : public AllStatic {
     ASSERT(initialized_);
     if (f == SSE2 && !FLAG_enable_sse2) return false;
     if (f == SSE3 && !FLAG_enable_sse3) return false;
+    if (f == SSE4_1 && !FLAG_enable_sse4_1) return false;
     if (f == CMOV && !FLAG_enable_cmov) return false;
     if (f == RDTSC && !FLAG_enable_rdtsc) return false;
     if (f == SAHF && !FLAG_enable_sahf) return false;
Index: src/x64/lithium-codegen-x64.cc
diff --git a/src/x64/lithium-codegen-x64.cc b/src/x64/lithium-codegen-x64.cc
index 57db277577ef3d9ca3258731893664add185809b..808c4d87355f3626d5829df45f62745d893f236c 100644
--- a/src/x64/lithium-codegen-x64.cc
+++ b/src/x64/lithium-codegen-x64.cc
@@ -3031,7 +3031,6 @@ void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
   XMMRegister xmm_scratch = xmm0;
   Register output_reg = ToRegister(instr->result());
   XMMRegister input_reg = ToDoubleRegister(instr->InputAt(0));
-  Label done;

   if (CpuFeatures::IsSupported(SSE4_1)) {
     CpuFeatures::Scope scope(SSE4_1);
@@ -3046,10 +3045,13 @@ void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {
     __ cmpl(output_reg, Immediate(0x80000000));
     DeoptimizeIf(equal, instr->environment());
   } else {
+    Label negative_sign, done;
     // Deoptimize on negative inputs.
     __ xorps(xmm_scratch, xmm_scratch);  // Zero the register.
     __ ucomisd(input_reg, xmm_scratch);
-    DeoptimizeIf(below, instr->environment());
+    DeoptimizeIf(parity_even, instr->environment());
+    __ j(below, &negative_sign, Label::kNear);
+
     if (instr->hydrogen()->CheckFlag(HValue::kBailoutOnMinusZero)) {
       // Check for negative zero.
       Label positive_sign;
@@ -3064,12 +3066,23 @@ void LCodeGen::DoMathFloor(LUnaryMathOperation* instr) {

     // Use truncating instruction (OK because input is positive).
     __ cvttsd2si(output_reg, input_reg);
-
     // Overflow is signalled with minint.
     __ cmpl(output_reg, Immediate(0x80000000));
     DeoptimizeIf(equal, instr->environment());
+    __ jmp(&done, Label::kNear);
+
+    // Non-zero negative reaches here.
+    __ bind(&negative_sign);
+    // Truncate, then compare and compensate.
+    __ cvttsd2si(output_reg, input_reg);
+    __ cvtlsi2sd(xmm_scratch, output_reg);
+    __ ucomisd(input_reg, xmm_scratch);
+    __ j(equal, &done, Label::kNear);
+    __ subl(output_reg, Immediate(1));
+    DeoptimizeIf(overflow, instr->environment());
+
+    __ bind(&done);
   }
-  __ bind(&done);
 }


Index: test/mjsunit/math-floor-negative.js
diff --git a/test/mjsunit/regress/regress-2110.js b/test/mjsunit/math-floor-negative.js
similarity index 70%
copy from test/mjsunit/regress/regress-2110.js
copy to test/mjsunit/math-floor-negative.js
index d7f78d26a7b4d16c4217d2b0ac136e52e4542eb1..4cabff577eafc1f7eb435f0e4b5ae3db27d1e0ae 100644
--- a/test/mjsunit/regress/regress-2110.js
+++ b/test/mjsunit/math-floor-negative.js
@@ -25,29 +25,35 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

-// Flags: --allow-natives-syntax
+// Flags: --noenable_sse4_1 --allow-natives-syntax

-var uint8 = new Uint8Array(1);
-
-function test() {
-  uint8[0] = 0x800000aa;
-  assertEquals(0xaa, uint8[0]);
+function test1() {
+  // Trigger overflow when converting/truncating double to integer.
+  // Divide by 10 to avoid overflow when smi-tagging at the end.
+  return Math.floor(-100000000000.5) / 10;
 }

-test();
-test();
-test();
-%OptimizeFunctionOnNextCall(test);
-test();
-
-var uint32 = new Uint32Array(1);
-
 function test2() {
-  uint32[0] = 0x80123456789abcde;
-  assertEquals(0x789ac000, uint32[0]);
+  // Trigger no overflow.
+  return Math.floor(-100.2);
 }

+function test3() {
+  // Trigger overflow when compensating by subtracting after compare.
+  // Divide by 10 to avoid overflow when smi-tagging at the end.
+  return Math.floor(-2147483648.1) / 10;
+}
+
+test1();
+test1();
+%OptimizeFunctionOnNextCall(test1);
 test2();
 test2();
 %OptimizeFunctionOnNextCall(test2);
-test2();
+test3();
+test3();
+%OptimizeFunctionOnNextCall(test3);
+
+assertEquals(-10000000000.1, test1());
+assertEquals(-101, test2());
+assertEquals(-214748364.9, test3());


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to