tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
xoshiro128starstar.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_RANDOM_XORSHIFT128STARSTAR_HPP
4#define TETL_RANDOM_XORSHIFT128STARSTAR_HPP
5
7#include <etl/_bit/rotl.hpp>
10#include <etl/_iterator/end.hpp>
12
13namespace etl {
14
19 static constexpr auto default_seed = result_type{5489U};
20
21 constexpr xoshiro128starstar() = default;
22
23 explicit constexpr xoshiro128starstar(result_type seed) noexcept
24 : _state{seed}
25 {
26 }
27
28 [[nodiscard]] static constexpr auto min() noexcept -> result_type { return numeric_limits<uint32_t>::min(); }
29
30 [[nodiscard]] static constexpr auto max() noexcept -> result_type { return numeric_limits<uint32_t>::max() - 1; }
31
32 constexpr auto seed(result_type value = default_seed) noexcept -> void { _state[0] = 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 uint32_t const result = rotl(_state[1] * 5, 7) * 9;
44 uint32_t const t = _state[1] << 9;
45
46 _state[2] ^= _state[0];
47 _state[3] ^= _state[1];
48 _state[1] ^= _state[2];
49 _state[0] ^= _state[3];
50
51 _state[2] ^= t;
52
53 _state[3] = rotl(_state[3], 11);
54
55 return result;
56 }
57
58 [[nodiscard]] friend constexpr auto
59 operator==(xoshiro128starstar const& lhs, xoshiro128starstar const& rhs) noexcept -> bool
60 {
61 return equal(begin(lhs._state), end(lhs._state), begin(rhs._state), end(rhs._state));
62 }
63
64 [[nodiscard]] friend constexpr auto
65 operator!=(xoshiro128starstar const& lhs, xoshiro128starstar const& rhs) noexcept -> bool
66 {
67 return !(lhs == rhs);
68 }
69
70private:
71 uint32_t _state[4]{default_seed};
72};
73
74} // namespace etl
75
76#endif // TETL_RANDOM_XORSHIFT128STARSTAR_HPP
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, Predicate p) -> bool
Returns true if the range [first1, last1) is equal to the range [first2, first2 + (last1 - first1)),...
Definition equal.hpp:18
constexpr auto rotl(UInt t, int s) noexcept -> UInt
Computes the result of bitwise left-rotating the value of x by s positions. This operation is also kn...
Definition rotl.hpp:16
constexpr auto end(C &c) -> decltype(c.end())
Returns an iterator to the end (i.e. the element after the last element) of the given container c or ...
Definition end.hpp:14
constexpr auto begin(C &c) -> decltype(c.begin())
Returns an iterator to the beginning of the given container c or array array. These templates rely on...
Definition begin.hpp:20
Definition adjacent_find.hpp:8
TETL_BUILTIN_UINT32 uint32_t
Unsigned integer type with width of exactly 32 bits.
Definition uint_t.hpp:17
static constexpr auto max() noexcept
Definition numeric_limits.hpp:21
static constexpr auto min() noexcept
Definition numeric_limits.hpp:20
constexpr auto seed(result_type value=default_seed) noexcept -> void
Definition xoshiro128starstar.hpp:32
constexpr auto discard(unsigned long long z) noexcept -> void
Definition xoshiro128starstar.hpp:34
constexpr xoshiro128starstar()=default
uint32_t result_type
Definition xoshiro128starstar.hpp:18
friend constexpr auto operator==(xoshiro128starstar const &lhs, xoshiro128starstar const &rhs) noexcept -> bool
Definition xoshiro128starstar.hpp:59
static constexpr auto max() noexcept -> result_type
Definition xoshiro128starstar.hpp:30
constexpr xoshiro128starstar(result_type seed) noexcept
Definition xoshiro128starstar.hpp:23
static constexpr auto min() noexcept -> result_type
Definition xoshiro128starstar.hpp:28
constexpr auto operator()() noexcept -> result_type
Definition xoshiro128starstar.hpp:41
friend constexpr auto operator!=(xoshiro128starstar const &lhs, xoshiro128starstar const &rhs) noexcept -> bool
Definition xoshiro128starstar.hpp:65
static constexpr auto default_seed
Definition xoshiro128starstar.hpp:19