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 define kRandomBatchSize = 64; | |
14 define kRandomNumberStart = 2; | |
Jakob Kummerow
2015/11/23 14:34:08
nit: maybe add a comment here that indices 0 and 1
| |
15 | |
16 var GlobalFloat64Array = global.Float64Array; | |
13 var GlobalMath = global.Math; | 17 var GlobalMath = global.Math; |
14 var GlobalObject = global.Object; | 18 var GlobalObject = global.Object; |
15 var InternalArray = utils.InternalArray; | 19 var InternalArray = utils.InternalArray; |
16 var NaN = %GetRootNaN(); | 20 var NaN = %GetRootNaN(); |
17 var rngstate = { a: 1, b: 2, c: 3, d: 4 }; | 21 var nextRandomIndex = kRandomBatchSize; |
22 var randomNumbers = UNDEFINED; | |
18 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | 23 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
19 | 24 |
20 utils.InitializeRNG = function() { | |
21 var state = %InitializeRNG(); | |
22 rngstate = { a: state[0], b: state[1], c: state[2], d: state[3] }; | |
23 }; | |
24 | |
25 //------------------------------------------------------------------- | 25 //------------------------------------------------------------------- |
26 | 26 |
27 // ECMA 262 - 15.8.2.1 | 27 // ECMA 262 - 15.8.2.1 |
28 function MathAbs(x) { | 28 function MathAbs(x) { |
29 x = +x; | 29 x = +x; |
30 return (x > 0) ? x : 0 - x; | 30 return (x > 0) ? x : 0 - x; |
31 } | 31 } |
32 | 32 |
33 // ECMA 262 - 15.8.2.2 | 33 // ECMA 262 - 15.8.2.2 |
34 function MathAcosJS(x) { | 34 function MathAcosJS(x) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 return r; | 128 return r; |
129 } | 129 } |
130 | 130 |
131 // ECMA 262 - 15.8.2.13 | 131 // ECMA 262 - 15.8.2.13 |
132 function MathPowJS(x, y) { | 132 function MathPowJS(x, y) { |
133 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); | 133 return %_MathPow(TO_NUMBER(x), TO_NUMBER(y)); |
134 } | 134 } |
135 | 135 |
136 // ECMA 262 - 15.8.2.14 | 136 // ECMA 262 - 15.8.2.14 |
137 function MathRandom() { | 137 function MathRandom() { |
138 var r0 = (MathImul(18030, rngstate.a) + rngstate.b) | 0; | 138 if (nextRandomIndex >= kRandomBatchSize) { |
139 var r1 = (MathImul(36969, rngstate.c) + rngstate.d) | 0; | 139 randomNumbers = %GenerateRandomNumbers(randomNumbers); |
140 rngstate.a = r0 & 0xFFFF; | 140 nextRandomIndex = kRandomNumberStart; |
141 rngstate.b = r0 >>> 16; | 141 } |
142 rngstate.c = r1 & 0xFFFF; | 142 return randomNumbers[nextRandomIndex++]; |
143 rngstate.d = r1 >>> 16; | |
144 var r = r0 ^ r1; | |
145 // Construct a double number 1.<32-bits of randomness> and subtract 1. | |
146 return %_ConstructDouble(0x3FF00000 | (r & 0x000FFFFF), r & 0xFFF00000) - 1; | |
147 } | 143 } |
148 | 144 |
149 function MathRandomRaw() { | 145 function MathRandomRaw() { |
150 var r0 = (MathImul(18030, rngstate.a) + rngstate.b) | 0; | 146 if (nextRandomIndex >= kRandomBatchSize) { |
151 var r1 = (MathImul(36969, rngstate.c) + rngstate.d) | 0; | 147 randomNumbers = %GenerateRandomNumbers(randomNumbers); |
152 rngstate.a = r0 & 0xFFFF; | 148 nextRandomIndex = kRandomNumberStart; |
153 rngstate.b = r0 >>> 16; | 149 } |
154 rngstate.c = r1 & 0xFFFF; | 150 return %_DoubleLo(randomNumbers[nextRandomIndex++]) & 0x3FFFFFFF; |
155 rngstate.d = r1 >>> 16; | |
156 var r = r0 ^ r1; | |
157 return r & 0x3FFFFFFF; | |
158 } | 151 } |
159 | 152 |
160 // ECMA 262 - 15.8.2.15 | 153 // ECMA 262 - 15.8.2.15 |
161 function MathRound(x) { | 154 function MathRound(x) { |
162 return %RoundNumber(TO_NUMBER(x)); | 155 return %RoundNumber(TO_NUMBER(x)); |
163 } | 156 } |
164 | 157 |
165 // ECMA 262 - 15.8.2.17 | 158 // ECMA 262 - 15.8.2.17 |
166 function MathSqrtJS(x) { | 159 function MathSqrtJS(x) { |
167 return %_MathSqrt(+x); | 160 return %_MathSqrt(+x); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 %SetForceInlineFlag(MathSqrtJS); | 340 %SetForceInlineFlag(MathSqrtJS); |
348 %SetForceInlineFlag(MathTrunc); | 341 %SetForceInlineFlag(MathTrunc); |
349 | 342 |
350 // ------------------------------------------------------------------- | 343 // ------------------------------------------------------------------- |
351 // Exports | 344 // Exports |
352 | 345 |
353 utils.Export(function(to) { | 346 utils.Export(function(to) { |
354 to.MathAbs = MathAbs; | 347 to.MathAbs = MathAbs; |
355 to.MathExp = MathExp; | 348 to.MathExp = MathExp; |
356 to.MathFloor = MathFloorJS; | 349 to.MathFloor = MathFloorJS; |
357 to.IntRandom = MathRandomRaw; | 350 to.MathRandomRaw = MathRandomRaw; |
358 to.MathMax = MathMax; | 351 to.MathMax = MathMax; |
359 to.MathMin = MathMin; | 352 to.MathMin = MathMin; |
360 }); | 353 }); |
361 | 354 |
362 }) | 355 }) |
OLD | NEW |