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// SPDX-FileCopyrightText: Copyright (C) 2022 Tobias Hienzsch
3
4#ifndef TETL_RANDOM_XORSHIFT_HPP
5#define TETL_RANDOM_XORSHIFT_HPP
6
7#include <etl/_concepts/unsigned_integral.hpp>
8#include <etl/_cstdint/uint_t.hpp>
9#include <etl/_limits/numeric_limits.hpp>
10#include <etl/_type_traits/is_same.hpp>
11
12namespace etl {
13
14/// https://en.wikipedia.org/wiki/Xorshift
15/// \note Non-standard extension
16/// \ingroup random
17template <unsigned_integral UInt, UInt X, UInt Y, UInt Z>
18struct xorshift {
19 using result_type = UInt;
20 static constexpr auto default_seed = result_type{5489U};
21
22 constexpr xorshift() = default;
23
24 explicit constexpr xorshift(result_type seed) noexcept
25 : _state{seed}
26 {
27 }
28
29 [[nodiscard]] static constexpr auto min() noexcept -> result_type
30 {
31 return numeric_limits<result_type>::min();
32 }
33
34 [[nodiscard]] static constexpr auto max() noexcept -> result_type
35 {
36 return numeric_limits<result_type>::max() - 1;
37 }
38
39 constexpr auto seed(result_type value = default_seed) noexcept -> void
40 {
41 _state = value;
42 }
43
44 constexpr auto discard(unsigned long long z) noexcept -> void
45 {
46 for (auto i{0ULL}; i < z; ++i) {
47 (void)(*this)();
48 }
49 }
50
51 [[nodiscard]] constexpr auto operator()() noexcept -> result_type
52 {
53 auto s = _state;
54 s ^= s << result_type(X);
55 s ^= s >> result_type(Y);
56 s ^= s << result_type(Z);
57 return _state = s;
58 }
59
60 [[nodiscard]] friend constexpr auto operator==(xorshift const& lhs, xorshift const& rhs) noexcept -> bool
61 {
62 return lhs._state == rhs._state;
63 }
64
65private:
66 result_type _state{default_seed};
67};
68
69/// \brief 16-bit pseudo number generator
70/// \relates xorshift
71/// \details
72/// - http://www.retroprogramming.com/2017/07/xorshift-pseudorandom-numbers-in-z80.html
73/// - https://codebase64.org/doku.php?id=base:16bit_xorshift_random_generator
74/// \ingroup random
75using xorshift16 = xorshift<uint16_t, 7, 9, 8>;
76
77/// \brief 32-bit pseudo number generator
78/// \relates xorshift
79/// \ingroup random
80using xorshift32 = xorshift<uint32_t, 13, 17, 5>;
81
82/// \brief 64-bit pseudo number generator
83/// \relates xorshift
84/// \ingroup random
85using xorshift64 = xorshift<uint64_t, 13, 7, 17>;
86
87} // namespace etl
88
89#endif // TETL_RANDOM_XORSHIFT_HPP
Definition adjacent_find.hpp:9
Definition numeric_limits.hpp:18
https://en.wikipedia.org/wiki/Xorshift
Definition xorshift.hpp:18
constexpr xorshift(result_type seed) noexcept
Definition xorshift.hpp:24
constexpr auto seed(result_type value=default_seed) noexcept -> void
Definition xorshift.hpp:39
constexpr auto discard(unsigned long long z) noexcept -> void
Definition xorshift.hpp:44
static constexpr auto max() noexcept -> result_type
Definition xorshift.hpp:34
friend constexpr auto operator==(xorshift const &lhs, xorshift const &rhs) noexcept -> bool
Definition xorshift.hpp:60
constexpr xorshift()=default
static constexpr auto min() noexcept -> result_type
Definition xorshift.hpp:29
constexpr auto operator()() noexcept -> result_type
Definition xorshift.hpp:51
static constexpr auto default_seed
Definition xorshift.hpp:20