-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotelyVectorSearchContext.cs
389 lines (308 loc) · 12.5 KB
/
MotelyVectorSearchContext.cs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
namespace Motely;
public struct MotelyVectorPrngStream(Vector512<double> state)
{
public Vector512<double> State = state;
}
public ref struct MotelyVectorResampleStream(MotelyVectorPrngStream initialPrngStream, bool isCached)
{
public const int StackResampleCount = 8;
[InlineArray(StackResampleCount)]
public struct MotelyResampleStreams
{
public MotelyVectorPrngStream PrngStream;
}
public MotelyVectorPrngStream InitialPrngStream = initialPrngStream;
public MotelyResampleStreams ResamplePrngStreams;
public int ResamplePrngStreamInitCount;
public List<object>? HighResamplePrngStreams;
public bool IsCached = isCached;
}
public delegate bool MotelyIndividualSeedSearcher(ref MotelySingleSearchContext searchContext);
internal unsafe struct MotelySearchContextParams(in SeedHashCache seedHashCache, int seedLength, char* seedLastCharacters, in Vector512<double> seedFirstCharacter)
{
public SeedHashCache SeedHashCache = seedHashCache;
public readonly int SeedLength = seedLength;
public readonly char* SeedLastCharacters = seedLastCharacters;
public readonly Vector512<double> SeedFirstCharacter = seedFirstCharacter;
}
public unsafe ref partial struct MotelyVectorSearchContext
{
private ref MotelySearchContextParams _params;
private ref SeedHashCache SeedHashCache => ref _params.SeedHashCache;
private readonly int SeedLength => _params.SeedLength;
private readonly char* SeedLastCharacters => _params.SeedLastCharacters;
private readonly Vector512<double> SeedFirstCharacter => _params.SeedFirstCharacter;
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal MotelyVectorSearchContext(ref MotelySearchContextParams @params)
{
_params = ref @params;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public VectorMask SearchIndividualSeeds(MotelyIndividualSeedSearcher searcher)
{
uint results = 0;
for (int lane = 0; lane < Vector512<double>.Count; lane++)
{
MotelySingleSearchContext singleSearchContext = new(ref _params, lane);
bool success = searcher(ref singleSearchContext);
if (success)
{
results |= 1u << lane;
}
}
return new(results);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public VectorMask SearchIndividualSeeds(VectorMask mask, MotelyIndividualSeedSearcher searcher)
{
if (mask.IsAllFalse()) return default;
uint results = 0;
uint maskShift = mask.Value;
for (int lane = 0; lane < Vector512<double>.Count; lane++)
{
if ((maskShift & 1) != 0)
{
MotelySingleSearchContext singleSearchContext = new(ref _params, lane);
bool success = searcher(ref singleSearchContext);
if (success)
{
results |= 1u << lane;
}
}
maskShift >>= 1;
}
return new(results);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> PseudoHashCached(string key)
{
#if MOTELY_SAFE
if (!_seedHashCache.HasPartialHash(key.Length))
throw new KeyNotFoundException("Cache does not contain key :c");
#endif
Vector512<double> calcVector = SeedHashCache.GetPartialHashVector(key.Length);
for (int i = key.Length - 1; i >= 0; i--)
{
calcVector = Vector512.Divide(Vector512.Create(1.1239285023), calcVector);
calcVector = Vector512.Multiply(calcVector, key[i]);
calcVector = Vector512.Multiply(calcVector, Math.PI);
calcVector = Vector512.Add(calcVector, Vector512.Create((i + 1) * Math.PI));
Vector512<double> intPart = Vector512.Floor(calcVector);
calcVector = Vector512.Subtract(calcVector, intPart);
}
return calcVector;
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> PseudoHash(string key)
{
if (SeedHashCache.HasPartialHash(key.Length))
{
return PseudoHashCached(key);
}
double num = 1;
// First we do the first 7 digits of the seed which are the same between all vector lanes
for (int i = SeedLength - 2; i >= 0; i--)
{
num = (1.1239285023 / num * SeedLastCharacters[i] * Math.PI + Math.PI * (i + key.Length + 2)) % 1;
}
// Then we vectorize and do the last digit of the seed
Vector512<double> numVector = Vector512.Create(num);
{
numVector = Vector512.Divide(Vector512.Create(1.1239285023), numVector);
numVector = Vector512.Multiply(numVector, SeedFirstCharacter);
numVector = Vector512.Multiply(numVector, Math.PI);
numVector = Vector512.Add(numVector, Vector512.Create((key.Length + 1) * Math.PI));
Vector512<double> intPart = Vector512.Floor(numVector);
numVector = Vector512.Subtract(numVector, intPart);
}
// Now finally the actual key
for (int i = key.Length - 1; i >= 0; i--)
{
numVector = Vector512.Divide(Vector512.Create(1.1239285023), numVector);
numVector = Vector512.Multiply(numVector, key[i]);
numVector = Vector512.Multiply(numVector, Math.PI);
numVector = Vector512.Add(numVector, Vector512.Create((i + 1) * Math.PI));
Vector512<double> intPart = Vector512.Floor(numVector);
numVector = Vector512.Subtract(numVector, intPart);
}
return numVector;
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public static Vector512<double> IteratePRNG(Vector512<double> state)
{
state = Vector512.Multiply(state, 1.72431234);
state = Vector512.Add(state, Vector512.Create(2.134453429141));
Vector512<double> intPart = Vector512.Floor(state);
state = Vector512.Subtract(state, intPart);
state = Vector512.Multiply(state, 10000000000000);
state = Vector512.Round(state);
state = Vector512.Divide(state, 10000000000000);
return state;
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public MotelyVectorPrngStream CreatePrngStreamCached(string key)
{
return new(PseudoHashCached(key));
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public MotelyVectorPrngStream CreatePrngStream(string key)
{
return new(PseudoHash(key));
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> GetNextPrngState(ref MotelyVectorPrngStream stream)
{
return stream.State = IteratePRNG(stream.State);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> GetNextPrngState(ref MotelyVectorPrngStream stream, in Vector512<double> mask)
{
return stream.State = Vector512.ConditionalSelect(mask, IteratePRNG(stream.State), stream.State);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> IteratePseudoSeed(ref MotelyVectorPrngStream stream)
{
return (GetNextPrngState(ref stream) + SeedHashCache.GetSeedHashVector()) / Vector512.Create<double>(2);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> IteratePseudoSeed(ref MotelyVectorPrngStream stream, in Vector512<double> mask)
{
return (GetNextPrngState(ref stream, mask) + SeedHashCache.GetSeedHashVector()) / Vector512.Create<double>(2);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> GetNextPseudoSeed(ref MotelyVectorPrngStream stream, in Vector512<double> mask)
{
return (GetNextPrngState(ref stream, mask) + SeedHashCache.GetSeedHashVector()) / Vector512.Create<double>(2);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> GetNextRandom(ref MotelyVectorPrngStream stream)
{
return VectorLuaRandom.Random(IteratePseudoSeed(ref stream));
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector512<double> GetNextRandom(ref MotelyVectorPrngStream stream, in Vector512<double> mask)
{
return VectorLuaRandom.Random(GetNextPseudoSeed(ref stream, mask));
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector256<int> GetNextRandomInt(ref MotelyVectorPrngStream stream, int min, int max)
{
return VectorLuaRandom.RandInt(IteratePseudoSeed(ref stream), min, max);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public Vector256<int> GetNextRandomInt(ref MotelyVectorPrngStream stream, int min, int max, in Vector512<double> mask)
{
return VectorLuaRandom.RandInt(IteratePseudoSeed(ref stream, mask), min, max);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public VectorEnum256<T> GetNextRandomElement<T>(ref MotelyVectorPrngStream stream, T[] choices) where T : unmanaged, Enum
{
return VectorEnum256.Create(GetNextRandomInt(ref stream, 0, choices.Length), choices);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
public VectorEnum256<T> GetNextRandomElement<T>(ref MotelyVectorPrngStream stream, T[] choices, in Vector512<double> mask) where T : unmanaged, Enum
{
return VectorEnum256.Create(GetNextRandomInt(ref stream, 0, choices.Length, mask), choices);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private MotelyVectorPrngStream CreateResamplePrngStream(string key, int resample)
{
return CreatePrngStream(key + MotelyPrngKeys.Resample + (resample + 2));
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private MotelyVectorPrngStream CreateResamplePrngStreamCached(string key, int resample)
{
// We don't cache resamples > 8 because they'd use an extra digit
if (resample < 8) return CreatePrngStreamCached(key + MotelyPrngKeys.Resample + (resample + 2));
return CreateResamplePrngStream(key, resample);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private MotelyVectorResampleStream CreateResampleStreamCached(string key)
{
return new(CreatePrngStreamCached(key), true);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private MotelyVectorResampleStream CreateResampleStream(string key)
{
return new(CreatePrngStream(key), false);
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
private ref MotelyVectorPrngStream GetResamplePrngStream(ref MotelyVectorResampleStream resampleStream, string key, int resample)
{
if (resample < MotelyVectorResampleStream.StackResampleCount)
{
ref MotelyVectorPrngStream prngStream = ref resampleStream.ResamplePrngStreams[resample];
if (resample == resampleStream.ResamplePrngStreamInitCount)
{
++resampleStream.ResamplePrngStreamInitCount;
if (resampleStream.IsCached) prngStream = CreateResamplePrngStreamCached(key, resample);
else prngStream = CreateResamplePrngStream(key, resample);
}
return ref prngStream;
}
{
if (resample == MotelyVectorResampleStream.StackResampleCount)
{
resampleStream.HighResamplePrngStreams = [];
}
Debug.Assert(resampleStream.HighResamplePrngStreams != null);
if (resample < resampleStream.HighResamplePrngStreams.Count)
{
return ref Unsafe.Unbox<MotelyVectorPrngStream>(resampleStream.HighResamplePrngStreams[resample]);
}
object prngStreamObject = new MotelyVectorPrngStream();
resampleStream.HighResamplePrngStreams.Add(prngStreamObject);
ref MotelyVectorPrngStream prngStream = ref Unsafe.Unbox<MotelyVectorPrngStream>(prngStreamObject);
if (resampleStream.IsCached) prngStream = CreateResamplePrngStreamCached(key, resample);
else prngStream = CreateResamplePrngStream(key, resample);
return ref prngStream;
}
}
}