Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | (function(global, utils) { |
| 6 | "use strict"; |
| 7 | |
| 8 | %CheckIsBootstrapping(); |
| 9 | |
| 10 | // ------------------------------------------------------------------- |
| 11 | // Imports |
| 12 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13 | // The first two slots are reserved to persist PRNG state. |
| 14 | define kRandomNumberStart = 2; |
| 15 | |
| 16 | var GlobalFloat64Array = global.Float64Array; |
| 17 | var GlobalMath = global.Math; |
| 18 | var GlobalObject = global.Object; |
| 19 | var InternalArray = utils.InternalArray; |
| 20 | var NaN = %GetRootNaN(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 21 | var nextRandomIndex = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | var randomNumbers = UNDEFINED; |
| 23 | var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
| 24 | |
| 25 | //------------------------------------------------------------------- |
| 26 | |
| 27 | // ECMA 262 - 15.8.2.1 |
| 28 | function MathAbs(x) { |
| 29 | x = +x; |
| 30 | return (x > 0) ? x : 0 - x; |
| 31 | } |
| 32 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | // ECMA 262 - 15.8.2.5 |
| 34 | // The naming of y and x matches the spec, as does the order in which |
| 35 | // ToNumber (valueOf) is called. |
| 36 | function MathAtan2JS(y, x) { |
| 37 | y = +y; |
| 38 | x = +x; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 39 | return %MathAtan2(y, x); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // ECMA 262 - 15.8.2.8 |
| 43 | function MathExp(x) { |
| 44 | return %MathExpRT(TO_NUMBER(x)); |
| 45 | } |
| 46 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 47 | // ECMA 262 - 15.8.2.10 |
| 48 | function MathLog(x) { |
| 49 | return %_MathLogRT(TO_NUMBER(x)); |
| 50 | } |
| 51 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 52 | // ECMA 262 - 15.8.2.13 |
| 53 | function MathPowJS(x, y) { |
| 54 | return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); |
| 55 | } |
| 56 | |
| 57 | // ECMA 262 - 15.8.2.14 |
| 58 | function MathRandom() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 59 | // While creating a startup snapshot, %GenerateRandomNumbers returns a |
| 60 | // normal array containing a single random number, and has to be called for |
| 61 | // every new random number. |
| 62 | // Otherwise, it returns a pre-populated typed array of random numbers. The |
| 63 | // first two elements are reserved for the PRNG state. |
| 64 | if (nextRandomIndex <= kRandomNumberStart) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | randomNumbers = %GenerateRandomNumbers(randomNumbers); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 66 | nextRandomIndex = randomNumbers.length; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 67 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 68 | return randomNumbers[--nextRandomIndex]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | function MathRandomRaw() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 72 | if (nextRandomIndex <= kRandomNumberStart) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 73 | randomNumbers = %GenerateRandomNumbers(randomNumbers); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 74 | nextRandomIndex = randomNumbers.length; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 75 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 76 | return %_DoubleLo(randomNumbers[--nextRandomIndex]) & 0x3FFFFFFF; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // ES6 draft 09-27-13, section 20.2.2.28. |
| 80 | function MathSign(x) { |
| 81 | x = +x; |
| 82 | if (x > 0) return 1; |
| 83 | if (x < 0) return -1; |
| 84 | // -0, 0 or NaN. |
| 85 | return x; |
| 86 | } |
| 87 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 88 | // ES6 draft 09-27-13, section 20.2.2.5. |
| 89 | function MathAsinh(x) { |
| 90 | x = TO_NUMBER(x); |
| 91 | // Idempotent for NaN, +/-0 and +/-Infinity. |
| 92 | if (x === 0 || !NUMBER_IS_FINITE(x)) return x; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 93 | if (x > 0) return MathLog(x + %math_sqrt(x * x + 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 94 | // This is to prevent numerical errors caused by large negative x. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 95 | return -MathLog(-x + %math_sqrt(x * x + 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // ES6 draft 09-27-13, section 20.2.2.3. |
| 99 | function MathAcosh(x) { |
| 100 | x = TO_NUMBER(x); |
| 101 | if (x < 1) return NaN; |
| 102 | // Idempotent for NaN and +Infinity. |
| 103 | if (!NUMBER_IS_FINITE(x)) return x; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 104 | return MathLog(x + %math_sqrt(x + 1) * %math_sqrt(x - 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // ES6 draft 09-27-13, section 20.2.2.7. |
| 108 | function MathAtanh(x) { |
| 109 | x = TO_NUMBER(x); |
| 110 | // Idempotent for +/-0. |
| 111 | if (x === 0) return x; |
| 112 | // Returns NaN for NaN and +/- Infinity. |
| 113 | if (!NUMBER_IS_FINITE(x)) return NaN; |
| 114 | return 0.5 * MathLog((1 + x) / (1 - x)); |
| 115 | } |
| 116 | |
| 117 | // ES6 draft 09-27-13, section 20.2.2.17. |
| 118 | function MathHypot(x, y) { // Function length is 2. |
| 119 | // We may want to introduce fast paths for two arguments and when |
| 120 | // normalization to avoid overflow is not necessary. For now, we |
| 121 | // simply assume the general case. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 122 | var length = arguments.length; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 123 | var max = 0; |
| 124 | for (var i = 0; i < length; i++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 125 | var n = MathAbs(arguments[i]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 126 | if (n > max) max = n; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 127 | arguments[i] = n; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 129 | if (max === INFINITY) return INFINITY; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | |
| 131 | // Kahan summation to avoid rounding errors. |
| 132 | // Normalize the numbers to the largest one to avoid overflow. |
| 133 | if (max === 0) max = 1; |
| 134 | var sum = 0; |
| 135 | var compensation = 0; |
| 136 | for (var i = 0; i < length; i++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 137 | var n = arguments[i] / max; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | var summand = n * n - compensation; |
| 139 | var preliminary = sum + summand; |
| 140 | compensation = (preliminary - sum) - summand; |
| 141 | sum = preliminary; |
| 142 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 143 | return %math_sqrt(sum) * max; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // ES6 draft 09-27-13, section 20.2.2.9. |
| 147 | // Cube root approximation, refer to: http://metamerist.com/cbrt/cbrt.htm |
| 148 | // Using initial approximation adapted from Kahan's cbrt and 4 iterations |
| 149 | // of Newton's method. |
| 150 | function MathCbrt(x) { |
| 151 | x = TO_NUMBER(x); |
| 152 | if (x == 0 || !NUMBER_IS_FINITE(x)) return x; |
| 153 | return x >= 0 ? CubeRoot(x) : -CubeRoot(-x); |
| 154 | } |
| 155 | |
| 156 | macro NEWTON_ITERATION_CBRT(x, approx) |
| 157 | (1.0 / 3.0) * (x / (approx * approx) + 2 * approx); |
| 158 | endmacro |
| 159 | |
| 160 | function CubeRoot(x) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 161 | var approx_hi = %math_floor(%_DoubleHi(x) / 3) + 0x2A9F7893; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 162 | var approx = %_ConstructDouble(approx_hi | 0, 0); |
| 163 | approx = NEWTON_ITERATION_CBRT(x, approx); |
| 164 | approx = NEWTON_ITERATION_CBRT(x, approx); |
| 165 | approx = NEWTON_ITERATION_CBRT(x, approx); |
| 166 | return NEWTON_ITERATION_CBRT(x, approx); |
| 167 | } |
| 168 | |
| 169 | // ------------------------------------------------------------------- |
| 170 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 171 | %InstallToContext([ |
| 172 | "math_pow", MathPowJS, |
| 173 | ]); |
| 174 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 175 | %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM); |
| 176 | |
| 177 | // Set up math constants. |
| 178 | utils.InstallConstants(GlobalMath, [ |
| 179 | // ECMA-262, section 15.8.1.1. |
| 180 | "E", 2.7182818284590452354, |
| 181 | // ECMA-262, section 15.8.1.2. |
| 182 | "LN10", 2.302585092994046, |
| 183 | // ECMA-262, section 15.8.1.3. |
| 184 | "LN2", 0.6931471805599453, |
| 185 | // ECMA-262, section 15.8.1.4. |
| 186 | "LOG2E", 1.4426950408889634, |
| 187 | "LOG10E", 0.4342944819032518, |
| 188 | "PI", 3.1415926535897932, |
| 189 | "SQRT1_2", 0.7071067811865476, |
| 190 | "SQRT2", 1.4142135623730951 |
| 191 | ]); |
| 192 | |
| 193 | // Set up non-enumerable functions of the Math object and |
| 194 | // set their names. |
| 195 | utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
| 196 | "random", MathRandom, |
| 197 | "abs", MathAbs, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 198 | "exp", MathExp, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 199 | "log", MathLog, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 200 | "atan2", MathAtan2JS, |
| 201 | "pow", MathPowJS, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 202 | "sign", MathSign, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 203 | "asinh", MathAsinh, |
| 204 | "acosh", MathAcosh, |
| 205 | "atanh", MathAtanh, |
| 206 | "hypot", MathHypot, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 207 | "cbrt", MathCbrt |
| 208 | ]); |
| 209 | |
| 210 | %SetForceInlineFlag(MathAbs); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 211 | %SetForceInlineFlag(MathAtan2JS); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 212 | %SetForceInlineFlag(MathRandom); |
| 213 | %SetForceInlineFlag(MathSign); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 214 | |
| 215 | // ------------------------------------------------------------------- |
| 216 | // Exports |
| 217 | |
| 218 | utils.Export(function(to) { |
| 219 | to.MathAbs = MathAbs; |
| 220 | to.MathExp = MathExp; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 221 | to.IntRandom = MathRandomRaw; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 222 | }); |
| 223 | |
| 224 | }) |