From ed6499a2c74ff1e5767352f3eaa711d2675b54ce Mon Sep 17 00:00:00 2001 From: Anthony Lloyd Date: Sun, 13 Oct 2024 09:24:13 +0100 Subject: [PATCH] current PCG with saved threshold --- Tests/PCGTests.cs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Tests/PCGTests.cs b/Tests/PCGTests.cs index 3020852..38b5b74 100644 --- a/Tests/PCGTests.cs +++ b/Tests/PCGTests.cs @@ -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), @@ -226,7 +226,7 @@ static void Ignore(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)), @@ -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)), @@ -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; @@ -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);