Bug 607711 - Constant fold JSOP_MOD. r=dvander

This commit is contained in:
Jan de Mooij 2010-10-28 11:29:55 +02:00
parent f83bd5ee73
commit 966e4e4247
4 changed files with 37 additions and 3 deletions

View File

@ -0,0 +1,8 @@
function f() {
var x = 2.6;
var y = 2.1;
return x % y;
}
assertEq(f(), 0.5);

View File

@ -0,0 +1,8 @@
function f() {
var i = 1000;
var rest = i % 3;
var div = (i - rest) / 3;
assertEq(div, 333);
}
f();

View File

@ -0,0 +1,8 @@
function f() {
var x = 5;
var y = 0;
return x % y;
}
assertEq(f(), NaN);

View File

@ -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