| 1 |
#ifdef __cplusplus |
| 2 |
extern "C" { |
| 3 |
#endif |
| 4 |
|
| 5 |
#include "ulib/splitmix.h" |
| 6 |
#include "ulib/gettime.h" |
| 7 |
#include "ulib/sysrand.h" |
| 8 |
|
| 9 |
#ifdef __cplusplus |
| 10 |
} |
| 11 |
#endif |
| 12 |
|
| 13 |
/* based on splitmix64 |
| 14 |
* https://xorshift.di.unimi.it/splitmix64.c |
| 15 |
*/ |
| 16 |
|
| 17 |
void uu_splitmix_srand(pUCXT) { |
| 18 |
unsigned int n; |
| 19 |
UV ptod[2]; |
| 20 |
U64 sysrand; |
| 21 |
|
| 22 |
/* gettimeofday(&tv, 0); */ |
| 23 |
(*uu_gettime_U2time)(aTHX_ ptod); |
| 24 |
SMEM->sm_x = (U64)ptod[0] * 1000000 |
| 25 |
+ (U64)ptod[1]; |
| 26 |
|
| 27 |
/* stir 16 - 31 times, time-based */ |
| 28 |
n = 16 + ((ptod[0] ^ ptod[1]) & 0x0f); |
| 29 |
while (n-- > 0) |
| 30 |
(void)uu_splitmix_rand(aUCXT); |
| 31 |
|
| 32 |
uu_sysrand_bytes(&sysrand, 8); |
| 33 |
SMEM->sm_x ^= sysrand; |
| 34 |
|
| 35 |
/* stir 16 - 31 times, rand-based */ |
| 36 |
n = 16 + (sysrand & 0x0f); |
| 37 |
while (n-- > 0) |
| 38 |
(void)uu_splitmix_rand(aUCXT); |
| 39 |
} |
| 40 |
|
| 41 |
U64 uu_splitmix_rand(pUCXT) { |
| 42 |
U64 z = (SMEM->sm_x += 0x9e3779b97f4a7c15ULL); |
| 43 |
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; |
| 44 |
z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; |
| 45 |
return z ^ (z >> 31); |
| 46 |
} |
| 47 |
|
| 48 |
/* ex:set ts=2 sw=2 itab=spaces: */ |