math-random.cc 2.63 KB
Newer Older
1 2 3 4
// 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.

5
#include "src/numbers/math-random.h"
6 7

#include "src/base/utils/random-number-generator.h"
8
#include "src/common/assert-scope.h"
9
#include "src/execution/isolate.h"
10
#include "src/objects/contexts-inl.h"
11
#include "src/objects/fixed-array.h"
12
#include "src/objects/smi.h"
13 14 15 16 17 18

namespace v8 {
namespace internal {

void MathRandom::InitializeContext(Isolate* isolate,
                                   Handle<Context> native_context) {
19 20
  Handle<FixedDoubleArray> cache = Handle<FixedDoubleArray>::cast(
      isolate->factory()->NewFixedDoubleArray(kCacheSize));
21 22
  for (int i = 0; i < kCacheSize; i++) cache->set(i, 0);
  native_context->set_math_random_cache(*cache);
23 24
  Handle<PodArray<State>> pod =
      PodArray<State>::New(isolate, 1, AllocationType::kOld);
25 26 27 28
  native_context->set_math_random_state(*pod);
  ResetContext(*native_context);
}

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

35
Address MathRandom::RefillCache(Isolate* isolate, Address raw_native_context) {
36
  Context native_context = Context::cast(Object(raw_native_context));
37
  DisallowGarbageCollection no_gc;
38
  PodArray<State> pod =
39 40
      PodArray<State>::cast(native_context.math_random_state());
  State state = pod.get(0);
41 42 43 44
  // 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.
45 46
  if (state.s0 == 0 && state.s1 == 0) {
    uint64_t seed;
47
    if (FLAG_random_seed != 0) {
48
      seed = FLAG_random_seed;
49
    } else {
50
      isolate->random_number_generator()->NextBytes(&seed, sizeof(seed));
51
    }
52 53 54
    state.s0 = base::RandomNumberGenerator::MurmurHash3(seed);
    state.s1 = base::RandomNumberGenerator::MurmurHash3(~seed);
    CHECK(state.s0 != 0 || state.s1 != 0);
55 56
  }

57
  FixedDoubleArray cache =
58
      FixedDoubleArray::cast(native_context.math_random_cache());
59 60 61 62
  // Create random numbers.
  for (int i = 0; i < kCacheSize; i++) {
    // Generate random numbers using xorshift128+.
    base::RandomNumberGenerator::XorShift128(&state.s0, &state.s1);
63
    cache.set(i, base::RandomNumberGenerator::ToDouble(state.s0));
64
  }
65
  pod.set(0, state);
66

67
  Smi new_index = Smi::FromInt(kCacheSize);
68
  native_context.set_math_random_index(new_index);
69
  return new_index.ptr();
70 71 72 73
}

}  // namespace internal
}  // namespace v8