rippled
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>
33 {
34 public:
36 
37  xor_shift_engine(xor_shift_engine const&) = default;
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 
59 private:
61 
62  static result_type
64 };
65 
66 template <class _>
68 {
69  seed(val);
70 }
71 
72 template <class _>
73 void
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
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
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
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