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

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

Issue 2760393002: Enable deterministic random number generation (Closed)
Patch Set: Reset in Runtime_GenerateRandomNumbers instead Created 3 years, 9 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
« no previous file with comments | « no previous file | test/cctest/test-api.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 20 matching lines...) Expand all
31 31
32 Handle<FixedDoubleArray> cache; 32 Handle<FixedDoubleArray> cache;
33 uint64_t state0 = 0; 33 uint64_t state0 = 0;
34 uint64_t state1 = 0; 34 uint64_t state1 = 0;
35 if (native_context->math_random_cache()->IsFixedDoubleArray()) { 35 if (native_context->math_random_cache()->IsFixedDoubleArray()) {
36 cache = Handle<FixedDoubleArray>( 36 cache = Handle<FixedDoubleArray>(
37 FixedDoubleArray::cast(native_context->math_random_cache()), isolate); 37 FixedDoubleArray::cast(native_context->math_random_cache()), isolate);
38 state0 = double_to_uint64(cache->get_scalar(kState0Offset)); 38 state0 = double_to_uint64(cache->get_scalar(kState0Offset));
39 state1 = double_to_uint64(cache->get_scalar(kState1Offset)); 39 state1 = double_to_uint64(cache->get_scalar(kState1Offset));
40 } else { 40 } else {
41 // If a fixed random seed was requested, reset it whenever a script asks for
42 // random numbers for the first time in this context. This ensures the
43 // script sees a consistent sequence.
44 if (FLAG_random_seed != 0)
45 isolate->random_number_generator()->SetSeed(FLAG_random_seed);
Yang 2017/03/23 12:30:13 I wonder whether it would be easier to simply set
Sami 2017/03/23 17:00:27 Sorry, missed this comment earlier. Yeah, that wou
41 cache = Handle<FixedDoubleArray>::cast( 46 cache = Handle<FixedDoubleArray>::cast(
42 isolate->factory()->NewFixedDoubleArray(kCacheSize, TENURED)); 47 isolate->factory()->NewFixedDoubleArray(kCacheSize, TENURED));
43 native_context->set_math_random_cache(*cache); 48 native_context->set_math_random_cache(*cache);
44 // Initialize state if not yet initialized. 49 // Initialize state if not yet initialized.
45 while (state0 == 0 || state1 == 0) { 50 while (state0 == 0 || state1 == 0) {
46 isolate->random_number_generator()->NextBytes(&state0, sizeof(state0)); 51 isolate->random_number_generator()->NextBytes(&state0, sizeof(state0));
47 isolate->random_number_generator()->NextBytes(&state1, sizeof(state1)); 52 isolate->random_number_generator()->NextBytes(&state1, sizeof(state1));
48 } 53 }
49 } 54 }
50 55
51 DisallowHeapAllocation no_gc; 56 DisallowHeapAllocation no_gc;
52 FixedDoubleArray* raw_cache = *cache; 57 FixedDoubleArray* raw_cache = *cache;
53 // Create random numbers. 58 // Create random numbers.
54 for (int i = 0; i < kInitialIndex; i++) { 59 for (int i = 0; i < kInitialIndex; i++) {
55 // Generate random numbers using xorshift128+. 60 // Generate random numbers using xorshift128+.
56 base::RandomNumberGenerator::XorShift128(&state0, &state1); 61 base::RandomNumberGenerator::XorShift128(&state0, &state1);
57 raw_cache->set(i, base::RandomNumberGenerator::ToDouble(state0, state1)); 62 raw_cache->set(i, base::RandomNumberGenerator::ToDouble(state0, state1));
58 } 63 }
59 64
60 // Persist current state. 65 // Persist current state.
61 raw_cache->set(kState0Offset, uint64_to_double(state0)); 66 raw_cache->set(kState0Offset, uint64_to_double(state0));
62 raw_cache->set(kState1Offset, uint64_to_double(state1)); 67 raw_cache->set(kState1Offset, uint64_to_double(state1));
63 return Smi::FromInt(kInitialIndex); 68 return Smi::FromInt(kInitialIndex);
64 } 69 }
65 } // namespace internal 70 } // namespace internal
66 } // namespace v8 71 } // namespace v8
OLDNEW
« no previous file with comments | « no previous file | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698