Skip to content

Commit

Permalink
PCG small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLloyd committed Oct 26, 2023
1 parent 23e5c86 commit f588bc8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 60 deletions.
107 changes: 50 additions & 57 deletions CsCheck/PCG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ namespace CsCheck;
public sealed class PCG
{
static int threadCount;
[ThreadStatic]
static PCG? threadPCG;
[ThreadStatic] static PCG? threadPCG;
public static PCG ThreadPCG => threadPCG ??= new PCG((uint)Interlocked.Increment(ref threadCount));
readonly ulong Inc;
public ulong State;
Expand Down Expand Up @@ -54,84 +53,78 @@ public uint Next(uint maxExclusive)
{
if (maxExclusive == 1U) return 0U;
var threshold = ((uint)-(int)maxExclusive) % maxExclusive;
uint n;
while ((n = Next()) < threshold) ;
var n = Next();
while (n < threshold) n = Next();
return n % maxExclusive;
}
public ulong Next64(ulong maxExclusive)
{
if (maxExclusive <= uint.MaxValue) return Next((uint)maxExclusive);
var threshold = ((ulong)-(long)maxExclusive) % maxExclusive;
ulong n;
while ((n = Next64()) < threshold) ;
var n = Next64();
while (n < threshold) n = Next64();
return n % maxExclusive;
}
public override string ToString() => ToSeedString(State, Stream);
public string ToString(ulong state) => ToSeedString(state, Stream);
public static PCG Parse(string seed)
{
var (state, stream) = ParseSeedString(seed);
var state = ParseSeedString(seed, out var stream);
return new PCG((stream << 1) | 1UL, state);
}

static readonly char[] Chars64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-".ToCharArray();
internal static string ToSeedString(ulong i0, uint i1)
internal static string ToSeedString(ulong state, uint stream)
{
var chars = new char[i1 < 64 ? 12
: i1 < 64 * 64 ? 13
: i1 < 64 * 64 * 64 ? 14
: i1 < 64 * 64 * 64 * 64 ? 15
: i1 < 64 * 64 * 64 * 64 * 64 ? 16
: 17];
chars[0] = Chars64[(int)((i0 >> 60) & 63)];
chars[1] = Chars64[(int)((i0 >> 54) & 63)];
chars[2] = Chars64[(int)((i0 >> 48) & 63)];
chars[3] = Chars64[(int)((i0 >> 42) & 63)];
chars[4] = Chars64[(int)((i0 >> 36) & 63)];
chars[5] = Chars64[(int)((i0 >> 30) & 63)];
chars[6] = Chars64[(int)((i0 >> 24) & 63)];
chars[7] = Chars64[(int)((i0 >> 18) & 63)];
chars[8] = Chars64[(int)((i0 >> 12) & 63)];
chars[9] = Chars64[(int)((i0 >> 6) & 63)];
chars[10] = Chars64[(int)(i0 & 63)];
chars[11] = Chars64[i1 & 63];
if (chars.Length > 12)
{
chars[12] = Chars64[(i1 >> 6) & 63];
if (chars.Length > 13)
return string.Create(
stream < 64 ? 12
: stream < 64 * 64 ? 13
: stream < 64 * 64 * 64 ? 14
: stream < 64 * 64 * 64 * 64 ? 15
: stream < 64 * 64 * 64 * 64 * 64 ? 16
: 17,
(state, stream, Chars64),
(chars, value) =>
{
chars[13] = Chars64[(i1 >> 12) & 63];
if (chars.Length > 14)
{
chars[14] = Chars64[(i1 >> 18) & 63];
if (chars.Length > 15)
{
chars[15] = Chars64[(i1 >> 24) & 63];
if (chars.Length > 16)
{
chars[16] = Chars64[(i1 >> 30) & 63];
}
}
}
}
}
return new string(chars);
var (state, stream, Chars64) = value;
chars[0] = Chars64[(int)((state >> 60) & 63)];
chars[1] = Chars64[(int)((state >> 54) & 63)];
chars[2] = Chars64[(int)((state >> 48) & 63)];
chars[3] = Chars64[(int)((state >> 42) & 63)];
chars[4] = Chars64[(int)((state >> 36) & 63)];
chars[5] = Chars64[(int)((state >> 30) & 63)];
chars[6] = Chars64[(int)((state >> 24) & 63)];
chars[7] = Chars64[(int)((state >> 18) & 63)];
chars[8] = Chars64[(int)((state >> 12) & 63)];
chars[9] = Chars64[(int)((state >> 6) & 63)];
chars[10] = Chars64[(int)(state & 63)];
chars[11] = Chars64[(int)(stream & 63)];
if (chars.Length < 13) return;
chars[12] = Chars64[(int)((stream >> 6) & 63)];
if (chars.Length < 14) return;
chars[13] = Chars64[(int)((stream >> 12) & 63)];
if (chars.Length < 15) return;
chars[14] = Chars64[(int)((stream >> 18) & 63)];
if (chars.Length < 16) return;
chars[15] = Chars64[(int)((stream >> 24) & 63)];
if (chars.Length < 17) return;
chars[16] = Chars64[(int)((stream >> 30) & 63)];
});
}
static int Index(char c)
{
int i = Array.IndexOf(Chars64, c);
if (i == -1) throw new Exception("Invalid seed");
return i;
return i != -1 ? i : throw new Exception("Invalid seed");
}
internal static (ulong, uint) ParseSeedString(string seed)
internal static ulong ParseSeedString(string seed, out uint stream)
{
var i = seed.Length == 12 ? Index(seed[11])
: seed.Length == 13 ? Index(seed[11]) + (Index(seed[12]) << 6)
: seed.Length == 14 ? Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12)
: seed.Length == 15 ? Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12) + (Index(seed[14]) << 18)
: seed.Length == 16 ? Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12) + (Index(seed[14]) << 18) + (Index(seed[15]) << 24)
: Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12) + (Index(seed[14]) << 18) + (Index(seed[15]) << 24) + (Index(seed[16]) << 30);
return ((((((((((((((((((((
stream = (uint)(seed.Length == 12 ? Index(seed[11])
: seed.Length == 13 ? Index(seed[11]) + (Index(seed[12]) << 6)
: seed.Length == 14 ? Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12)
: seed.Length == 15 ? Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12) + (Index(seed[14]) << 18)
: seed.Length == 16 ? Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12) + (Index(seed[14]) << 18) + (Index(seed[15]) << 24)
: Index(seed[11]) + (Index(seed[12]) << 6) + (Index(seed[13]) << 12) + (Index(seed[14]) << 18) + (Index(seed[15]) << 24) + (Index(seed[16]) << 30));
return (((((((((((((((((((
(ulong)Index(seed[0]) << 6)
+ (ulong)Index(seed[1])) << 6)
+ (ulong)Index(seed[2])) << 6)
Expand All @@ -142,6 +135,6 @@ internal static (ulong, uint) ParseSeedString(string seed)
+ (ulong)Index(seed[7])) << 6)
+ (ulong)Index(seed[8])) << 6)
+ (ulong)Index(seed[9])) << 6)
+ (ulong)Index(seed[10]), (uint)i);
+ (ulong)Index(seed[10]);
}
}
7 changes: 4 additions & 3 deletions Tests/PCGTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,11 @@ public void PCG_Next64_ULong()
public void SeedString_RoundTrip()
{
Gen.Select(Gen.ULong, Gen.UInt)
.Sample((i0, i1) =>
.Sample((state, stream) =>
{
var seed = PCG.ToSeedString(i0, i1);
Assert.Equal((i0, i1), PCG.ParseSeedString(seed));
var seed = PCG.ToSeedString(state, stream);
var state2 = PCG.ParseSeedString(seed, out var stream2);
Assert.Equal((state, stream), (state2, stream2));
});
}

Expand Down

0 comments on commit f588bc8

Please sign in to comment.