tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
xorshift.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_RANDOM_XORSHIFT_HPP
4#define TETL_RANDOM_XORSHIFT_HPP
5
10
11namespace etl {
12
16template <unsigned_integral UInt, UInt X, UInt Y, UInt Z>
17struct xorshift {
18 using result_type = UInt;
19 static constexpr auto default_seed = result_type{5489U};
20
21 constexpr xorshift() = default;
22
23 explicit constexpr xorshift(result_type seed) noexcept
24 : _state{seed}
25 {
26 }
27
28 [[nodiscard]] static constexpr auto min() noexcept -> result_type { return numeric_limits<result_type>::min(); }
29
30 [[nodiscard]] static constexpr auto max() noexcept -> result_type { return numeric_limits<result_type>::max() - 1; }
31
32 constexpr auto seed(result_type value = default_seed) noexcept -> void { _state = value; }
33
34 constexpr auto discard(unsigned long long z) noexcept -> void
35 {
36 for (auto i{0ULL}; i < z; ++i) {
37 (void)(*this)();
38 }
39 }
40
41 [[nodiscard]] constexpr auto operator()() noexcept -> result_type
42 {
43 auto s = _state;
44 s ^= s << result_type(X);
45 s ^= s >> result_type(Y);
46 s ^= s << result_type(Z);
47 return _state = s;
48 }
49
50 [[nodiscard]] friend constexpr auto operator==(xorshift const& lhs, xorshift const& rhs) noexcept -> bool
51 {
52 return lhs._state == rhs._state;
53 }
54
55private:
57};
58
66
71
76
77} // namespace etl
78
79#endif // TETL_RANDOM_XORSHIFT_HPP
xorshift< uint64_t, 13, 7, 17 > xorshift64
Definition xorshift.hpp:75
xorshift< uint32_t, 13, 17, 5 > xorshift32
Definition xorshift.hpp:70
xorshift< uint16_t, 7, 9, 8 > xorshift16
16-bit pseudo number generator
Definition xorshift.hpp:65
Definition adjacent_find.hpp:8
static constexpr auto max() noexcept
Definition numeric_limits.hpp:21
static constexpr auto min() noexcept
Definition numeric_limits.hpp:20
constexpr xorshift(result_type seed) noexcept
Definition xorshift.hpp:23
constexpr auto seed(result_type value=default_seed) noexcept -> void
Definition xorshift.hpp:32
constexpr auto discard(unsigned long long z) noexcept -> void
Definition xorshift.hpp:34
static constexpr auto max() noexcept -> result_type
Definition xorshift.hpp:30
friend constexpr auto operator==(xorshift const &lhs, xorshift const &rhs) noexcept -> bool
Definition xorshift.hpp:50
constexpr xorshift()=default
UInt result_type
Definition xorshift.hpp:18
static constexpr auto min() noexcept -> result_type
Definition xorshift.hpp:28
constexpr auto operator()() noexcept -> result_type
Definition xorshift.hpp:41
static constexpr auto default_seed
Definition xorshift.hpp:19