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// SPDX-FileCopyrightText: Copyright (C) 2023 Tobias Hienzsch
3
4#ifndef TETL_RANDOM_XORSHIFT128STARSTAR_HPP
5#define TETL_RANDOM_XORSHIFT128STARSTAR_HPP
6
7#include <etl/_algorithm/equal.hpp>
8#include <etl/_bit/rotl.hpp>
9#include <etl/_cstdint/uint_t.hpp>
10#include <etl/_iterator/begin.hpp>
11#include <etl/_iterator/end.hpp>
12#include <etl/_limits/numeric_limits.hpp>
13
14namespace etl {
15
16/// \note Non-standard extension
17/// \ingroup random
19 using result_type = uint32_t;
20 static constexpr auto default_seed = result_type{5489U};
21
22 constexpr xoshiro128starstar() = default;
23
24 explicit constexpr xoshiro128starstar(result_type seed) noexcept
25 : _state{seed}
26 {
27 }
28
29 [[nodiscard]] static constexpr auto min() noexcept -> result_type
30 {
31 return numeric_limits<uint32_t>::min();
32 }
33
34 [[nodiscard]] static constexpr auto max() noexcept -> result_type
35 {
36 return numeric_limits<uint32_t>::max() - 1;
37 }
38
39 constexpr auto seed(result_type value = default_seed) noexcept -> void
40 {
41 _state[0] = 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 uint32_t const result = rotl(_state[1] * 5, 7) * 9;
54 uint32_t const t = _state[1] << 9;
55
56 _state[2] ^= _state[0];
57 _state[3] ^= _state[1];
58 _state[1] ^= _state[2];
59 _state[0] ^= _state[3];
60
61 _state[2] ^= t;
62
63 _state[3] = rotl(_state[3], 11);
64
65 return result;
66 }
67
68 [[nodiscard]] friend constexpr auto
69 operator==(xoshiro128starstar const& lhs, xoshiro128starstar const& rhs) noexcept -> bool
70 {
71 return equal(begin(lhs._state), end(lhs._state), begin(rhs._state), end(rhs._state));
72 }
73
74 [[nodiscard]] friend constexpr auto
75 operator!=(xoshiro128starstar const& lhs, xoshiro128starstar const& rhs) noexcept -> bool
76 {
77 return !(lhs == rhs);
78 }
79
80private:
81 uint32_t _state[4]{default_seed};
82};
83
84} // namespace etl
85
86#endif // TETL_RANDOM_XORSHIFT128STARSTAR_HPP
constexpr auto equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) -> bool
Definition equal.hpp:55
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:17
constexpr auto begin(T(&array)[N]) noexcept -> T *
Definition begin.hpp:35
constexpr auto end(T(&array)[N]) noexcept -> T *
Definition end.hpp:29
Definition adjacent_find.hpp:9
static constexpr auto min() noexcept -> unsigned int
Definition numeric_limits.hpp:665
static constexpr auto max() noexcept -> unsigned int
Definition numeric_limits.hpp:669
Definition numeric_limits.hpp:18
Definition xoshiro128starstar.hpp:18
constexpr auto seed(result_type value=default_seed) noexcept -> void
Definition xoshiro128starstar.hpp:39
constexpr auto discard(unsigned long long z) noexcept -> void
Definition xoshiro128starstar.hpp:44
constexpr xoshiro128starstar()=default
friend constexpr auto operator==(xoshiro128starstar const &lhs, xoshiro128starstar const &rhs) noexcept -> bool
Definition xoshiro128starstar.hpp:69
static constexpr auto max() noexcept -> result_type
Definition xoshiro128starstar.hpp:34
constexpr xoshiro128starstar(result_type seed) noexcept
Definition xoshiro128starstar.hpp:24
static constexpr auto min() noexcept -> result_type
Definition xoshiro128starstar.hpp:29
constexpr auto operator()() noexcept -> result_type
Definition xoshiro128starstar.hpp:51
friend constexpr auto operator!=(xoshiro128starstar const &lhs, xoshiro128starstar const &rhs) noexcept -> bool
Definition xoshiro128starstar.hpp:75
static constexpr auto default_seed
Definition xoshiro128starstar.hpp:20