HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
xorshift128p.hpp
1// Copyright Take Vos 2021.
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_generator.hpp"
8#include "../rapid/numeric_array.hpp"
9#include "../required.hpp"
10#include <random>
11
12namespace tt {
13
17public:
18 constexpr xorshift128p(xorshift128p const &) noexcept = default;
19 constexpr xorshift128p(xorshift128p &&) noexcept = default;
20 constexpr xorshift128p &operator=(xorshift128p const &) noexcept = default;
21 constexpr xorshift128p &operator=(xorshift128p &&) noexcept = default;
22
23 [[nodiscard]] constexpr explicit xorshift128p(u64x2 new_state) noexcept : _state(new_state) {}
24
25 [[nodiscard]] explicit xorshift128p(seed_generator &sg) noexcept : _state(sg.next_not_zero<u64x2>()) {}
26
27 [[nodiscard]] xorshift128p() noexcept : _state(seed_generator{}.next_not_zero<u64x2>()) {}
28
29 template<typename T>
30 [[nodiscard]] T next() noexcept;
31
34 template<>
35 [[nodiscard]] uint64_t next() noexcept
36 {
37 auto s = _state[0];
38 ttlet t = _state[1];
39
40 s ^= s << 23; // a
41 s ^= s >> 17; // b
42 s ^= t ^ (t >> 26); // c
43
44 _state[0] = t;
45 _state[1] = s;
46 return s + t;
47 }
48
54 template<>
55 u64x2 next() noexcept
56 {
57 // scalar: uint64_t x = _state[0];
58 // scalar: uint64_t y = y_ = _state[1];
59 auto s = _state;
60 auto t = s.yx();
61
62 // scalar: x ^= x << 23;
63 // scalar: y ^= y << 23;
64 s ^= (s << 23);
65
66 // scalar: x ^= x >> 17;
67 // scalar: y ^= y >> 17;
68 s ^= (s >> 17);
69
70 // scalar: x ^= y_ ^ (y_ >> 26)
71 auto tmp = s ^ t ^ (t >> 26);
72
73 // scalar: auto x_ = x;
74 // scalar: t.y() = tmp.x();
75 t = insert<0,1>(t, tmp);
76
77 // scalar: y ^= x_ ^ (x_ >> 26);
78 s ^= t ^ (t >> 26);
79
80 // scalar: state[0] = x
81 // scalar: state[1] = y
82 _state = s;
83
84 // scalar: return {x + y_, y + x_}
85 return s + t;
86 }
87
88 template<>
89 [[nodiscard]] u32x4 next() noexcept
90 {
91 return bit_cast<u32x4>(next<u64x2>());
92 }
93
94 template<>
95 [[nodiscard]] i32x4 next() noexcept
96 {
97 return bit_cast<i32x4>(next<u64x2>());
98 }
99
100 template<>
101 [[nodiscard]] i16x8 next() noexcept
102 {
103 return bit_cast<i16x8>(next<u64x2>());
104 }
105
106private:
107 u64x2 _state;
108};
109
110
111}
Definition seed_generator.hpp:14
xorshift128+
Definition xorshift128p.hpp:16
uint64_t next() noexcept
Get the next 64 bit of random value.
Definition xorshift128p.hpp:35
u64x2 next() noexcept
Get next 128 bit of random value.
Definition xorshift128p.hpp:55