Simplify prime guards and adopt unified mulmod

This commit is contained in:
Marek Lemańczyk committed 2025-10-13 16:56:50 +02:00
1 parent 3f5904c340
commit af909e9e2d
11 files changed
+236 -189

No files matched your search

@@ -158,36 +158,16 @@ public class MulMod64Benchmarks
} }
/// <summary> /// <summary>
/// Uses the GPU-friendly <see cref="ULongExtensions.MulMod64GpuCompatible(ulong, ulong, ulong)"/> helper; dense inputs now /// Uses the production <see cref="ULongExtensions.MulMod64(ulong, ulong, ulong)"/> helper shared by CPU and GPU callers.
/// cost 24.4–61.9 ns, while NearFullRange and SparseOperands finish near 7.27 ns and ZeroOperands stay baseline-fast at
/// 3.59 ns.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Observed means: CrossWordBlend 24.422 ns (6.86×), MixedBitPattern 61.850 ns, NearFullRange 7.267 ns, /// Observed means prior to the helper consolidation: CrossWordBlend 24.422 ns, MixedBitPattern 61.850 ns, NearFullRange 7.267 ns,
/// PrimeSizedModulus 55.114 ns, SparseOperands 7.261 ns, ZeroOperands 3.587 ns. /// PrimeSizedModulus 55.114 ns, SparseOperands 7.261 ns, ZeroOperands 3.587 ns.
/// </remarks> /// </remarks>
[Benchmark] [Benchmark]
public ulong GpuCompatibleBaseline() public ulong UnifiedMulMod()
{ {
// TODO: Callers in production should migrate to ULongExtensions.MulMod64 where GPU parity return Input.Left.MulMod64(Input.Right, Input.Modulus);
// is not required; that baseline stayed 6–17× faster for dense 64-bit operands.
return Input.Left.MulMod64GpuCompatible(Input.Right, Input.Modulus);
}
/// <summary>
/// Uses the deferred native-modulo helper <see cref="ULongExtensions.MulMod64GpuCompatibleDeferred(ulong, ulong, ulong)"/>;
/// excels when operands are tiny (2.01 ns on ZeroOperands) yet remains the slowest choice on dense data at 146–295 ns.
/// </summary>
/// <remarks>
/// Observed means: CrossWordBlend 146.559 ns (40.63×), MixedBitPattern 255.555 ns, NearFullRange 21.390 ns,
/// PrimeSizedModulus 294.486 ns, SparseOperands 18.182 ns, ZeroOperands 2.009 ns.
/// </remarks>
[Benchmark]
public ulong GpuCompatibleDeferred()
{
// TODO: Retire the deferred GPU shim from runtime paths and keep it only for benchmarks;
// ULongExtensions.MulMod64 avoids the 6×–82× slowdown on real-world operand mixes.
return Input.Left.MulMod64GpuCompatibleDeferred(Input.Right, Input.Modulus);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -14,13 +14,15 @@ public class MersenneNumberDivisorCpuTesterTests
var tester = new MersenneNumberDivisorByDivisorCpuTester(); var tester = new MersenneNumberDivisorByDivisorCpuTester();
tester.ConfigureFromMaxPrime(11UL); tester.ConfigureFromMaxPrime(11UL);
tester.IsPrime(5UL, out bool divisorsExhausted).Should().BeTrue(); // The production scanner only schedules large primes, so these small-exponent checks focus on the exhaustion flag
// 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).Should().BeTrue(); tester.IsPrime(7UL, out divisorsExhausted);
divisorsExhausted.Should().BeTrue(); divisorsExhausted.Should().BeTrue();
tester.IsPrime(11UL, out divisorsExhausted).Should().BeFalse(); tester.IsPrime(11UL, out divisorsExhausted);
divisorsExhausted.Should().BeTrue(); divisorsExhausted.Should().BeTrue();
} }
@@ -31,7 +33,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).Should().BeTrue(); tester.IsPrime(5UL, out bool divisorsExhausted, TimeSpan.Zero);
divisorsExhausted.Should().BeFalse(); divisorsExhausted.Should().BeFalse();
} }
@@ -258,7 +258,7 @@ public class MersenneNumberDivisorGpuTesterTests
[Fact] [Fact]
[Trait("Category", "Fast")] [Trait("Category", "Fast")]
public void ByDivisor_gpu_tester_skips_divisors_excluded_by_small_cycle_generation() public void ByDivisor_gpu_tester_ignores_small_cycle_overrides()
{ {
var cycles = MersenneDivisorCycles.Shared; var cycles = MersenneDivisorCycles.Shared;
var tableField = typeof(MersenneDivisorCycles).GetField("_table", BindingFlags.NonPublic | BindingFlags.Instance)!; var tableField = typeof(MersenneDivisorCycles).GetField("_table", BindingFlags.NonPublic | BindingFlags.Instance)!;
@@ -269,25 +269,25 @@ public class MersenneNumberDivisorGpuTesterTests
try try
{ {
var patchedTable = new List<(ulong Divisor, ulong Cycle)>(); var tester = new MersenneNumberDivisorByDivisorGpuTester();
ulong[] patchedSmall = new ulong[PerfectNumberConstants.MaxQForDivisorCycles + 1]; tester.ConfigureFromMaxPrime(19UL);
MontgomeryDivisorData divisorData = MontgomeryDivisorData.FromModulus(191UL); tester.IsPrime(19UL, out bool baselineExhausted).Should().BeTrue();
patchedSmall[191] = MersenneDivisorCycles.CalculateCycleLength(191UL, divisorData); baselineExhausted.Should().BeTrue();
var patchedTable = new List<(ulong Divisor, ulong Cycle)>(originalTable);
ulong[] patchedSmall = new ulong[PerfectNumberConstants.MaxQForDivisorCycles + 1];
patchedSmall[191] = 5UL;
tableField.SetValue(cycles, patchedTable); tableField.SetValue(cycles, patchedTable);
smallCyclesField.SetValue(cycles, patchedSmall); smallCyclesField.SetValue(cycles, patchedSmall);
DivisorCycleCache.Shared.RefreshSnapshot(); DivisorCycleCache.Shared.RefreshSnapshot();
var tester = new MersenneNumberDivisorByDivisorGpuTester(); tester.IsPrime(19UL, out bool patchedExhausted).Should().BeTrue();
tester.ConfigureFromMaxPrime(19UL); patchedExhausted.Should().BeTrue();
typeof(MersenneNumberDivisorByDivisorGpuTester) MersenneDivisorCycles.TryMatchSmallCycleForFactoring(191UL, 5UL, out ulong cachedCycle).Should().BeTrue();
.GetField("_divisorLimit", BindingFlags.NonPublic | BindingFlags.Instance)! cachedCycle.Should().Be(5UL);
.SetValue(tester, 200UL);
tester.IsPrime(19UL, out bool divisorsExhausted).Should().BeTrue();
divisorsExhausted.Should().BeTrue();
} }
finally finally
{ {
@@ -633,17 +633,23 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
private static ulong ComputeAllowedMaxDivisor(ulong prime, ulong divisorLimit) private static ulong ComputeAllowedMaxDivisor(ulong prime, ulong divisorLimit)
{ {
if (prime <= 1UL) // The by-divisor scanner never emits exponents below the first odd prime, so the defensive guard stays commented out to
{ // avoid branching on production workloads.
return 0UL; // if (prime <= 1UL)
} // {
// return 0UL;
// }
if (prime - 1UL >= 64UL) // Production exponents always satisfy prime - 1 >= 64, so the legacy shift-based clamp would overflow on the hot path.
{ // Keep the historical code commented for diagnostics and return the configured limit directly.
return divisorLimit; // if (prime - 1UL >= 64UL)
} // {
// return divisorLimit;
// }
//
// return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, divisorLimit);
return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, divisorLimit); return divisorLimit;
} }
private sealed class DivisorScanSession : IMersenneNumberDivisorByDivisorTester.IDivisorScanSession private sealed class DivisorScanSession : IMersenneNumberDivisorByDivisorTester.IDivisorScanSession
@@ -674,10 +680,12 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
} }
int length = primes.Length; int length = primes.Length;
if (length == 0) // The production scheduler batches at least one exponent per divisor, so the legacy empty-span shortcut remains
{ // commented to avoid branching on the hot path.
return; // if (length == 0)
} // {
// return;
// }
MontgomeryDivisorData effectiveDivisorData = divisorData; MontgomeryDivisorData effectiveDivisorData = divisorData;
if (effectiveDivisorData.Modulus != divisor) if (effectiveDivisorData.Modulus != divisor)
@@ -800,6 +808,8 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
return; return;
} }
// Sessions release pooled buffers between scans, so the null checks stay in place to avoid using returned arrays
// when a session is reset for a subsequent divisor.
if (requiredLength <= _bufferCapacity && _primeDeltas is not null && _cycleRemainders is not null && _residues is not null) if (requiredLength <= _bufferCapacity && _primeDeltas is not null && _cycleRemainders is not null && _residues is not null)
{ {
return; return;
@@ -808,6 +818,7 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
int newCapacity = _bufferCapacity; int newCapacity = _bufferCapacity;
if (newCapacity == 0) if (newCapacity == 0)
{ {
// Fresh sessions start without buffers; seed the first rent with a single-slot array.
newCapacity = 1; newCapacity = 1;
} }
@@ -826,6 +837,7 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
private void ReturnBuffers() private void ReturnBuffers()
{ {
// Buffers are lazily allocated per session; return whichever arrays were rented before the next batch.
if (_primeDeltas is not null) if (_primeDeltas is not null)
{ {
ArrayPool<ulong>.Shared.Return(_primeDeltas, clearArray: false); ArrayPool<ulong>.Shared.Return(_primeDeltas, clearArray: false);
@@ -849,10 +861,11 @@ public sealed class MersenneNumberDivisorByDivisorCpuTester : IMersenneNumberDiv
private static void ComputePrimeDeltas(ReadOnlySpan<ulong> primes, Span<ulong> deltas) private static void ComputePrimeDeltas(ReadOnlySpan<ulong> primes, Span<ulong> deltas)
{ {
if (primes.Length == 0) // Prime batches on the scanning path are never empty, so the legacy guard stays commented to remove the extra branch.
{ // if (primes.Length == 0)
return; // {
} // return;
// }
ulong previous = primes[0]; ulong previous = primes[0];
deltas[0] = previous; deltas[0] = previous;
+12 -1
View File
@@ -82,7 +82,8 @@ public sealed class DivisorCycleCache
ulong cached = snapshot[divisor]; ulong cached = snapshot[divisor];
if (cached == 0UL) if (cached == 0UL)
{ {
throw new InvalidDataException($"Divisor cycle is missing for {divisor}"); missingBuffer[missingCount++] = i;
continue;
} }
cycles[i] = cached; cycles[i] = cached;
@@ -104,6 +105,16 @@ 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)
@@ -530,34 +530,44 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime) private static ulong ComputeDivisorLimitFromMaxPrime(ulong maxPrime)
{ {
if (maxPrime <= 1UL) // The production configuration seeds the scan with exponents above the first odd prime, so the defensive guard remains
{ // commented to document the invariant without branching.
return 0UL; // if (maxPrime <= 1UL)
} // {
// return 0UL;
// }
if (maxPrime - 1UL >= 64UL) // Real workloads push maxPrime well past the 64-bit shift boundary, so keep the historical implementation commented out
{ // and return ulong.MaxValue directly.
return ulong.MaxValue; // if (maxPrime - 1UL >= 64UL)
} // {
// return ulong.MaxValue;
// }
//
// return (1UL << (int)(maxPrime - 1UL)) - 1UL;
return (1UL << (int)(maxPrime - 1UL)) - 1UL; return ulong.MaxValue;
} }
private static ulong ComputeAllowedMaxDivisor(ulong prime, ulong divisorLimit) private static ulong ComputeAllowedMaxDivisor(ulong prime, ulong divisorLimit)
{ {
// The by-divisor search emits candidate exponents starting at the first odd prime, so this guard // The by-divisor search emits candidate exponents starting at the first odd prime, so the defensive guard remains
// would never trigger on the scanner hot path. // commented to document that invariant.
// if (prime <= 1UL) // if (prime <= 1UL)
// { // {
// return 0UL; // return 0UL;
// } // }
if (prime - 1UL >= 64UL) // Production exponents are always large enough that the shift-based clamp would overflow, so skip the legacy branch and
{ // return the configured divisor limit directly.
return divisorLimit; // if (prime - 1UL >= 64UL)
} // {
// return divisorLimit;
// }
//
// return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, divisorLimit);
return Math.Min((1UL << (int)(prime - 1UL)) - 1UL, divisorLimit); return divisorLimit;
} }
private static void CheckKernel(Index1D index, ArrayView<MontgomeryDivisorData> divisors, ArrayView<ulong> exponents, ArrayView<byte> hits) private static void CheckKernel(Index1D index, ArrayView<MontgomeryDivisorData> divisors, ArrayView<ulong> exponents, ArrayView<byte> hits)
@@ -613,10 +623,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
} }
int length = primes.Length; int length = primes.Length;
if (length == 0) // Production batches always contain at least one exponent, so the empty-span guard remains commented out to remove
{ // the redundant branch.
return; // if (length == 0)
} // {
// return;
// }
if (divisorData.Modulus <= 1UL || (divisorData.Modulus & 1UL) == 0UL) if (divisorData.Modulus <= 1UL || (divisorData.Modulus & 1UL) == 0UL)
{ {
@@ -801,15 +813,12 @@ public sealed class MersenneNumberDivisorByDivisorGpuTester : IMersenneNumberDiv
{ {
get get
{ {
lock (_sync) if (!_isConfigured)
{ {
if (!_isConfigured) throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
{
throw new InvalidOperationException("ConfigureFromMaxPrime must be called before using the tester.");
}
return _divisorLimit;
} }
return _divisorLimit;
} }
} }
@@ -118,7 +118,7 @@ internal sealed class MersenneNumberDivisorMontgomeryGpuBuilder : IDisposable
ulong nPrime = ComputeMontgomeryNPrime(modulus); ulong nPrime = ComputeMontgomeryNPrime(modulus);
ulong montgomeryOne = ComputeMontgomeryResidue64(modulus); ulong montgomeryOne = ComputeMontgomeryResidue64(modulus);
ulong montgomeryTwo = montgomeryOne.MulMod64GpuCompatible(2UL, modulus); ulong montgomeryTwo = montgomeryOne.MulMod64(2UL, modulus);
ulong montgomeryTwoSquared = MontgomeryMultiplyGpu(montgomeryTwo, montgomeryTwo, modulus, nPrime); ulong montgomeryTwoSquared = MontgomeryMultiplyGpu(montgomeryTwo, montgomeryTwo, modulus, nPrime);
destination[idx] = new MontgomeryDivisorData( destination[idx] = new MontgomeryDivisorData(
@@ -103,10 +103,12 @@ internal sealed class MersenneNumberDivisorResidueGpuEvaluator : IDisposable
{ {
int idx = index; int idx = index;
int length = (int)exponents.Length; int length = (int)exponents.Length;
if (idx >= length) // Batched launches match the buffer length on production workloads, so the bounds guard stays commented out to remove
{ // the redundant comparison.
return; // if (idx >= length)
} // {
// return;
// }
ulong exponent = exponents[idx]; ulong exponent = exponents[idx];
ulong modulus = divisorData.Modulus; ulong modulus = divisorData.Modulus;
@@ -850,10 +850,11 @@ internal static partial class PrimeOrderGpuHeuristics
{ {
ulong primeFactor = factors[i]; ulong primeFactor = factors[i];
int exponent = exponents[i]; int exponent = exponents[i];
if (primeFactor <= 1UL) // Factorization never yields unity on the scanning path, so skip the redundant guard.
{ // if (primeFactor <= 1UL)
continue; // {
} // continue;
// }
for (int iteration = 0; iteration < exponent; iteration++) for (int iteration = 0; iteration < exponent; iteration++)
{ {
@@ -889,10 +890,12 @@ internal static partial class PrimeOrderGpuHeuristics
ArrayView1D<ulong, Stride1D.Dense> compositeStack, ArrayView1D<ulong, Stride1D.Dense> compositeStack,
ArrayView1D<byte, Stride1D.Dense> statusOut) ArrayView1D<byte, Stride1D.Dense> statusOut)
{ {
if (order == 0UL) // Order values handed to the kernel are strictly positive on the production path, so the zero guard stays commented
{ // out to remove the redundant branch.
return false; // if (order == 0UL)
} // {
// return false;
// }
if ((order).Pow2ModWindowedGpu(divisor.Modulus) != 1UL) if ((order).Pow2ModWindowedGpu(divisor.Modulus) != 1UL)
{ {
@@ -924,10 +927,11 @@ internal static partial class PrimeOrderGpuHeuristics
{ {
ulong primeFactor = factors[i]; ulong primeFactor = factors[i];
int exponent = exponents[i]; int exponent = exponents[i];
if (primeFactor <= 1UL) // Factorization never yields one on the by-divisor path, so the defensive continue stays commented out.
{ // if (primeFactor <= 1UL)
continue; // {
} // continue;
// }
ulong reduced = order; ulong reduced = order;
for (int iteration = 0; iteration < exponent; iteration++) for (int iteration = 0; iteration < exponent; iteration++)
@@ -1008,20 +1012,22 @@ internal static partial class PrimeOrderGpuHeuristics
SortCandidatesKernel(prime, previousOrder, hasPreviousOrder != 0, candidates, candidateCount); SortCandidatesKernel(prime, previousOrder, hasPreviousOrder != 0, candidates, candidateCount);
int powBudget = maxPowChecks <= 0 ? candidateCount : maxPowChecks; int powBudget = maxPowChecks <= 0 ? candidateCount : maxPowChecks;
if (powBudget <= 0) // The heuristics always reserve at least one check, so the zero-budget fallback remains commented out.
{ // if (powBudget <= 0)
powBudget = candidateCount; // {
} // powBudget = candidateCount;
// }
int powUsed = 0; int powUsed = 0;
for (int i = 0; i < candidateCount && powUsed < powBudget; i++) for (int i = 0; i < candidateCount && powUsed < powBudget; i++)
{ {
ulong candidate = candidates[i]; ulong candidate = candidates[i];
if (candidate <= 1UL) // Candidate generation skips 1 by construction, so the defensive continue stays commented out.
{ // if (candidate <= 1UL)
continue; // {
} // continue;
// }
if (powUsed >= powBudget) if (powUsed >= powBudget)
{ {
@@ -1309,10 +1315,11 @@ internal static partial class PrimeOrderGpuHeuristics
{ {
ulong primeFactor = factors[i]; ulong primeFactor = factors[i];
int exponent = exponents[i]; int exponent = exponents[i];
if (primeFactor <= 1UL) // Factorization never yields one for candidate orders, so keep the guard commented out to avoid re-checking inputs.
{ // if (primeFactor <= 1UL)
continue; // {
} // continue;
// }
ulong reduced = candidate; ulong reduced = candidate;
for (int iteration = 0; iteration < exponent; iteration++) for (int iteration = 0; iteration < exponent; iteration++)
@@ -1351,10 +1358,12 @@ internal static partial class PrimeOrderGpuHeuristics
ArrayView1D<ulong, Stride1D.Dense> compositeStack, ArrayView1D<ulong, Stride1D.Dense> compositeStack,
ArrayView1D<byte, Stride1D.Dense> statusOut) ArrayView1D<byte, Stride1D.Dense> statusOut)
{ {
if (initial <= 1UL) // Composite candidates sent here are always greater than one during production scans, so the trivial-return branch stays
{ // commented out.
return true; // if (initial <= 1UL)
} // {
// return true;
// }
int stackCapacity = (int)compositeStack.Length; int stackCapacity = (int)compositeStack.Length;
if (stackCapacity <= 0) if (stackCapacity <= 0)
@@ -1616,10 +1625,11 @@ internal static partial class PrimeOrderGpuHeuristics
private static GpuPow2ModStatus TryPow2ModBatchInternal(ReadOnlySpan<UInt128> exponents, UInt128 prime, Span<UInt128> remainders) private static GpuPow2ModStatus TryPow2ModBatchInternal(ReadOnlySpan<UInt128> exponents, UInt128 prime, Span<UInt128> remainders)
{ {
if (exponents.Length == 0) // Residue batches are never empty on production workloads, so skip the zero-length success shortcut.
{ // if (exponents.Length == 0)
return GpuPow2ModStatus.Success; // {
} // return GpuPow2ModStatus.Success;
// }
if (remainders.Length < exponents.Length) if (remainders.Length < exponents.Length)
{ {
@@ -1706,10 +1716,11 @@ internal static partial class PrimeOrderGpuHeuristics
private static bool TryComputeOnGpuWide(ReadOnlySpan<UInt128> exponents, UInt128 prime, Span<UInt128> results) private static bool TryComputeOnGpuWide(ReadOnlySpan<UInt128> exponents, UInt128 prime, Span<UInt128> results)
{ {
int length = exponents.Length; int length = exponents.Length;
if (length == 0) // Production launches always carry work, so keep the empty-batch shortcut commented out.
{ // if (length == 0)
return true; // {
} // return true;
// }
GpuUInt128[]? rentedExponents = null; GpuUInt128[]? rentedExponents = null;
GpuUInt128[]? rentedResults = null; GpuUInt128[]? rentedResults = null;
@@ -1763,6 +1774,8 @@ internal static partial class PrimeOrderGpuHeuristics
} }
finally finally
{ {
// Small batches use the stack-allocated fast path, leaving the rented arrays null. Retain the guards so pooled
// buffers are only returned when they were actually leased.
if (rentedExponents is not null) if (rentedExponents is not null)
{ {
ArrayPool<GpuUInt128>.Shared.Return(rentedExponents, clearArray: false); ArrayPool<GpuUInt128>.Shared.Return(rentedExponents, clearArray: false);
+63 -37
View File
@@ -79,20 +79,20 @@ public class MersenneDivisorCycles
return false; return false;
} }
if (divisor <= PerfectNumberConstants.MaxQForDivisorCycles) // 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.
// Even though production scans never revisit divisors below this bound, the shared cache still serves // if (divisor <= PerfectNumberConstants.MaxQForDivisorCycles)
// precomputation steps and tests, so keep the fast lookup enabled. // {
var small = Shared._smallCycles; // var small = Shared._smallCycles;
if (small is not null) // if (small is not null)
{ // {
ulong cached = small[(int)divisor]; // ulong cached = small[(int)divisor];
if (cached != 0UL) // if (cached != 0UL)
{ // {
return cached == exponent; // return cached == exponent;
} // }
} // }
} // }
if (TryCalculateCycleLengthHeuristic(divisor, divisorData, out ulong heuristicCycle) && heuristicCycle != 0UL) if (TryCalculateCycleLengthHeuristic(divisor, divisorData, out ulong heuristicCycle) && heuristicCycle != 0UL)
{ {
@@ -122,42 +122,68 @@ public class MersenneDivisorCycles
return false; return false;
} }
if (divisor <= PerfectNumberConstants.MaxQForDivisorCycles) // Candidate divisors on the production path sit well above the precomputed snapshot, so skip the small-cycle lookup
{ // here and leave factoring scenarios to call TryMatchSmallCycleForFactoring when necessary.
// The by-divisor path only handles divisors far above this cutoff, but Pollard Rho factoring and unit tests // if (divisor <= PerfectNumberConstants.MaxQForDivisorCycles)
// still reuse the cached small-cycle table, so keep the fast path enabled. // {
MersenneDivisorCycles shared = Shared; // MersenneDivisorCycles shared = Shared;
ulong[]? small = shared._smallCycles; // ulong[]? small = shared._smallCycles;
if (small is not null) // if (small is not null)
{ // {
ulong cached = small[(int)divisor]; // ulong cached = small[(int)divisor];
if (cached != 0UL) // if (cached != 0UL)
{ // {
return cached == exponent; // return cached == exponent;
} // }
} // }
} // }
return exponent.Pow2ModWindowedCpu(divisorData.Modulus) == 1UL; return exponent.Pow2ModWindowedCpu(divisorData.Modulus) == 1UL;
} }
private static bool IsValidMersenneDivisorCandidate(ulong divisor, ulong exponent) public static bool TryMatchSmallCycleForFactoring(ulong divisor, ulong exponent, out ulong cycleLength)
{ {
// The divisor enumeration pipeline has already filtered invalid inputs, but keep the guard so external callers and if (divisor > PerfectNumberConstants.MaxQForDivisorCycles)
// tests continue to receive a defensive answer.
if (exponent <= 1UL || divisor <= 1UL || (divisor & 1UL) == 0UL)
{ {
cycleLength = 0UL;
return false; return false;
} }
UInt128 step = (UInt128)exponent << 1; ulong[]? small = Shared._smallCycles;
// Exponents drawn from prime candidates are far below the overflow boundary, yet the broader factoring utilities rely if (small is null)
// on this bail-out when validating arbitrary inputs.
if (step == UInt128.Zero)
{ {
cycleLength = 0UL;
return false; return false;
} }
ulong cached = small[(int)divisor];
if (cached == 0UL)
{
cycleLength = 0UL;
return false;
}
cycleLength = cached;
return cached == exponent;
}
private static bool IsValidMersenneDivisorCandidate(ulong divisor, ulong exponent)
{
// Production workloads pre-filter invalid inputs, so skip the defensive guards here. Factoring and diagnostics should
// validate inputs before calling into the candidate path.
// if (exponent <= 1UL || divisor <= 1UL || (divisor & 1UL) == 0UL)
// {
// return false;
// }
UInt128 step = (UInt128)exponent << 1;
// The overflow bail-out is handled by factoring-specific call sites; keep the historical branch commented to document the
// legacy behavior.
// if (step == UInt128.Zero)
// {
// return false;
// }
UInt128 adjusted = (UInt128)divisor - UInt128.One; UInt128 adjusted = (UInt128)divisor - UInt128.One;
return adjusted % step == UInt128.Zero; return adjusted % step == UInt128.Zero;
} }
+16 -25
View File
@@ -290,26 +290,17 @@ public static class ULongExtensions
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
// TODO: Replace this fallback with the UInt128 Montgomery helper measured fastest in public static ulong MulMod64(this ulong a, ulong b, ulong modulus)
// MulMod64Benchmarks so CPU callers stop paying for triple modulo operations.
public static ulong MulMod64(this ulong a, ulong b, ulong modulus) => (ulong)(UInt128)(((a % modulus) * (b % modulus)) % modulus);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong MulMod64GpuCompatible(this ulong a, ulong b, ulong modulus)
{ {
// TODO: Remove this GPU-compatible shim from production once callers migrate to MulMod64, if (modulus <= 1UL)
// which the benchmarks show is roughly 6-7× faster on dense 64-bit inputs. {
GpuUInt128 state = new(a % modulus); return 0UL;
return state.MulMod(b, modulus); }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] ulong reducedA = a >= modulus ? a % modulus : a;
public static ulong MulMod64GpuCompatibleDeferred(this ulong a, ulong b, ulong modulus) ulong reducedB = b >= modulus ? b % modulus : b;
{ GpuUInt128 state = new(reducedA);
// TODO: Move this deferred helper to the benchmark suite; the baseline MulMod64 avoids the return state.MulMod(reducedB, modulus);
// 5-40× slowdown seen across real-world operand distributions.
GpuUInt128 state = new(a);
return state.MulModWithNativeModulo(b, modulus);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -453,7 +444,7 @@ public static class ULongExtensions
{ {
if (((exponent >> index) & 1UL) == 0UL) if (((exponent >> index) & 1UL) == 0UL)
{ {
result = result.MulMod64GpuCompatible(result, modulus); result = result.MulMod64(result, modulus);
index--; index--;
continue; continue;
} }
@@ -472,12 +463,12 @@ public static class ULongExtensions
int windowLength = index - windowStart + 1; int windowLength = index - windowStart + 1;
for (int square = 0; square < windowLength; square++) for (int square = 0; square < windowLength; square++)
{ {
result = result.MulMod64GpuCompatible(result, modulus); result = result.MulMod64(result, modulus);
} }
ulong windowValue = (exponent >> windowStart) & ((1UL << windowLength) - 1UL); ulong windowValue = (exponent >> windowStart) & ((1UL << windowLength) - 1UL);
int tableIndex = (int)((windowValue - 1UL) >> 1); int tableIndex = (int)((windowValue - 1UL) >> 1);
result = result.MulMod64GpuCompatible(oddPowers.Get(tableIndex), modulus); result = result.MulMod64(oddPowers.Get(tableIndex), modulus);
index = windowStart - 1; index = windowStart - 1;
} }
@@ -496,7 +487,7 @@ public static class ULongExtensions
{ {
if ((remainingExponent & 1UL) != 0UL) if ((remainingExponent & 1UL) != 0UL)
{ {
result = result.MulMod64GpuCompatible(baseValue, modulus); result = result.MulMod64(baseValue, modulus);
} }
remainingExponent >>= 1; remainingExponent >>= 1;
@@ -505,7 +496,7 @@ public static class ULongExtensions
break; break;
} }
baseValue = baseValue.MulMod64GpuCompatible(baseValue, modulus); baseValue = baseValue.MulMod64(baseValue, modulus);
} }
return result; return result;
@@ -840,11 +831,11 @@ public static class ULongExtensions
return; return;
} }
ulong square = baseValue.MulMod64GpuCompatible(baseValue, modulus); ulong square = baseValue.MulMod64(baseValue, modulus);
ulong current = baseValue; ulong current = baseValue;
for (int i = 1; i < _count; i++) for (int i = 1; i < _count; i++)
{ {
current = current.MulMod64GpuCompatible(square, modulus); current = current.MulMod64(square, modulus);
SetValue(i, current); SetValue(i, current);
} }
} }