OLD | NEW |
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 // ------------------------------------------------------------------- |
11 // Imports | 11 // Imports |
12 | 12 |
13 // The first two slots are reserved to persist PRNG state. | 13 // The first two slots are reserved to persist PRNG state. |
14 define kRandomNumberStart = 2; | 14 define kRandomNumberStart = 2; |
15 | 15 |
16 var GlobalFloat64Array = global.Float64Array; | |
17 var GlobalMath = global.Math; | 16 var GlobalMath = global.Math; |
18 var GlobalObject = global.Object; | |
19 var NaN = %GetRootNaN(); | 17 var NaN = %GetRootNaN(); |
20 var nextRandomIndex = 0; | 18 var nextRandomIndex = 0; |
21 var randomNumbers = UNDEFINED; | 19 var randomNumbers = UNDEFINED; |
22 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 20 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
23 | 21 |
24 //------------------------------------------------------------------- | 22 //------------------------------------------------------------------- |
25 | |
26 // ECMA 262 - 15.8.2.1 | |
27 function MathAbs(x) { | |
28 x = +x; | |
29 return (x > 0) ? x : 0 - x; | |
30 } | |
31 | |
32 // ECMA 262 - 15.8.2.14 | 23 // ECMA 262 - 15.8.2.14 |
33 function MathRandom() { | 24 function MathRandom() { |
34 // While creating a startup snapshot, %GenerateRandomNumbers returns a | 25 // While creating a startup snapshot, %GenerateRandomNumbers returns a |
35 // normal array containing a single random number, and has to be called for | 26 // normal array containing a single random number, and has to be called for |
36 // every new random number. | 27 // every new random number. |
37 // Otherwise, it returns a pre-populated typed array of random numbers. The | 28 // Otherwise, it returns a pre-populated typed array of random numbers. The |
38 // first two elements are reserved for the PRNG state. | 29 // first two elements are reserved for the PRNG state. |
39 if (nextRandomIndex <= kRandomNumberStart) { | 30 if (nextRandomIndex <= kRandomNumberStart) { |
40 randomNumbers = %GenerateRandomNumbers(randomNumbers); | 31 randomNumbers = %GenerateRandomNumbers(randomNumbers); |
41 if (%_IsTypedArray(randomNumbers)) { | 32 if (%_IsTypedArray(randomNumbers)) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 } | 67 } |
77 | 68 |
78 // ES6 draft 09-27-13, section 20.2.2.17. | 69 // ES6 draft 09-27-13, section 20.2.2.17. |
79 function MathHypot(x, y) { // Function length is 2. | 70 function MathHypot(x, y) { // Function length is 2. |
80 // We may want to introduce fast paths for two arguments and when | 71 // We may want to introduce fast paths for two arguments and when |
81 // normalization to avoid overflow is not necessary. For now, we | 72 // normalization to avoid overflow is not necessary. For now, we |
82 // simply assume the general case. | 73 // simply assume the general case. |
83 var length = arguments.length; | 74 var length = arguments.length; |
84 var max = 0; | 75 var max = 0; |
85 for (var i = 0; i < length; i++) { | 76 for (var i = 0; i < length; i++) { |
86 var n = MathAbs(arguments[i]); | 77 var n = %math_abs(arguments[i]); |
87 if (n > max) max = n; | 78 if (n > max) max = n; |
88 arguments[i] = n; | 79 arguments[i] = n; |
89 } | 80 } |
90 if (max === INFINITY) return INFINITY; | 81 if (max === INFINITY) return INFINITY; |
91 | 82 |
92 // Kahan summation to avoid rounding errors. | 83 // Kahan summation to avoid rounding errors. |
93 // Normalize the numbers to the largest one to avoid overflow. | 84 // Normalize the numbers to the largest one to avoid overflow. |
94 if (max === 0) max = 1; | 85 if (max === 0) max = 1; |
95 var sum = 0; | 86 var sum = 0; |
96 var compensation = 0; | 87 var compensation = 0; |
(...skipping 15 matching lines...) Expand all Loading... |
112 utils.InstallConstants(GlobalMath, [ | 103 utils.InstallConstants(GlobalMath, [ |
113 "PI", 3.1415926535897932, | 104 "PI", 3.1415926535897932, |
114 "SQRT1_2", 0.7071067811865476, | 105 "SQRT1_2", 0.7071067811865476, |
115 "SQRT2", 1.4142135623730951 | 106 "SQRT2", 1.4142135623730951 |
116 ]); | 107 ]); |
117 | 108 |
118 // Set up non-enumerable functions of the Math object and | 109 // Set up non-enumerable functions of the Math object and |
119 // set their names. | 110 // set their names. |
120 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ | 111 utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
121 "random", MathRandom, | 112 "random", MathRandom, |
122 "abs", MathAbs, | |
123 "sign", MathSign, | 113 "sign", MathSign, |
124 "asinh", MathAsinh, | 114 "asinh", MathAsinh, |
125 "acosh", MathAcosh, | 115 "acosh", MathAcosh, |
126 "hypot", MathHypot, | 116 "hypot", MathHypot, |
127 ]); | 117 ]); |
128 | 118 |
129 %SetForceInlineFlag(MathRandom); | 119 %SetForceInlineFlag(MathRandom); |
130 %SetForceInlineFlag(MathSign); | 120 %SetForceInlineFlag(MathSign); |
131 | 121 |
132 // ------------------------------------------------------------------- | 122 // ------------------------------------------------------------------- |
133 // Exports | 123 // Exports |
134 | 124 |
135 utils.Export(function(to) { | 125 utils.Export(function(to) { |
136 to.MathAbs = MathAbs; | |
137 to.MathRandom = MathRandom; | 126 to.MathRandom = MathRandom; |
138 }); | 127 }); |
139 | 128 |
140 }) | 129 }) |
OLD | NEW |