v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
math-random.cc
Go to the documentation of this file.
1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
12#include "src/objects/smi.h"
13
14namespace v8 {
15namespace internal {
16
19 auto cache = Cast<FixedDoubleArray>(
20 isolate->factory()->NewFixedDoubleArray(kCacheSize));
21 for (int i = 0; i < kCacheSize; i++) cache->set(i, 0);
22 native_context->set_math_random_cache(*cache);
25 native_context->set_math_random_state(*pod);
27}
28
30 native_context->set_math_random_index(Smi::zero());
31 State state = {0, 0};
32 Cast<PodArray<State>>(native_context->math_random_state())->set(0, state);
33}
34
35Address MathRandom::RefillCache(Isolate* isolate, Address raw_native_context) {
37 Cast<Context>(Tagged<Object>(raw_native_context));
40 Cast<PodArray<State>>(native_context->math_random_state());
41 State state = pod->get(0);
42 // Initialize state if not yet initialized. If a fixed random seed was
43 // requested, use it to reset our state the first time a script asks for
44 // random numbers in this context. This ensures the script sees a consistent
45 // sequence.
46 if (state.s0 == 0 && state.s1 == 0) {
47 uint64_t seed;
48 if (v8_flags.random_seed != 0) {
49 seed = v8_flags.random_seed;
50 } else {
51 isolate->random_number_generator()->NextBytes(&seed, sizeof(seed));
52 }
55 CHECK(state.s0 != 0 || state.s1 != 0);
56 }
57
59 Cast<FixedDoubleArray>(native_context->math_random_cache());
60 // Create random numbers.
61 for (int i = 0; i < kCacheSize; i++) {
62 // Generate random numbers using xorshift128+.
63 base::RandomNumberGenerator::XorShift128(&state.s0, &state.s1);
64 cache->set(i, base::RandomNumberGenerator::ToDouble(state.s0));
65 }
66 pod->set(0, state);
67
69 native_context->set_math_random_index(new_index);
70 return new_index.ptr();
71}
72
73} // namespace internal
74} // namespace v8
static double ToDouble(uint64_t state0)
static void XorShift128(uint64_t *state0, uint64_t *state1)
static Address RefillCache(Isolate *isolate, Address raw_native_context)
static void ResetContext(Tagged< Context > native_context)
static const int kCacheSize
Definition math-random.h:24
static void InitializeContext(Isolate *isolate, DirectHandle< Context > native_context)
static Handle< PodArray< T > > New(Isolate *isolate, int length, AllocationType allocation=AllocationType::kYoung)
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
static constexpr Tagged< Smi > zero()
Definition smi.h:99
V8_INLINE constexpr StorageType ptr() const
V8_EXPORT_PRIVATE FlagValues v8_flags
!IsContextMap !IsContextMap native_context
Definition map-inl.h:877
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define CHECK(condition)
Definition logging.h:124