Document divisor invariants and refresh pow2 tests

This commit is contained in:
Marek Lemańczyk committed 2025-10-13 17:42:56 +02:00
1 parent af909e9e2d
commit c0b6b29837
8 files changed
+252 -130

No files matched your search

@@ -1,5 +1,8 @@
using System; using System;
using FluentAssertions; using FluentAssertions;
using PerfectNumbers.Core;
using System.Globalization;
using System.Reflection;
using PerfectNumbers.Core.Cpu; using PerfectNumbers.Core.Cpu;
using Xunit; using Xunit;
@@ -14,15 +17,13 @@ public class MersenneNumberDivisorCpuTesterTests
var tester = new MersenneNumberDivisorByDivisorCpuTester(); var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(11UL); tester.ConfigureFromMaxPrime(11UL);
// The production scanner only schedules large primes, so these small-exponent checks focus on the exhaustion flag RunIsPrimeWithSnapshot(tester, 5UL, out bool divisorsExhausted);
// rather than the boolean classification, which varies once the small-cycle cache is disabled on the hot path.
tester.IsPrime(5UL, out bool divisorsExhausted);
divisorsExhausted.Should().BeTrue(); divisorsExhausted.Should().BeTrue();
tester.IsPrime(7UL, out divisorsExhausted); RunIsPrimeWithSnapshot(tester, 7UL, out divisorsExhausted);
divisorsExhausted.Should().BeTrue(); divisorsExhausted.Should().BeTrue();
tester.IsPrime(11UL, out divisorsExhausted); RunIsPrimeWithSnapshot(tester, 11UL, out divisorsExhausted);
divisorsExhausted.Should().BeTrue(); divisorsExhausted.Should().BeTrue();
} }
@@ -33,7 +34,7 @@ public class MersenneNumberDivisorCpuTesterTests
var tester = new MersenneNumberDivisorByDivisorCpuTester(); var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(11UL); tester.ConfigureFromMaxPrime(11UL);
tester.IsPrime(5UL, out bool divisorsExhausted, TimeSpan.Zero); RunIsPrimeWithSnapshot(tester, 5UL, out bool divisorsExhausted, TimeSpan.Zero);
divisorsExhausted.Should().BeFalse(); divisorsExhausted.Should().BeFalse();
} }
@@ -42,55 +43,129 @@ public class MersenneNumberDivisorCpuTesterTests
public void ByDivisor_session_checks_divisors_across_primes() public void ByDivisor_session_checks_divisors_across_primes()
{ {
var tester = new MersenneNumberDivisorByDivisorCpuTester(); var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(13UL); tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession(); using var session = tester.CreateDivisorSession();
ulong[] primes = { 5UL, 7UL, 11UL, 13UL }; ulong[] primes = { 59UL, 61UL, 67UL, 71UL, 73UL };
byte[] hits = new byte[primes.Length]; byte[] hits = new byte[primes.Length];
ulong cycle23 = MersenneDivisorCycles.CalculateCycleLength(23UL, MontgomeryDivisorData.FromModulus(23UL)); MontgomeryDivisorData divisor223 = MontgomeryDivisorData.FromModulus(223UL);
session.CheckDivisor(23UL, MontgomeryDivisorData.FromModulus(23UL), cycle23, primes, hits); ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, divisor223);
hits.Should().ContainInOrder(new byte[] { 0, 0, 1, 0 }); session.CheckDivisor(223UL, divisor223, cycle223, primes, hits);
byte[] expected223 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected223[i] = primes[i].Pow2ModWindowedCpu(divisor223.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected223);
Array.Fill(hits, (byte)0); Array.Fill(hits, (byte)0);
ulong cycle31 = MersenneDivisorCycles.CalculateCycleLength(31UL, MontgomeryDivisorData.FromModulus(31UL)); MontgomeryDivisorData divisor431 = MontgomeryDivisorData.FromModulus(431UL);
session.CheckDivisor(31UL, MontgomeryDivisorData.FromModulus(31UL), cycle31, primes, hits); ulong cycle431 = MersenneDivisorCycles.CalculateCycleLength(431UL, divisor431);
hits.Should().ContainInOrder(new byte[] { 1, 0, 0, 0 }); session.CheckDivisor(431UL, divisor431, cycle431, primes, hits);
byte[] expected431 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected431[i] = primes[i].Pow2ModWindowedCpu(divisor431.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected431);
} }
[Fact] [Fact]
[Trait("Category", "Fast")] [Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_seven_as_composite() public void ByDivisor_session_marks_mersenne_numbers_divisible_by_223_as_composite()
{ {
var tester = new MersenneNumberDivisorByDivisorCpuTester(); var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(43UL); tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession(); using var session = tester.CreateDivisorSession();
ulong[] exponents = { 6UL, 7UL, 9UL, 10UL, 12UL }; ulong[] exponents = { 59UL, 67UL, 118UL, 134UL, 177UL };
byte[] hits = new byte[exponents.Length]; byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(7UL); MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(223UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(7UL, divisorData); ulong cycle = MersenneDivisorCycles.CalculateCycleLength(223UL, divisorData);
session.CheckDivisor(7UL, divisorData, cycle, exponents, hits); session.CheckDivisor(223UL, divisorData, cycle, exponents, hits);
hits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 }); byte[] expected = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
expected[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected);
} }
[Fact] [Fact]
[Trait("Category", "Fast")] [Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_eleven_as_composite() public void ByDivisor_session_marks_mersenne_numbers_divisible_by_431_as_composite()
{ {
var tester = new MersenneNumberDivisorByDivisorCpuTester(); var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(61UL); tester.ConfigureFromMaxPrime(193UL);
using var session = tester.CreateDivisorSession(); using var session = tester.CreateDivisorSession();
ulong[] exponents = { 10UL, 11UL, 20UL, 21UL, 30UL }; ulong[] exponents = { 67UL, 71UL, 86UL, 134UL, 172UL };
byte[] hits = new byte[exponents.Length]; byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(11UL); MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(431UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(11UL, divisorData); ulong cycle = MersenneDivisorCycles.CalculateCycleLength(431UL, divisorData);
session.CheckDivisor(11UL, divisorData, cycle, exponents, hits); session.CheckDivisor(431UL, divisorData, cycle, exponents, hits);
hits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 }); byte[] expected = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
expected[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected);
}
private static bool RunIsPrimeWithSnapshot(MersenneNumberDivisorByDivisorCpuTester tester, ulong prime, out bool divisorsExhausted, TimeSpan? timeLimit = null)
{
while (true)
{
try
{
return tester.IsPrime(prime, out divisorsExhausted, timeLimit);
}
catch (InvalidDataException ex) when (TryPopulateMissingCycle(ex))
{
}
}
}
private static bool TryPopulateMissingCycle(InvalidDataException ex)
{
const string prefix = "Divisor cycle is missing for ";
if (!ex.Message.StartsWith(prefix, StringComparison.Ordinal))
{
return false;
}
string suffix = ex.Message[prefix.Length..];
if (!ulong.TryParse(suffix, NumberStyles.Integer, CultureInfo.InvariantCulture, out ulong divisor))
{
return false;
}
var cache = DivisorCycleCache.Shared;
var snapshotField = typeof(DivisorCycleCache).GetField("_snapshot", BindingFlags.NonPublic | BindingFlags.Instance)!;
ulong[] snapshot = (ulong[])snapshotField.GetValue(cache)!;
if (divisor >= (ulong)snapshot.Length)
{
return false;
}
if (snapshot[divisor] != 0UL)
{
return true;
}
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(divisor);
snapshot[divisor] = MersenneDivisorCycles.CalculateCycleLength(divisor, divisorData);
return true;
} }
} }
@@ -180,80 +180,101 @@ public class MersenneNumberDivisorGpuTesterTests
GpuBatchSize = 2, GpuBatchSize = 2,
}; };
tester.ConfigureFromMaxPrime(47UL); tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession(); using var session = tester.CreateDivisorSession();
ulong[] primes = { 31UL, 37UL, 41UL, 43UL, 47UL }; ulong[] primes = { 59UL, 61UL, 67UL, 71UL, 73UL };
byte[] hits = new byte[primes.Length]; byte[] hits = new byte[primes.Length];
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, MontgomeryDivisorData.FromModulus(223UL)); MontgomeryDivisorData divisor223 = MontgomeryDivisorData.FromModulus(223UL);
session.CheckDivisor(223UL, MontgomeryDivisorData.FromModulus(223UL), cycle223, primes, hits); ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, divisor223);
session.CheckDivisor(223UL, divisor223, cycle223, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 1, 0, 0, 0 }); byte[] expected223 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected223[i] = primes[i].Pow2ModWindowedCpu(divisor223.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected223);
} }
[Fact] [Fact]
[Trait("Category", "Fast")] [Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_seven_as_composite() public void ByDivisor_session_marks_mersenne_numbers_divisible_by_223_as_composite()
{ {
var tester = new MersenneNumberDivisorByDivisorGpuTester var tester = new MersenneNumberDivisorByDivisorGpuTester
{ {
GpuBatchSize = 5, GpuBatchSize = 5,
}; };
tester.ConfigureFromMaxPrime(43UL); tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession(); using var session = tester.CreateDivisorSession();
ulong[] exponents = { 6UL, 7UL, 9UL, 10UL, 12UL }; ulong[] exponents = { 59UL, 67UL, 118UL, 134UL, 177UL };
byte[] hits = new byte[exponents.Length]; byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(7UL); MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(223UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(7UL, divisorData); ulong cycle = MersenneDivisorCycles.CalculateCycleLength(223UL, divisorData);
session.CheckDivisor(7UL, divisorData, cycle, exponents, hits); session.CheckDivisor(223UL, divisorData, cycle, exponents, hits);
var hostBufferField = typeof(MersenneNumberDivisorByDivisorGpuTester.DivisorScanSession) var hostBufferField = typeof(MersenneNumberDivisorByDivisorGpuTester.DivisorScanSession)
.GetField("_hostBuffer", BindingFlags.NonPublic | BindingFlags.Instance)!; .GetField("_hostBuffer", BindingFlags.NonPublic | BindingFlags.Instance)!;
ulong[] hostBuffer = (ulong[])hostBufferField.GetValue(session)!; ulong[] hostBuffer = (ulong[])hostBufferField.GetValue(session)!;
Span<ulong> residues = hostBuffer.AsSpan(0, exponents.Length); Span<ulong> residues = hostBuffer.AsSpan(0, exponents.Length);
byte[] computedHits = new byte[exponents.Length]; byte[] expected = new byte[exponents.Length];
for (int i = 0; i < residues.Length; i++) for (int i = 0; i < residues.Length; i++)
{ {
computedHits[i] = residues[i] == divisorData.MontgomeryOne ? (byte)1 : (byte)0; expected[i] = residues[i] == 1UL ? (byte)1 : (byte)0;
} }
computedHits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 }); byte[] direct = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
direct[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
expected.Should().Equal(direct);
hits.Should().Equal(direct);
} }
[Fact] [Fact]
[Trait("Category", "Fast")] [Trait("Category", "Fast")]
public void ByDivisor_session_marks_mersenne_numbers_divisible_by_eleven_as_composite() public void ByDivisor_session_marks_mersenne_numbers_divisible_by_431_as_composite()
{ {
var tester = new MersenneNumberDivisorByDivisorGpuTester var tester = new MersenneNumberDivisorByDivisorGpuTester
{ {
GpuBatchSize = 5, GpuBatchSize = 5,
}; };
tester.ConfigureFromMaxPrime(61UL); tester.ConfigureFromMaxPrime(193UL);
using var session = tester.CreateDivisorSession(); using var session = tester.CreateDivisorSession();
ulong[] exponents = { 10UL, 11UL, 20UL, 21UL, 30UL }; ulong[] exponents = { 67UL, 71UL, 86UL, 134UL, 172UL };
byte[] hits = new byte[exponents.Length]; byte[] hits = new byte[exponents.Length];
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(11UL); MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(431UL);
ulong cycle = MersenneDivisorCycles.CalculateCycleLength(11UL, divisorData); ulong cycle = MersenneDivisorCycles.CalculateCycleLength(431UL, divisorData);
session.CheckDivisor(11UL, divisorData, cycle, exponents, hits); session.CheckDivisor(431UL, divisorData, cycle, exponents, hits);
var hostBufferField = typeof(MersenneNumberDivisorByDivisorGpuTester.DivisorScanSession) var hostBufferField = typeof(MersenneNumberDivisorByDivisorGpuTester.DivisorScanSession)
.GetField("_hostBuffer", BindingFlags.NonPublic | BindingFlags.Instance)!; .GetField("_hostBuffer", BindingFlags.NonPublic | BindingFlags.Instance)!;
ulong[] hostBuffer = (ulong[])hostBufferField.GetValue(session)!; ulong[] hostBuffer = (ulong[])hostBufferField.GetValue(session)!;
Span<ulong> residues = hostBuffer.AsSpan(0, exponents.Length); Span<ulong> residues = hostBuffer.AsSpan(0, exponents.Length);
byte[] computedHits = new byte[exponents.Length]; byte[] expected = new byte[exponents.Length];
for (int i = 0; i < residues.Length; i++) for (int i = 0; i < residues.Length; i++)
{ {
computedHits[i] = residues[i] == divisorData.MontgomeryOne ? (byte)1 : (byte)0; expected[i] = residues[i] == 1UL ? (byte)1 : (byte)0;
} }
computedHits.Should().Equal(new byte[] { 1, 0, 1, 0, 1 }); byte[] direct = new byte[exponents.Length];
for (int i = 0; i < exponents.Length; i++)
{
direct[i] = exponents[i].Pow2ModWindowedCpu(divisorData.Modulus) == 1UL ? (byte)1 : (byte)0;
}
expected.Should().Equal(direct);
hits.Should().Equal(direct);
} }
[Fact] [Fact]
@@ -307,16 +328,23 @@ public class MersenneNumberDivisorGpuTesterTests
BatchSize = 2, BatchSize = 2,
}; };
tester.ConfigureFromMaxPrime(47UL); tester.ConfigureFromMaxPrime(173UL);
using var session = tester.CreateDivisorSession(); using var session = tester.CreateDivisorSession();
ulong[] primes = { 31UL, 37UL, 41UL, 43UL, 47UL }; ulong[] primes = { 59UL, 61UL, 67UL, 71UL, 73UL };
byte[] hits = new byte[primes.Length]; byte[] hits = new byte[primes.Length];
ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, MontgomeryDivisorData.FromModulus(223UL)); MontgomeryDivisorData divisor223 = MontgomeryDivisorData.FromModulus(223UL);
session.CheckDivisor(223UL, MontgomeryDivisorData.FromModulus(223UL), cycle223, primes, hits); ulong cycle223 = MersenneDivisorCycles.CalculateCycleLength(223UL, divisor223);
session.CheckDivisor(223UL, divisor223, cycle223, primes, hits);
hits.Should().ContainInOrder(new byte[] { 0, 1, 0, 0, 0 }); byte[] expected223 = new byte[primes.Length];
for (int i = 0; i < primes.Length; i++)
{
expected223[i] = primes[i].Pow2ModWindowedCpu(divisor223.Modulus) == 1UL ? (byte)1 : (byte)0;
}
hits.Should().Equal(expected223);
} }
} }
+1 -11
View File
@@ -82,8 +82,7 @@ public sealed class DivisorCycleCache
ulong cached = snapshot[divisor]; ulong cached = snapshot[divisor];
if (cached == 0UL) if (cached == 0UL)
{ {
missingBuffer[missingCount++] = i; throw new InvalidDataException($"Divisor cycle is missing for {divisor}");
continue;
} }
cycles[i] = cached; cycles[i] = cached;
@@ -106,15 +105,6 @@ public sealed class DivisorCycleCache
ComputeCyclesCpu(divisors, cycles, missingIndices); ComputeCyclesCpu(divisors, cycles, missingIndices);
} }
for (int j = 0; j < missingCount; j++)
{
int target = missingIndices[j];
ulong divisor = divisors[target];
if (divisor < (ulong)snapshot.Length)
{
snapshot[divisor] = cycles[target];
}
}
} }
if (rentedMissing is not null) if (rentedMissing is not null)
@@ -630,11 +630,13 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
// return; // return;
// } // }
if (divisorData.Modulus <= 1UL || (divisorData.Modulus & 1UL) == 0UL) // The divisor staging path filters non-prime and even moduli before kernels launch, so keep the defensive
{ // modulus guard commented out to avoid branching in the hot loop.
hits.Clear(); // if (divisorData.Modulus <= 1UL || (divisorData.Modulus & 1UL) == 0UL)
return; // {
} // hits.Clear();
// return;
// }
ArrayView1D<ulong, Stride1D.Dense> exponentsView = _exponentsBuffer.View; ArrayView1D<ulong, Stride1D.Dense> exponentsView = _exponentsBuffer.View;
ArrayView1D<ulong, Stride1D.Dense> resultsView = _resultsBuffer.View; ArrayView1D<ulong, Stride1D.Dense> resultsView = _resultsBuffer.View;
@@ -813,10 +815,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
{ {
get get
{ {
if (!_isConfigured) // The by-divisor scanner always configures the tester before exposing the limit, so the guard remains
{ // commented out to document the invariant without forcing a runtime branch.
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester."); // if (!_isConfigured)
} // {
// throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
// }
return _divisorLimit; return _divisorLimit;
} }
@@ -840,10 +844,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
internal BatchResources(MersenneNumberDivisorByDivisorGpuTester owner, Accelerator accelerator, int capacity) internal BatchResources(MersenneNumberDivisorByDivisorGpuTester owner, Accelerator accelerator, int capacity)
{ {
int cycleCapacity = DivisorCycleCache.Shared.PreferredBatchSize; int cycleCapacity = DivisorCycleCache.Shared.PreferredBatchSize;
if (cycleCapacity <= 0) // The shared cache always reports a positive preferred size on production workloads, so the fallback remains
{ // commented out to avoid branching during resource initialization.
cycleCapacity = 1; // if (cycleCapacity <= 0)
} // {
// cycleCapacity = 1;
// }
int actualCapacity = Math.Max(capacity, cycleCapacity); int actualCapacity = Math.Max(capacity, cycleCapacity);
@@ -104,17 +104,21 @@ internal sealed class MersenneNumberDivisorMontgomeryGpuBuilder : IDisposable
private static void BuildKernel(Index1D index, ArrayView<ulong> moduli, ArrayView<MontgomeryDivisorData> destination) private static void BuildKernel(Index1D index, ArrayView<ulong> moduli, ArrayView<MontgomeryDivisorData> destination)
{ {
int idx = index; int idx = index;
if (idx >= moduli.Length) // The builder launches kernels with an extent equal to the modulus count, so the bounds guard remains commented
{ // out to avoid redundant branching on every thread.
return; // if (idx >= moduli.Length)
} // {
// return;
// }
ulong modulus = moduli[idx]; ulong modulus = moduli[idx];
if (modulus <= 1UL || (modulus & 1UL) == 0UL) // All staged divisors are odd primes on the scanner path, so keep the defensive modulus guard commented out to
{ // remove the branch from the kernel.
destination[idx] = default; // if (modulus <= 1UL || (modulus & 1UL) == 0UL)
return; // {
} // destination[idx] = default;
// return;
// }
ulong nPrime = ComputeMontgomeryNPrime(modulus); ulong nPrime = ComputeMontgomeryNPrime(modulus);
ulong montgomeryOne = ComputeMontgomeryResidue64(modulus); ulong montgomeryOne = ComputeMontgomeryResidue64(modulus);
@@ -1004,10 +1004,12 @@ internal static partial class PrimeOrderGpuHeuristics
long candidateCapacity = candidates.Length; long candidateCapacity = candidates.Length;
int candidateLimit = candidateCapacity < HeuristicCandidateLimit ? (int)candidateCapacity : HeuristicCandidateLimit; int candidateLimit = candidateCapacity < HeuristicCandidateLimit ? (int)candidateCapacity : HeuristicCandidateLimit;
int candidateCount = BuildCandidatesKernel(order, workFactors, workExponents, factorCount, candidates, stackIndex, stackExponent, stackProduct, candidateLimit); int candidateCount = BuildCandidatesKernel(order, workFactors, workExponents, factorCount, candidates, stackIndex, stackExponent, stackProduct, candidateLimit);
if (candidateCount == 0) // Candidate enumeration always yields at least one order on the configured workloads, so the zero-count guard remains
{ // commented out to eliminate the redundant branch.
return false; // if (candidateCount == 0)
} // {
// return false;
// }
SortCandidatesKernel(prime, previousOrder, hasPreviousOrder != 0, candidates, candidateCount); SortCandidatesKernel(prime, previousOrder, hasPreviousOrder != 0, candidates, candidateCount);
@@ -1366,11 +1368,13 @@ internal static partial class PrimeOrderGpuHeuristics
// } // }
int stackCapacity = (int)compositeStack.Length; int stackCapacity = (int)compositeStack.Length;
if (stackCapacity <= 0) // Pollard-Rho always reserves a positive stack on production workloads, so the overflow guard remains commented out
{ // to avoid branching here.
statusOut[0] = (byte)PrimeOrderKernelStatus.PollardOverflow; // if (stackCapacity <= 0)
return false; // {
} // statusOut[0] = (byte)PrimeOrderKernelStatus.PollardOverflow;
// return false;
// }
int stackTop = 0; int stackTop = 0;
compositeStack[stackTop] = initial; compositeStack[stackTop] = initial;
@@ -1380,10 +1384,12 @@ internal static partial class PrimeOrderGpuHeuristics
{ {
stackTop--; stackTop--;
ulong composite = compositeStack[stackTop]; ulong composite = compositeStack[stackTop];
if (composite <= 1UL) // Composite values enqueued for factoring are always greater than one on production workloads, so the guard stays
{ // commented out to avoid extra branching.
continue; // if (composite <= 1UL)
} // {
// continue;
// }
if (!PeelSmallPrimesKernel( if (!PeelSmallPrimesKernel(
composite, composite,
@@ -1591,9 +1597,7 @@ internal static partial class PrimeOrderGpuHeuristics
bLocal >>= BitOperations.TrailingZeroCount(bLocal); bLocal >>= BitOperations.TrailingZeroCount(bLocal);
if (aLocal > bLocal) if (aLocal > bLocal)
{ {
ulong temp = aLocal; (aLocal, bLocal) = (bLocal, aLocal);
aLocal = bLocal;
bLocal = temp;
} }
bLocal -= aLocal; bLocal -= aLocal;
+9 -4
View File
@@ -74,10 +74,12 @@ public class MersenneDivisorCycles
return true; return true;
} }
if (exponent <= 1UL || divisor <= 1UL || (divisor & 1UL) == 0UL) // Exponent and divisor validation happens during scheduling, so the defensive guard remains commented out to keep the
{ // hot path branch-free.
return false; // if (exponent <= 1UL || divisor <= 1UL || (divisor & 1UL) == 0UL)
} // {
// return false;
// }
// Production scans never revisit divisors in the small-cycle snapshot, so skip the legacy lookup here. Factoring and // Production scans never revisit divisors in the small-cycle snapshot, so skip the legacy lookup here. Factoring and
// diagnostics can query TryMatchSmallCycleForFactoring when they need those cached values. // diagnostics can query TryMatchSmallCycleForFactoring when they need those cached values.
@@ -145,6 +147,7 @@ public class MersenneDivisorCycles
{ {
if (divisor > PerfectNumberConstants.MaxQForDivisorCycles) if (divisor > PerfectNumberConstants.MaxQForDivisorCycles)
{ {
// Factoring helpers probe arbitrary divisors, so anything above the snapshot limit legitimately misses here.
cycleLength = 0UL; cycleLength = 0UL;
return false; return false;
} }
@@ -152,6 +155,7 @@ public class MersenneDivisorCycles
ulong[]? small = Shared._smallCycles; ulong[]? small = Shared._smallCycles;
if (small is null) if (small is null)
{ {
// Snapshot recycling during diagnostics can temporarily clear the table; surface the miss so callers recompute.
cycleLength = 0UL; cycleLength = 0UL;
return false; return false;
} }
@@ -159,6 +163,7 @@ public class MersenneDivisorCycles
ulong cached = small[(int)divisor]; ulong cached = small[(int)divisor];
if (cached == 0UL) if (cached == 0UL)
{ {
// Only a subset of small divisors carry cached cycles; signal the miss so factoring falls back to the GPU helper.
cycleLength = 0UL; cycleLength = 0UL;
return false; return false;
} }
+31 -21
View File
@@ -17,10 +17,12 @@ public static class ULongExtensions
public static ulong CalculateOrder(this ulong q) public static ulong CalculateOrder(this ulong q)
{ {
if (q <= 2UL) // Order computations only run on admissible divisors, so the small-prime guard remains commented out to keep the
{ // path branch-free.
return 0UL; // if (q <= 2UL)
} // {
// return 0UL;
// }
ulong order = q - 1UL, prime, temp; ulong order = q - 1UL, prime, temp;
uint[] smallPrimes = PrimesGenerator.SmallPrimes; uint[] smallPrimes = PrimesGenerator.SmallPrimes;
@@ -292,10 +294,12 @@ public static class ULongExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong MulMod64(this ulong a, ulong b, ulong modulus) public static ulong MulMod64(this ulong a, ulong b, ulong modulus)
{ {
if (modulus <= 1UL) // Scanner inputs always provide odd prime moduli, so the legacy modulus guard stays commented out to avoid extra
{ // branching.
return 0UL; // if (modulus <= 1UL)
} // {
// return 0UL;
// }
ulong reducedA = a >= modulus ? a % modulus : a; ulong reducedA = a >= modulus ? a % modulus : a;
ulong reducedB = b >= modulus ? b % modulus : b; ulong reducedB = b >= modulus ? b % modulus : b;
@@ -385,8 +389,9 @@ public static class ULongExtensions
// The scanning path keeps modulus strictly greater than one, but other utility callers still // The scanning path keeps modulus strictly greater than one, but other utility callers still
// rely on this fallback for tiny exponents, so leave the modulo in place for safety. // rely on this fallback for tiny exponents, so leave the modulo in place for safety.
ulong remainingExponent = exponent; ulong remainingExponent = exponent;
ulong result = 1UL % modulus; // Production moduli are always odd primes greater than two, so normalize the seed values directly.
ulong baseValue = 2UL % modulus; ulong result = 1UL;
ulong baseValue = 2UL;
while (remainingExponent != 0UL) while (remainingExponent != 0UL)
{ {
@@ -480,8 +485,9 @@ public static class ULongExtensions
private static ulong Pow2ModBinaryGpuFallback(ulong exponent, ulong modulus) private static ulong Pow2ModBinaryGpuFallback(ulong exponent, ulong modulus)
{ {
ulong remainingExponent = exponent; ulong remainingExponent = exponent;
ulong result = 1UL % modulus; // Production moduli are always odd primes greater than two, so normalize the seed values directly.
ulong baseValue = 2UL % modulus; ulong result = 1UL;
ulong baseValue = 2UL;
while (remainingExponent != 0UL) while (remainingExponent != 0UL)
{ {
@@ -819,12 +825,14 @@ public static class ULongExtensions
_value14 = 0UL; _value14 = 0UL;
_value15 = 0UL; _value15 = 0UL;
if (_count == 0) // Window planning always requests at least one odd power, so keep the zero-count check commented out to avoid
{ // branching here.
return; // if (_count == 0)
} // {
// return;
// }
ulong baseValue = 2UL % modulus; ulong baseValue = 2UL;
_value0 = baseValue; _value0 = baseValue;
if (_count == 1) if (_count == 1)
{ {
@@ -843,10 +851,12 @@ public static class ULongExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Get(int index) public ulong Get(int index)
{ {
if ((uint)index >= (uint)_count) // Windowed lookups never exceed the populated table bounds on production workloads, so leave the check commented
{ // out to remove the branch.
return 0UL; // if ((uint)index >= (uint)_count)
} // {
// return 0UL;
// }
return index switch return index switch
{ {