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; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 19 | var NaN = %GetRootNaN(); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 20 | var nextRandomIndex = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 21 | var randomNumbers = UNDEFINED; |
| 22 | var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
| 23 | |
| 24 | //------------------------------------------------------------------- |
| 25 | |
| 26 | // ECMA 262 - 15.8.2.1 |
| 27 | function MathAbs(x) { |
| 28 | x = +x; |
| 29 | return (x > 0) ? x : 0 - x; |
| 30 | } |
| 31 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | // ECMA 262 - 15.8.2.13 |
| 33 | function MathPowJS(x, y) { |
| 34 | return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); |
| 35 | } |
| 36 | |
| 37 | // ECMA 262 - 15.8.2.14 |
| 38 | function MathRandom() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 39 | // While creating a startup snapshot, %GenerateRandomNumbers returns a |
| 40 | // normal array containing a single random number, and has to be called for |
| 41 | // every new random number. |
| 42 | // Otherwise, it returns a pre-populated typed array of random numbers. The |
| 43 | // first two elements are reserved for the PRNG state. |
| 44 | if (nextRandomIndex <= kRandomNumberStart) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 45 | randomNumbers = %GenerateRandomNumbers(randomNumbers); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 46 | if (%_IsTypedArray(randomNumbers)) { |
| 47 | nextRandomIndex = %_TypedArrayGetLength(randomNumbers); |
| 48 | } else { |
| 49 | nextRandomIndex = randomNumbers.length; |
| 50 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 51 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 52 | return randomNumbers[--nextRandomIndex]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | function MathRandomRaw() { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 56 | if (nextRandomIndex <= kRandomNumberStart) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 57 | randomNumbers = %GenerateRandomNumbers(randomNumbers); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 58 | nextRandomIndex = %_TypedArrayGetLength(randomNumbers); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 59 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 60 | return %_DoubleLo(randomNumbers[--nextRandomIndex]) & 0x3FFFFFFF; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // ES6 draft 09-27-13, section 20.2.2.28. |
| 64 | function MathSign(x) { |
| 65 | x = +x; |
| 66 | if (x > 0) return 1; |
| 67 | if (x < 0) return -1; |
| 68 | // -0, 0 or NaN. |
| 69 | return x; |
| 70 | } |
| 71 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | // ES6 draft 09-27-13, section 20.2.2.5. |
| 73 | function MathAsinh(x) { |
| 74 | x = TO_NUMBER(x); |
| 75 | // Idempotent for NaN, +/-0 and +/-Infinity. |
| 76 | if (x === 0 || !NUMBER_IS_FINITE(x)) return x; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 77 | if (x > 0) return %math_log(x + %math_sqrt(x * x + 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 78 | // This is to prevent numerical errors caused by large negative x. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 79 | return -%math_log(-x + %math_sqrt(x * x + 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // ES6 draft 09-27-13, section 20.2.2.3. |
| 83 | function MathAcosh(x) { |
| 84 | x = TO_NUMBER(x); |
| 85 | if (x < 1) return NaN; |
| 86 | // Idempotent for NaN and +Infinity. |
| 87 | if (!NUMBER_IS_FINITE(x)) return x; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 88 | return %math_log(x + %math_sqrt(x + 1) * %math_sqrt(x - 1)); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // ES6 draft 09-27-13, section 20.2.2.17. |
| 92 | function MathHypot(x, y) { // Function length is 2. |
| 93 | // We may want to introduce fast paths for two arguments and when |
| 94 | // normalization to avoid overflow is not necessary. For now, we |
| 95 | // simply assume the general case. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 96 | var length = arguments.length; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | var max = 0; |
| 98 | for (var i = 0; i < length; i++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 99 | var n = MathAbs(arguments[i]); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 100 | if (n > max) max = n; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 101 | arguments[i] = n; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | } |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 103 | if (max === INFINITY) return INFINITY; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 104 | |
| 105 | // Kahan summation to avoid rounding errors. |
| 106 | // Normalize the numbers to the largest one to avoid overflow. |
| 107 | if (max === 0) max = 1; |
| 108 | var sum = 0; |
| 109 | var compensation = 0; |
| 110 | for (var i = 0; i < length; i++) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 111 | var n = arguments[i] / max; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | var summand = n * n - compensation; |
| 113 | var preliminary = sum + summand; |
| 114 | compensation = (preliminary - sum) - summand; |
| 115 | sum = preliminary; |
| 116 | } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 117 | return %math_sqrt(sum) * max; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 120 | // ------------------------------------------------------------------- |
| 121 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 122 | %InstallToContext([ |
| 123 | "math_pow", MathPowJS, |
| 124 | ]); |
| 125 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 126 | %AddNamedProperty(GlobalMath, toStringTagSymbol, "Math", READ_ONLY | DONT_ENUM); |
| 127 | |
| 128 | // Set up math constants. |
| 129 | utils.InstallConstants(GlobalMath, [ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | "PI", 3.1415926535897932, |
| 131 | "SQRT1_2", 0.7071067811865476, |
| 132 | "SQRT2", 1.4142135623730951 |
| 133 | ]); |
| 134 | |
| 135 | // Set up non-enumerable functions of the Math object and |
| 136 | // set their names. |
| 137 | utils.InstallFunctions(GlobalMath, DONT_ENUM, [ |
| 138 | "random", MathRandom, |
| 139 | "abs", MathAbs, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 140 | "pow", MathPowJS, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | "sign", MathSign, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 142 | "asinh", MathAsinh, |
| 143 | "acosh", MathAcosh, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 144 | "hypot", MathHypot, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | ]); |
| 146 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 147 | %SetForceInlineFlag(MathRandom); |
| 148 | %SetForceInlineFlag(MathSign); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 149 | |
| 150 | // ------------------------------------------------------------------- |
| 151 | // Exports |
| 152 | |
| 153 | utils.Export(function(to) { |
| 154 | to.MathAbs = MathAbs; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 155 | to.IntRandom = MathRandomRaw; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 156 | }); |
| 157 | |
| 158 | }) |