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

Side by Side Diff: src/math.js

Issue 990883002: Hide Math function implementations in a closure. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: remove printf and unnecessary test case change. Created 5 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
« no previous file with comments | « src/macros.py ('k') | src/third_party/fdlibm/fdlibm.js » ('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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 "use strict"; 5 "use strict";
6 6
7 // This file relies on the fact that the following declarations have been made 7 // This file relies on the fact that the following declarations have been made
8 // in runtime.js: 8 // in runtime.js:
9 // var $Object = global.Object; 9 // var $Object = global.Object;
10 10
11 // Keep reference to original values of some global properties. This
12 // has the added benefit that the code in this file is isolated from
13 // changes to these properties.
14 var $floor = MathFloor;
15 var $abs = MathAbs;
16
17 // Instance class name can only be set on functions. That is the only 11 // Instance class name can only be set on functions. That is the only
18 // purpose for MathConstructor. 12 // purpose for MathConstructor.
19 function MathConstructor() {} 13 function MathConstructor() {}
20 var $Math = new MathConstructor(); 14 var $Math = new MathConstructor();
21 15
16 var rngstate; // Initialized to a Uint32Array during genesis.
17
18 var $abs;
19 var $exp;
20 var $floor;
21 var $max;
22 var $min;
23
22 // ------------------------------------------------------------------- 24 // -------------------------------------------------------------------
23 25
26 (function() {
27
24 // ECMA 262 - 15.8.2.1 28 // ECMA 262 - 15.8.2.1
25 function MathAbs(x) { 29 function MathAbs(x) {
26 if (%_IsSmi(x)) return x >= 0 ? x : -x; 30 if (%_IsSmi(x)) return x >= 0 ? x : -x;
27 x = TO_NUMBER_INLINE(x); 31 x = TO_NUMBER_INLINE(x);
28 if (x === 0) return 0; // To handle -0. 32 if (x === 0) return 0; // To handle -0.
29 return x > 0 ? x : -x; 33 return x > 0 ? x : -x;
30 } 34 }
31 35
32 // ECMA 262 - 15.8.2.2 36 // ECMA 262 - 15.8.2.2
33 function MathAcosJS(x) { 37 function MathAcosJS(x) {
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 } 139 }
136 return r; 140 return r;
137 } 141 }
138 142
139 // ECMA 262 - 15.8.2.13 143 // ECMA 262 - 15.8.2.13
140 function MathPow(x, y) { 144 function MathPow(x, y) {
141 return %_MathPow(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y)); 145 return %_MathPow(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y));
142 } 146 }
143 147
144 // ECMA 262 - 15.8.2.14 148 // ECMA 262 - 15.8.2.14
145 var rngstate; // Initialized to a Uint32Array during genesis.
146 function MathRandom() { 149 function MathRandom() {
147 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0; 150 var r0 = (MathImul(18030, rngstate[0] & 0xFFFF) + (rngstate[0] >>> 16)) | 0;
148 rngstate[0] = r0; 151 rngstate[0] = r0;
149 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0; 152 var r1 = (MathImul(36969, rngstate[1] & 0xFFFF) + (rngstate[1] >>> 16)) | 0;
150 rngstate[1] = r1; 153 rngstate[1] = r1;
151 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0; 154 var x = ((r0 << 16) + (r1 & 0xFFFF)) | 0;
152 // Division by 0x100000000 through multiplication by reciprocal. 155 // Division by 0x100000000 through multiplication by reciprocal.
153 return (x < 0 ? (x + 0x100000000) : x) * 2.3283064365386962890625e-10; 156 return (x < 0 ? (x + 0x100000000) : x) * 2.3283064365386962890625e-10;
154 } 157 }
155 158
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 var approx_hi = MathFloor(%_DoubleHi(x) / 3) + 0x2A9F7893; 299 var approx_hi = MathFloor(%_DoubleHi(x) / 3) + 0x2A9F7893;
297 var approx = %_ConstructDouble(approx_hi, 0); 300 var approx = %_ConstructDouble(approx_hi, 0);
298 approx = NEWTON_ITERATION_CBRT(x, approx); 301 approx = NEWTON_ITERATION_CBRT(x, approx);
299 approx = NEWTON_ITERATION_CBRT(x, approx); 302 approx = NEWTON_ITERATION_CBRT(x, approx);
300 approx = NEWTON_ITERATION_CBRT(x, approx); 303 approx = NEWTON_ITERATION_CBRT(x, approx);
301 return NEWTON_ITERATION_CBRT(x, approx); 304 return NEWTON_ITERATION_CBRT(x, approx);
302 } 305 }
303 306
304 // ------------------------------------------------------------------- 307 // -------------------------------------------------------------------
305 308
306 function SetUpMath() { 309 %CheckIsBootstrapping();
307 %CheckIsBootstrapping();
308 310
309 %InternalSetPrototype($Math, $Object.prototype); 311 %InternalSetPrototype($Math, $Object.prototype);
310 %AddNamedProperty(global, "Math", $Math, DONT_ENUM); 312 %AddNamedProperty(global, "Math", $Math, DONT_ENUM);
311 %FunctionSetInstanceClassName(MathConstructor, 'Math'); 313 %FunctionSetInstanceClassName(MathConstructor, 'Math');
312 314
313 %AddNamedProperty($Math, symbolToStringTag, "Math", READ_ONLY | DONT_ENUM); 315 %AddNamedProperty($Math, symbolToStringTag, "Math", READ_ONLY | DONT_ENUM);
314 316
315 // Set up math constants. 317 // Set up math constants.
316 InstallConstants($Math, $Array( 318 InstallConstants($Math, $Array(
317 // ECMA-262, section 15.8.1.1. 319 // ECMA-262, section 15.8.1.1.
318 "E", 2.7182818284590452354, 320 "E", 2.7182818284590452354,
319 // ECMA-262, section 15.8.1.2. 321 // ECMA-262, section 15.8.1.2.
320 "LN10", 2.302585092994046, 322 "LN10", 2.302585092994046,
321 // ECMA-262, section 15.8.1.3. 323 // ECMA-262, section 15.8.1.3.
322 "LN2", 0.6931471805599453, 324 "LN2", 0.6931471805599453,
323 // ECMA-262, section 15.8.1.4. 325 // ECMA-262, section 15.8.1.4.
324 "LOG2E", 1.4426950408889634, 326 "LOG2E", 1.4426950408889634,
325 "LOG10E", 0.4342944819032518, 327 "LOG10E", 0.4342944819032518,
326 "PI", 3.1415926535897932, 328 "PI", 3.1415926535897932,
327 "SQRT1_2", 0.7071067811865476, 329 "SQRT1_2", 0.7071067811865476,
328 "SQRT2", 1.4142135623730951 330 "SQRT2", 1.4142135623730951
329 )); 331 ));
330 332
331 // Set up non-enumerable functions of the Math object and 333 // Set up non-enumerable functions of the Math object and
332 // set their names. 334 // set their names.
333 InstallFunctions($Math, DONT_ENUM, $Array( 335 InstallFunctions($Math, DONT_ENUM, $Array(
334 "random", MathRandom, 336 "random", MathRandom,
335 "abs", MathAbs, 337 "abs", MathAbs,
336 "acos", MathAcosJS, 338 "acos", MathAcosJS,
337 "asin", MathAsinJS, 339 "asin", MathAsinJS,
338 "atan", MathAtanJS, 340 "atan", MathAtanJS,
339 "ceil", MathCeil, 341 "ceil", MathCeil,
340 "cos", MathCos, // implemented by third_party/fdlibm 342 "exp", MathExp,
341 "exp", MathExp, 343 "floor", MathFloor,
342 "floor", MathFloor, 344 "log", MathLog,
343 "log", MathLog, 345 "round", MathRound,
344 "round", MathRound, 346 "sqrt", MathSqrt,
345 "sin", MathSin, // implemented by third_party/fdlibm 347 "atan2", MathAtan2JS,
346 "sqrt", MathSqrt, 348 "pow", MathPow,
347 "tan", MathTan, // implemented by third_party/fdlibm 349 "max", MathMax,
348 "atan2", MathAtan2JS, 350 "min", MathMin,
349 "pow", MathPow, 351 "imul", MathImul,
350 "max", MathMax, 352 "sign", MathSign,
351 "min", MathMin, 353 "trunc", MathTrunc,
352 "imul", MathImul, 354 "tanh", MathTanh,
353 "sign", MathSign, 355 "asinh", MathAsinh,
354 "trunc", MathTrunc, 356 "acosh", MathAcosh,
355 "sinh", MathSinh, // implemented by third_party/fdlibm 357 "atanh", MathAtanh,
356 "cosh", MathCosh, // implemented by third_party/fdlibm 358 "hypot", MathHypot,
357 "tanh", MathTanh, 359 "fround", MathFroundJS,
358 "asinh", MathAsinh, 360 "clz32", MathClz32,
359 "acosh", MathAcosh, 361 "cbrt", MathCbrt
360 "atanh", MathAtanh, 362 ));
361 "log10", MathLog10, // implemented by third_party/fdlibm
362 "log2", MathLog2, // implemented by third_party/fdlibm
363 "hypot", MathHypot,
364 "fround", MathFroundJS,
365 "clz32", MathClz32,
366 "cbrt", MathCbrt,
367 "log1p", MathLog1p, // implemented by third_party/fdlibm
368 "expm1", MathExpm1 // implemented by third_party/fdlibm
369 ));
370 363
371 %SetInlineBuiltinFlag(MathCeil); 364 %SetInlineBuiltinFlag(MathCeil);
372 %SetInlineBuiltinFlag(MathRandom); 365 %SetInlineBuiltinFlag(MathRandom);
373 %SetInlineBuiltinFlag(MathSin);
374 %SetInlineBuiltinFlag(MathCos);
375 }
376 366
377 SetUpMath(); 367 // Keep reference to original values of some global properties. This
368 // has the added benefit that the code in this file is isolated from
369 // changes to these properties.
370 $abs = MathAbs;
371 $exp = MathExp;
372 $floor = MathFloor;
373 $max = MathMax;
374 $min = MathMin;
375
376 })();
OLDNEW
« no previous file with comments | « src/macros.py ('k') | src/third_party/fdlibm/fdlibm.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698