Improve performance of Math.min and Math.max for the case of two arguments.

BUG=v8:1325
TEST=

Review URL: http://codereview.chromium.org/9065008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@10321 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
yangguo@chromium.org 2012-01-03 10:45:28 +00:00
parent 79f18cae93
commit fa2688a101

View File

@ -1,4 +1,4 @@
// Copyright 2006-2008 the V8 project authors. All rights reserved. // Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are // modification, are permitted provided that the following conditions are
// met: // met:
@ -119,6 +119,19 @@ function MathLog(x) {
// ECMA 262 - 15.8.2.11 // ECMA 262 - 15.8.2.11
function MathMax(arg1, arg2) { // length == 2 function MathMax(arg1, arg2) { // length == 2
var length = %_ArgumentsLength(); var length = %_ArgumentsLength();
if (length == 2) {
if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1);
if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2);
if (arg2 > arg1) return arg2;
if (arg1 > arg2) return arg1;
if (arg1 == arg2) {
// Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
// a Smi or a heap number.
return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg2 : arg1;
}
// All comparisons failed, one of the arguments must be NaN.
return 0/0; // Compiler constant-folds this to NaN.
}
if (length == 0) { if (length == 0) {
return -1/0; // Compiler constant-folds this to -Infinity. return -1/0; // Compiler constant-folds this to -Infinity.
} }
@ -131,7 +144,7 @@ function MathMax(arg1, arg2) { // length == 2
if (NUMBER_IS_NAN(n)) return n; if (NUMBER_IS_NAN(n)) return n;
// Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be
// a Smi or heap number. // a Smi or heap number.
if (n > r || (r === 0 && n === 0 && !%_IsSmi(r) && 1 / r < 0)) r = n; if (n > r || (r == 0 && n == 0 && !%_IsSmi(r) && 1 / r < 0)) r = n;
} }
return r; return r;
} }
@ -139,6 +152,19 @@ function MathMax(arg1, arg2) { // length == 2
// ECMA 262 - 15.8.2.12 // ECMA 262 - 15.8.2.12
function MathMin(arg1, arg2) { // length == 2 function MathMin(arg1, arg2) { // length == 2
var length = %_ArgumentsLength(); var length = %_ArgumentsLength();
if (length == 2) {
if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1);
if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2);
if (arg2 > arg1) return arg1;
if (arg1 > arg2) return arg2;
if (arg1 == arg2) {
// Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
// a Smi or a heap number.
return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg1 : arg2;
}
// All comparisons failed, one of the arguments must be NaN.
return 0/0; // Compiler constant-folds this to NaN.
}
if (length == 0) { if (length == 0) {
return 1/0; // Compiler constant-folds this to Infinity. return 1/0; // Compiler constant-folds this to Infinity.
} }
@ -149,9 +175,9 @@ function MathMin(arg1, arg2) { // length == 2
var n = %_Arguments(i); var n = %_Arguments(i);
if (!IS_NUMBER(n)) n = NonNumberToNumber(n); if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
if (NUMBER_IS_NAN(n)) return n; if (NUMBER_IS_NAN(n)) return n;
// Make sure -0 is considered less than +0. -0 is never a Smi, +0 can b a // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be a
// Smi or a heap number. // Smi or a heap number.
if (n < r || (r === 0 && n === 0 && !%_IsSmi(n) && 1 / n < 0)) r = n; if (n < r || (r == 0 && n == 0 && !%_IsSmi(n) && 1 / n < 0)) r = n;
} }
return r; return r;
} }