5 #include "src/math-random.h" 7 #include "src/assert-scope.h" 8 #include "src/base/utils/random-number-generator.h" 9 #include "src/contexts-inl.h" 10 #include "src/isolate.h" 11 #include "src/objects/fixed-array.h" 12 #include "src/objects/smi.h" 17 void MathRandom::InitializeContext(Isolate* isolate,
18 Handle<Context> native_context) {
19 Handle<FixedDoubleArray> cache = Handle<FixedDoubleArray>::cast(
20 isolate->factory()->NewFixedDoubleArray(kCacheSize, TENURED));
21 for (
int i = 0;
i < kCacheSize;
i++) cache->set(
i, 0);
22 native_context->set_math_random_cache(*cache);
23 Handle<PodArray<State>> pod = PodArray<State>::New(isolate, 1, TENURED);
24 native_context->set_math_random_state(*pod);
25 ResetContext(*native_context);
28 void MathRandom::ResetContext(Context native_context) {
29 native_context->set_math_random_index(Smi::zero());
31 PodArray<State>::cast(native_context->math_random_state())->
set(0, state);
34 Address MathRandom::RefillCache(Isolate* isolate, Address raw_native_context) {
35 Context native_context = Context::cast(ObjectPtr(raw_native_context));
36 DisallowHeapAllocation no_gc;
38 PodArray<State>::cast(native_context->math_random_state());
39 State state = pod->get(0);
44 if (state.s0 == 0 && state.s1 == 0) {
46 if (FLAG_random_seed != 0) {
47 seed = FLAG_random_seed;
49 isolate->random_number_generator()->NextBytes(&seed,
sizeof(seed));
51 state.s0 = base::RandomNumberGenerator::MurmurHash3(seed);
52 state.s1 = base::RandomNumberGenerator::MurmurHash3(~seed);
53 CHECK(state.s0 != 0 || state.s1 != 0);
56 FixedDoubleArray cache =
57 FixedDoubleArray::cast(native_context->math_random_cache());
59 for (
int i = 0;
i < kCacheSize;
i++) {
61 base::RandomNumberGenerator::XorShift128(&state.s0, &state.s1);
62 cache->set(
i, base::RandomNumberGenerator::ToDouble(state.s0));
66 Smi new_index = Smi::FromInt(kCacheSize);
67 native_context->set_math_random_index(new_index);
68 return new_index.ptr();