Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
prng_xoshiro.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_PRNG_XOSHIRO_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_PRNG_XOSHIRO_HPP_
7
9#include "arene/base/stdlib_choice/cstdint.hpp"
10
11namespace arene {
12namespace base {
13namespace testing {
14
15/// @brief pseudorandom number generator implementing the xoshiro256++ algorithm
16///
17/// xoshiro256++ is a general-purpose PRNG designed by David Blackman and
18/// Sebastiano Vigna. It offers sub-nanosecond speed, a 256-bit state space
19/// large enough for parallel applications, and passes all known statistical
20/// test suites.
21///
22/// The generator satisfies the C++ UniformRandomBitGenerator named
23/// requirement, so it can be used with standard library distributions
24/// and algorithms that accept a URBG.
25///
26/// A single 64-bit seed is expanded into the full 256-bit internal state
27/// using the SplitMix64 generator, as recommended by the xoshiro authors.
28/// This ensures the state is never all-zero (the only disallowed state)
29/// for any seed value.
30///
31/// @see https://prng.di.unimi.it/
32/// @see https://prng.di.unimi.it/xoshiro256plusplus.c
34 /// @brief internal state of the generator
36
37 /// @brief bitwise rotate a value to the left
38 /// @tparam N number of bit positions to rotate
39 /// @param arg value to rotate.
40 /// @return rotated value
41 template <std::uint8_t N>
42 static constexpr auto rotate_left(std::uint64_t arg) -> std::uint64_t {
43 constexpr auto upper_limit = 64U;
44 static_assert(N < upper_limit, "rotation amount must be less than 64");
45
46 return N == 0U ? arg : ((arg << N) | (arg >> (upper_limit - N)));
47 }
48
49 /// @brief 64-bit PRNG used to expand a single seed into the four 64-bit state
50 /// words required by xoshiro256++
51 /// @see https://prng.di.unimi.it/splitmix64.c
52 struct splitmix64 {
53 /// @brief splimix64 state
54 std::uint64_t state{};
55
56 /// @brief advance the generator by one step and return the
57 /// scrambled output
58 /// @return next 64-bit pseudorandom value
59 constexpr auto next() noexcept -> std::uint64_t {
60 // fixed constants specified by the algorithm description
61 // NOLINTBEGIN(readability-magic-numbers)
62 auto mixed = (state += std::uint64_t{0x9e3779b97f4a7c15ULL});
63 mixed = (mixed ^ (mixed >> 30U)) * std::uint64_t{0xbf58476d1ce4e5b9ULL};
64 mixed = (mixed ^ (mixed >> 27U)) * std::uint64_t{0x94d049bb133111ebULL};
65 return mixed ^ (mixed >> 31U);
66 // NOLINTEND(readability-magic-numbers)
67 }
68 };
69
70 public:
71 /// @brief seed argument type
73
74 /// @brief prng result type
76
77 /// @brief construct the generator with an explicit seed
78 /// @param initial_seed initial seed value
79 constexpr explicit prng_xoshiro(seed_type initial_seed) noexcept {
80 auto seeder = splitmix64{initial_seed};
81 for (auto& state : state_) {
82 state = seeder.next();
83 }
84 }
85
86 /// @brief construct the generator with a seed of zero
87 constexpr prng_xoshiro() noexcept
89
90 /// @brief generate the next pseudorandom value and advance the
91 /// internal state
92 /// @return a uniformly distributed 64-bit pseudorandom value
93 /// in the range [min(), max()]
94 /// @see https://prng.di.unimi.it/xoshiro256plusplus.c
95 constexpr auto operator()() noexcept -> result_type {
96 // fixed constants specified by the algorithm description
97 // NOLINTBEGIN(readability-magic-numbers)
98 auto const result = rotate_left<23>(state_[0] + state_[3]) + state_[0];
99
100 auto const shifted = state_[1] << 17U;
101
102 state_[2] ^= state_[0];
103 state_[3] ^= state_[1];
104 state_[1] ^= state_[2];
105 state_[0] ^= state_[3];
106
107 state_[2] ^= shifted;
108
109 state_[3] = rotate_left<45>(state_[3]);
110 // NOLINTEND(readability-magic-numbers)
111
112 return result;
113 }
114
115 /// @brief smallest value that @c operator() can return
116 /// @return zero
117 static constexpr auto min() noexcept -> result_type { return {}; }
118
119 /// @brief largest value that @c operator() can return
120 /// @return ~min()
121 static constexpr auto max() noexcept -> result_type { return ~min(); }
122};
123
124} // namespace testing
125} // namespace base
126} // namespace arene
127#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_PRNG_XOSHIRO_HPP_
A fixed-size array of T objects.
Definition array.hpp:77
A sum type which can hold either a value or an error. Similar to std::expected.
Definition result.hpp:104
std::uint64_t result_type
prng result type
Definition prng_xoshiro.hpp:75
constexpr auto operator()() noexcept -> result_type
generate the next pseudorandom value and advance the internal state
Definition prng_xoshiro.hpp:95
static constexpr auto min() noexcept -> result_type
smallest value that operator() can return
Definition prng_xoshiro.hpp:117
constexpr prng_xoshiro() noexcept
construct the generator with a seed of zero
Definition prng_xoshiro.hpp:87
std::uint64_t seed_type
seed argument type
Definition prng_xoshiro.hpp:72
static constexpr auto max() noexcept -> result_type
largest value that operator() can return
Definition prng_xoshiro.hpp:121
constexpr prng_xoshiro(seed_type initial_seed) noexcept
construct the generator with an explicit seed
Definition prng_xoshiro.hpp:79
Definition customization.hpp:31
Definition accumulate.hpp:37
ARENE_NODISCARD constexpr auto next(Iterator itr, typename std::iterator_traits< Iterator >::difference_type steps=1) noexcept(noexcept(::arene::base::advance(itr, steps))) -> Iterator
Produces the Nth successor (or predecessor if steps is negative) of an iterator.
Definition next.hpp:38
Definition accumulate.hpp:36
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47