54 lines
1.1 KiB
C
54 lines
1.1 KiB
C
#include "xorshift.h"
|
|
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
|
|
static uint32_t xorshift128_seed[4] = {1, 1, 1, 1};
|
|
static uint64_t xorshift128plus_seed[2] = {1, 1};
|
|
|
|
void set_xorshift128_seed(uint32_t seed[4])
|
|
{
|
|
for (size_t i = 0; i < 4; ++i) {
|
|
|
|
assert(seed[i] != (uint32_t)0);
|
|
xorshift128_seed[i] = seed[i];
|
|
}
|
|
}
|
|
|
|
uint32_t xorshift128()
|
|
{
|
|
uint32_t t = xorshift128_seed[0];
|
|
|
|
t ^= t << 11;
|
|
t ^= t >> 8;
|
|
|
|
xorshift128_seed[0] = xorshift128_seed[1];
|
|
xorshift128_seed[1] = xorshift128_seed[2];
|
|
xorshift128_seed[2] = xorshift128_seed[3];
|
|
|
|
xorshift128_seed[3] ^= xorshift128_seed[3] >> 19;
|
|
xorshift128_seed[3] ^= t;
|
|
return xorshift128_seed[3];
|
|
}
|
|
|
|
|
|
void set_xorshift128plus_seed(uint64_t seed[2])
|
|
{
|
|
for (size_t i = 0; i < 2; ++i) {
|
|
|
|
assert(seed[i] != (uint64_t)0);
|
|
xorshift128plus_seed[i] = seed[i];
|
|
}
|
|
}
|
|
|
|
uint64_t xorshift128plus()
|
|
{
|
|
uint64_t x = xorshift128plus_seed[0];
|
|
uint64_t const y = xorshift128plus_seed[1];
|
|
|
|
xorshift128plus_seed[0] = y;
|
|
x ^= x << 23;
|
|
xorshift128plus_seed[1] = x ^ y ^ (x >> 17) ^ (y >> 26);
|
|
|
|
return xorshift128plus_seed[1] + y;
|
|
}
|