Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
random.cc
Go to the documentation of this file.
1// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/random.h"
6#include "vm/dart.h"
7#include "vm/flags.h"
8#include "vm/os.h"
9
10namespace dart {
11
12DEFINE_FLAG(uint64_t,
13 random_seed,
14 0,
15 "Override the random seed for debugging.");
16
18 uint64_t seed = FLAG_random_seed;
19 if (seed == 0) {
21 if (callback != nullptr) {
22 if (!callback(reinterpret_cast<uint8_t*>(&seed), sizeof(seed))) {
23 // Callback failed. Reset the seed to 0.
24 seed = 0;
25 }
26 }
27 }
28 if (seed == 0) {
29 // We did not get a seed so far. As a fallback we do use the current time.
31 }
32 Initialize(seed);
33}
34
35void Random::Initialize(uint64_t seed) {
36 ASSERT(seed != 0);
37 // Crank the next state a couple of times.
38 _state = seed;
39 NextState();
40 NextState();
41 NextState();
42 NextState();
43}
44
45Random::Random(uint64_t seed) {
46 Initialize(seed);
47}
48
50 // Nothing to be done here.
51}
52
53// The algorithm used here is Multiply with Carry (MWC) with a Base b = 2^32.
54// http://en.wikipedia.org/wiki/Multiply-with-carry
55// The constant A is selected from "Numerical Recipes 3rd Edition" p.348 B1.
56uint64_t Random::NextState() {
57 const uint64_t MASK_32 = 0xffffffff;
58 const uint64_t A = 0xffffda61;
59
60 uint64_t old_state = _state;
61 while (true) {
62 const uint64_t state_lo = old_state & MASK_32;
63 const uint64_t state_hi = (old_state >> 32) & MASK_32;
64 const uint64_t new_state = (A * state_lo) + state_hi;
65 if (_state.compare_exchange_weak(old_state, new_state,
66 std::memory_order_relaxed,
67 std::memory_order_relaxed)) {
68 return new_state;
69 }
70 }
71}
72
74 const uint64_t MASK_32 = 0xffffffff;
75 return static_cast<uint32_t>(NextState() & MASK_32);
76}
77
78static Random* global_random = nullptr;
79static Mutex* global_random_mutex = nullptr;
80
82 ASSERT(global_random_mutex == nullptr);
83 global_random_mutex = new Mutex(NOT_IN_PRODUCT("global_random_mutex"));
84 ASSERT(global_random == nullptr);
85 global_random = new Random();
86}
87
90 global_random_mutex = nullptr;
91 delete global_random;
92 global_random = nullptr;
93}
94
99
101 uint64_t mantissa = NextUInt64() & 0xFFFFFFFFFFFFF;
102 // The exponent value 0 in biased form.
103 const uint64_t exp = 1023;
104 return bit_cast<double>(exp << 52 | mantissa) - 1.0;
105}
106
107} // namespace dart
static Dart_EntropySource entropy_source_callback()
Definition dart.h:135
static int64_t GetCurrentTimeMicros()
static uint64_t GlobalNextUInt64()
Definition random.cc:95
static void Cleanup()
Definition random.cc:88
uint64_t NextUInt64()
Definition random.h:26
static void Init()
Definition random.cc:81
double NextDouble()
Definition random.cc:100
uint32_t NextUInt32()
Definition random.cc:73
bool(* Dart_EntropySource)(uint8_t *buffer, intptr_t length)
Definition dart_api.h:821
#define ASSERT(E)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define DEFINE_FLAG(type, name, default_value, comment)
Definition flags.h:16
static Mutex * global_random_mutex
Definition random.cc:79
static Random * global_random
Definition random.cc:78
#define NOT_IN_PRODUCT(code)
Definition globals.h:84