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

Side by Side Diff: src/harmony-math.js

Issue 183743018: Harmony: implement Math.cbrt in Javascript. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 9 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 | src/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 // Binary search. 167 // Binary search.
168 if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; }; 168 if ((x & 0xFFFF0000) === 0) { x <<= 16; result += 16; };
169 if ((x & 0xFF000000) === 0) { x <<= 8; result += 8; }; 169 if ((x & 0xFF000000) === 0) { x <<= 8; result += 8; };
170 if ((x & 0xF0000000) === 0) { x <<= 4; result += 4; }; 170 if ((x & 0xF0000000) === 0) { x <<= 4; result += 4; };
171 if ((x & 0xC0000000) === 0) { x <<= 2; result += 2; }; 171 if ((x & 0xC0000000) === 0) { x <<= 2; result += 2; };
172 if ((x & 0x80000000) === 0) { x <<= 1; result += 1; }; 172 if ((x & 0x80000000) === 0) { x <<= 1; result += 1; };
173 return result; 173 return result;
174 } 174 }
175 175
176 176
177 //ES6 draft 09-27-13, section 20.2.2.9. 177 // ES6 draft 09-27-13, section 20.2.2.9.
178 // Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm
179 // Using initial approximation adapted from Kahan's cbrt and 4 iterations
180 // of Newton's method.
178 function MathCbrt(x) { 181 function MathCbrt(x) {
179 return %Math_cbrt(TO_NUMBER_INLINE(x)); 182 if (!IS_NUMBER(x)) x = NonNumberToNumber(x);
183 if (x == 0 || !NUMBER_IS_FINITE(x)) return x;
184 return x >= 0 ? CubeRoot(x) : -CubeRoot(-x);
185 }
186
187 macro NEWTON_ITERATION_CBRT(x, approx)
188 (1.0 / 3.0) * (x / (approx * approx) + 2 * approx);
189 endmacro
190
191 function CubeRoot(x) {
192 var approx_hi = MathFloor(%_DoubleHi(x) / 3) + 0x2A9F7893;
193 var approx = %_ConstructDouble(approx_hi, 0);
194 approx = NEWTON_ITERATION_CBRT(x, approx);
195 approx = NEWTON_ITERATION_CBRT(x, approx);
196 approx = NEWTON_ITERATION_CBRT(x, approx);
197 return NEWTON_ITERATION_CBRT(x, approx);
180 } 198 }
181 199
182 200
201 function NewtonIterationCbrt(x, approx) {
202 return (1.0 / 3.0) * (x / (approx * approx) + 2 * approx);
203 }
204
205
183 //ES6 draft 09-27-13, section 20.2.2.14. 206 //ES6 draft 09-27-13, section 20.2.2.14.
184 function MathExpm1(x) { 207 function MathExpm1(x) {
185 return %Math_expm1(TO_NUMBER_INLINE(x)); 208 return %Math_expm1(TO_NUMBER_INLINE(x));
186 } 209 }
187 210
188 211
189 //ES6 draft 09-27-13, section 20.2.2.20. 212 //ES6 draft 09-27-13, section 20.2.2.20.
190 function MathLog1p(x) { 213 function MathLog1p(x) {
191 return %Math_log1p(TO_NUMBER_INLINE(x)); 214 return %Math_log1p(TO_NUMBER_INLINE(x));
192 } 215 }
(...skipping 18 matching lines...) Expand all
211 "fround", MathFround, 234 "fround", MathFround,
212 "clz32", MathClz32, 235 "clz32", MathClz32,
213 "cbrt", MathCbrt, 236 "cbrt", MathCbrt,
214 "log1p", MathLog1p, 237 "log1p", MathLog1p,
215 "expm1", MathExpm1 238 "expm1", MathExpm1
216 )); 239 ));
217 } 240 }
218 241
219 242
220 ExtendMath(); 243 ExtendMath();
OLDNEW
« no previous file with comments | « no previous file | src/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698