2018-11-28 23:18:09 +01:00
|
|
|
using Ryujinx.Common;
|
|
|
|
|
2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Common
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
class MersenneTwister
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
private int _index;
|
|
|
|
private uint[] _mt;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public MersenneTwister(uint seed)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_mt = new uint[624];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_mt[0] = seed;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int mtIdx = 1; mtIdx < _mt.Length; mtIdx++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
uint prev = _mt[mtIdx - 1];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_mt[mtIdx] = (uint)(0x6c078965 * (prev ^ (prev >> 30)) + mtIdx);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_index = _mt.Length;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
public long GenRandomNumber(long min, long max)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long range = max - min;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (min == max)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
return min;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (range == -1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
//Increment would cause a overflow, special case.
|
|
|
|
return GenRandomNumber(2, 2, 32, 0xffffffffu, 0xffffffffu);
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
range++;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//This is log2(Range) plus one.
|
2018-12-06 12:16:24 +01:00
|
|
|
int nextRangeLog2 = 64 - BitUtils.CountLeadingZeros64(range);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
//If Range is already power of 2, subtract one to use log2(Range) directly.
|
2018-12-06 12:16:24 +01:00
|
|
|
int rangeLog2 = nextRangeLog2 - (BitUtils.IsPowerOfTwo64(range) ? 1 : 0);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int parts = rangeLog2 > 32 ? 2 : 1;
|
|
|
|
int bitsPerPart = rangeLog2 / parts;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int fullParts = parts - (rangeLog2 - parts * bitsPerPart);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
uint mask = 0xffffffffu >> (32 - bitsPerPart);
|
|
|
|
uint maskPlus1 = 0xffffffffu >> (31 - bitsPerPart);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
long randomNumber;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
randomNumber = GenRandomNumber(parts, fullParts, bitsPerPart, mask, maskPlus1);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2018-12-06 12:16:24 +01:00
|
|
|
while ((ulong)randomNumber >= (ulong)range);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return min + randomNumber;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private long GenRandomNumber(
|
2018-12-06 12:16:24 +01:00
|
|
|
int parts,
|
|
|
|
int fullParts,
|
|
|
|
int bitsPerPart,
|
|
|
|
uint mask,
|
|
|
|
uint maskPlus1)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
long randomNumber = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
int part = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (; part < fullParts; part++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
randomNumber <<= bitsPerPart;
|
|
|
|
randomNumber |= GenRandomNumber() & mask;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
for (; part < parts; part++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
randomNumber <<= bitsPerPart + 1;
|
|
|
|
randomNumber |= GenRandomNumber() & maskPlus1;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return randomNumber;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private uint GenRandomNumber()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
if (_index >= _mt.Length)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
Twist();
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
uint value = _mt[_index++];
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
value ^= value >> 11;
|
|
|
|
value ^= (value << 7) & 0x9d2c5680;
|
|
|
|
value ^= (value << 15) & 0xefc60000;
|
|
|
|
value ^= value >> 18;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
return value;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Twist()
|
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
for (int mtIdx = 0; mtIdx < _mt.Length; mtIdx++)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
uint value = (_mt[mtIdx] & 0x80000000) + (_mt[(mtIdx + 1) % _mt.Length] & 0x7fffffff);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_mt[mtIdx] = _mt[(mtIdx + 397) % _mt.Length] ^ (value >> 1);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if ((value & 1) != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
_mt[mtIdx] ^= 0x9908b0df;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
_index = 0;
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|