-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRandom.h
1017 lines (952 loc) · 35.9 KB
/
Random.h
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <CppCore/Root.h>
#include <CppCore/Math/Util.h>
namespace CppCore
{
/// <summary>
/// Random Value Generation
/// </summary>
class Random
{
public:
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// SEEDING
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Small Helper
/// </summary>
INLINE static uint32_t seedmix32(uint32_t v)
{
v ^= v << 13;
v ^= v >> 17;
v ^= v << 5;
return v;
}
/// <summary>
/// Small Helper
/// </summary>
INLINE static uint64_t seedmix64(uint64_t v)
{
v ^= v << 23;
v ^= v << 31;
v ^= v >> 29;
v ^= v >> 23;
v ^= v << 9;
return v;
}
/// <summary>
/// Returns a 32-bit seed for a random number generator.
/// Quality and performance ranges from good to poor depending on available options.
/// </summary>
INLINE static uint32_t seed32()
{
uint32_t v = 1;
uint64_t t;
#if defined(CPPCORE_CPUFEAT_RDSEED)
while (0 == _rdseed32_step(&v)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
#elif defined(CPPCORE_CPUFEAT_RDRAND)
while (0 == _rdrand32_step(&v)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
#elif defined(CPPCORE_CPUFEAT_ARM_RNG)
while (0 != __rndr(&t)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
v = (uint32_t)t;
#elif defined(CPPCORE_OS_WASI)
auto r = __wasi_random_get((uint8_t*)&v, 4);
#else
v ^= seedmix32((uint32_t)(size_t)&v);
#if defined(CPPCORE_CPU_X86ORX64)
v ^= seedmix32((uint32_t)__rdtsc());
#elif defined(CPPCORE_CPU_ARM64) && defined(CPPCORE_COMPILER_CLANG)
__asm volatile ("MRS %0, CNTVCT_EL0;" : "=r"(t) :: "memory");
v ^= seedmix32((uint32_t)t);
#else
v ^= seedmix32((uint32_t)::time(0));
#endif
#endif
return v;
}
/// <summary>
/// Returns a 64-bit seed for a random number generator.
/// Quality and performance ranges from good to poor depending on available options.
/// </summary>
INLINE static uint64_t seed64()
{
uint64_t v = 1;
uint64_t t;
#if defined(CPPCORE_CPU_X64) && defined(CPPCORE_CPUFEAT_RDSEED)
while (0 == _rdseed64_step((unsigned long long*)&v)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
#elif defined(CPPCORE_CPU_X64) && defined(CPPCORE_CPUFEAT_RDRAND)
while (0 == _rdrand64_step((unsigned long long*)&v)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
#elif defined(CPPCORE_CPU_X86) && defined(CPPCORE_CPUFEAT_RDSEED)
while (0 == _rdseed32_step( (uint32_t*)&v)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
while (0 == _rdseed32_step(((uint32_t*)&v)+1)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
#elif defined(CPPCORE_CPU_X86) && defined(CPPCORE_CPUFEAT_RDRAND)
while (0 == _rdrand32_step( (uint32_t*)&v)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
while (0 == _rdrand32_step(((uint32_t*)&v)+1)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
#elif defined(CPPCORE_CPU_ARM64) && defined(CPPCORE_CPUFEAT_ARM_RNG)
while (0 != __rndr(&v)) CPPCORE_UNLIKELY CPPCORE_NANOSLEEP();
#elif defined(CPPCORE_OS_WASI)
auto r = __wasi_random_get((uint8_t*)&v, 8);
#else
v ^= seedmix64((uint64_t)&v);
#if defined(CPPCORE_CPU_X86ORX64)
v ^= seedmix64((uint64_t)__rdtsc());
#elif defined(CPPCORE_CPU_ARM64) && defined(CPPCORE_COMPILER_CLANG)
__asm volatile ("MRS %0, CNTVCT_EL0;" : "=r"(t) :: "memory");
v ^= seedmix64(t);
#else
v ^= seedmix64((uint64_t)::time(0));
#endif
#endif
return v;
}
#if defined(CPPCORE_CPUFEAT_SSE2)
/// <summary>
/// Returns four 32-bit seeds for a packed random number generator.
/// Quality and performance ranges from good to poor depending on available options.
/// </summary>
INLINE static __m128i seed32x4()
{
return _mm_set_epi32(
Random::seed32(),
Random::seed32(),
Random::seed32(),
Random::seed32());
}
/// <summary>
/// Returns two 64-bit seeds for a packed random number generator.
/// Quality and performance ranges from good to poor depending on available options.
/// </summary>
template <bool LOWBITONE = true>
INLINE static __m128i seed64x2()
{
return _mm_set_epi64x(
Random::seed64(),
Random::seed64());
}
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// CUSTOM PSEUDO RANDOM NUMBER GENERATORS
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Base Class for Pseudo Random Number Generators
/// </summary>
template<typename T, typename INT, typename SINT, typename REAL>
class PRNG
{
protected:
INLINE T* thiss() const { return (T*)this; }
public:
/// <summary>
/// Size of Generated Values in Bytes
/// </summary>
static constexpr size_t GENSIZE = sizeof(INT);
/// <summary>
/// Empty Constructor
/// </summary>
INLINE PRNG() { }
///////////////////////////////////////////////////////////////////////////////////////////////////
// RANDOMS WITHIN SPECIFIED TYPE AND RANGE
///////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Returns next unsigned pseudo random number in range [min, max] (inclusive).
/// Min must be smaller than max and range (max-min) must be smaller than unsigned MAXINT (e.g. 0xFFFFFFFF for 32-Bit).
/// Uniform=true makes distribution more uniform at cost of indeterminstic and higher runtime.
/// </summary>
INLINE INT next(const INT min, const INT max, const bool uniform = false)
{
assert(min < max);
assert(max - min < std::numeric_limits<INT>::max());
if (uniform)
{
const INT range = (INT)1U + max - min;
const INT bins = std::numeric_limits<INT>::max() / range;
const INT limit = bins * range;
INT r;
do { r = thiss()->next(); }
while (r >= limit);
return min + (r / bins);
}
else
return (thiss()->next() % ((max - min) + 1U)) + min;
}
/// <summary>
/// Returns next signed pseudo random number in range [min, max] (inclusive).
/// Min must be smaller than max and range (max-min) must be smaller than or equal to signed MAXINT (e.g. 0x7FFFFFFF for 32-Bit).
/// Uniform=true makes distribution more uniform at cost of indeterminstic and higher runtime.
/// </summary>
INLINE SINT next(const SINT min, const SINT max, const bool uniform = false)
{
assert(min < max);
const INT d = (INT)(max - min);
assert(d <= (INT)std::numeric_limits<SINT>::max());
const INT r = thiss()->next(0U, d, uniform);
return (SINT)r + min;
}
/// <summary>
/// Returns next pseudo random floating point number in range [min, max] (inclusive).
/// Min must be smaller than max and there are more limitations due to floating point precision.
/// </summary>
INLINE REAL next(const REAL min, const REAL max)
{
assert(min < max);
const REAL r = (REAL)thiss()->next() / (REAL)std::numeric_limits<INT>::max();
return CppCore::madd(r, (max - min), min);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// ARRAY FILLING
///////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Fills memory m with len random bytes
/// </summary>
INLINE void fill(void* m, size_t len)
{
INT* pm = (INT*)m;
while (len >= sizeof(INT))
{
*pm++ = thiss()->next();
len -= sizeof(INT);
}
if (len)
{
INT t = thiss()->next();
uint8_t* p8 = (uint8_t*)pm;
while (len)
{
*p8++ = (uint8_t)t;
t >>= 8;
len--;
}
}
}
/// <summary>
/// Fills memory m with random bytes
/// </summary>
template<typename TSTRUCT>
INLINE void fill(TSTRUCT& m)
{
thiss()->fill((void*)&m, sizeof(TSTRUCT));
}
/// <summary>
/// Fills memory m with n unsigned and unbound random integer values.
/// </summary>
INLINE void fill(INT* m, const size_t n)
{
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
m[i] = thiss()->next();
}
/// <summary>
/// Fills memory m with n unsigned random integer values in range [min, max] (inclusive).
/// </summary>
INLINE void fill(INT* m, const size_t n, const INT min, const INT max, const bool uniform = false)
{
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
m[i] = thiss()->next(min, max, uniform);
}
/// <summary>
/// Fills memory m with n signed random integer values in range [min, max] (inclusive).
/// </summary>
INLINE void fill(SINT* m, const size_t n, const SINT min, const SINT max, const bool uniform = false)
{
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
m[i] = thiss()->next(min, max, uniform);
}
/// <summary>
/// Fills memory m with n random floating point values in range [min, max] (inclusive).
/// </summary>
INLINE void fill(REAL* m, const size_t n, const REAL min, const REAL max)
{
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
m[i] = thiss()->next(min, max);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// STD C++ PRNG COMPATIBILITY
///////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Same as INT. For some compatibility with std C++ generators.
/// </summary>
using result_type = INT;
/// <summary>
/// Same as next(). For some compatibility with std C++ generators.
/// </summary>
INLINE INT operator()() { return thiss()->next(); }
/// <summary>
/// Maximum value returned from next(). For some compatibility with std C++ generators.
/// </summary>
INLINE static INT max() { return std::numeric_limits<INT>::max(); }
/// <summary>
/// Minimum value returned from next(). For some compatibility with std C++ generators.
/// </summary>
INLINE static INT min() { return (INT)0; }
};
/// <summary>
/// Xorshift32 PRNG by George Marsaglia creating 32-Bit pseudo randoms.
/// Must not be seeded with zero.
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Xorshift
/// </remarks>
class Xorshift32 : public PRNG<Xorshift32, uint32_t, int32_t, float>
{
typedef PRNG<Xorshift32, uint32_t, int32_t, float> Xorshift32b;
protected:
uint32_t s;
public:
using Xorshift32b::next;
INLINE Xorshift32(): s(Random::seed32()) { assert(s != 0U); }
INLINE Xorshift32(const uint32_t seed) : s(seed) { assert(s != 0U); }
INLINE uint32_t next()
{
s ^= s << 13;
s ^= s >> 17;
s ^= s << 5;
return s;
}
};
/// <summary>
/// Xorshift64 PRNG by George Marsaglia creating 64-Bit pseudo randoms.
/// Must not be seeded with zero.
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Xorshift
/// </remarks>
class Xorshift64 : public PRNG<Xorshift64, uint64_t, int64_t, double>
{
typedef PRNG<Xorshift64, uint64_t, int64_t, double> Xorshift64b;
protected:
uint64_t s;
public:
using Xorshift64b::next;
INLINE Xorshift64() : s(Random::seed64()) { assert(s != 0ULL); }
INLINE Xorshift64(const uint64_t seed) : s(seed) { assert(s != 0ULL); }
INLINE uint64_t next()
{
s ^= s << 13;
s ^= s >> 7;
s ^= s << 17;
return s;
}
};
/// <summary>
/// xoshiro128++ 1.0 PRNG by David Blackman and Sebastiano Vigna creating 32-Bit pseudo randoms.
/// Must not be seeded with zero.
/// </summary>
/// <remarks>
/// https://prng.di.unimi.it/xoshiro128plusplus.c
/// </remarks>
class Xoshiro32 : public PRNG<Xoshiro32, uint32_t, int32_t, float>
{
typedef PRNG<Xoshiro32, uint32_t, int32_t, float> Xoshiro32b;
protected:
uint32_t s[4];
public:
using Xoshiro32b::next;
INLINE Xoshiro32()
{
s[0] = Random::seed32();
s[1] = Random::seed32();
s[2] = Random::seed32();
s[3] = Random::seed32();
assert(s[0] != 0U || s[1] != 0U || s[2] != 0U || s[3] != 0U);
}
INLINE Xoshiro32(const uint32_t seed1, const uint32_t seed2, const uint32_t seed3, const uint32_t seed4)
{
s[0] = seed1;
s[1] = seed2;
s[2] = seed3;
s[3] = seed4;
assert(s[0] != 0U || s[1] != 0U || s[2] != 0U || s[3] != 0U);
}
INLINE uint32_t next()
{
const uint32_t r = CppCore::rotl32(s[0] + s[3], 7U) + s[0];
const uint32_t t = s[1] << 9;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = CppCore::rotl32(s[3], 11U);
return r;
}
};
/// <summary>
/// xoshiro256++ 1.0 PRNG by David Blackman and Sebastiano Vigna creating 64-Bit pseudo randoms.
/// Must not be seeded with zero.
/// </summary>
/// <remarks>
/// https://prng.di.unimi.it/xoshiro256plusplus.c
/// </remarks>
class Xoshiro64 : public PRNG<Xoshiro64, uint64_t, int64_t, double>
{
typedef PRNG<Xoshiro64, uint64_t, int64_t, double> Xoshiro64b;
protected:
uint64_t s[4];
public:
using Xoshiro64b::next;
INLINE Xoshiro64()
{
s[0] = Random::seed64();
s[1] = Random::seed64();
s[2] = Random::seed64();
s[3] = Random::seed64();
assert(s[0] != 0ULL || s[1] != 0ULL || s[2] != 0ULL || s[3] != 0ULL);
}
INLINE Xoshiro64(const uint64_t seed1, const uint64_t seed2, const uint64_t seed3, const uint64_t seed4)
{
s[0] = seed1;
s[1] = seed2;
s[2] = seed3;
s[3] = seed4;
assert(s[0] != 0ULL || s[1] != 0ULL || s[2] != 0ULL || s[3] != 0ULL);
}
INLINE uint64_t next()
{
const uint64_t r = CppCore::rotl64(s[0] + s[3], 23U) + s[0];
const uint64_t t = s[1] << 17;
s[2] ^= s[0];
s[3] ^= s[1];
s[1] ^= s[2];
s[0] ^= s[3];
s[2] ^= t;
s[3] = CppCore::rotl64(s[3], 45U);
return r;
}
};
/// <summary>
/// Mulberry32 PRNG by Tommy Ettinger creating 32-Bit pseudo randoms.
/// </summary>
/// <remarks>
/// https://gist.github.com/tommyettinger/46a874533244883189143505d203312c
/// </remarks>
class Mulberry32 : public PRNG<Mulberry32, uint32_t, int32_t, float>
{
typedef PRNG<Mulberry32, uint32_t, int32_t, float> Mulberry32b;
protected:
uint32_t s;
public:
using Mulberry32b::next;
INLINE Mulberry32() : s(Random::seed32()) { }
INLINE Mulberry32(const uint32_t seed) : s(seed) { }
INLINE uint32_t next()
{
uint32_t r;
r = s + 0x6D2B79F5U;
s = r;
r = (r ^ (r >> 15)) * (r | 1U);
r = r ^ (r + ((r ^ (r >> 7)) * (r | 61U)));
return r ^ (r >> 14);
}
};
/// <summary>
/// Splitmix64 PRNG by Sebastiano Vigna creating 64-Bit pseudo randoms.
/// </summary>
/// <remarks>
/// https://prng.di.unimi.it/splitmix64.c
/// </remarks>
class Splitmix64 : public PRNG<Splitmix64, uint64_t, int64_t, double>
{
typedef PRNG<Splitmix64, uint64_t, int64_t, double> Splitmix64b;
protected:
uint64_t s;
public:
using Splitmix64b::next;
INLINE Splitmix64() : s(Random::seed64()) { }
INLINE Splitmix64(const uint64_t seed) : s(seed) { }
INLINE uint64_t next()
{
uint64_t r = (s += 0x9E3779B97F4A7C15ULL);
r = (r ^ (r >> 30)) * 0xBF58476D1CE4E5B9ULL;
r = (r ^ (r >> 27)) * 0x94D049BB133111EBULL;
return r ^ (r >> 31);
}
};
#if defined(CPPCORE_CPUFEAT_RDRAND) && defined(CPPCORE_CPUFEAT_SSE2)
/// <summary>
/// CPU PRNG creating 32-Bit pseudo random. Uses RDRAND instruction.
/// Beware: This instruction is known to be buggy, consider testing with validate()
/// </summary>
class Cpu32 : public PRNG<Cpu32, uint32_t, int32_t, float>
{
typedef PRNG<Cpu32, uint32_t, int32_t, float> Cpu32b;
public:
using Cpu32b::next;
INLINE Cpu32() { }
INLINE static bool validate(const size_t loops = 512U)
{
uint32_t v;
for (size_t i = 0; i < loops; i++)
if (_rdrand32_step(&v)) return true;
else _mm_pause();
return false;
}
INLINE uint32_t next()
{
uint32_t v;
while (0 == _rdrand32_step(&v))
_mm_pause();
return v;
}
};
/// <summary>
/// CPU PRNG creating 64-Bit pseudo random. Uses RDRAND instruction.
/// Beware: This instruction is known to be buggy, consider testing with validate()
/// </summary>
class Cpu64 : public PRNG<Cpu64, uint64_t, int64_t, double>
{
typedef PRNG<Cpu64, uint64_t, int64_t, double> Cpu64b;
public:
using Cpu64b::next;
INLINE Cpu64() { }
INLINE static bool validate(const size_t loops = 512U)
{
#if defined(CPPCORE_CPU_X64)
uint64_t v;
for (size_t i = 0; i < loops; i++)
if (_rdrand64_step((unsigned long long*)&v)) return true;
else _mm_pause();
#elif defined(CPPCORE_CPU_X86)
uint32_t v;
for (size_t i = 0; i < loops; i++)
if (_rdrand32_step(&v)) return true;
else _mm_pause();
#endif
return false;
}
INLINE uint64_t next()
{
uint64_t v;
#if defined(CPPCORE_CPU_X64)
while (0 == _rdrand64_step((unsigned long long*)&v))
_mm_pause();
#elif defined(CPPCORE_CPU_X86)
while (0 == _rdrand32_step((uint32_t*)&v))
_mm_pause();
while (0 == _rdrand32_step(((uint32_t*)&v) + 1))
_mm_pause();
#endif
return v;
}
};
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// STD C++ PRNG
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// Pseudo Random Number Generators using std C++
/// </summary>
class Std
{
protected:
/// <summary>
/// Base Class for C++ STD Generators
/// </summary>
template <typename T, typename INT, typename SINT, typename REAL, typename GENERATOR>
class Base : public PRNG<T, INT, SINT, REAL>
{
protected:
GENERATOR mGenerator;
public:
using PRNG<T, INT, SINT, REAL>::next;
INLINE Base(const INT seed) : mGenerator(seed) { }
INLINE INT next() { return (INT)mGenerator(); }
INLINE INT next(const INT min, const INT max, const bool uniform = false)
{
return uniform ?
std::uniform_int_distribution<INT>(min, max)(mGenerator) :
PRNG<T, INT, SINT, REAL>::next(min, max, false);
}
INLINE SINT next(const SINT min, const SINT max, const bool uniform = false)
{
return uniform ?
std::uniform_int_distribution<SINT>(min, max)(mGenerator) :
PRNG<T, INT, SINT, REAL>::next(min, max, false);
}
INLINE REAL next(const REAL min, const REAL max)
{
return std::uniform_real_distribution<REAL>(min, max)(mGenerator);
}
};
public:
/// <summary>
/// Mersenne Twister 19937 PRNG from C++ STD creating 32-Bit pseudo randoms.
/// </summary>
class Mt32 : public Base<Mt32, uint32_t, int32_t, float, std::mt19937>
{
typedef Base<Mt32, uint32_t, int32_t, float, std::mt19937> Mt32b;
public:
INLINE Mt32() : Mt32b(Random::seed32()) { }
INLINE Mt32(const uint32_t seed) : Mt32b(seed) { assert(seed != 0U); }
};
/// <summary>
/// Mersenne Twister 19937 PRNG from C++ STD creating 64-Bit pseudo randoms.
/// </summary>
class Mt64 : public Base<Mt64, uint64_t, int64_t, double, std::mt19937_64>
{
typedef Base<Mt64, uint64_t, int64_t, double, std::mt19937_64> Mt64b;
public:
INLINE Mt64() : Mt64b(Random::seed64()) { }
INLINE Mt64(const uint64_t seed) : Mt64b(seed) { assert(seed != 0ULL); }
};
using Int32 = Mt32; // for backwards compatibility, todo: remove
using Int64 = Mt64; // for backwards compatibility, todo: remove
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// DEFAULT PSEUDO RANDOM NUMBER GENERATOR
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#if !defined(CPPCORE_PRNG_DEFAULT32)
#define CPPCORE_PRNG_DEFAULT32 Random::Mulberry32
#endif
#if !defined(CPPCORE_PRNG_DEFAULT64)
#define CPPCORE_PRNG_DEFAULT64 Random::Splitmix64
#endif
using Default32 = CPPCORE_PRNG_DEFAULT32;
using Default64 = CPPCORE_PRNG_DEFAULT64;
#if defined(CPPCORE_CPU_64BIT)
using Default = Default64;
#else
using Default = Default32;
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////
// CUSTOM PACKED PSEUDO RANDOM NUMBER GENERATORS
/////////////////////////////////////////////////////////////////////////////////////////////////////////
#if defined(CPPCORE_CPUFEAT_SSE2)
/// <summary>
/// Base Class for 32x4 Bits Packed Pseudo Random Number Generators. Requires SSE2.
/// </summary>
template<typename T>
class PPRNG32x4
{
protected:
INLINE T* thiss() const { return (T*)this; }
public:
/// <summary>
/// Empty Constructor
/// </summary>
INLINE PPRNG32x4() { }
/// <summary>
///
/// </summary>
INLINE __m128i next(const uint32_t min, const uint32_t max)
{
assert(min < max);
assert(max - min < std::numeric_limits<uint32_t>::max());
const uint32_t d = (max - min) + 1U;
union {
__m128i n;
uint32_t n32[4];
};
n = thiss()->next();
n32[0] %= d; n32[1] %= d;
n32[2] %= d; n32[3] %= d;
return _mm_add_epi32(n, _mm_set1_epi32(min));
}
/// <summary>
///
/// </summary>
INLINE __m128i next(const int32_t min, const int32_t max)
{
assert(min < max);
const uint32_t d = (uint32_t)(max - min);
assert(d <= (uint32_t)std::numeric_limits<int32_t>::max());
const __m128i r = thiss()->next(0U, d);
return _mm_add_epi32(r, _mm_set1_epi32(min));
}
/// <summary>
///
/// </summary>
INLINE __m128 next(const float min, const float max)
{
assert(min < max);
const __m128i v = thiss()->next();
const __m128 n = CppCore::cvtepu32f(v);
const __m128 d = _mm_set1_ps((float)std::numeric_limits<uint32_t>::max());
const __m128 r = _mm_div_ps(n, d);
return CppCore::madd128f(r, _mm_set1_ps(max - min), _mm_set1_ps(min));
}
/// <summary>
///
/// </summary>
INLINE void fill(__m128i* m, const size_t n, const uint32_t min, const uint32_t max)
{
assert(min < max);
assert(max - min < std::numeric_limits<uint32_t>::max());
const uint32_t d = (max - min) + 1U;
const __m128i u = _mm_set1_epi32(min);
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
{
union {
__m128i n;
uint32_t n32[4];
};
n = thiss()->next();
n32[0] %= d; n32[1] %= d;
n32[2] %= d; n32[3] %= d;
m[i] = _mm_add_epi32(n, u);
}
}
/// <summary>
///
/// </summary>
INLINE void fill(__m128i* m, const size_t n, const int32_t min, const int32_t max)
{
assert(min < max);
const uint32_t d = (uint32_t)(max - min);
assert(d <= (uint32_t)std::numeric_limits<int32_t>::max());
const __m128i u = _mm_set1_epi32(min);
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
m[i] = _mm_add_epi32(thiss()->next(0U, d), u);
}
/// <summary>
///
/// </summary>
INLINE void fill(__m128* m, const size_t n, const float min, const float max)
{
assert(min < max);
const __m128 c1 = _mm_set1_ps((float)std::numeric_limits<uint32_t>::max());
const __m128 c2 = _mm_set1_ps(max - min);
const __m128 c3 = _mm_set1_ps(min);
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
{
const __m128i v = thiss()->next();
const __m128 n = CppCore::cvtepu32f(v);
m[i] = _mm_add_ps(_mm_mul_ps(_mm_div_ps(n, c1), c2), c3);
}
}
};
#endif
#if defined(CPPCORE_CPUFEAT_AVX512F) && defined(CPPCORE_OS_WINDOWS)
/// <summary>
/// Base Class for 64x2 Bits Packed Pseudo Random Number Generators. Requires AVX512.
/// </summary>
template<typename T>
class PPRNG64x2
{
protected:
INLINE T* thiss() const { return (T*)this; }
public:
/// <summary>
/// Empty Constructor
/// </summary>
INLINE PPRNG64x2() { }
/// <summary>
///
/// </summary>
INLINE __m128i next(const uint64_t min, const uint64_t max)
{
// _mm_mullo_epi64 is avx-512
assert(min < max);
assert(max - min < std::numeric_limits<uint64_t>::max());
const __m128i n = thiss()->next();
const __m128i d = _mm_set1_epi64x(((max - min) + 1U));
const __m128i div = _mm_div_epu64(n, d);
const __m128i rem = _mm_sub_epi64(n, _mm_mullo_epi64(div, d));
return _mm_add_epi64(rem, _mm_set1_epi64x(min));
}
/// <summary>
///
/// </summary>
INLINE __m128i next(const int64_t min, const int64_t max)
{
assert(min < max);
const uint64_t d = (uint64_t)(max - min);
assert(d <= (uint64_t)std::numeric_limits<int64_t>::max());
const __m128i r = thiss()->next(0ULL, d);
return _mm_add_epi64(r, _mm_set1_epi64x(min));
}
/// <summary>
///
/// </summary>
INLINE __m128d next(const double min, const double max)
{
assert(min < max);
const __m128i v = thiss()->next();
const __m128d n = _mm_cvtepu64_pd(v); // this is avx-512
const __m128d d = _mm_set1_pd((double)std::numeric_limits<uint64_t>::max());
const __m128d r = _mm_div_pd(n, d);
return _mm_add_pd(_mm_mul_pd(r, _mm_set1_pd(max - min)), _mm_set1_pd(min));
}
/// <summary>
/// TODO: TEST IT
/// </summary>
INLINE void fill(__m128i* m, const size_t n, const uint64_t min, const uint64_t max)
{
assert(min < max);
assert(max - min < std::numeric_limits<uint64_t>::max());
const __m128i d = _mm_set1_epi64x(((max - min) + 1U));
const __m128i u = _mm_set1_epi64x(min);
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
{
const __m128i n = thiss()->next();
const __m128i div = _mm_div_epu64(n, d);
const __m128i rem = _mm_sub_epi64(n, _mm_mullo_epi64(div, d));
m[i] = _mm_add_epi64(rem, u);
}
}
/// <summary>
/// TODO: TEST IT
/// </summary>
INLINE void fill(__m128i* m, const size_t n, const int64_t min, const int64_t max)
{
assert(min < max);
const uint64_t d = (uint64_t)(max - min);
assert(d <= (uint64_t)std::numeric_limits<int64_t>::max());
const __m128i u = _mm_set1_epi64x(min);
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
m[i] = _mm_add_epi64(thiss()->next(0ULL, d), u);
}
/// <summary>
/// TODO: TEST IT
/// </summary>
INLINE void fill(__m128d* m, const size_t n, const double min, const double max)
{
assert(min < max);
const __m128d c1 = _mm_set1_pd((double)std::numeric_limits<uint64_t>::max());
const __m128d c2 = _mm_set1_pd(max - min);
const __m128d c3 = _mm_set1_pd(min);
CPPCORE_UNROLL
for (size_t i = 0; i < n; i++)
{
const __m128i v = thiss()->next();
const __m128d n = _mm_cvtepu64_pd(v); // this is avx-512
m[i] = _mm_add_pd(_mm_mul_pd(_mm_div_pd(n, c1), c2), c3);
}
}
};
#endif
#if defined(CPPCORE_CPUFEAT_SSE2)
/// <summary>
/// Xorshift32 four times in parallel. Requires SSE2.
/// </summary>
class Xorshift32x4 : public PPRNG32x4<Xorshift32x4>
{
typedef PPRNG32x4<Xorshift32x4> Xorshift32x4b;
protected:
__m128i s;
public:
using Xorshift32x4b::next;
INLINE Xorshift32x4() : s(Random::seed32x4()) { }
INLINE Xorshift32x4(const __m128i& seed) : s(seed)
{
assert(_mm_movemask_epi8(_mm_cmpeq_epi32(seed, _mm_setzero_si128())) == 0);
}
INLINE Xorshift32x4(const uint32_t s1, const uint32_t s2, const uint32_t s3, const uint32_t s4) :
s(_mm_set_epi32(s4, s3, s2, s1))
{
assert(s1 != 0U);
assert(s2 != 0U);
assert(s3 != 0U);
assert(s4 != 0U);
}
INLINE __m128i next()
{
s = _mm_xor_si128(s, _mm_slli_epi32(s, 13));
s = _mm_xor_si128(s, _mm_srli_epi32(s, 17));
s = _mm_xor_si128(s, _mm_slli_epi32(s, 5));
return s;
}
};
#endif
#if defined(CPPCORE_CPUFEAT_AVX512F) && defined(CPPCORE_OS_WINDOWS)
/// <summary>
/// Xorshift64 two times in parallel. Requires AVX512.
/// </summary>
class Xorshift64x2 : public PPRNG64x2<Xorshift64x2>
{
typedef PPRNG64x2<Xorshift64x2> Xorshift64x2b;
protected:
__m128i s;
public:
using Xorshift64x2b::next;
INLINE Xorshift64x2() : s(Random::seed64x2()) { }
INLINE Xorshift64x2(const __m128i& seed) : s(seed)
{
assert(_mm_movemask_epi8(_mm_cmpeq_epi64(seed, _mm_setzero_si128())) == 0);
}
INLINE Xorshift64x2(const uint64_t s1, const uint64_t s2) :
s(_mm_set_epi64x(s2, s1))
{
assert(s1 != 0ULL);
assert(s2 != 0ULL);
}
INLINE __m128i next()
{
s = _mm_xor_si128(s, _mm_slli_epi64(s, 13));
s = _mm_xor_si128(s, _mm_srli_epi64(s, 7));
s = _mm_xor_si128(s, _mm_slli_epi64(s, 17));
return s;
}
};
#endif
#if defined(CPPCORE_CPUFEAT_SSE41)
/// <summary>
/// Mulberry32 four times in parallel. Requires SSE4.1.
/// </summary>
class Mulberry32x4 : public PPRNG32x4<Mulberry32x4>
{
typedef PPRNG32x4<Mulberry32x4> Mulberry32x4b;
protected:
__m128i s;
public:
using Mulberry32x4b::next;
INLINE Mulberry32x4() : s(Random::seed32x4()) { }
INLINE Mulberry32x4(const __m128i& seed) : s(seed) { }
INLINE Mulberry32x4(const uint32_t s1, const uint32_t s2, const uint32_t s3, const uint32_t s4) :
s(_mm_set_epi32(s4, s3, s2, s1)) { }
INLINE __m128i next()
{
// _mm_mullo_epi32 is sse4.1
__m128i r;
const __m128i c1 = _mm_set1_epi32(0x6D2B79F5U);
const __m128i c2 = _mm_set1_epi32(1U);
const __m128i c3 = _mm_set1_epi32(61U);
r = _mm_add_epi32(s, c1);
s = r;
r = _mm_mullo_epi32(_mm_xor_si128(r, _mm_srli_epi32(r, 15)), _mm_or_si128(r, c2));
r = _mm_xor_si128(r, _mm_add_epi32(r, _mm_mullo_epi32(_mm_xor_si128(r, _mm_srli_epi32(r, 7)), _mm_or_si128(r, c3))));
r = _mm_xor_si128(r, _mm_srli_epi32(r, 14));
return r;
}
};
#endif
#if defined(CPPCORE_CPUFEAT_AVX512F) && defined(CPPCORE_OS_WINDOWS)
/// <summary>
/// Splitmix64 two times in parallel. Requires AVX512.
/// </summary>
class Splitmix64x2 : public PPRNG64x2<Splitmix64x2>
{
typedef PPRNG64x2<Splitmix64x2> Splitmix64x2b;
protected:
__m128i s;
public:
using Splitmix64x2b::next;
INLINE Splitmix64x2() : s(Random::seed64x2()) { }
INLINE Splitmix64x2(const __m128i& seed) : s(seed) { }
INLINE Splitmix64x2(const uint64_t s1, const uint64_t s2) :
s(_mm_set_epi64x(s2, s1)) { }
INLINE __m128i next()