Index: src/x64/lithium-codegen-x64.cc |
diff --git a/src/x64/lithium-codegen-x64.cc b/src/x64/lithium-codegen-x64.cc |
index 49068e7fc7fce47392642fcdac98c5c419b1e0a6..d1c893dc88c39920ba1f7ba60e0d0aa619bfc6ef 100644 |
--- a/src/x64/lithium-codegen-x64.cc |
+++ b/src/x64/lithium-codegen-x64.cc |
@@ -1160,8 +1160,31 @@ void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) { |
DeoptimizeIf(zero, instr->environment()); |
} |
- // TODO(svenpanne) Add correction terms. |
- __ TruncatingDiv(dividend, divisor); |
+ // Easy case: We need no dynamic check for the dividend and the flooring |
+ // division is the same as the truncating division. |
+ if ((divisor > 0 && !hdiv->CheckFlag(HValue::kLeftCanBeNegative)) || |
+ (divisor < 0 && !hdiv->CheckFlag(HValue::kLeftCanBePositive))) { |
+ __ TruncatingDiv(dividend, Abs(divisor)); |
+ if (divisor < 0) __ negl(rdx); |
+ return; |
+ } |
+ |
+ // In the general case we may need to adjust before and after the truncating |
+ // division to get a flooring division. |
+ Register temp = ToRegister(instr->temp3()); |
+ ASSERT(!temp.is(dividend) && !temp.is(rax) && !temp.is(rdx)); |
+ Label needs_adjustment, done; |
+ __ cmpl(dividend, Immediate(0)); |
+ __ j(divisor > 0 ? less : greater, &needs_adjustment, Label::kNear); |
+ __ TruncatingDiv(dividend, Abs(divisor)); |
+ if (divisor < 0) __ negl(rdx); |
+ __ jmp(&done, Label::kNear); |
+ __ bind(&needs_adjustment); |
+ __ leal(temp, Operand(dividend, divisor > 0 ? 1 : -1)); |
+ __ TruncatingDiv(temp, Abs(divisor)); |
+ if (divisor < 0) __ negl(rdx); |
+ __ decl(rdx); |
+ __ bind(&done); |
} |