xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
beast
xor_shift_engine.h
1
#pragma once
2
3
#include <
cstdint
>
4
#include <
limits
>
5
#include <
stdexcept
>
6
7
namespace
beast
{
8
9
namespace
detail
{
10
11
template
<
class
=
void
>
12
class
XorShiftEngine
13
{
14
public
:
15
using
result_type
=
std::uint64_t
;
16
17
XorShiftEngine
(
XorShiftEngine
const
&) =
default
;
18
XorShiftEngine
&
19
operator=
(
XorShiftEngine
const
&) =
default
;
20
21
explicit
XorShiftEngine
(
result_type
val = 1977u);
22
23
void
24
seed
(
result_type
seed
);
25
26
result_type
27
operator()
();
28
29
static
constexpr
result_type
30
min
()
31
{
32
return
std::numeric_limits<result_type>::min
();
33
}
34
35
static
constexpr
result_type
36
max
()
37
{
38
return
std::numeric_limits<result_type>::max
();
39
}
40
41
private
:
42
result_type
s_
[2]{};
43
44
static
result_type
45
murmurhash3
(
result_type
x);
46
};
47
48
template
<
class
Unused>
49
XorShiftEngine<Unused>::XorShiftEngine
(
result_type
val)
50
{
51
seed
(val);
52
}
53
54
template
<
class
Unused>
55
void
56
XorShiftEngine<Unused>::seed
(
result_type
seed
)
57
{
58
if
(
seed
== 0)
59
throw
std::domain_error
(
"invalid seed"
);
60
s_
[0] =
murmurhash3
(
seed
);
61
s_
[1] =
murmurhash3
(
s_
[0]);
62
}
63
64
template
<
class
Unused>
65
auto
66
XorShiftEngine<Unused>::operator()
() ->
result_type
67
{
68
result_type
s1 =
s_
[0];
69
result_type
const
s0 =
s_
[1];
70
s_
[0] = s0;
71
s1 ^= s1 << 23;
72
return
(
s_
[1] = (s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26))) + s0;
73
}
74
75
template
<
class
Unused>
76
auto
77
XorShiftEngine<Unused>::murmurhash3
(
result_type
x) ->
result_type
78
{
79
x ^= x >> 33;
80
x *= 0xff51afd7ed558ccdULL;
81
x ^= x >> 33;
82
x *= 0xc4ceb9fe1a85ec53ULL;
83
return
x ^= x >> 33;
84
}
85
86
}
// namespace detail
87
97
using
XorShiftEngine
=
detail::XorShiftEngine<>
;
98
99
}
// namespace beast
beast::detail::XorShiftEngine
Definition
xor_shift_engine.h:13
beast::detail::XorShiftEngine::min
static constexpr result_type min()
Definition
xor_shift_engine.h:30
beast::detail::XorShiftEngine::max
static constexpr result_type max()
Definition
xor_shift_engine.h:36
beast::detail::XorShiftEngine::result_type
std::uint64_t result_type
Definition
xor_shift_engine.h:15
beast::detail::XorShiftEngine::seed
void seed(result_type seed)
Definition
xor_shift_engine.h:56
beast::detail::XorShiftEngine::XorShiftEngine
XorShiftEngine(XorShiftEngine const &)=default
beast::detail::XorShiftEngine::s_
result_type s_[2]
Definition
xor_shift_engine.h:42
beast::detail::XorShiftEngine::operator()
result_type operator()()
Definition
xor_shift_engine.h:66
beast::detail::XorShiftEngine::murmurhash3
static result_type murmurhash3(result_type x)
Definition
xor_shift_engine.h:77
beast::detail::XorShiftEngine::operator=
XorShiftEngine & operator=(XorShiftEngine const &)=default
cstdint
std::domain_error
std::uint64_t
limits
std::numeric_limits::max
T max(T... args)
std::numeric_limits::min
T min(T... args)
beast::detail
Definition
abstract_clock.h:59
beast
Definition
base_uint.h:697
beast::XorShiftEngine
detail::XorShiftEngine<> XorShiftEngine
XOR-shift Generator.
Definition
xor_shift_engine.h:97
stdexcept
beast::result_type
Generated by
1.17.0