Merge pull request #218 from mlemanczyk/codex/analyze-mersenne=bydivisor-path-for-tracking-remainder-bbul2g
Reuse stack spans across divisor processing loops
This commit is contained in:
4 files changed
+530
-147
No files matched your search
@@ -27,6 +27,11 @@ internal struct ExponentRemainderStepper
|
|||||||
|
|
||||||
public ulong PreviousExponent => _previousExponent;
|
public ulong PreviousExponent => _previousExponent;
|
||||||
|
|
||||||
|
public bool MatchesDivisor(in MontgomeryDivisorData divisor)
|
||||||
|
{
|
||||||
|
return _modulus == divisor.Modulus && _nPrime == divisor.NPrime && _montgomeryOne == divisor.MontgomeryOne;
|
||||||
|
}
|
||||||
|
|
||||||
public void Reset()
|
public void Reset()
|
||||||
{
|
{
|
||||||
_previousExponent = 0UL;
|
_previousExponent = 0UL;
|
||||||
|
|||||||
@@ -322,6 +322,7 @@ public class MersenneDivisorCycles
|
|||||||
primeOrderFailed = false;
|
primeOrderFailed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
factorCounts.Clear();
|
||||||
ThreadStaticPools.ReturnFactorCountDictionary(factorCounts);
|
ThreadStaticPools.ReturnFactorCountDictionary(factorCounts);
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
@@ -339,7 +340,7 @@ public class MersenneDivisorCycles
|
|||||||
bool success = TryFactorIntoCountsInternal(value, scratch);
|
bool success = TryFactorIntoCountsInternal(value, scratch);
|
||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
// The scan recalculates factors every time; the old cache never produced hits.
|
scratch.Clear();
|
||||||
ThreadStaticPools.ReturnFactorScratchDictionary(scratch);
|
ThreadStaticPools.ReturnFactorScratchDictionary(scratch);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -350,6 +351,7 @@ public class MersenneDivisorCycles
|
|||||||
AddFactor(counts, pair.Key, pair.Value);
|
AddFactor(counts, pair.Key, pair.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scratch.Clear();
|
||||||
ThreadStaticPools.ReturnFactorScratchDictionary(scratch);
|
ThreadStaticPools.ReturnFactorScratchDictionary(scratch);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -459,47 +461,122 @@ public class MersenneDivisorCycles
|
|||||||
}
|
}
|
||||||
|
|
||||||
int count = factorCounts.Count;
|
int count = factorCounts.Count;
|
||||||
ulong[] primes = ArrayPool<ulong>.Shared.Rent(count);
|
ulong[] primes = ThreadStaticPools.UlongPool.Rent(count);
|
||||||
try
|
|
||||||
|
int index = 0;
|
||||||
|
foreach (KeyValuePair<ulong, int> entry in factorCounts)
|
||||||
{
|
{
|
||||||
int index = 0;
|
primes[index] = entry.Key;
|
||||||
foreach (KeyValuePair<ulong, int> entry in factorCounts)
|
index++;
|
||||||
{
|
|
||||||
primes[index] = entry.Key;
|
|
||||||
index++;
|
|
||||||
}
|
|
||||||
|
|
||||||
Array.Sort(primes, 0, count);
|
|
||||||
|
|
||||||
ulong order = initialOrder;
|
|
||||||
for (int i = 0; i < count; i++)
|
|
||||||
{
|
|
||||||
ulong prime = primes[i];
|
|
||||||
int multiplicity = factorCounts[prime];
|
|
||||||
|
|
||||||
for (int iteration = 0; iteration < multiplicity; iteration++)
|
|
||||||
{
|
|
||||||
if (order % prime != 0UL)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ulong candidate = order / prime;
|
|
||||||
if (candidate.Pow2MontgomeryModWindowedCpu(divisorData, keepMontgomery: false) == 1UL)
|
|
||||||
{
|
|
||||||
order = candidate;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return order;
|
|
||||||
}
|
}
|
||||||
finally
|
|
||||||
|
Array.Sort(primes, 0, count);
|
||||||
|
|
||||||
|
const int StackMultiplicityCapacity = 16;
|
||||||
|
const int HeapMultiplicityCapacity = 256;
|
||||||
|
|
||||||
|
// Scanner workloads keep almost all multiplicities within the stack-backed span, so the stack path handles the hot loop.
|
||||||
|
Span<ulong> stackCandidates = stackalloc ulong[StackMultiplicityCapacity];
|
||||||
|
Span<bool> stackEvaluations = stackalloc bool[StackMultiplicityCapacity];
|
||||||
|
|
||||||
|
ulong[]? heapCandidateArray = null;
|
||||||
|
bool[]? heapEvaluationArray = null;
|
||||||
|
|
||||||
|
ExponentRemainderStepper stepper = ThreadStaticPools.RentExponentStepper(divisorData);
|
||||||
|
|
||||||
|
ulong order = initialOrder;
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
ArrayPool<ulong>.Shared.Return(primes, clearArray: false);
|
ulong prime = primes[i];
|
||||||
|
int multiplicity = factorCounts[prime];
|
||||||
|
// Multiplicities collected by TryFactorIntoCountsInternal are strictly positive along the scanner path.
|
||||||
|
// Keeping the guard commented documents the assumption so it does not resurface in future reviews.
|
||||||
|
// if (multiplicity <= 0)
|
||||||
|
// {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (multiplicity <= StackMultiplicityCapacity)
|
||||||
|
{
|
||||||
|
ProcessReduceOrderPrime(stackCandidates.Slice(0, multiplicity), stackEvaluations.Slice(0, multiplicity), ref order, prime, multiplicity, ref stepper);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (multiplicity > HeapMultiplicityCapacity)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Factor multiplicity {multiplicity} exceeds the supported limit of {HeapMultiplicityCapacity}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Larger multiplicities appear during stress tests; reuse a shared heap buffer in those cases.
|
||||||
|
heapCandidateArray ??= ThreadStaticPools.UlongPool.Rent(HeapMultiplicityCapacity);
|
||||||
|
heapEvaluationArray ??= ThreadStaticPools.BoolPool.Rent(HeapMultiplicityCapacity);
|
||||||
|
ProcessReduceOrderPrime(heapCandidateArray.AsSpan(0, multiplicity), heapEvaluationArray.AsSpan(0, multiplicity), ref order, prime, multiplicity, ref stepper);
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadStaticPools.ReturnExponentStepper(stepper);
|
||||||
|
ThreadStaticPools.UlongPool.Return(primes);
|
||||||
|
|
||||||
|
if (heapCandidateArray is not null)
|
||||||
|
{
|
||||||
|
ThreadStaticPools.UlongPool.Return(heapCandidateArray);
|
||||||
|
ThreadStaticPools.BoolPool.Return(heapEvaluationArray!);
|
||||||
|
}
|
||||||
|
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ProcessReduceOrderPrime(Span<ulong> candidateBuffer, Span<bool> evaluationBuffer, ref ulong order, ulong prime, int multiplicity, ref ExponentRemainderStepper stepper)
|
||||||
|
{
|
||||||
|
ulong working = order;
|
||||||
|
int actual = 0;
|
||||||
|
while (actual < multiplicity)
|
||||||
|
{
|
||||||
|
if (working < prime)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong reduced = working / prime;
|
||||||
|
if (reduced == 0UL)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
candidateBuffer[actual] = reduced;
|
||||||
|
working = reduced;
|
||||||
|
actual++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Span<ulong> candidates = candidateBuffer.Slice(0, actual);
|
||||||
|
Span<bool> evaluations = evaluationBuffer.Slice(0, actual);
|
||||||
|
|
||||||
|
for (int left = 0, right = actual - 1; left < right; left++, right--)
|
||||||
|
{
|
||||||
|
ulong tmp = candidates[left];
|
||||||
|
candidates[left] = candidates[right];
|
||||||
|
candidates[right] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
stepper.Reset();
|
||||||
|
|
||||||
|
for (int j = 0; j < actual; j++)
|
||||||
|
{
|
||||||
|
evaluations[j] = stepper.ComputeNextIsUnity(candidates[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = actual - 1; j >= 0; j--)
|
||||||
|
{
|
||||||
|
if (!evaluations[j])
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
order = candidates[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
@@ -110,27 +111,70 @@ internal static partial class PrimeOrderCalculator
|
|||||||
private static bool TrySpecialMaxCpu(ulong phi, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData)
|
private static bool TrySpecialMaxCpu(ulong phi, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData)
|
||||||
{
|
{
|
||||||
int length = factors.Count;
|
int length = factors.Count;
|
||||||
|
// The phi factorization on the scanner path always yields at least one entry for phi >= 2,
|
||||||
|
// preventing the zero-length case from occurring outside synthetic tests.
|
||||||
|
// if (length == 0)
|
||||||
|
// {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
ReadOnlySpan<FactorEntry> factorSpan = new(factors.Factors, 0, length);
|
ReadOnlySpan<FactorEntry> factorSpan = new(factors.Factors, 0, length);
|
||||||
for (int i = 0; i < length; i++)
|
|
||||||
|
if (length <= 32)
|
||||||
{
|
{
|
||||||
ulong factor = factorSpan[i].Value;
|
Span<ulong> stackBuffer = stackalloc ulong[length];
|
||||||
// Don't uncomment this. If there is division by zero, that means something wrong.
|
return EvaluateSpecialMaxCandidates(stackBuffer, factorSpan, phi, prime, divisorData);
|
||||||
// if (factor == 0UL)
|
}
|
||||||
|
|
||||||
|
ulong[] rented = ThreadStaticPools.UlongPool.Rent(length);
|
||||||
|
bool result = EvaluateSpecialMaxCandidates(rented.AsSpan(0, length), factorSpan, phi, prime, divisorData);
|
||||||
|
ThreadStaticPools.UlongPool.Return(rented, clearArray: false);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool EvaluateSpecialMaxCandidates(Span<ulong> buffer, ReadOnlySpan<FactorEntry> factors, ulong phi, ulong prime, in MontgomeryDivisorData divisorData)
|
||||||
|
{
|
||||||
|
int actual = 0;
|
||||||
|
int factorCount = factors.Length;
|
||||||
|
for (int i = 0; i < factorCount; i++)
|
||||||
|
{
|
||||||
|
ulong factor = factors[i].Value;
|
||||||
|
// The partial factor pipeline never feeds zero or oversized factors while scanning candidate orders.
|
||||||
|
// if (factor == 0UL || factor > phi)
|
||||||
// {
|
// {
|
||||||
// continue;
|
// continue;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// TODO: Can we optimize it thanks to knowing the factors, differences between factors, factors and / or phi
|
|
||||||
// being monotonically increasing etc. For example, if phi increases and factor increases, what does it tell us
|
|
||||||
// about the value of reduced? Or, maybe wee can use residue stepping to calculate the residue instead of calling
|
|
||||||
// Pow2equalsOneCpu? Maybe we don't need to calculate it from scratch?
|
|
||||||
ulong reduced = phi / factor;
|
ulong reduced = phi / factor;
|
||||||
if (Pow2EqualsOneCpu(reduced, prime, divisorData))
|
if (reduced == 0UL)
|
||||||
{
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[actual] = reduced;
|
||||||
|
actual++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Span<ulong> candidates = buffer.Slice(0, actual);
|
||||||
|
candidates.Sort();
|
||||||
|
|
||||||
|
ExponentRemainderStepper stepper = ThreadStaticPools.RentExponentStepper(divisorData);
|
||||||
|
int candidateCount = candidates.Length;
|
||||||
|
for (int i = 0; i < candidateCount; i++)
|
||||||
|
{
|
||||||
|
if (stepper.ComputeNextIsUnity(candidates[i]))
|
||||||
|
{
|
||||||
|
ThreadStaticPools.ReturnExponentStepper(stepper);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ThreadStaticPools.ReturnExponentStepper(stepper);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,8 +204,6 @@ internal static partial class PrimeOrderCalculator
|
|||||||
Span<FactorEntry> buffer = tempArray;
|
Span<FactorEntry> buffer = tempArray;
|
||||||
factorSpan.CopyTo(buffer);
|
factorSpan.CopyTo(buffer);
|
||||||
|
|
||||||
// bool isPrime = PrimeTester.IsPrimeInternal(factors.Cofactor, CancellationToken.None);
|
|
||||||
// bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factors.Cofactor);
|
|
||||||
if (!factors.FullyFactored && factors.Cofactor > 1UL && factors.CofactorIsPrime)
|
if (!factors.FullyFactored && factors.Cofactor > 1UL && factors.CofactorIsPrime)
|
||||||
{
|
{
|
||||||
buffer[length] = new FactorEntry(factors.Cofactor, 1, true);
|
buffer[length] = new FactorEntry(factors.Cofactor, 1, true);
|
||||||
@@ -170,32 +212,110 @@ internal static partial class PrimeOrderCalculator
|
|||||||
|
|
||||||
buffer.Slice(0, length).Sort(static (a, b) => a.Value.CompareTo(b.Value));
|
buffer.Slice(0, length).Sort(static (a, b) => a.Value.CompareTo(b.Value));
|
||||||
|
|
||||||
|
ExponentRemainderStepper stepper = ThreadStaticPools.RentExponentStepper(divisorData);
|
||||||
|
|
||||||
|
const int StackExponentCapacity = 16;
|
||||||
|
const int ExponentHardLimit = 256;
|
||||||
|
|
||||||
|
Span<ulong> stackCandidates = stackalloc ulong[StackExponentCapacity];
|
||||||
|
Span<bool> stackEvaluations = stackalloc bool[StackExponentCapacity];
|
||||||
|
|
||||||
|
ulong[]? heapCandidateArray = null;
|
||||||
|
bool[]? heapEvaluationArray = null;
|
||||||
|
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
ulong primeFactor = buffer[i].Value;
|
ulong primeFactor = buffer[i].Value;
|
||||||
int exponent = buffer[i].Exponent;
|
int exponent = buffer[i].Exponent;
|
||||||
for (int iteration = 0; iteration < exponent; iteration++)
|
// Factor exponents produced by Pollard-Rho and trial division are always positive in the scanner flow.
|
||||||
|
// if (exponent <= 0)
|
||||||
|
// {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (exponent > ExponentHardLimit)
|
||||||
{
|
{
|
||||||
if ((order % primeFactor) != 0UL)
|
throw new InvalidOperationException($"Prime factor exponent {exponent} exceeds the supported limit of {ExponentHardLimit}.");
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ulong reduced = order / primeFactor;
|
|
||||||
if (Pow2EqualsOneCpu(reduced, prime, divisorData))
|
|
||||||
{
|
|
||||||
order = reduced;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (exponent <= StackExponentCapacity)
|
||||||
|
{
|
||||||
|
ProcessExponentLoweringPrime(stackCandidates.Slice(0, exponent), stackEvaluations.Slice(0, exponent), ref order, primeFactor, exponent, ref stepper);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
heapCandidateArray ??= ThreadStaticPools.UlongPool.Rent(ExponentHardLimit);
|
||||||
|
heapEvaluationArray ??= ThreadStaticPools.BoolPool.Rent(ExponentHardLimit);
|
||||||
|
ProcessExponentLoweringPrime(heapCandidateArray.AsSpan(0, exponent), heapEvaluationArray.AsSpan(0, exponent), ref order, primeFactor, exponent, ref stepper);
|
||||||
|
}
|
||||||
|
|
||||||
|
ThreadStaticPools.ReturnExponentStepper(stepper);
|
||||||
|
|
||||||
|
if (heapCandidateArray is not null)
|
||||||
|
{
|
||||||
|
ThreadStaticPools.UlongPool.Return(heapCandidateArray);
|
||||||
|
ThreadStaticPools.BoolPool.Return(heapEvaluationArray!);
|
||||||
}
|
}
|
||||||
|
|
||||||
pool.Return(tempArray, clearArray: false);
|
pool.Return(tempArray, clearArray: false);
|
||||||
return order;
|
return order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ProcessExponentLoweringPrime(Span<ulong> candidateBuffer, Span<bool> evaluationBuffer, ref ulong order, ulong primeFactor, int exponent, ref ExponentRemainderStepper stepper)
|
||||||
|
{
|
||||||
|
ulong working = order;
|
||||||
|
int actual = 0;
|
||||||
|
while (actual < exponent)
|
||||||
|
{
|
||||||
|
if (working < primeFactor)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong reduced = working / primeFactor;
|
||||||
|
if (reduced == 0UL)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
candidateBuffer[actual] = reduced;
|
||||||
|
working = reduced;
|
||||||
|
actual++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Span<ulong> candidates = candidateBuffer.Slice(0, actual);
|
||||||
|
Span<bool> evaluations = evaluationBuffer.Slice(0, actual);
|
||||||
|
|
||||||
|
for (int left = 0, right = actual - 1; left < right; left++, right--)
|
||||||
|
{
|
||||||
|
ulong tmp = candidates[left];
|
||||||
|
candidates[left] = candidates[right];
|
||||||
|
candidates[right] = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
stepper.Reset();
|
||||||
|
|
||||||
|
for (int j = 0; j < actual; j++)
|
||||||
|
{
|
||||||
|
evaluations[j] = stepper.ComputeNextIsUnity(candidates[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = actual - 1; j >= 0; j--)
|
||||||
|
{
|
||||||
|
if (!evaluations[j])
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
order = candidates[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static bool TryConfirmOrderCpu(
|
private static bool TryConfirmOrderCpu(
|
||||||
ulong prime,
|
ulong prime,
|
||||||
ulong order,
|
ulong order,
|
||||||
@@ -253,28 +373,101 @@ internal static partial class PrimeOrderCalculator
|
|||||||
{
|
{
|
||||||
ReadOnlySpan<FactorEntry> span = factorization.Factors!;
|
ReadOnlySpan<FactorEntry> span = factorization.Factors!;
|
||||||
int length = factorization.Count;
|
int length = factorization.Count;
|
||||||
|
|
||||||
|
const int StackExponentCapacity = 32;
|
||||||
|
const int ExponentHardLimit = 256;
|
||||||
|
|
||||||
|
Span<ulong> stackBuffer = stackalloc ulong[StackExponentCapacity];
|
||||||
|
|
||||||
|
ulong[]? heapCandidateArray = null;
|
||||||
|
ExponentRemainderStepper stepper = ThreadStaticPools.RentExponentStepper(divisorData);
|
||||||
|
bool violates = false;
|
||||||
|
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
ulong primeFactor = span[i].Value;
|
ulong primeFactor = span[i].Value;
|
||||||
ulong reduced = order;
|
|
||||||
int exponent = span[i].Exponent;
|
int exponent = span[i].Exponent;
|
||||||
for (int iteration = 0; iteration < exponent; iteration++)
|
// Factor exponents emitted by TryConfirmCandidateCpu stay positive for production workloads.
|
||||||
|
// if (exponent <= 0)
|
||||||
|
// {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (exponent > ExponentHardLimit)
|
||||||
{
|
{
|
||||||
// TODO: Benchmark and / or implement DivRem like solution or residue stepper?
|
throw new InvalidOperationException($"Factor exponent {exponent} exceeds the supported limit of {ExponentHardLimit}.");
|
||||||
if ((reduced % primeFactor) != 0UL)
|
}
|
||||||
|
|
||||||
|
if (exponent <= StackExponentCapacity)
|
||||||
|
{
|
||||||
|
if (ValidateOrderForFactor(stackBuffer.Slice(0, exponent), primeFactor, exponent, order, ref stepper))
|
||||||
{
|
{
|
||||||
|
violates = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
reduced /= primeFactor;
|
continue;
|
||||||
if (Pow2EqualsOneCpu(reduced, prime, divisorData))
|
}
|
||||||
{
|
|
||||||
return false;
|
heapCandidateArray ??= ThreadStaticPools.UlongPool.Rent(ExponentHardLimit);
|
||||||
}
|
if (ValidateOrderForFactor(heapCandidateArray.AsSpan(0, exponent), primeFactor, exponent, order, ref stepper))
|
||||||
|
{
|
||||||
|
violates = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
ThreadStaticPools.ReturnExponentStepper(stepper);
|
||||||
|
|
||||||
|
if (heapCandidateArray is not null)
|
||||||
|
{
|
||||||
|
ThreadStaticPools.UlongPool.Return(heapCandidateArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
return !violates;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool ValidateOrderForFactor(Span<ulong> buffer, ulong primeFactor, int exponent, ulong order, ref ExponentRemainderStepper stepper)
|
||||||
|
{
|
||||||
|
ulong working = order;
|
||||||
|
int actual = 0;
|
||||||
|
while (actual < exponent)
|
||||||
|
{
|
||||||
|
if (working < primeFactor)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong reduced = working / primeFactor;
|
||||||
|
if (reduced == 0UL)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[actual] = reduced;
|
||||||
|
working = reduced;
|
||||||
|
actual++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Span<ulong> candidates = buffer.Slice(0, actual);
|
||||||
|
candidates.Reverse();
|
||||||
|
|
||||||
|
stepper.Reset();
|
||||||
|
|
||||||
|
for (int j = 0; j < actual; j++)
|
||||||
|
{
|
||||||
|
if (stepper.ComputeNextIsUnity(candidates[j]))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static bool TryHeuristicFinishCpu(
|
private static bool TryHeuristicFinishCpu(
|
||||||
@@ -328,6 +521,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity);
|
BuildCandidates(order, factorArray, orderFactors.Count, candidates, capacity);
|
||||||
if (candidates.Count == 0)
|
if (candidates.Count == 0)
|
||||||
{
|
{
|
||||||
|
candidates.Clear();
|
||||||
ThreadStaticPools.ReturnUlongList(candidates);
|
ThreadStaticPools.ReturnUlongList(candidates);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -340,6 +534,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
int candidateCount = candidates.Count;
|
int candidateCount = candidates.Count;
|
||||||
bool allowGpuBatch = true;
|
bool allowGpuBatch = true;
|
||||||
Span<ulong> candidateSpan = CollectionsMarshal.AsSpan(candidates);
|
Span<ulong> candidateSpan = CollectionsMarshal.AsSpan(candidates);
|
||||||
|
ExponentRemainderStepper powStepper = ThreadStaticPools.RentExponentStepper(divisorData);
|
||||||
|
|
||||||
// DebugLog(() => $"Checking candidates ({candidateCount} candidates, {powBudget} pow budget)");
|
// DebugLog(() => $"Checking candidates ({candidateCount} candidates, {powBudget} pow budget)");
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -412,7 +607,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
equalsOne = Pow2EqualsOneCpu(candidate, prime, divisorData);
|
equalsOne = powStepper.ComputeNextIsUnity(candidate);
|
||||||
}
|
}
|
||||||
if (!equalsOne)
|
if (!equalsOne)
|
||||||
{
|
{
|
||||||
@@ -429,7 +624,9 @@ internal static partial class PrimeOrderCalculator
|
|||||||
pool.Return(gpuPool, clearArray: false);
|
pool.Return(gpuPool, clearArray: false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
candidates.Clear();
|
||||||
ThreadStaticPools.ReturnUlongList(candidates);
|
ThreadStaticPools.ReturnUlongList(candidates);
|
||||||
|
ThreadStaticPools.ReturnExponentStepper(powStepper);
|
||||||
result = candidate;
|
result = candidate;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -442,7 +639,9 @@ internal static partial class PrimeOrderCalculator
|
|||||||
index += batchSize;
|
index += batchSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
candidates.Clear();
|
||||||
ThreadStaticPools.ReturnUlongList(candidates);
|
ThreadStaticPools.ReturnUlongList(candidates);
|
||||||
|
ThreadStaticPools.ReturnExponentStepper(powStepper);
|
||||||
// DebugLog("No candidate confirmed");
|
// DebugLog("No candidate confirmed");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -635,8 +834,6 @@ internal static partial class PrimeOrderCalculator
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factorization.Cofactor);
|
|
||||||
// bool isPrime = PrimeTester.IsPrimeInternal(factorization.Cofactor, CancellationToken.None);
|
|
||||||
if (!factorization.CofactorIsPrime)
|
if (!factorization.CofactorIsPrime)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -649,33 +846,57 @@ internal static partial class PrimeOrderCalculator
|
|||||||
|
|
||||||
ReadOnlySpan<FactorEntry> span = factorization.Factors;
|
ReadOnlySpan<FactorEntry> span = factorization.Factors;
|
||||||
int length = factorization.Count;
|
int length = factorization.Count;
|
||||||
|
|
||||||
|
const int StackExponentCapacity = 32;
|
||||||
|
const int ExponentHardLimit = 256;
|
||||||
|
|
||||||
|
Span<ulong> stackBuffer = stackalloc ulong[StackExponentCapacity];
|
||||||
|
ulong[]? heapCandidateArray = null;
|
||||||
|
ExponentRemainderStepper stepper = ThreadStaticPools.RentExponentStepper(divisorData);
|
||||||
|
bool violates = false;
|
||||||
|
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
ulong primeFactor = span[i].Value;
|
ulong primeFactor = span[i].Value;
|
||||||
ulong reduced = candidate;
|
int exponent = span[i].Exponent;
|
||||||
for (int iteration = 0; iteration < span[i].Exponent; iteration++)
|
// Factorization entries for candidate confirmation are always positive here.
|
||||||
|
// if (exponent <= 0)
|
||||||
|
// {
|
||||||
|
// continue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (exponent > ExponentHardLimit)
|
||||||
{
|
{
|
||||||
// TODO: Measure and implement DivRem like solution or residue stepper to reuse calculation results from previous iteration
|
throw new InvalidOperationException($"Candidate factor exponent {exponent} exceeds the supported limit of {ExponentHardLimit}.");
|
||||||
if ((reduced % primeFactor) != 0UL)
|
}
|
||||||
|
|
||||||
|
if (exponent <= StackExponentCapacity)
|
||||||
|
{
|
||||||
|
if (CheckCandidateViolation(stackBuffer.Slice(0, exponent), primeFactor, exponent, candidate, prime, ref powUsed, powBudget, ref stepper))
|
||||||
{
|
{
|
||||||
|
violates = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
reduced /= primeFactor;
|
continue;
|
||||||
if (powUsed >= powBudget && powBudget > 0)
|
}
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
powUsed++;
|
heapCandidateArray ??= ThreadStaticPools.UlongPool.Rent(ExponentHardLimit);
|
||||||
if (Pow2EqualsOneCpu(reduced, prime, divisorData))
|
if (CheckCandidateViolation(heapCandidateArray.AsSpan(0, exponent), primeFactor, exponent, candidate, prime, ref powUsed, powBudget, ref stepper))
|
||||||
{
|
{
|
||||||
return false;
|
violates = true;
|
||||||
}
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
ThreadStaticPools.ReturnExponentStepper(stepper);
|
||||||
|
|
||||||
|
if (heapCandidateArray is not null)
|
||||||
|
{
|
||||||
|
ThreadStaticPools.UlongPool.Return(heapCandidateArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
return !violates;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -683,6 +904,55 @@ internal static partial class PrimeOrderCalculator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool CheckCandidateViolation(Span<ulong> buffer, ulong primeFactor, int exponent, ulong candidate, ulong prime, ref int powUsed, int powBudget, ref ExponentRemainderStepper stepper)
|
||||||
|
{
|
||||||
|
ulong working = candidate;
|
||||||
|
int actual = 0;
|
||||||
|
while (actual < exponent)
|
||||||
|
{
|
||||||
|
if (working < primeFactor)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ulong reduced = working / primeFactor;
|
||||||
|
if (reduced == 0UL)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[actual] = reduced;
|
||||||
|
working = reduced;
|
||||||
|
actual++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actual == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Span<ulong> candidates = buffer.Slice(0, actual);
|
||||||
|
candidates.Reverse();
|
||||||
|
|
||||||
|
stepper.Reset();
|
||||||
|
|
||||||
|
for (int j = 0; j < actual; j++)
|
||||||
|
{
|
||||||
|
if (powUsed >= powBudget && powBudget > 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
powUsed++;
|
||||||
|
if (stepper.ComputeNextIsUnity(candidates[j]))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private static bool Pow2EqualsOneCpu(ulong exponent, ulong prime, in MontgomeryDivisorData divisorData)
|
private static bool Pow2EqualsOneCpu(ulong exponent, ulong prime, in MontgomeryDivisorData divisorData)
|
||||||
{
|
{
|
||||||
if (IsGpuPow2Allowed)
|
if (IsGpuPow2Allowed)
|
||||||
@@ -763,6 +1033,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
}
|
}
|
||||||
|
|
||||||
factorCount = copyIndex;
|
factorCount = copyIndex;
|
||||||
|
counts.Clear();
|
||||||
ThreadStaticPools.ReturnUlongIntDictionary(counts);
|
ThreadStaticPools.ReturnUlongIntDictionary(counts);
|
||||||
counts = null;
|
counts = null;
|
||||||
}
|
}
|
||||||
@@ -967,6 +1238,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
|
|
||||||
if (counts is not null)
|
if (counts is not null)
|
||||||
{
|
{
|
||||||
|
counts.Clear();
|
||||||
ThreadStaticPools.ReturnUlongIntDictionary(counts);
|
ThreadStaticPools.ReturnUlongIntDictionary(counts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -996,7 +1268,7 @@ internal static partial class PrimeOrderCalculator
|
|||||||
// value will never <= 1 in production code
|
// value will never <= 1 in production code
|
||||||
// if (value <= 1UL)
|
// if (value <= 1UL)
|
||||||
// {
|
// {
|
||||||
// return true;
|
// return true;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
int capacity = Math.Min(primeTargets.Length, exponentTargets.Length);
|
int capacity = Math.Min(primeTargets.Length, exponentTargets.Length);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Buffers;
|
using System.Buffers;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using PerfectNumbers.Core.Cpu;
|
using PerfectNumbers.Core.Cpu;
|
||||||
|
|
||||||
@@ -30,7 +31,6 @@ namespace PerfectNumbers.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[ThreadStatic]
|
[ThreadStatic]
|
||||||
private static MersenneCpuDivisorScanSession? _mersenneCpuDivisorSession;
|
private static MersenneCpuDivisorScanSession? _mersenneCpuDivisorSession;
|
||||||
|
|
||||||
@@ -66,6 +66,18 @@ namespace PerfectNumbers.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[ThreadStatic]
|
||||||
|
private static ArrayPool<bool>? _boolPool;
|
||||||
|
|
||||||
|
public static ArrayPool<bool> BoolPool
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return _boolPool ??= ArrayPool<bool>.Create();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[ThreadStatic]
|
[ThreadStatic]
|
||||||
private static ArrayPool<byte>? _bytePool;
|
private static ArrayPool<byte>? _bytePool;
|
||||||
|
|
||||||
@@ -102,7 +114,6 @@ namespace PerfectNumbers.Core
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[ThreadStatic]
|
[ThreadStatic]
|
||||||
private static List<List<ulong>>? _ulongListPool;
|
private static List<List<ulong>>? _ulongListPool;
|
||||||
|
|
||||||
@@ -110,22 +121,21 @@ namespace PerfectNumbers.Core
|
|||||||
public static List<ulong> RentUlongList(int capacityHint)
|
public static List<ulong> RentUlongList(int capacityHint)
|
||||||
{
|
{
|
||||||
List<List<ulong>>? pool = _ulongListPool;
|
List<List<ulong>>? pool = _ulongListPool;
|
||||||
if (pool is not null && pool.Count > 0)
|
if (pool is not null)
|
||||||
{
|
{
|
||||||
int lastIndex = pool.Count - 1;
|
int poolCount = pool.Count;
|
||||||
List<ulong> list = pool[lastIndex];
|
if (poolCount > 0)
|
||||||
pool.RemoveAt(lastIndex);
|
|
||||||
if (list.Count > 0)
|
|
||||||
{
|
{
|
||||||
list.Clear();
|
int lastIndex = poolCount - 1;
|
||||||
}
|
List<ulong> list = pool[lastIndex];
|
||||||
|
pool.RemoveAt(lastIndex);
|
||||||
|
if (list.Capacity < capacityHint)
|
||||||
|
{
|
||||||
|
list.Capacity = capacityHint;
|
||||||
|
}
|
||||||
|
|
||||||
if (list.Capacity < capacityHint)
|
return list;
|
||||||
{
|
|
||||||
list.Capacity = capacityHint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new List<ulong>(capacityHint);
|
return new List<ulong>(capacityHint);
|
||||||
@@ -151,22 +161,21 @@ namespace PerfectNumbers.Core
|
|||||||
internal static List<PrimeOrderCalculator.PendingEntry> RentPrimeOrderPendingEntryList(int capacityHint)
|
internal static List<PrimeOrderCalculator.PendingEntry> RentPrimeOrderPendingEntryList(int capacityHint)
|
||||||
{
|
{
|
||||||
List<List<PrimeOrderCalculator.PendingEntry>>? pool = _primeOrderPendingEntryListPool;
|
List<List<PrimeOrderCalculator.PendingEntry>>? pool = _primeOrderPendingEntryListPool;
|
||||||
if (pool is not null && pool.Count > 0)
|
if (pool is not null)
|
||||||
{
|
{
|
||||||
int lastIndex = pool.Count - 1;
|
int poolCount = pool.Count;
|
||||||
List<PrimeOrderCalculator.PendingEntry> list = pool[lastIndex];
|
if (poolCount > 0)
|
||||||
pool.RemoveAt(lastIndex);
|
|
||||||
if (list.Count > 0)
|
|
||||||
{
|
{
|
||||||
list.Clear();
|
int lastIndex = poolCount - 1;
|
||||||
}
|
List<PrimeOrderCalculator.PendingEntry> list = pool[lastIndex];
|
||||||
|
pool.RemoveAt(lastIndex);
|
||||||
|
if (list.Capacity < capacityHint)
|
||||||
|
{
|
||||||
|
list.Capacity = capacityHint;
|
||||||
|
}
|
||||||
|
|
||||||
if (list.Capacity < capacityHint)
|
return list;
|
||||||
{
|
|
||||||
list.Capacity = capacityHint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new List<PrimeOrderCalculator.PendingEntry>(capacityHint);
|
return new List<PrimeOrderCalculator.PendingEntry>(capacityHint);
|
||||||
@@ -192,22 +201,22 @@ namespace PerfectNumbers.Core
|
|||||||
public static Stack<ulong> RentUlongStack(int capacityHint)
|
public static Stack<ulong> RentUlongStack(int capacityHint)
|
||||||
{
|
{
|
||||||
List<Stack<ulong>>? pool = _ulongStackPool;
|
List<Stack<ulong>>? pool = _ulongStackPool;
|
||||||
if (pool is not null && pool.Count > 0)
|
if (pool is not null)
|
||||||
{
|
{
|
||||||
int lastIndex = pool.Count - 1;
|
int poolCount = pool.Count;
|
||||||
Stack<ulong> stack = pool[lastIndex];
|
if (poolCount > 0)
|
||||||
pool.RemoveAt(lastIndex);
|
|
||||||
if (stack.Count > 0)
|
|
||||||
{
|
{
|
||||||
stack.Clear();
|
int lastIndex = poolCount - 1;
|
||||||
}
|
Stack<ulong> stack = pool[lastIndex];
|
||||||
|
pool.RemoveAt(lastIndex);
|
||||||
|
|
||||||
if (stack.Count < capacityHint)
|
if (stack.Count < capacityHint)
|
||||||
{
|
{
|
||||||
stack.EnsureCapacity(capacityHint);
|
stack.EnsureCapacity(capacityHint);
|
||||||
}
|
}
|
||||||
|
|
||||||
return stack;
|
return stack;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Stack<ulong>(capacityHint);
|
return new Stack<ulong>(capacityHint);
|
||||||
@@ -233,22 +242,18 @@ namespace PerfectNumbers.Core
|
|||||||
public static Dictionary<ulong, int> RentUlongIntDictionary(int capacityHint)
|
public static Dictionary<ulong, int> RentUlongIntDictionary(int capacityHint)
|
||||||
{
|
{
|
||||||
List<Dictionary<ulong, int>>? pool = _ulongIntDictionaryPool;
|
List<Dictionary<ulong, int>>? pool = _ulongIntDictionaryPool;
|
||||||
if (pool is not null && pool.Count > 0)
|
if (pool is not null)
|
||||||
{
|
{
|
||||||
int lastIndex = pool.Count - 1;
|
int poolCount = pool.Count;
|
||||||
Dictionary<ulong, int> dictionary = pool[lastIndex];
|
if (poolCount > 0)
|
||||||
pool.RemoveAt(lastIndex);
|
|
||||||
if (dictionary.Count > 0)
|
|
||||||
{
|
|
||||||
dictionary.Clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dictionary.Count < capacityHint)
|
|
||||||
{
|
{
|
||||||
|
int lastIndex = poolCount - 1;
|
||||||
|
Dictionary<ulong, int> dictionary = pool[lastIndex];
|
||||||
|
pool.RemoveAt(lastIndex);
|
||||||
dictionary.EnsureCapacity(capacityHint);
|
dictionary.EnsureCapacity(capacityHint);
|
||||||
}
|
|
||||||
|
|
||||||
return dictionary;
|
return dictionary;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Dictionary<ulong, int>(capacityHint);
|
return new Dictionary<ulong, int>(capacityHint);
|
||||||
@@ -280,14 +285,12 @@ namespace PerfectNumbers.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
_factorCountDictionary = null;
|
_factorCountDictionary = null;
|
||||||
dictionary.Clear();
|
|
||||||
return dictionary;
|
return dictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void ReturnFactorCountDictionary(Dictionary<ulong, int> dictionary)
|
public static void ReturnFactorCountDictionary(Dictionary<ulong, int> dictionary)
|
||||||
{
|
{
|
||||||
dictionary.Clear();
|
|
||||||
_factorCountDictionary = dictionary;
|
_factorCountDictionary = dictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,15 +307,41 @@ namespace PerfectNumbers.Core
|
|||||||
}
|
}
|
||||||
|
|
||||||
_factorScratchDictionary = null;
|
_factorScratchDictionary = null;
|
||||||
dictionary.Clear();
|
|
||||||
return dictionary;
|
return dictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static void ReturnFactorScratchDictionary(Dictionary<ulong, int> dictionary)
|
public static void ReturnFactorScratchDictionary(Dictionary<ulong, int> dictionary)
|
||||||
{
|
{
|
||||||
dictionary.Clear();
|
|
||||||
_factorScratchDictionary = dictionary;
|
_factorScratchDictionary = dictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[ThreadStatic]
|
||||||
|
private static ExponentRemainderStepper _exponentRemainderStepper;
|
||||||
|
|
||||||
|
[ThreadStatic]
|
||||||
|
private static bool _hasExponentRemainderStepper;
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal static ExponentRemainderStepper RentExponentStepper(in MontgomeryDivisorData divisorData)
|
||||||
|
{
|
||||||
|
if (_hasExponentRemainderStepper && _exponentRemainderStepper.MatchesDivisor(divisorData))
|
||||||
|
{
|
||||||
|
ExponentRemainderStepper stepper = _exponentRemainderStepper;
|
||||||
|
_hasExponentRemainderStepper = false;
|
||||||
|
stepper.Reset();
|
||||||
|
return stepper;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ExponentRemainderStepper(divisorData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal static void ReturnExponentStepper(ExponentRemainderStepper stepper)
|
||||||
|
{
|
||||||
|
stepper.Reset();
|
||||||
|
_exponentRemainderStepper = stepper;
|
||||||
|
_hasExponentRemainderStepper = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user