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
&
operator=
(
xor_shift_engine
const
&) =
default
;
39
40
explicit
41
xor_shift_engine
(
result_type
val = 1977u);
42
43
void
44
seed
(
result_type
seed
);
45
46
result_type
47
operator()
();
48
49
static
50
result_type
constexpr
51
min
()
52
{
53
return
std::numeric_limits<result_type>::min
();
54
}
55
56
static
57
result_type
constexpr
58
max
()
59
{
60
return
std::numeric_limits<result_type>::max
();
61
}
62
63
private
:
64
result_type
s_
[2];
65
66
static
67
result_type
68
murmurhash3
(
result_type
x);
69
};
70
71
template
<
class
_>
72
xor_shift_engine<_>::xor_shift_engine
(
73
result_type
val)
74
{
75
seed (val);
76
}
77
78
template
<
class
_>
79
void
80
xor_shift_engine<_>::seed
(
result_type
seed)
81
{
82
if
(seed == 0)
83
throw
std::domain_error
(
"invalid seed"
);
84
s_[0] = murmurhash3 (seed);
85
s_[1] = murmurhash3 (s_[0]);
86
}
87
88
template
<
class
_>
89
auto
90
xor_shift_engine<_>::operator()
() ->
91
result_type
92
{
93
result_type
s1 = s_[0];
94
result_type
const
s0 = s_[1];
95
s_[0] = s0;
96
s1 ^= s1 << 23;
97
return
(s_[1] = (s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26))) + s0;
98
}
99
100
template
<
class
_>
101
auto
102
xor_shift_engine<_>::murmurhash3
(
result_type
x)
103
->
result_type
104
{
105
x ^= x >> 33;
106
x *= 0xff51afd7ed558ccdULL;
107
x ^= x >> 33;
108
x *= 0xc4ceb9fe1a85ec53ULL;
109
return
x ^= x >> 33;
110
}
111
112
}
// detail
113
122
using
xor_shift_engine
=
detail::xor_shift_engine<>
;
123
124
}
125
126
#endif
std::domain_error
STL class.
beast::detail::xor_shift_engine::result_type
std::uint64_t result_type
Definition:
xor_shift_engine.h:35
beast::detail::xor_shift_engine::operator()
result_type operator()()
Definition:
xor_shift_engine.h:90
beast::detail::xor_shift_engine::murmurhash3
static result_type murmurhash3(result_type x)
Definition:
xor_shift_engine.h:102
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:80
cstdint
std::uint64_t
beast::detail::xor_shift_engine::max
static constexpr result_type max()
Definition:
xor_shift_engine.h:58
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:64
beast::detail::xor_shift_engine::min
static constexpr result_type min()
Definition:
xor_shift_engine.h:51
beast
Definition:
base_uint.h:582
Generated by
1.8.17