Bitcoin Core  26.99.0
P2P Digital Currency
xoroshiro128plusplus.h
Go to the documentation of this file.
1 // Copyright (c) 2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_TEST_UTIL_XOROSHIRO128PLUSPLUS_H
6 #define BITCOIN_TEST_UTIL_XOROSHIRO128PLUSPLUS_H
7 
8 #include <cstdint>
9 #include <limits>
10 
20 {
21  uint64_t m_s0;
22  uint64_t m_s1;
23 
24  [[nodiscard]] constexpr static uint64_t rotl(uint64_t x, int n)
25  {
26  return (x << n) | (x >> (64 - n));
27  }
28 
29  [[nodiscard]] constexpr static uint64_t SplitMix64(uint64_t& seedval) noexcept
30  {
31  uint64_t z = (seedval += UINT64_C(0x9e3779b97f4a7c15));
32  z = (z ^ (z >> 30U)) * UINT64_C(0xbf58476d1ce4e5b9);
33  z = (z ^ (z >> 27U)) * UINT64_C(0x94d049bb133111eb);
34  return z ^ (z >> 31U);
35  }
36 
37 public:
38  using result_type = uint64_t;
39 
40  constexpr explicit XoRoShiRo128PlusPlus(uint64_t seedval) noexcept
41  : m_s0(SplitMix64(seedval)), m_s1(SplitMix64(seedval))
42  {
43  }
44 
45  // no copy - that is dangerous, we don't want accidentally copy the RNG and then have two streams
46  // with exactly the same results. If you need a copy, call copy().
49 
50  // allow moves
53 
54  ~XoRoShiRo128PlusPlus() = default;
55 
56  constexpr result_type operator()() noexcept
57  {
58  uint64_t s0 = m_s0, s1 = m_s1;
59  const uint64_t result = rotl(s0 + s1, 17) + s0;
60  s1 ^= s0;
61  m_s0 = rotl(s0, 49) ^ s1 ^ (s1 << 21);
62  m_s1 = rotl(s1, 28);
63  return result;
64  }
65 
66  static constexpr result_type min() noexcept { return std::numeric_limits<result_type>::min(); }
67  static constexpr result_type max() noexcept { return std::numeric_limits<result_type>::max(); }
68  static constexpr double entropy() noexcept { return 0.0; }
69 };
70 
71 #endif // BITCOIN_TEST_UTIL_XOROSHIRO128PLUSPLUS_H
xoroshiro128++ PRNG.
constexpr XoRoShiRo128PlusPlus(uint64_t seedval) noexcept
constexpr static uint64_t rotl(uint64_t x, int n)
static constexpr result_type max() noexcept
XoRoShiRo128PlusPlus & operator=(XoRoShiRo128PlusPlus &&)=default
constexpr static uint64_t SplitMix64(uint64_t &seedval) noexcept
static constexpr double entropy() noexcept
XoRoShiRo128PlusPlus(const XoRoShiRo128PlusPlus &)=delete
~XoRoShiRo128PlusPlus()=default
XoRoShiRo128PlusPlus(XoRoShiRo128PlusPlus &&)=default
static constexpr result_type min() noexcept
XoRoShiRo128PlusPlus & operator=(const XoRoShiRo128PlusPlus &)=delete
constexpr result_type operator()() noexcept