AuroraRuntime/Source/RNG/WELL.cpp

115 lines
2.9 KiB
C++
Raw Normal View History

2021-09-06 10:58:08 +00:00
/*
* Copyright (C) 2008, Chris Lomont
* Public Domain
*
* http://lomont.org/papers/2008/Lomont_PRNG_2008.pdf
*/
2021-09-30 14:57:41 +00:00
#include <Source/RuntimeInternal.hpp>
2021-09-06 10:58:08 +00:00
#include "WELL.hpp"
#include "mtwister.hpp"
// Redunced seed entropy should not matter to the extent that MT can make up for it.
// WELL is used in the fast rng backend, and i feel as though it would be unreasonable
// to go above UInt64 seeds. For toy purposes, U32 seeds are probably fine. Treat WELL
// like MT without 624 prediction and performance issues.
// Should we look into xorshift soon?
// NOTE: Just to make it clear, the point is that MT entropy should be good enough
// to maintain WELL and xorshift randomness, while acknowledging that reduced seed
// entropy does make it easier to bruteforce especially if the index count is known.
inline static void WELL_SeedRand(WELLRand *rand, AuUInt32 seed)
{
MTRand mtrand = MT_SeedRand(seed);
for (unsigned int i = 0; i < 16; i++)
{
rand->state[i] = MT_NextLong(&mtrand);
}
}
inline static void WELL_SeedRand64(WELLRand *rand, AuUInt64 seed)
{
MTRand mtrand = MT_SeedRand(seed & 0xffffffff);
MTRand mtrand2 = MT_SeedRand(seed >> 32);
for (unsigned int i = 0; i < 16; i += 2)
{
rand->state[i] = MT_NextLong(&mtrand);
rand->state[i + 1] = MT_NextLong(&mtrand2);
}
}
2021-09-06 10:58:08 +00:00
/**
* Creates a new random number generator from a given seed.
*/
WELLRand WELL_SeedRand(AuUInt32 seed)
{
WELLRand rand {};
WELL_SeedRand(&rand, seed);
return rand;
}
WELLRand WELL_SeedRand64(AuUInt64 seed)
{
WELLRand rand {};
WELL_SeedRand64(&rand, seed);
return rand;
}
2021-09-06 10:58:08 +00:00
/**
* Generates a pseudo-randomly generated long.
*/
2021-10-01 17:22:59 +00:00
AuUInt32 WELL_NextLong_Unlocked(WELLRand *rand)
2021-09-06 10:58:08 +00:00
{
AuUInt32 a, b, c, d, ret;
a = rand->state[rand->index];
c = rand->state[(rand->index + 13) & 15];
b = a ^ c ^ (a << 16) ^ (c << 15);
c = rand->state[(rand->index + 9) & 15];
c ^= (c >> 11);
a = rand->state[rand->index] = b ^ c;
d = a ^ ((a << 5) & 0xDA442D24UL);
rand->index = (rand->index + 15) & 15;
a = rand->state[rand->index];
rand->state[rand->index] = a ^ b ^ d ^ (a << 2) ^ (b << 18) ^ (c << 28);
ret = rand->state[rand->index];
2021-10-01 17:22:59 +00:00
return ret;
}
/**
* Generates a pseudo-randomly generated long.
*/
AuUInt32 WELL_NextLong(WELLRand *rand)
{
rand->lock.Lock();
AuUInt32 ret = WELL_NextLong_Unlocked(rand);
2021-09-06 10:58:08 +00:00
rand->lock.Unlock();
return ret;
}
void WELL_NextBytes(WELLRand *rand, void *in, AuUInt32 length)
{
AuUInt i;
AuUInt8 *base;
i = 0;
base = reinterpret_cast<AuUInt8 *>(in);
2021-10-01 17:22:59 +00:00
rand->lock.Lock();
2021-09-06 10:58:08 +00:00
for (; i < length; i += 4)
{
2021-10-01 17:22:59 +00:00
AuUInt32 rng = WELL_NextLong_Unlocked(rand);
2021-09-06 10:58:08 +00:00
std::memcpy(base + i, &rng, 4);
}
if (i > length)
{
i -= 4;
2021-10-01 17:22:59 +00:00
AuUInt32 padRng = WELL_NextLong_Unlocked(rand);
2021-09-06 10:58:08 +00:00
std::memcpy(base + i, &padRng, length - i);
}
2021-10-01 17:22:59 +00:00
rand->lock.Unlock();
2021-09-06 10:58:08 +00:00
}