rippled
ripple
beast
xor_shift_engine.h
1
//------------------------------------------------------------------------------
2
/*
3
This file is part of Beast: https://github.com/vinniefalco/Beast
4
Copyright 2014, Vinnie Falco <vinnie.falco@gmail.com>
5
6
Permission to use, copy, modify, and/or distribute this software for any
7
purpose with or without fee is hereby granted, provided that the above
8
copyright notice and this permission notice appear in all copies.
9
10
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
//==============================================================================
19
20
#ifndef BEAST_RANDOM_XOR_SHIFT_ENGINE_H_INCLUDED
21
#define BEAST_RANDOM_XOR_SHIFT_ENGINE_H_INCLUDED
22
23
#include <
cstdint
>
24
#include <
limits
>
25
#include <
stdexcept
>
26
27
namespace
beast
{
28
29
namespace
detail {
30
31
template
<
class
=
void
>
32
class
xor_shift_engine
33
{
34
public
:
35
using
result_type
=
std::uint64_t
;
36
37
xor_shift_engine
(
xor_shift_engine
const
&) =
default
;
38
xor_shift_engine
&
39
operator=
(
xor_shift_engine
const
&) =
default
;
40
41
explicit
xor_shift_engine
(
result_type
val = 1977u);
42
43
void
44
seed
(
result_type
seed
);
45
46
result_type
47
operator()
();
48
49
static
result_type
constexpr
min
()
50
{
51
return
std::numeric_limits<result_type>::min
();
52
}
53
54
static
result_type
constexpr
max
()
55
{
56
return
std::numeric_limits<result_type>::max
();
57
}
58
59
private
:
60
result_type
s_
[2];
61
62
static
result_type
63
murmurhash3
(
result_type
x);
64
};
65
66
template
<
class
_>
67
xor_shift_engine<_>::xor_shift_engine
(
result_type
val)
68
{
69
seed(val);
70
}
71
72
template
<
class
_>
73
void
74
xor_shift_engine<_>::seed
(
result_type
seed)
75
{
76
if
(seed == 0)
77
throw
std::domain_error
(
"invalid seed"
);
78
s_[0] = murmurhash3(seed);
79
s_[1] = murmurhash3(s_[0]);
80
}
81
82
template
<
class
_>
83
auto
84
xor_shift_engine<_>::operator()
() ->
result_type
85
{
86
result_type
s1 = s_[0];
87
result_type
const
s0 = s_[1];
88
s_[0] = s0;
89
s1 ^= s1 << 23;
90
return
(s_[1] = (s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26))) + s0;
91
}
92
93
template
<
class
_>
94
auto
95
xor_shift_engine<_>::murmurhash3
(
result_type
x) ->
result_type
96
{
97
x ^= x >> 33;
98
x *= 0xff51afd7ed558ccdULL;
99
x ^= x >> 33;
100
x *= 0xc4ceb9fe1a85ec53ULL;
101
return
x ^= x >> 33;
102
}
103
104
}
// namespace detail
105
114
using
xor_shift_engine
=
detail::xor_shift_engine<>
;
115
116
}
// namespace beast
117
118
#endif
std::domain_error
STL class.
beast::detail::xor_shift_engine::operator()
result_type operator()()
Definition:
xor_shift_engine.h:84
beast::detail::xor_shift_engine::murmurhash3
static result_type murmurhash3(result_type x)
Definition:
xor_shift_engine.h:95
stdexcept
beast::detail::xor_shift_engine::xor_shift_engine
xor_shift_engine(xor_shift_engine const &)=default
beast::detail::xor_shift_engine::seed
void seed(result_type seed)
Definition:
xor_shift_engine.h:74
cstdint
std::uint64_t
beast::detail::xor_shift_engine::max
static constexpr result_type max()
Definition:
xor_shift_engine.h:54
beast::detail::xor_shift_engine::operator=
xor_shift_engine & operator=(xor_shift_engine const &)=default
std::numeric_limits::min
T min(T... args)
limits
beast::detail::xor_shift_engine
Definition:
xor_shift_engine.h:32
std::numeric_limits::max
T max(T... args)
beast::detail::xor_shift_engine::s_
result_type s_[2]
Definition:
xor_shift_engine.h:60
beast::detail::xor_shift_engine::min
static constexpr result_type min()
Definition:
xor_shift_engine.h:49
beast
Definition:
base_uint.h:641
Generated by
1.8.17