-
Notifications
You must be signed in to change notification settings - Fork 0
/
staff.hpp
298 lines (285 loc) · 8.55 KB
/
staff.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#pragma once
#ifndef SIMPLE_UNIFORM_NOISE_STAFF
#define SIMPLE_UNIFORM_NOISE_STAFF
#include <cstdint>
namespace utils {
#ifdef __GNUG__
# define SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
#endif
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC push_options
# pragma GCC optimize ("O0")
#endif
inline uint64_t MurmurHash2_x64_64A(
const void* key, const uint64_t len, const uint64_t seed) {
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC pop_options
#endif
constexpr uint64_t m = UINT64_C(0xC6A4A7935BD1E995);
constexpr uint64_t r = 47;
uint64_t h = seed ^ (len * m);
const uint64_t* data = static_cast<const uint64_t*>(key);
const uint64_t* end = data + (len >> 3);
while (data != end) {
uint64_t k = *data++;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
}
const uint8_t* tail = reinterpret_cast<const uint8_t*>(data);
switch (len & 7) {
case 7: h ^= static_cast<uint64_t>(tail[6]) << 48; [[fallthrough]];
case 6: h ^= static_cast<uint64_t>(tail[5]) << 40; [[fallthrough]];
case 5: h ^= static_cast<uint64_t>(tail[4]) << 32; [[fallthrough]];
case 4: h ^= static_cast<uint64_t>(tail[3]) << 24; [[fallthrough]];
case 3: h ^= static_cast<uint64_t>(tail[2]) << 16; [[fallthrough]];
case 2: h ^= static_cast<uint64_t>(tail[1]) << 8; [[fallthrough]];
case 1: h ^= static_cast<uint64_t>(tail[0]);
h *= m;
};
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC push_options
# pragma GCC optimize ("O0")
#endif
inline constexpr uint64_t MurmurHash2_x64_64A(
const uint64_t key, const uint64_t seed) {
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC pop_options
#endif
constexpr uint64_t m = UINT64_C(0xC6A4A7935BD1E995);
constexpr uint64_t m8 = sizeof(key) * UINT64_C(0xC6A4A7935BD1E995);
constexpr uint64_t r = 47;
uint64_t h = seed ^ m8;
uint64_t k = key;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
h ^= h >> r;
h *= m;
h ^= h >> r;
return h;
}
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC push_options
# pragma GCC optimize ("O0")
#endif
inline uint32_t MurmurHash2_x32_32A(
const void* key, const uint32_t len, const uint32_t seed) {
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC pop_options
#endif
constexpr uint32_t m = 0x5BD1E995;
constexpr uint32_t r = 24;
uint32_t h = seed;
const uint32_t* data = static_cast<const uint32_t*>(key);
const uint32_t* end = data + (len >> 2);
while (data != end) {
uint32_t k = *data++;
k *= m;
k ^= k >> r;
k *= m;
h *= m;
h ^= k;
}
const uint8_t* tail = reinterpret_cast<const uint8_t*>(data);
uint32_t t = 0;
switch (len & 3) {
case 3: t ^= static_cast<uint32_t>(tail[2]) << 16; [[fallthrough]];
case 2: t ^= static_cast<uint32_t>(tail[1]) << 8; [[fallthrough]];
case 1: t ^= static_cast<uint32_t>(tail[0]);
};
t *= m;
t ^= t >> r;
t *= m;
h *= m;
h ^= t;
//mmix(h, len);
h ^= h >> 13;
h *= m;
h ^= h >> 15;
return h;
}
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC push_options
# pragma GCC optimize ("O0")
#endif
inline uint32_t MurmurHash3_x32_32(
const void* key, const uint32_t len, const uint32_t seed) {
#ifdef SIMPLE_UNIFORM_NOISE_STAFF_GCC_WORKAROUND
# pragma GCC pop_options
#endif
constexpr uint32_t c1 = 0xCC9E2D51;
constexpr uint32_t c2 = 0x1B873593;
uint32_t h1 = seed;
const uint32_t* data = static_cast<const uint32_t*>(key);
const uint32_t* end = data + (len >> 2);
while (data != end) {
uint32_t k1 = *data++;
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >> (32 - 13));
h1 = h1 * 5 + 0xE6546B64;
}
const uint8_t* tail = reinterpret_cast<const uint8_t*>(data);
uint32_t k1 = 0;
switch (len & 3) {
case 3: k1 ^= static_cast<uint32_t>(tail[2]) << 16; [[fallthrough]];
case 2: k1 ^= static_cast<uint32_t>(tail[1]) << 8; [[fallthrough]];
case 1: k1 ^= static_cast<uint32_t>(tail[0]);
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
h1 ^= k1;
};
h1 ^= len;
h1 ^= h1 >> 16;
h1 *= 0x85EBCA6B;
h1 ^= h1 >> 13;
h1 *= 0xC2B2AE35;
h1 ^= h1 >> 16;
return h1;
}
class lcg32 {
public:
lcg32() = default;
lcg32(const uint32_t seed_) noexcept {
seed(seed_);
}
void seed(const uint32_t seed_) noexcept {
//m_x = seed_ == 0 ? 1 : seed_;
m_x = seed_ | (1 << 31);
}
//void set(const uint64_t a, const uint64_t c) {
// m_a = a;
// m_c = c;
//}
uint32_t operator()() noexcept { // 2147483647 4294967295
//m_x = (UINT64_C(1035118825) * m_x + UINT64_C(718786826)) % INT32_MAX; // 63.4056 %
//m_x = (UINT64_C(1664525) * m_x + UINT64_C(1013904223)) % INT32_MAX; // 63.3217 %
//m_x = (UINT64_C(48271) * m_x + UINT64_C(0)) % INT32_MAX; // 63.2122 %
m_x = (UINT64_C(1260864976) * m_x + UINT64_C(1379216869)) % UINT32_MAX; // 68.2358 %
//m_x = (UINT64_C(1664525) * m_x + UINT64_C(1013904223)) % UINT32_MAX; // 12.1635 %
//m_x = (m_a * m_x + m_c) % INT32_MAX;
return m_x;
}
private:
//uint64_t m_a = 1;
//uint64_t m_c = 1;
uint32_t m_x = 1;
};
class lcg64 {
public:
lcg64() = default;
lcg64(const uint64_t seed_) noexcept {
seed(seed_);
}
void seed(const uint64_t seed_) noexcept {
m_lo.seed(seed_ & UINT32_MAX);
m_hi.seed(seed_ >> 32);
}
uint64_t operator()() noexcept {
return (static_cast<uint64_t>(m_hi()) << 32) | static_cast<uint64_t>(m_lo());
}
private:
lcg32 m_lo;
lcg32 m_hi;
};
class rng64 {
public:
void seed(const uint64_t value) {
m_x = value;
}
uint64_t operator()() noexcept {
// MurmurHash64A
constexpr uint64_t m = UINT64_C(0xC6A4A7935BD1E995);
constexpr uint64_t m8 = sizeof(uint64_t) * UINT64_C(0xC6A4A7935BD1E995);
constexpr uint64_t r = 47;
uint64_t h = m8;
uint64_t k = m_x;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
h ^= h >> r;
h *= m;
h ^= h >> r;
m_x = h;
return m_x;
}
private:
uint64_t m_x = 1;
};
// from_a from_t from_b
// to_a result to_b
inline constexpr uint32_t lerp_u32(
const uint32_t from_a, const uint32_t from_t, const uint32_t from_b,
const uint32_t to_a, const uint32_t to_b) {
const uint64_t from_a_ = from_a;
const uint64_t from_t_ = from_t;
const uint64_t from_b_ = from_b;
const uint64_t to_a_ = to_a;
const uint64_t to_b_ = to_b;
if (to_a < to_b) {
// 0 from_t-from_a from_b-from_a
// 0 result-to_a to_b-to_a
return ((from_t_ - from_a_) * (to_b_ - to_a_)) / (from_b_ - from_a_) + to_a_;
}
else {
// 0 from_t-from_a from_b-from_a
// 0 to_a-result to_a-to_b
return to_a_ - ((from_t_ - from_a_) * (to_a_ - to_b_)) / (from_b_ - from_a_);
}
}
// 0 from_t from_b
// to_a result to_b
inline constexpr uint32_t lerp_u32(
const uint32_t from_t, const uint32_t from_b,
const uint32_t to_a, const uint32_t to_b) {
const uint64_t from_t_ = from_t;
const uint64_t from_b_ = from_b;
const uint64_t to_a_ = to_a;
const uint64_t to_b_ = to_b;
if (to_a < to_b) {
// 0 from_t from_b
// 0 result-to_a to_b-to_a
return (from_t_ * (to_b_ - to_a_)) / from_b_ + to_a_;
}
else {
// 0 from_t from_b
// 0 to_a-result to_a-to_b
return to_a_ - (from_t_ * (to_a_ - to_b_)) / from_b_;
}
}
// 0 from_t from_b
// 0 result to_b
inline constexpr uint32_t lerp_u32(
const uint32_t from_t, const uint32_t from_b,
const uint32_t to_b) {
const uint64_t from_t_ = from_t;
const uint64_t from_b_ = from_b;
const uint64_t to_b_ = to_b;
return (from_t_ * to_b_) / from_b_;
}
// from_a from_t from_b
// to_a result to_b
inline constexpr double lerp_f64(
const double from_a, const double from_t, const double from_b,
const double to_a, const double to_b) {
// 0 from_t-from_a from_b-from_a
// 0 result-to_a to_b-to_a
return ((from_t - from_a) * (to_b - to_a)) / (from_b - from_a) + to_a;
};
} // namespace utils
#endif // SIMPLE_UNIFORM_NOISE_STAFF