tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
xoshiro128plus.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_RANDOM_XORSHIFT128PLUS_HPP
4#define TETL_RANDOM_XORSHIFT128PLUS_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 xoshiro128plus() = default;
22
23 explicit constexpr xoshiro128plus(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 = _state[0] + _state[3];
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 _state[3] = rotl(_state[3], 11);
53
54 return result;
55 }
56
57 [[nodiscard]] friend constexpr auto operator==(xoshiro128plus const& lhs, xoshiro128plus const& rhs) noexcept
58 -> bool
59 {
60 return equal(begin(lhs._state), end(lhs._state), begin(rhs._state), end(rhs._state));
61 }
62
63private:
64 uint32_t _state[4]{default_seed};
65};
66
67} // namespace etl
68
69#endif // TETL_RANDOM_XORSHIFT128PLUS_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
friend constexpr auto operator==(xoshiro128plus const &lhs, xoshiro128plus const &rhs) noexcept -> bool
Definition xoshiro128plus.hpp:57
constexpr auto seed(result_type value=default_seed) noexcept -> void
Definition xoshiro128plus.hpp:32
constexpr xoshiro128plus(result_type seed) noexcept
Definition xoshiro128plus.hpp:23
constexpr auto discard(unsigned long long z) noexcept -> void
Definition xoshiro128plus.hpp:34
constexpr xoshiro128plus()=default
uint32_t result_type
Definition xoshiro128plus.hpp:18
static constexpr auto max() noexcept -> result_type
Definition xoshiro128plus.hpp:30
static constexpr auto min() noexcept -> result_type
Definition xoshiro128plus.hpp:28
constexpr auto operator()() noexcept -> result_type
Definition xoshiro128plus.hpp:41
static constexpr auto default_seed
Definition xoshiro128plus.hpp:19