19 static constexpr std::uint64_t
default_seed = 0x853C49E6748FEA9BULL;
27 template <
class SeedSeq>
28 requires(!std::convertible_to<SeedSeq &, std::uint64_t>)
29 explicit Pcg32(SeedSeq &sequence)
40 increment = (stream << 1) | 1;
46 template <
class SeedSeq>
47 requires(!std::convertible_to<SeedSeq &, std::uint64_t>)
48 void seed(SeedSeq &sequence)
50 std::uint32_t words[4] = {};
51 sequence.generate(words, words + 4);
52 seed((
static_cast<std::uint64_t
>(words[0]) << 32) | words[1],
53 (
static_cast<std::uint64_t
>(words[2]) << 32) | words[3]);
58 std::uint64_t previous = state;
60 auto xorShifted =
static_cast<std::uint32_t
>(((previous >> 18) ^ previous) >> 27);
61 return std::rotr(xorShifted,
static_cast<int>(previous >> 59));
64 constexpr void discard(
unsigned long long n)
noexcept
73 static constexpr std::uint64_t MULTIPLIER = 6364136223846793005ULL;
74 std::uint64_t state = 0;
75 std::uint64_t increment = 1;
77 constexpr void step() noexcept { state = state * MULTIPLIER + increment; }
89 static constexpr std::uint64_t
default_seed = 0xCAFEF00DD15EA5E5ULL;
97 template <
class SeedSeq>
98 requires(!std::convertible_to<SeedSeq &, std::uint64_t>)
99 explicit Pcg64(SeedSeq &sequence)
110 seed(UInt128{0, value}, UInt128{0, stream});
113 template <
class SeedSeq>
114 requires(!std::convertible_to<SeedSeq &, std::uint64_t>)
117 std::uint32_t w[8] = {};
118 sequence.generate(w, w + 8);
119 auto word = [&w](
int i) {
return (
static_cast<std::uint64_t
>(w[i]) << 32) | w[i + 1]; };
120 seed(UInt128{word(0), word(2)}, UInt128{word(4), word(6)});
126 return std::rotr(state.hi ^ state.lo,
static_cast<int>(state.hi >> 58));
129 constexpr void discard(
unsigned long long n)
noexcept
140 std::uint64_t hi = 0;
141 std::uint64_t lo = 0;
143 friend constexpr bool operator==(
const UInt128 &,
const UInt128 &) =
default;
145 friend constexpr UInt128 operator+(UInt128 a, UInt128 b)
noexcept
147 std::uint64_t lowSum = a.lo + b.lo;
148 return {a.hi + b.hi + (lowSum < a.lo ? 1 : 0), lowSum};
151 friend constexpr UInt128 operator*(UInt128 a, UInt128 b)
noexcept
154 std::uint64_t a0 = a.lo & 0xFFFFFFFFULL, a1 = a.lo >> 32, b0 = b.lo & 0xFFFFFFFFULL, b1 = b.lo >> 32;
155 std::uint64_t p00 = a0 * b0, p01 = a0 * b1, p10 = a1 * b0, p11 = a1 * b1;
156 std::uint64_t middle = (p00 >> 32) + (p01 & 0xFFFFFFFFULL) + (p10 & 0xFFFFFFFFULL);
157 std::uint64_t lowProduct = (middle << 32) | (p00 & 0xFFFFFFFFULL);
158 std::uint64_t highProduct = p11 + (p01 >> 32) + (p10 >> 32) + (middle >> 32);
159 highProduct += a.hi * b.lo + a.lo * b.hi;
160 return {highProduct, lowProduct};
164 static constexpr UInt128 MULTIPLIER{0x2360ED051FC65DA4ULL, 0x4385DF649FCCF645ULL};
166 UInt128 increment{0, 1};
168 constexpr void seed(UInt128 value, UInt128 stream)
noexcept
171 increment = UInt128{(stream.hi << 1) | (stream.lo >> 63), (stream.lo << 1) | 1};
173 state = state + value;
177 constexpr void step() noexcept { state = state * MULTIPLIER + increment; }
The Pcg32 class PCG-XSH-RR with 64 bits of state and 32-bit output (Melissa O'Neill,...
Definition pcg.hpp:16
static constexpr result_type max() noexcept
Definition pcg.hpp:35
constexpr result_type operator()() noexcept
Definition pcg.hpp:56
constexpr Pcg32(std::uint64_t value=default_seed, std::uint64_t stream=default_stream) noexcept
Definition pcg.hpp:22
friend constexpr bool operator==(const Pcg32 &, const Pcg32 &)=default
static constexpr std::uint64_t default_seed
Definition pcg.hpp:19
constexpr void discard(unsigned long long n) noexcept
Definition pcg.hpp:64
std::uint32_t result_type
Definition pcg.hpp:18
void seed(SeedSeq &sequence)
Definition pcg.hpp:48
Pcg32(SeedSeq &sequence)
Definition pcg.hpp:29
static constexpr result_type min() noexcept
Definition pcg.hpp:34
constexpr void seed(std::uint64_t value, std::uint64_t stream=default_stream) noexcept
Definition pcg.hpp:37
static constexpr std::uint64_t default_stream
Definition pcg.hpp:20
The Pcg64 class PCG-XSL-RR with 128 bits of state and 64-bit output, the pcg64 engine of the refere...
Definition pcg.hpp:86
static constexpr result_type min() noexcept
Definition pcg.hpp:104
static constexpr result_type max() noexcept
Definition pcg.hpp:105
static constexpr std::uint64_t default_seed
Definition pcg.hpp:89
constexpr void seed(std::uint64_t value, std::uint64_t stream=default_stream) noexcept
pcg_engines::setseq_xsl_rr_128_64(seed, stream) with 64-bit seed and stream
Definition pcg.hpp:108
Pcg64(SeedSeq &sequence)
Definition pcg.hpp:99
constexpr Pcg64(std::uint64_t value=default_seed, std::uint64_t stream=default_stream) noexcept
Definition pcg.hpp:92
std::uint64_t result_type
Definition pcg.hpp:88
constexpr result_type operator()() noexcept
Definition pcg.hpp:123
constexpr void discard(unsigned long long n) noexcept
Definition pcg.hpp:129
void seed(SeedSeq &sequence)
Definition pcg.hpp:115
static constexpr std::uint64_t default_stream
Definition pcg.hpp:90
friend constexpr bool operator==(const Pcg64 &, const Pcg64 &)=default
Definition bivariate_normal.hpp:10