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

Unified Diff: src/runtime/runtime-maths.cc

Issue 1475493003: Revert of Implement xorshift128+ for Math.random. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/runtime/runtime-maths.cc
diff --git a/src/runtime/runtime-maths.cc b/src/runtime/runtime-maths.cc
index cb44c60113cc9c49469d39f86344627f0b8b8405..70c587d7454b401fbfe9f2fc61812b039a6a0531 100644
--- a/src/runtime/runtime-maths.cc
+++ b/src/runtime/runtime-maths.cc
@@ -247,48 +247,18 @@
}
-RUNTIME_FUNCTION(Runtime_GenerateRandomNumbers) {
- HandleScope scope(isolate);
- DCHECK(args.length() == 1);
- static const int kState0Offset = 0;
- static const int kState1Offset = 1;
- static const int kRandomBatchSize = 64;
- CONVERT_ARG_HANDLE_CHECKED(Object, maybe_typed_array, 0);
- Handle<JSTypedArray> typed_array;
- // Allocate typed array if it does not yet exist.
- if (maybe_typed_array->IsJSTypedArray()) {
- typed_array = Handle<JSTypedArray>::cast(maybe_typed_array);
- } else {
- static const int kByteLength = kRandomBatchSize * kDoubleSize;
- Handle<JSArrayBuffer> buffer =
- isolate->factory()->NewJSArrayBuffer(SharedFlag::kNotShared, TENURED);
- JSArrayBuffer::SetupAllocatingData(buffer, isolate, kByteLength, true,
- SharedFlag::kNotShared);
- typed_array = isolate->factory()->NewJSTypedArray(
- kExternalFloat64Array, buffer, 0, kRandomBatchSize);
- }
-
- DisallowHeapAllocation no_gc;
- double* array =
- reinterpret_cast<double*>(typed_array->GetBuffer()->backing_store());
- // Fetch existing state.
- uint64_t state0 = double_to_uint64(array[kState0Offset]);
- uint64_t state1 = double_to_uint64(array[kState1Offset]);
- // Initialize state if not yet initialized.
- while (state0 == 0 || state1 == 0) {
- isolate->random_number_generator()->NextBytes(&state0, sizeof(state0));
- isolate->random_number_generator()->NextBytes(&state1, sizeof(state1));
- }
- // Create random numbers.
- for (int i = kState1Offset + 1; i < kRandomBatchSize; i++) {
- // Generate random numbers using xorshift128+.
- base::RandomNumberGenerator::XorShift128(&state0, &state1);
- array[i] = base::RandomNumberGenerator::ToDouble(state0, state1);
- }
- // Persist current state.
- array[kState0Offset] = uint64_to_double(state0);
- array[kState1Offset] = uint64_to_double(state1);
- return *typed_array;
+RUNTIME_FUNCTION(Runtime_InitializeRNG) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 0);
+ static const int kSize = 4;
+ Handle<FixedArray> array = isolate->factory()->NewFixedArray(kSize);
+ uint16_t seeds[kSize];
+ do {
+ isolate->random_number_generator()->NextBytes(seeds,
+ kSize * sizeof(*seeds));
+ } while (!(seeds[0] && seeds[1] && seeds[2] && seeds[3]));
+ for (int i = 0; i < kSize; i++) array->set(i, Smi::FromInt(seeds[i]));
+ return *isolate->factory()->NewJSArrayWithElements(array);
}
} // namespace internal
} // namespace v8
« no previous file with comments | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698