Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions c_src/pcg32.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,36 @@
#include "rdtsc.h"
#endif

static __thread uint64_t state = 5573589319906701683ULL;

static uint32_t pcg32(void)
{
uint64_t oldstate = state;
state = oldstate * 6364136223846793005ULL;
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
uint32_t v = (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
return v;
}

uint32_t uniform32(void)
{
static __thread bool seeded = false;
static __thread uint64_t state;
if (unlikely(!seeded)) {
uint64_t t;
#ifdef HAVE_RDRAND
state = rdrand64();
t = rdrand64();
#else
uint64_t u;
rdtsc(&state, &u);
rdtsc(&t, &u);
#endif
pcg32();
state += t;
pcg32();
seeded = true;
}

uint64_t oldstate = state;
state = oldstate * 6364136223846793005ULL;
uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
uint32_t rot = oldstate >> 59u;
uint32_t v = (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
return v;
return pcg32();
}

ERL_NIF_TERM uniform_1(ErlNifEnv *env,
Expand Down
2 changes: 1 addition & 1 deletion c_src/stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main(void)

int main(void)
{
uint32_t modulus = uniform32();
uint32_t modulus = uniform32() || uniform32();
for (size_t i = 0; i < 10000000UL; ++i)
uniform_1(NULL, 1, (const ERL_NIF_TERM[]){modulus});
return 0;
Expand Down