2018-11-28 17:18:09 -05:00
|
|
|
using Ryujinx.Common;
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
|
|
|
{
|
|
|
|
class MersenneTwister
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
private int Index;
|
|
|
|
private uint[] Mt;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public MersenneTwister(uint Seed)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Mt = new uint[624];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Mt[0] = Seed;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (int MtIdx = 1; MtIdx < Mt.Length; MtIdx++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
uint Prev = Mt[MtIdx - 1];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Mt[MtIdx] = (uint)(0x6c078965 * (Prev ^ (Prev >> 30)) + MtIdx);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Index = Mt.Length;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
public long GenRandomNumber(long Min, long Max)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long Range = Max - Min;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Min == Max)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
return Min;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Range == -1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
//Increment would cause a overflow, special case.
|
|
|
|
return GenRandomNumber(2, 2, 32, 0xffffffffu, 0xffffffffu);
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Range++;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
//This is log2(Range) plus one.
|
2018-12-04 19:52:39 -05:00
|
|
|
int NextRangeLog2 = 64 - BitUtils.CountLeadingZeros64(Range);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
//If Range is already power of 2, subtract one to use log2(Range) directly.
|
2018-12-04 19:52:39 -05:00
|
|
|
int RangeLog2 = NextRangeLog2 - (BitUtils.IsPowerOfTwo64(Range) ? 1 : 0);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int Parts = RangeLog2 > 32 ? 2 : 1;
|
|
|
|
int BitsPerPart = RangeLog2 / Parts;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int FullParts = Parts - (RangeLog2 - Parts * BitsPerPart);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
uint Mask = 0xffffffffu >> (32 - BitsPerPart);
|
|
|
|
uint MaskPlus1 = 0xffffffffu >> (31 - BitsPerPart);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
long RandomNumber;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
RandomNumber = GenRandomNumber(Parts, FullParts, BitsPerPart, Mask, MaskPlus1);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
2018-12-04 19:52:39 -05:00
|
|
|
while ((ulong)RandomNumber >= (ulong)Range);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Min + RandomNumber;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private long GenRandomNumber(
|
2018-12-04 19:52:39 -05:00
|
|
|
int Parts,
|
|
|
|
int FullParts,
|
|
|
|
int BitsPerPart,
|
|
|
|
uint Mask,
|
|
|
|
uint MaskPlus1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
long RandomNumber = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
int Part = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (; Part < FullParts; Part++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
RandomNumber <<= BitsPerPart;
|
|
|
|
RandomNumber |= GenRandomNumber() & Mask;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
for (; Part < Parts; Part++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
RandomNumber <<= BitsPerPart + 1;
|
|
|
|
RandomNumber |= GenRandomNumber() & MaskPlus1;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return RandomNumber;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private uint GenRandomNumber()
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
if (Index >= Mt.Length)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
Twist();
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
uint Value = Mt[Index++];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Value ^= Value >> 11;
|
|
|
|
Value ^= (Value << 7) & 0x9d2c5680;
|
|
|
|
Value ^= (Value << 15) & 0xefc60000;
|
|
|
|
Value ^= Value >> 18;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
return Value;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Twist()
|
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
for (int MtIdx = 0; MtIdx < Mt.Length; MtIdx++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
uint Value = (Mt[MtIdx] & 0x80000000) + (Mt[(MtIdx + 1) % Mt.Length] & 0x7fffffff);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Mt[MtIdx] = Mt[(MtIdx + 397) % Mt.Length] ^ (Value >> 1);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
if ((Value & 1) != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-04 19:52:39 -05:00
|
|
|
Mt[MtIdx] ^= 0x9908b0df;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 19:52:39 -05:00
|
|
|
Index = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|