pRC
multi-purpose Tensor Train library for C++
Loading...
Searching...
No Matches
seq.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_RANDOM_SEQ_H
4#define pRC_CORE_RANDOM_SEQ_H
5
8
9namespace pRC
10{
11 template<Size S>
13 {
14 private:
15 static_assert(IsSame<Seed, std::uint32_t>());
16
17 public:
19
20 static constexpr auto size()
21 {
22 return S;
23 }
24
25 public:
26 template<class... Xs, If<All<IsConvertible<Xs, Seed>...>> = 0>
27 constexpr SeedSequence(Xs &&...seeds)
28 : mSeed(forward<Xs>(seeds)...)
29 {
30 }
31
32 template<Size N>
33 constexpr auto generate() const
34 {
36 for(Size i = 0; i < N; ++i)
37 {
38 out[i] = 0x8b8b8b8bU;
39 }
40
41 constexpr Size t = (N >= 623) ? 11
42 : (N >= 68) ? 7
43 : (N >= 39) ? 5
44 : (N >= 7) ? 3
45 : (N - 1) / 2;
46 constexpr Size p = (N - t) / 2;
47 constexpr Size q = p + t;
48 constexpr Size m = max(S + 1, N);
49
50 auto const T = [&out, &p](auto const k)
51 {
52 Seed const arg =
53 out[k % N] ^ out[(k + p) % N] ^ out[(k - 1) % N];
54 return arg ^ (arg >> 27);
55 };
56
57 for(Size k = 0; k < m; ++k)
58 {
59 Seed const r1 = 1664525U * T(k);
60 Seed const r2 = r1 +
61 [&]()
62 {
63 if(k == 0)
64 {
65 return S;
66 }
67 else if(k <= S)
68 {
69 return k % N + mSeed[k - 1];
70 }
71 else
72 {
73 return k % N;
74 }
75 }();
76
77 out[(k + p) % N] += r1;
78 out[(k + q) % N] += r2;
79 out[k % N] = r2;
80 }
81
82 for(Size k = m; k < m + N; ++k)
83 {
84 Seed const r3 = 1566083941U * T(k);
85 Seed const r4 = r3 - k % N;
86
87 out[(k + p) % N] ^= r3;
88 out[(k + q) % N] ^= r4;
89 out[k % N] = r4;
90 }
91
92 return out;
93 }
94
95 constexpr auto &param() const
96 {
97 return mSeed;
98 }
99
100 private:
101 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
102 StackArray<Seed, S> mSeed;
104 };
105
106 template<class... Ts>
108}
109#endif // pRC_CORE_RANDOM_SEQ_H
Definition type_traits.hpp:49
Definition seq.hpp:13
constexpr auto generate() const
Definition seq.hpp:33
static constexpr auto size()
Definition seq.hpp:20
Seed result_type
Definition seq.hpp:18
constexpr SeedSequence(Xs &&...seeds)
Definition seq.hpp:27
constexpr auto & param() const
Definition seq.hpp:95
Definition cholesky.hpp:18
static constexpr auto arg(Complex< T > const &a)
Definition arg.hpp:12
std::uint32_t Seed
Definition type_traits.hpp:23
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
std::size_t Size
Definition type_traits.hpp:20
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13
static constexpr X max(X &&a)
Definition max.hpp:13
#define BEGIN_IGNORE_DIAGNOSTIC_GCC(warning)
Definition pragma.hpp:42
#define END_IGNORE_DIAGNOSTIC_GCC
Definition pragma.hpp:43