Index: src/mips/lithium-codegen-mips.cc |
diff --git a/src/mips/lithium-codegen-mips.cc b/src/mips/lithium-codegen-mips.cc |
index e306c174cf7b62b0168ccfc6d249cc6359787969..5e47665c0c3c4298e33d898341f41f50642d00bc 100644 |
--- a/src/mips/lithium-codegen-mips.cc |
+++ b/src/mips/lithium-codegen-mips.cc |
@@ -1367,8 +1367,31 @@ void LCodeGen::DoFlooringDivByConstI(LFlooringDivByConstI* instr) { |
DeoptimizeIf(eq, instr->environment(), dividend, Operand(zero_reg)); |
} |
- // TODO(svenpanne) Add correction terms. |
- __ TruncatingDiv(result, 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(result, dividend, Abs(divisor)); |
+ if (divisor < 0) __ Subu(result, zero_reg, result); |
+ 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->temp()); |
+ ASSERT(!temp.is(dividend) && !temp.is(result)); |
+ Label needs_adjustment, done; |
+ __ Branch(&needs_adjustment, divisor > 0 ? lt : gt, |
+ dividend, Operand(zero_reg)); |
+ __ TruncatingDiv(result, dividend, Abs(divisor)); |
+ if (divisor < 0) __ Subu(result, zero_reg, result); |
+ __ jmp(&done); |
+ __ bind(&needs_adjustment); |
+ __ Addu(temp, dividend, Operand(divisor > 0 ? 1 : -1)); |
+ __ TruncatingDiv(result, temp, Abs(divisor)); |
+ if (divisor < 0) __ Subu(result, zero_reg, result); |
+ __ Subu(result, result, Operand(1)); |
+ __ bind(&done); |
} |