math-random.cc 2.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/math-random.h"

#include "src/assert-scope.h"
#include "src/base/utils/random-number-generator.h"
#include "src/contexts-inl.h"
#include "src/isolate.h"
#include "src/objects/fixed-array.h"
12
#include "src/objects/smi.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

namespace v8 {
namespace internal {

void MathRandom::InitializeContext(Isolate* isolate,
                                   Handle<Context> native_context) {
  Handle<FixedDoubleArray> cache = Handle<FixedDoubleArray>::cast(
      isolate->factory()->NewFixedDoubleArray(kCacheSize, TENURED));
  for (int i = 0; i < kCacheSize; i++) cache->set(i, 0);
  native_context->set_math_random_cache(*cache);
  Handle<PodArray<State>> pod = PodArray<State>::New(isolate, 1, TENURED);
  native_context->set_math_random_state(*pod);
  ResetContext(*native_context);
}

void MathRandom::ResetContext(Context* native_context) {
29
  native_context->set_math_random_index(Smi::zero());
30 31 32 33
  State state = {0, 0};
  PodArray<State>::cast(native_context->math_random_state())->set(0, state);
}

34
Address MathRandom::RefillCache(Isolate* isolate, Context* native_context) {
35 36 37 38 39 40 41 42
  DisallowHeapAllocation no_gc;
  PodArray<State>* pod =
      PodArray<State>::cast(native_context->math_random_state());
  State state = pod->get(0);
  // Initialize state if not yet initialized. If a fixed random seed was
  // requested, use it to reset our state the first time a script asks for
  // random numbers in this context. This ensures the script sees a consistent
  // sequence.
43 44
  if (state.s0 == 0 && state.s1 == 0) {
    uint64_t seed;
45
    if (FLAG_random_seed != 0) {
46
      seed = FLAG_random_seed;
47
    } else {
48
      isolate->random_number_generator()->NextBytes(&seed, sizeof(seed));
49
    }
50 51 52
    state.s0 = base::RandomNumberGenerator::MurmurHash3(seed);
    state.s1 = base::RandomNumberGenerator::MurmurHash3(~seed);
    CHECK(state.s0 != 0 || state.s1 != 0);
53 54 55 56 57 58 59 60 61 62 63 64
  }

  FixedDoubleArray* cache =
      FixedDoubleArray::cast(native_context->math_random_cache());
  // Create random numbers.
  for (int i = 0; i < kCacheSize; i++) {
    // Generate random numbers using xorshift128+.
    base::RandomNumberGenerator::XorShift128(&state.s0, &state.s1);
    cache->set(i, base::RandomNumberGenerator::ToDouble(state.s0));
  }
  pod->set(0, state);

65
  Smi new_index = Smi::FromInt(kCacheSize);
66
  native_context->set_math_random_index(new_index);
67
  return new_index.ptr();
68 69 70 71
}

}  // namespace internal
}  // namespace v8