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

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

Issue 2116753002: [builtins] Unify most of the remaining Math builtins. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@2102223005
Patch Set: Created 4 years, 5 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
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 (function(global, utils) { 5 (function(global, utils) {
6 "use strict"; 6 "use strict";
7 7
8 %CheckIsBootstrapping(); 8 %CheckIsBootstrapping();
9 9
10 // ------------------------------------------------------------------- 10 // -------------------------------------------------------------------
(...skipping 20 matching lines...) Expand all
31 randomNumbers = %GenerateRandomNumbers(randomNumbers); 31 randomNumbers = %GenerateRandomNumbers(randomNumbers);
32 if (%_IsTypedArray(randomNumbers)) { 32 if (%_IsTypedArray(randomNumbers)) {
33 nextRandomIndex = %_TypedArrayGetLength(randomNumbers); 33 nextRandomIndex = %_TypedArrayGetLength(randomNumbers);
34 } else { 34 } else {
35 nextRandomIndex = randomNumbers.length; 35 nextRandomIndex = randomNumbers.length;
36 } 36 }
37 } 37 }
38 return randomNumbers[--nextRandomIndex]; 38 return randomNumbers[--nextRandomIndex];
39 } 39 }
40 40
41 // ES6 draft 09-27-13, section 20.2.2.28.
42 function MathSign(x) {
43 x = +x;
44 if (x > 0) return 1;
45 if (x < 0) return -1;
46 // -0, 0 or NaN.
47 return x;
48 }
49
50 // ES6 draft 09-27-13, section 20.2.2.5.
51 function MathAsinh(x) {
52 x = TO_NUMBER(x);
53 // Idempotent for NaN, +/-0 and +/-Infinity.
54 if (x === 0 || !NUMBER_IS_FINITE(x)) return x;
55 if (x > 0) return %math_log(x + %math_sqrt(x * x + 1));
56 // This is to prevent numerical errors caused by large negative x.
57 return -%math_log(-x + %math_sqrt(x * x + 1));
58 }
59
60 // ES6 draft 09-27-13, section 20.2.2.3.
61 function MathAcosh(x) {
62 x = TO_NUMBER(x);
63 if (x < 1) return NaN;
64 // Idempotent for NaN and +Infinity.
65 if (!NUMBER_IS_FINITE(x)) return x;
66 return %math_log(x + %math_sqrt(x + 1) * %math_sqrt(x - 1));
67 }
68
69
70 // ------------------------------------------------------------------- 41 // -------------------------------------------------------------------
71 42
72 %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM); 43 %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM);
73 44
74 // Set up math constants.
75 utils.InstallConstants(GlobalMath, [
76 "PI", 3.1415926535897932,
77 "SQRT1_2", 0.7071067811865476,
78 "SQRT2", 1.4142135623730951
79 ]);
80
81 // Set up non-enumerable functions of the Math object and 45 // Set up non-enumerable functions of the Math object and
82 // set their names. 46 // set their names.
83 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ 47 utils.InstallFunctions(GlobalMath, DONT_ENUM, [
84 "random", MathRandom, 48 "random", MathRandom,
85 "sign", MathSign,
86 "asinh", MathAsinh,
87 "acosh", MathAcosh
88 ]); 49 ]);
89 50
90 %SetForceInlineFlag(MathRandom); 51 %SetForceInlineFlag(MathRandom);
91 %SetForceInlineFlag(MathSign);
92 52
93 // ------------------------------------------------------------------- 53 // -------------------------------------------------------------------
94 // Exports 54 // Exports
95 55
96 utils.Export(function(to) { 56 utils.Export(function(to) {
97 to.MathRandom = MathRandom; 57 to.MathRandom = MathRandom;
98 }); 58 });
99 59
100 }) 60 })
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698