OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/assembler.h" | 8 #include "src/assembler.h" |
9 #include "src/base/utils/random-number-generator.h" | 9 #include "src/base/utils/random-number-generator.h" |
10 #include "src/codegen.h" | 10 #include "src/codegen.h" |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
240 RUNTIME_FUNCTION(Runtime_IsMinusZero) { | 240 RUNTIME_FUNCTION(Runtime_IsMinusZero) { |
241 SealHandleScope shs(isolate); | 241 SealHandleScope shs(isolate); |
242 DCHECK(args.length() == 1); | 242 DCHECK(args.length() == 1); |
243 CONVERT_ARG_CHECKED(Object, obj, 0); | 243 CONVERT_ARG_CHECKED(Object, obj, 0); |
244 if (!obj->IsHeapNumber()) return isolate->heap()->false_value(); | 244 if (!obj->IsHeapNumber()) return isolate->heap()->false_value(); |
245 HeapNumber* number = HeapNumber::cast(obj); | 245 HeapNumber* number = HeapNumber::cast(obj); |
246 return isolate->heap()->ToBoolean(IsMinusZero(number->value())); | 246 return isolate->heap()->ToBoolean(IsMinusZero(number->value())); |
247 } | 247 } |
248 | 248 |
249 | 249 |
250 RUNTIME_FUNCTION(Runtime_InitializeRNG) { | 250 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) { |
251 HandleScope scope(isolate); | 251 HandleScope scope(isolate); |
252 DCHECK(args.length() == 0); | 252 DCHECK(args.length() == 1); |
253 static const int kSize = 4; | 253 static const int kState0Offset = 0; |
254 Handle<FixedArray> array = isolate->factory()->NewFixedArray(kSize); | 254 static const int kState1Offset = 1; |
255 uint16_t seeds[kSize]; | 255 static const int kRandomBatchSize = 64; |
256 do { | 256 CONVERT_ARG_HANDLE_CHECKED(Object, maybe_typed_array, 0); |
257 isolate->random_number_generator()->NextBytes(seeds, | 257 Handle<JSTypedArray> typed_array; |
258 kSize * sizeof(*seeds)); | 258 // Allocate typed array if it does not yet exist. |
259 } while (!(seeds[0] && seeds[1] && seeds[2] && seeds[3])); | 259 if (maybe_typed_array->IsJSTypedArray()) { |
260 for (int i = 0; i < kSize; i++) array->set(i, Smi::FromInt(seeds[i])); | 260 typed_array = Handle<JSTypedArray>::cast(maybe_typed_array); |
261 return *isolate->factory()->NewJSArrayWithElements(array); | 261 } else { |
262 static const int kByteLength = kRandomBatchSize * sizeof(double); | |
Jakob Kummerow
2015/11/23 14:34:08
"sizeof(double)" used to be a linter violation. Yo
| |
263 Handle<JSArrayBuffer> buffer = | |
264 isolate->factory()->NewJSArrayBuffer(SharedFlag::kNotShared, TENURED); | |
265 JSArrayBuffer::SetupAllocatingData(buffer, isolate, kByteLength, true, | |
266 SharedFlag::kNotShared); | |
267 typed_array = isolate->factory()->NewJSTypedArray( | |
268 kExternalFloat64Array, buffer, 0, kRandomBatchSize); | |
269 } | |
270 | |
271 DisallowHeapAllocation no_gc; | |
272 double* array = | |
273 reinterpret_cast<double*>(typed_array->GetBuffer()->backing_store()); | |
274 // Fetch existing state. | |
275 uint64_t state0 = double_to_uint64(array[kState0Offset]); | |
276 uint64_t state1 = double_to_uint64(array[kState1Offset]); | |
277 // Initialize state if not yet initialized. | |
278 while (state0 == 0 || state1 == 0) { | |
279 isolate->random_number_generator()->NextBytes(&state0, sizeof(state0)); | |
280 isolate->random_number_generator()->NextBytes(&state1, sizeof(state1)); | |
281 } | |
282 // Create random numbers. | |
283 for (int i = kState1Offset + 1; i < kRandomBatchSize; i++) { | |
284 // Generate random numbers using xorshift128+. | |
285 base::RandomNumberGenerator::XorShift128(&state0, &state1); | |
286 array[i] = base::RandomNumberGenerator::ToDouble(state0, state1); | |
287 } | |
288 // Persist current state. | |
289 array[kState0Offset] = uint64_to_double(state0); | |
290 array[kState1Offset] = uint64_to_double(state1); | |
291 return *typed_array; | |
262 } | 292 } |
263 } // namespace internal | 293 } // namespace internal |
264 } // namespace v8 | 294 } // namespace v8 |
OLD | NEW |