Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: src/math.js

Issue 9065008: Improve performance of Math.min and Math.max for the case of two arguments. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 112
113 // ECMA 262 - 15.8.2.10 113 // ECMA 262 - 15.8.2.10
114 function MathLog(x) { 114 function MathLog(x) {
115 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); 115 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
116 return %_MathLog(x); 116 return %_MathLog(x);
117 } 117 }
118 118
119 // ECMA 262 - 15.8.2.11 119 // ECMA 262 - 15.8.2.11
120 function MathMax(arg1, arg2) { // length == 2 120 function MathMax(arg1, arg2) { // length == 2
121 var length = %_ArgumentsLength(); 121 var length = %_ArgumentsLength();
122 if (length == 2) {
123 if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1);
124 if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2);
125 if (arg2 > arg1) return arg2;
126 if (arg1 > arg2) return arg1;
127 if (arg1 == arg2) {
fschneider 2012/01/03 09:52:06 It does not make a difference for numbers, but I'd
128 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
129 // a Smi or a heap number.
130 return (arg1 === 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg2 : arg1;
Lasse Reichstein 2012/01/03 06:17:45 You can drop the 1/arg1 test, and actually also th
Lasse Reichstein 2012/01/03 07:57:48 Ignore that. If you have +0 as a HeapNumber, that
131 }
132 // All comparisons failed, one of the arguments must be NaN.
133 return 0/0; // Compiler constant-folds this to NaN.
134 }
122 if (length == 0) { 135 if (length == 0) {
123 return -1/0; // Compiler constant-folds this to -Infinity. 136 return -1/0; // Compiler constant-folds this to -Infinity.
124 } 137 }
125 var r = arg1; 138 var r = arg1;
126 if (!IS_NUMBER(r)) r = NonNumberToNumber(r); 139 if (!IS_NUMBER(r)) r = NonNumberToNumber(r);
127 if (NUMBER_IS_NAN(r)) return r; 140 if (NUMBER_IS_NAN(r)) return r;
128 for (var i = 1; i < length; i++) { 141 for (var i = 1; i < length; i++) {
129 var n = %_Arguments(i); 142 var n = %_Arguments(i);
130 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); 143 if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
131 if (NUMBER_IS_NAN(n)) return n; 144 if (NUMBER_IS_NAN(n)) return n;
132 // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be 145 // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be
133 // a Smi or heap number. 146 // a Smi or heap number.
134 if (n > r || (r === 0 && n === 0 && !%_IsSmi(r) && 1 / r < 0)) r = n; 147 if (n > r || (r === 0 && n === 0 && !%_IsSmi(r) && 1 / r < 0)) r = n;
135 } 148 }
136 return r; 149 return r;
137 } 150 }
138 151
139 // ECMA 262 - 15.8.2.12 152 // ECMA 262 - 15.8.2.12
140 function MathMin(arg1, arg2) { // length == 2 153 function MathMin(arg1, arg2) { // length == 2
141 var length = %_ArgumentsLength(); 154 var length = %_ArgumentsLength();
155 if (length == 2) {
156 if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1);
157 if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2);
158 if (arg2 > arg1) return arg1;
159 if (arg1 > arg2) return arg2;
160 if (arg1 == arg2) {
161 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be
162 // a Smi or a heap number.
163 return (arg1 === 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg1 : arg2;
Lasse Reichstein 2012/01/03 06:17:45 Here you want to return the non-smi if the values
Lasse Reichstein 2012/01/03 07:57:48 Same problem here if +0 and -0 both are non-smi, s
164 }
165 // All comparisons failed, one of the arguments must be NaN.
166 return 0/0; // Compiler constant-folds this to NaN.
167 }
142 if (length == 0) { 168 if (length == 0) {
143 return 1/0; // Compiler constant-folds this to Infinity. 169 return 1/0; // Compiler constant-folds this to Infinity.
144 } 170 }
145 var r = arg1; 171 var r = arg1;
146 if (!IS_NUMBER(r)) r = NonNumberToNumber(r); 172 if (!IS_NUMBER(r)) r = NonNumberToNumber(r);
147 if (NUMBER_IS_NAN(r)) return r; 173 if (NUMBER_IS_NAN(r)) return r;
148 for (var i = 1; i < length; i++) { 174 for (var i = 1; i < length; i++) {
149 var n = %_Arguments(i); 175 var n = %_Arguments(i);
150 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); 176 if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
151 if (NUMBER_IS_NAN(n)) return n; 177 if (NUMBER_IS_NAN(n)) return n;
152 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can b a 178 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be a
153 // Smi or a heap number. 179 // Smi or a heap number.
154 if (n < r || (r === 0 && n === 0 && !%_IsSmi(n) && 1 / n < 0)) r = n; 180 if (n < r || (r === 0 && n === 0 && !%_IsSmi(n) && 1 / n < 0)) r = n;
155 } 181 }
156 return r; 182 return r;
157 } 183 }
158 184
159 // ECMA 262 - 15.8.2.13 185 // ECMA 262 - 15.8.2.13
160 function MathPow(x, y) { 186 function MathPow(x, y) {
161 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); 187 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
162 if (!IS_NUMBER(y)) y = NonNumberToNumber(y); 188 if (!IS_NUMBER(y)) y = NonNumberToNumber(y);
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 "sqrt", MathSqrt, 281 "sqrt", MathSqrt,
256 "tan", MathTan, 282 "tan", MathTan,
257 "atan2", MathAtan2, 283 "atan2", MathAtan2,
258 "pow", MathPow, 284 "pow", MathPow,
259 "max", MathMax, 285 "max", MathMax,
260 "min", MathMin 286 "min", MathMin
261 )); 287 ));
262 } 288 }
263 289
264 SetUpMath(); 290 SetUpMath();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698