Skip to content

Commit

Permalink
current PCG with saved threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyLloyd committed Oct 13, 2024
1 parent 00538a7 commit ed6499a
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Tests/PCGTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public void Double_Exp_Bug()
[Fact]
public void PCG_Multiplier_Is_Not_Faster()
{
Gen.Select(Gen.UInt, Gen.ULong, Gen.UInt[1, 10_000])
Gen.Select(Gen.UInt, Gen.ULong, Gen.UInt[2, 10_000])
.Select((i, s, m) => (new PCG(i, s), new PCGTest(i, s), m, HashHelper.GetFastModMultiplier(m)))
.Faster(
(_, n, u, m) => n.Next(u, m),
Expand All @@ -226,7 +226,7 @@ static void Ignore<T>(T _) { }
[Fact]
public void PCG_New_Is_Not_Faster()
{
Gen.Select(Gen.UInt, Gen.ULong, Gen.UInt[1, 10_000])
Gen.Select(Gen.UInt, Gen.ULong, Gen.UInt[2, 10_000])
.Select((i, s, m) => (new PCG(i, s), new PCGTest(i, s), m))
.Faster(
(_, n, m) => Ignore(n.Next(m)),
Expand All @@ -240,7 +240,7 @@ public void PCG_New_Is_Not_Faster()
[Fact]
public void PCG_Lemire_Is_Faster()
{
Gen.Select(Gen.UInt, Gen.ULong, Gen.UInt[1, 10_000])
Gen.Select(Gen.UInt, Gen.ULong, Gen.UInt[2, 10_000])
.Select((i, s, m) => (new PCG(i, s), new PCGTest(i, s), m, ((ulong)-m) % m))
.Faster(
(_, n, m, t) => Ignore(n.NextLemire(m, t)),
Expand All @@ -251,6 +251,20 @@ public void PCG_Lemire_Is_Faster()
);
}

[Fact]
public void PCG_Current_With_Threshold_Is_Faster()
{
Gen.Select(Gen.UInt, Gen.ULong, Gen.UInt[2, 10_000])
.Select((i, s, m) => (new PCGTest(i, s), new PCGTest(i, s), m, ((uint)-(int)m) % m))
.Faster(
(_, n, m, t) => Ignore(n.NextLemire(m, t)),
(o, _, m, t) => Ignore(o.NextCurrentWithThreshold(m, t)),
repeat: 100,
raiseexception: false,
writeLine: output.WriteLine
);
}

public sealed class PCGTest
{
static int threadCount;
Expand Down Expand Up @@ -311,6 +325,13 @@ public uint NextLemire(uint maxExclusive, ulong threshold)
while ((uint)m < threshold);
return (uint)(m >> 32);
}
public uint NextCurrentWithThreshold(uint maxExclusive, uint threshold)
{
if (maxExclusive == 1U) return 0U;
var n = Next();
while (n < threshold) n = Next();
return n % maxExclusive;
}
public ulong Next64(ulong maxExclusive)
{
if (maxExclusive <= uint.MaxValue) return Next((uint)maxExclusive);
Expand Down

0 comments on commit ed6499a

Please sign in to comment.