Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Side by Side Diff: src/runtime/runtime-maths.cc

Issue 2402363002: [Math] implement Math.random as TFJ builtin. (Closed)
Patch Set: fix golden file Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/bootstrapper.h" 10 #include "src/bootstrapper.h"
11 #include "src/codegen.h" 11 #include "src/codegen.h"
12 12
13 namespace v8 { 13 namespace v8 {
14 namespace internal { 14 namespace internal {
15 15
16 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) { 16 RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) {
17 HandleScope scope(isolate); 17 HandleScope scope(isolate);
18 DCHECK(args.length() == 1); 18 DCHECK(args.length() == 0);
19 if (isolate->serializer_enabled()) {
20 // Random numbers in the snapshot are not really that random. And we cannot
21 // return a typed array as it cannot be serialized. To make calling
22 // Math.random possible when creating a custom startup snapshot, we simply
23 // return a normal array with a single random number.
24 Handle<HeapNumber> random_number = isolate->factory()->NewHeapNumber(
25 isolate->random_number_generator()->NextDouble());
26 Handle<FixedArray> array_backing = isolate->factory()->NewFixedArray(1);
27 array_backing->set(0, *random_number);
28 return *isolate->factory()->NewJSArrayWithElements(array_backing);
29 }
30 19
31 static const int kState0Offset = 0; 20 Handle<Context> native_context = isolate->native_context();
32 static const int kState1Offset = 1; 21 DCHECK_EQ(0, native_context->math_random_index()->value());
33 static const int kRandomBatchSize = 64; 22
34 CONVERT_ARG_HANDLE_CHECKED(Object, maybe_typed_array, 0); 23 static const int kCacheSize = 64;
35 Handle<JSTypedArray> typed_array; 24 static const int kState0Offset = kCacheSize - 1;
36 // Allocate typed array if it does not yet exist. 25 static const int kState1Offset = kState0Offset - 1;
37 if (maybe_typed_array->IsJSTypedArray()) { 26 // The index is decremented before used to access the cache.
38 typed_array = Handle<JSTypedArray>::cast(maybe_typed_array); 27 static const int kInitialIndex = kState1Offset;
28
29 Handle<FixedDoubleArray> cache;
30 uint64_t state0 = 0;
31 uint64_t state1 = 0;
32 if (native_context->math_random_cache()->IsFixedDoubleArray()) {
33 cache = Handle<FixedDoubleArray>(
34 FixedDoubleArray::cast(native_context->math_random_cache()), isolate);
35 state0 = double_to_uint64(cache->get_scalar(kState0Offset));
36 state1 = double_to_uint64(cache->get_scalar(kState1Offset));
39 } else { 37 } else {
40 static const int kByteLength = kRandomBatchSize * kDoubleSize; 38 cache = Handle<FixedDoubleArray>::cast(
41 Handle<JSArrayBuffer> buffer = 39 isolate->factory()->NewFixedDoubleArray(kCacheSize, TENURED));
42 isolate->factory()->NewJSArrayBuffer(SharedFlag::kNotShared, TENURED); 40 native_context->set_math_random_cache(*cache);
43 JSArrayBuffer::SetupAllocatingData(buffer, isolate, kByteLength, true, 41 // Initialize state if not yet initialized.
44 SharedFlag::kNotShared); 42 while (state0 == 0 || state1 == 0) {
45 typed_array = isolate->factory()->NewJSTypedArray( 43 isolate->random_number_generator()->NextBytes(&state0, sizeof(state0));
46 kExternalFloat64Array, buffer, 0, kRandomBatchSize); 44 isolate->random_number_generator()->NextBytes(&state1, sizeof(state1));
45 }
47 } 46 }
48 47
49 DisallowHeapAllocation no_gc; 48 DisallowHeapAllocation no_gc;
50 double* array = 49 FixedDoubleArray* raw_cache = *cache;
51 reinterpret_cast<double*>(typed_array->GetBuffer()->backing_store());
52 // Fetch existing state.
53 uint64_t state0 = double_to_uint64(array[kState0Offset]);
54 uint64_t state1 = double_to_uint64(array[kState1Offset]);
55 // Initialize state if not yet initialized.
56 while (state0 == 0 || state1 == 0) {
57 isolate->random_number_generator()->NextBytes(&state0, sizeof(state0));
58 isolate->random_number_generator()->NextBytes(&state1, sizeof(state1));
59 }
60 // Create random numbers. 50 // Create random numbers.
61 for (int i = kState1Offset + 1; i < kRandomBatchSize; i++) { 51 for (int i = 0; i < kInitialIndex; i++) {
62 // Generate random numbers using xorshift128+. 52 // Generate random numbers using xorshift128+.
63 base::RandomNumberGenerator::XorShift128(&state0, &state1); 53 base::RandomNumberGenerator::XorShift128(&state0, &state1);
64 array[i] = base::RandomNumberGenerator::ToDouble(state0, state1); 54 raw_cache->set(i, base::RandomNumberGenerator::ToDouble(state0, state1));
65 } 55 }
56
66 // Persist current state. 57 // Persist current state.
67 array[kState0Offset] = uint64_to_double(state0); 58 raw_cache->set(kState0Offset, uint64_to_double(state0));
68 array[kState1Offset] = uint64_to_double(state1); 59 raw_cache->set(kState1Offset, uint64_to_double(state1));
69 return *typed_array; 60 return Smi::FromInt(kInitialIndex);
70 } 61 }
71 } // namespace internal 62 } // namespace internal
72 } // namespace v8 63 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698