diff --git a/js/src/jit-test/tests/jaeger/modConstDoubles.js b/js/src/jit-test/tests/jaeger/modConstDoubles.js new file mode 100644 index 000000000000..807782ce6e4d --- /dev/null +++ b/js/src/jit-test/tests/jaeger/modConstDoubles.js @@ -0,0 +1,8 @@ + +function f() { + var x = 2.6; + var y = 2.1; + return x % y; +} +assertEq(f(), 0.5); + diff --git a/js/src/jit-test/tests/jaeger/modConstInt.js b/js/src/jit-test/tests/jaeger/modConstInt.js new file mode 100644 index 000000000000..cdb0711da3c8 --- /dev/null +++ b/js/src/jit-test/tests/jaeger/modConstInt.js @@ -0,0 +1,8 @@ + +function f() { + var i = 1000; + var rest = i % 3; + var div = (i - rest) / 3; + assertEq(div, 333); +} +f(); \ No newline at end of file diff --git a/js/src/jit-test/tests/jaeger/modConstZeroRhs.js b/js/src/jit-test/tests/jaeger/modConstZeroRhs.js new file mode 100644 index 000000000000..75ef884100bd --- /dev/null +++ b/js/src/jit-test/tests/jaeger/modConstZeroRhs.js @@ -0,0 +1,8 @@ + +function f() { + var x = 5; + var y = 0; + return x % y; +} +assertEq(f(), NaN); + diff --git a/js/src/methodjit/FastArithmetic.cpp b/js/src/methodjit/FastArithmetic.cpp index ff46433e5138..5daba7cab607 100644 --- a/js/src/methodjit/FastArithmetic.cpp +++ b/js/src/methodjit/FastArithmetic.cpp @@ -73,10 +73,14 @@ mjit::Compiler::tryBinaryConstantFold(JSContext *cx, FrameState &frame, JSOp op, case JSOP_SUB: case JSOP_MUL: case JSOP_DIV: - case JSOP_MOD: needInt = false; break; + case JSOP_MOD: + needInt = (L.isInt32() && R.isInt32() && + L.toInt32() >= 0 && R.toInt32() > 0); + break; + case JSOP_RSH: needInt = true; break; @@ -129,10 +133,12 @@ mjit::Compiler::tryBinaryConstantFold(JSContext *cx, FrameState &frame, JSOp op, } break; case JSOP_MOD: - if (dL == 0) + if (needInt) + nL %= nR; + else if (dR == 0) dL = js_NaN; else - dL = js_fmod(dR, dL); + dL = js_fmod(dL, dR); break; case JSOP_RSH: @@ -828,6 +834,10 @@ mjit::Compiler::jsop_mod() #if defined(JS_CPU_X86) FrameEntry *lhs = frame.peek(-2); FrameEntry *rhs = frame.peek(-1); + + if (tryBinaryConstantFold(cx, frame, JSOP_MOD, lhs, rhs)) + return; + if ((lhs->isTypeKnown() && lhs->getKnownType() != JSVAL_TYPE_INT32) || (rhs->isTypeKnown() && rhs->getKnownType() != JSVAL_TYPE_INT32)) #endif