Commit f36f5f23 authored by kasperl@chromium.org's avatar kasperl@chromium.org

Optimize the slow case implementations of bit operations by

avoiding excessive ToNumber calls and by dealing with NaNs
in BIT_AND and SAR.
Review URL: http://codereview.chromium.org/125118

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@2163 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 3a3a6924
...@@ -97,12 +97,12 @@ function STRICT_EQUALS(x) { ...@@ -97,12 +97,12 @@ function STRICT_EQUALS(x) {
if (IS_STRING(this)) { if (IS_STRING(this)) {
if (!IS_STRING(x)) return 1; // not equal if (!IS_STRING(x)) return 1; // not equal
return %StringEquals(this, x); return %StringEquals(this, x);
} }
if (IS_NUMBER(this)) { if (IS_NUMBER(this)) {
if (!IS_NUMBER(x)) return 1; // not equal if (!IS_NUMBER(x)) return 1; // not equal
return %NumberEquals(this, x); return %NumberEquals(this, x);
} }
// If anything else gets here, we just do simple identity check. // If anything else gets here, we just do simple identity check.
// Objects (including functions), null, undefined and booleans were // Objects (including functions), null, undefined and booleans were
...@@ -148,7 +148,7 @@ function ADD(x) { ...@@ -148,7 +148,7 @@ function ADD(x) {
// Default implementation. // Default implementation.
var a = %ToPrimitive(this, NO_HINT); var a = %ToPrimitive(this, NO_HINT);
var b = %ToPrimitive(x, NO_HINT); var b = %ToPrimitive(x, NO_HINT);
if (IS_STRING(a)) { if (IS_STRING(a)) {
return %StringAdd(a, %ToString(b)); return %StringAdd(a, %ToString(b));
} else if (IS_STRING(b)) { } else if (IS_STRING(b)) {
...@@ -160,40 +160,48 @@ function ADD(x) { ...@@ -160,40 +160,48 @@ function ADD(x) {
// Left operand (this) is already a string. // Left operand (this) is already a string.
function STRING_ADD_LEFT(x) { function STRING_ADD_LEFT(y) {
x = %ToString(%ToPrimitive(x, NO_HINT)); if (!IS_STRING(y)) y = %ToString(%ToPrimitive(y, NO_HINT));
return %StringAdd(this, x); return %StringAdd(this, y);
} }
// Right operand (x) is already a string. // Right operand (y) is already a string.
function STRING_ADD_RIGHT(x) { function STRING_ADD_RIGHT(y) {
var a = %ToString(%ToPrimitive(this, NO_HINT)); var x = IS_STRING(this) ? this : %ToString(%ToPrimitive(this, NO_HINT));
return %StringAdd(a, x); return %StringAdd(x, y);
} }
// ECMA-262, section 11.6.2, page 50. // ECMA-262, section 11.6.2, page 50.
function SUB(x) { function SUB(y) {
return %NumberSub(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberSub(x, y);
} }
// ECMA-262, section 11.5.1, page 48. // ECMA-262, section 11.5.1, page 48.
function MUL(x) { function MUL(y) {
return %NumberMul(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberMul(x, y);
} }
// ECMA-262, section 11.5.2, page 49. // ECMA-262, section 11.5.2, page 49.
function DIV(x) { function DIV(y) {
return %NumberDiv(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberDiv(x, y);
} }
// ECMA-262, section 11.5.3, page 49. // ECMA-262, section 11.5.3, page 49.
function MOD(x) { function MOD(y) {
return %NumberMod(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberMod(x, y);
} }
...@@ -204,50 +212,82 @@ function MOD(x) { ...@@ -204,50 +212,82 @@ function MOD(x) {
*/ */
// ECMA-262, section 11.10, page 57. // ECMA-262, section 11.10, page 57.
function BIT_OR(x) { function BIT_OR(y) {
return %NumberOr(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberOr(x, y);
} }
// ECMA-262, section 11.10, page 57. // ECMA-262, section 11.10, page 57.
function BIT_AND(x) { function BIT_AND(y) {
return %NumberAnd(%ToNumber(this), %ToNumber(x)); var x;
if (IS_NUMBER(this)) {
x = this;
} else {
x = %ToNumber(this);
// Optimize for the case where we end up AND'ing a value
// that doesn't convert to a number. This is common in
// certain benchmarks.
if (NUMBER_IS_NAN(x)) return 0;
}
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberAnd(x, y);
} }
// ECMA-262, section 11.10, page 57. // ECMA-262, section 11.10, page 57.
function BIT_XOR(x) { function BIT_XOR(y) {
return %NumberXor(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberXor(x, y);
} }
// ECMA-262, section 11.4.7, page 47. // ECMA-262, section 11.4.7, page 47.
function UNARY_MINUS() { function UNARY_MINUS() {
return %NumberUnaryMinus(%ToNumber(this)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
return %NumberUnaryMinus(x);
} }
// ECMA-262, section 11.4.8, page 48. // ECMA-262, section 11.4.8, page 48.
function BIT_NOT() { function BIT_NOT() {
return %NumberNot(%ToNumber(this)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
return %NumberNot(x);
} }
// ECMA-262, section 11.7.1, page 51. // ECMA-262, section 11.7.1, page 51.
function SHL(x) { function SHL(y) {
return %NumberShl(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberShl(x, y);
} }
// ECMA-262, section 11.7.2, page 51. // ECMA-262, section 11.7.2, page 51.
function SAR(x) { function SAR(y) {
return %NumberSar(%ToNumber(this), %ToNumber(x)); var x;
if (IS_NUMBER(this)) {
x = this;
} else {
x = %ToNumber(this);
// Optimize for the case where we end up shifting a value
// that doesn't convert to a number. This is common in
// certain benchmarks.
if (NUMBER_IS_NAN(x)) return 0;
}
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberSar(x, y);
} }
// ECMA-262, section 11.7.3, page 52. // ECMA-262, section 11.7.3, page 52.
function SHR(x) { function SHR(y) {
return %NumberShr(%ToNumber(this), %ToNumber(x)); var x = IS_NUMBER(this) ? this : %ToNumber(this);
if (!IS_NUMBER(y)) y = %ToNumber(y);
return %NumberShr(x, y);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment