Skip to content

Commit

Permalink
More changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCWills committed Mar 19, 2024
1 parent ebdad6e commit 0ff3d98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
21 changes: 16 additions & 5 deletions Source/engine/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,29 @@ std::linear_congruential_engine<uint32_t, 0x015A4E35, 1, 0> diabloGenerator;
/** Xoshiro pseudo-random number generator to provide less predictable seeds */
xoshiro128plusplus seedGenerator;

uint32_t xoshiro128plusplus::rotl(const uint32_t x, int k)
uint32_t xoshiro128plusplus::next()
{
return std::rotl(x, k);
const uint32_t result = std::rotl(s[0] + s[3], 7) + 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] = std::rotl(s[3], 11);

return result;
}

uint64_t xoshiro128plusplus::timeSeed()
{
auto now = std::chrono::system_clock::now();
auto nano = std::chrono::nanoseconds(now.time_since_epoch());
long long time = nano.count();
SplitMix64 seedSequence { static_cast<uint64_t>(time) };
return seedSequence.next();
return static_cast<uint64_t>(nano.count());
}

void xoshiro128plusplus::copy(state &dst, const state &src)
Expand Down
33 changes: 7 additions & 26 deletions Source/engine/random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,38 +212,22 @@ class xoshiro128plusplus {
xoshiro128plusplus(uint64_t initialSeed) { seed(initialSeed); }
xoshiro128plusplus(uint32_t initialSeed) { seed(initialSeed); }

uint32_t next()
{
const uint32_t result = rotl(s[0] + s[3], 7) + 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] = rotl(s[3], 11);

return result;
}
uint32_t next();

/* This is the jump function for the generator. It is equivalent
to 2^64 calls to next(); it can be used to generate 2^64
non-overlapping subsequences for parallel computations. */
void jump()
{
static const uint32_t JUMP[] = { 0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b };
static constexpr uint32_t JUMP[] = { 0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b };

uint32_t s0 = 0;
uint32_t s1 = 0;
uint32_t s2 = 0;
uint32_t s3 = 0;
for (int i = 0; i < sizeof JUMP / sizeof *JUMP; i++)
for (const uint32_t entry : JUMP)
for (int b = 0; b < 32; b++) {
if (JUMP[i] & UINT32_C(1) << b) {
if (entry & UINT32_C(1) << b) {
s0 ^= s[0];
s1 ^= s[1];
s2 ^= s[2];
Expand Down Expand Up @@ -286,17 +270,14 @@ class xoshiro128plusplus {

void seed()
{
uint64_t ts = timeSeed();
s[0] = static_cast<uint32_t>(ts >> 32);
s[1] = static_cast<uint32_t>(ts);
seed(timeSeed());

static std::random_device rd;
std::uniform_int_distribution<uint32_t> dist;
s[2] = dist(rd);
s[3] = dist(rd);
for (uint32_t &cell : s)
cell ^= dist(rd);
}

static uint32_t rotl(const uint32_t x, int k);
static uint64_t timeSeed();
static void copy(state &dst, const state &src);
};
Expand Down

0 comments on commit 0ff3d98

Please sign in to comment.