HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
xorshift128p.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "seed.hpp"
8#include "../utility/utility.hpp"
9#include "../macros.hpp"
10#include <hikocpu/hikocpu.hpp>
11#include <random>
12
13hi_export_module(hikogui.random.xorshift128p);
14
15hi_export namespace hi::inline v1 {
16
20public:
21 constexpr xorshift128p(xorshift128p const &) noexcept = default;
22 constexpr xorshift128p(xorshift128p &&) noexcept = default;
23 constexpr xorshift128p &operator=(xorshift128p const &) noexcept = default;
24 constexpr xorshift128p &operator=(xorshift128p &&) noexcept = default;
25
26 [[nodiscard]] constexpr explicit xorshift128p(u64x2 new_state) noexcept : _state(new_state) {}
27
28 [[nodiscard]] xorshift128p() noexcept : _state{}
29 {
30 while (_state.x() == 0 or _state.y() == 0) {
31 _state = seed<u64x2>{}();
32 }
33 }
34
35 template<typename T>
36 [[nodiscard]] T next() noexcept;
37
40 template<>
41 [[nodiscard]] uint64_t next() noexcept
42 {
43 auto s = _state[0];
44 auto const t = _state[1];
45
46 s ^= s << 23; // a
47 s ^= s >> 17; // b
48 s ^= t ^ (t >> 26); // c
49
50 _state[0] = t;
51 _state[1] = s;
52 return s + t;
53 }
54
55private:
56 u64x2 _state;
57};
58
59} // namespace hi::inline v1
Cryptographically secure entropy.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Randomly generate an object.
Definition seed_intf.hpp:39
xorshift128+
Definition xorshift128p.hpp:19
uint64_t next() noexcept
Get the next 64 bit of random value.
Definition xorshift128p.hpp:41