initial implementation of xorshift128 and xorshift128plus
This commit is contained in:
commit
45342ac49d
6 changed files with 120 additions and 0 deletions
54
xorshift.c
Normal file
54
xorshift.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue