rippled
Loading...
Searching...
No Matches
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
27namespace beast {
28
29namespace detail {
30
31template <class = void>
33{
34public:
36
39 operator=(xor_shift_engine const&) = default;
40
41 explicit xor_shift_engine(result_type val = 1977u);
42
43 void
45
47 operator()();
48
49 static result_type constexpr min()
50 {
52 }
53
54 static result_type constexpr max()
55 {
57 }
58
59private:
61
62 static result_type
64};
65
66template <class _>
68{
69 seed(val);
70}
71
72template <class _>
73void
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
82template <class _>
83auto
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
93template <class _>
94auto
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
115
116} // namespace beast
117
118#endif
static result_type constexpr min()
void seed(result_type seed)
static result_type constexpr max()
xor_shift_engine & operator=(xor_shift_engine const &)=default
xor_shift_engine(xor_shift_engine const &)=default
static result_type murmurhash3(result_type x)
T max(T... args)
T min(T... args)