Debian Codegraph @

src/mpt/random/engine_lcg.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_RANDOM_ENGINE_LCG_HPP
4 #define MPT_RANDOM_ENGINE_LCG_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/macros.hpp"
10 #include "mpt/base/integer.hpp"
11 #include "mpt/base/namespace.hpp"
12 #include "mpt/base/numeric.hpp"
13
14 #include <array>
15 #include <random>
16
17 #include <cstddef>
18
19
20
21 namespace mpt {
22 inline namespace MPT_INLINE_NS {
23
24
25 #if MPT_COMPILER_MSVC
26 #pragma warning(push)
27 #pragma warning(disable : 4724) // potential mod by 0
28 #endif // MPT_COMPILER_MSVC
29
30 template <typename Tstate, typename Tvalue, Tstate m, Tstate a, Tstate c, Tstate result_mask, int result_shift, int result_bits_>
31 class lcg_engine {
32 public:
33 typedef Tstate state_type;
34 typedef Tvalue result_type;
35 static inline constexpr std::size_t seed_bits = sizeof(state_type) * 8;
36
37 private:
38 state_type state;
39
40 private:
41 static inline state_type seed_state(std::seed_seq & seed) {
42 state_type result = 0;
43 std::array<unsigned int, mpt::align_up<std::size_t>(seed_bits, sizeof(unsigned int) * 8) / (sizeof(unsigned int) * 8)> seeds = {};
44 seed.generate(seeds.begin(), seeds.end());
45 for (const auto & seed_value : seeds) {
46 result <<= 16;
47 result <<= 16;
48 result |= static_cast<state_type>(seed_value);
49 }
50 return result;
51 }
52
53 public:
54 explicit inline lcg_engine(std::seed_seq & seed)
55 : state(seed_state(seed)) {
56 operator()(); // we return results from the current state and update state after returning. results in better pipelining.
57 }
58 explicit inline lcg_engine(state_type seed)
59 : state(seed) {
60 operator()(); // we return results from the current state and update state after returning. results in better pipelining.
61 }
62
63 public:
64 static MPT_CONSTEXPRINLINE result_type min() {
65 return static_cast<result_type>(0);
66 }
67 static MPT_CONSTEXPRINLINE result_type max() {
68 static_assert(((result_mask >> result_shift) << result_shift) == result_mask);
69 return static_cast<result_type>(result_mask >> result_shift);
70 }
71 static MPT_CONSTEXPRINLINE int result_bits() {
72 static_assert(((static_cast<Tstate>(1) << result_bits_) - 1) == (result_mask >> result_shift));
73 return result_bits_;
74 }
75 inline result_type operator()() {
76 // we return results from the current state and update state after returning. results in better pipelining.
77 state_type s = state;
78 result_type result = static_cast<result_type>((s & result_mask) >> result_shift);
79 s = mpt::modulo_if_not_zero<state_type, m>((a * s) + c);
80 state = s;
81 return result;
82 }
83 };
84
85 #if MPT_COMPILER_MSVC
86 #pragma warning(pop)
87 #endif // MPT_COMPILER_MSVC
88
89 typedef lcg_engine<uint32, uint16, 0u, 214013u, 2531011u, 0x7fff0000u, 16, 15> lcg_msvc;
90 typedef lcg_engine<uint32, uint16, 0x80000000u, 1103515245u, 12345u, 0x7fff0000u, 16, 15> lcg_c99;
91 typedef lcg_engine<uint64, uint32, 0ull, 6364136223846793005ull, 1ull, 0xffffffff00000000ull, 32, 32> lcg_musl;
92
93
94
95 } // namespace MPT_INLINE_NS
96 } // namespace mpt
97
98
99
100 #endif // MPT_RANDOM_ENGINE_LCG_HPP