Skip to content

Commit 8924f51

Browse files
committed
random: modernize XoRoShiRo128PlusPlus a bit
Make use of C++20 functions in XoRoShiRo128PlusPlus.
1 parent ddb7d26 commit 8924f51

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/test/util/xoroshiro128plusplus.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_TEST_UTIL_XOROSHIRO128PLUSPLUS_H
66
#define BITCOIN_TEST_UTIL_XOROSHIRO128PLUSPLUS_H
77

8+
#include <bit>
89
#include <cstdint>
910
#include <limits>
1011

@@ -21,26 +22,19 @@ class XoRoShiRo128PlusPlus
2122
uint64_t m_s0;
2223
uint64_t m_s1;
2324

24-
[[nodiscard]] constexpr static uint64_t rotl(uint64_t x, int n)
25-
{
26-
return (x << n) | (x >> (64 - n));
27-
}
28-
2925
[[nodiscard]] constexpr static uint64_t SplitMix64(uint64_t& seedval) noexcept
3026
{
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);
27+
uint64_t z = (seedval += 0x9e3779b97f4a7c15);
28+
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
29+
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
30+
return z ^ (z >> 31);
3531
}
3632

3733
public:
3834
using result_type = uint64_t;
3935

4036
constexpr explicit XoRoShiRo128PlusPlus(uint64_t seedval) noexcept
41-
: m_s0(SplitMix64(seedval)), m_s1(SplitMix64(seedval))
42-
{
43-
}
37+
: m_s0(SplitMix64(seedval)), m_s1(SplitMix64(seedval)) {}
4438

4539
// no copy - that is dangerous, we don't want accidentally copy the RNG and then have two streams
4640
// with exactly the same results. If you need a copy, call copy().
@@ -51,15 +45,13 @@ class XoRoShiRo128PlusPlus
5145
XoRoShiRo128PlusPlus(XoRoShiRo128PlusPlus&&) = default;
5246
XoRoShiRo128PlusPlus& operator=(XoRoShiRo128PlusPlus&&) = default;
5347

54-
~XoRoShiRo128PlusPlus() = default;
55-
5648
constexpr result_type operator()() noexcept
5749
{
5850
uint64_t s0 = m_s0, s1 = m_s1;
59-
const uint64_t result = rotl(s0 + s1, 17) + s0;
51+
const uint64_t result = std::rotl(s0 + s1, 17) + s0;
6052
s1 ^= s0;
61-
m_s0 = rotl(s0, 49) ^ s1 ^ (s1 << 21);
62-
m_s1 = rotl(s1, 28);
53+
m_s0 = std::rotl(s0, 49) ^ s1 ^ (s1 << 21);
54+
m_s1 = std::rotl(s1, 28);
6355
return result;
6456
}
6557

0 commit comments

Comments
 (0)