Use stack buffers for CPU exponent lowering and add MutableUInt128

This commit is contained in:
Marek Lemańczyk committed 2025-10-11 23:13:12 +02:00
1 parent 2da549fe8c
commit 9d62278bb3
2 files changed
+177 -54

No files matched your search

+69 -54
View File
@@ -314,69 +314,84 @@ internal static partial class PrimeOrderCalculator
return order;
}
[ThreadStatic]
private static ArrayPool<FactorEntry>? _factorPool;
private static ArrayPool<FactorEntry> FactorPool => _factorPool ??= ArrayPool<FactorEntry>.Create();
[ThreadStatic]
private static ArrayPool<FactorEntry>? _factorPool;
private static ArrayPool<FactorEntry> FactorPool => _factorPool ??= ArrayPool<FactorEntry>.Create();
[ThreadStatic]
private static ArrayPool<ulong>? _ulongPool;
private static ArrayPool<ulong> ULongPool => _ulongPool ??= ArrayPool<ulong>.Create();
private const int FactorEntryStackThreshold = 32;
private static ulong ExponentLoweringCpu(ulong order, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData)
{
ArrayPool<FactorEntry> factorPool = FactorPool;
[ThreadStatic]
private static ArrayPool<ulong>? _ulongPool;
private static ArrayPool<ulong> ULongPool => _ulongPool ??= ArrayPool<ulong>.Create();
FactorEntry[]? tempArray = null;
try
{
ReadOnlySpan<FactorEntry> factorSpan = factors.Factors;
int length = factors.Count;
tempArray = factorPool.Rent(length + 1);
Span<FactorEntry> buffer = tempArray;
factorSpan.CopyTo(buffer);
private static ulong ExponentLoweringCpu(ulong order, ulong prime, PartialFactorResult factors, in MontgomeryDivisorData divisorData)
{
ArrayPool<FactorEntry> factorPool = FactorPool;
bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factors.Cofactor);
// bool isPrime = PrimeTester.IsPrimeInternal(factors.Cofactor, CancellationToken.None);
if (!factors.FullyFactored && factors.Cofactor > 1UL && isPrime)
{
buffer[length] = new FactorEntry(factors.Cofactor);
length++;
}
FactorEntry[]? tempArray = null;
try
{
ReadOnlySpan<FactorEntry> factorSpan = factors.Factors;
int length = factors.Count;
int capacity = length + 1;
buffer.Slice(0, length).Sort(static (a, b) => a.Value.CompareTo(b.Value));
Span<FactorEntry> buffer;
if (capacity <= FactorEntryStackThreshold)
{
buffer = stackalloc FactorEntry[capacity];
}
else
{
tempArray = factorPool.Rent(capacity);
buffer = tempArray.AsSpan(0, capacity);
}
for (int i = 0; i < length; i++)
{
ulong primeFactor = buffer[i].Value;
int exponent = buffer[i].Exponent;
for (int iteration = 0; iteration < exponent; iteration++)
{
if ((order % primeFactor) != 0UL)
{
break;
}
factorSpan.CopyTo(buffer);
ulong reduced = order / primeFactor;
if (Pow2EqualsOneCpu(reduced, prime, divisorData))
{
order = reduced;
continue;
}
bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factors.Cofactor);
// bool isPrime = PrimeTester.IsPrimeInternal(factors.Cofactor, CancellationToken.None);
if (!factors.FullyFactored && factors.Cofactor > 1UL && isPrime)
{
buffer[length] = new FactorEntry(factors.Cofactor);
length++;
}
break;
}
}
buffer = buffer.Slice(0, length); // Reuse the same span restricted to populated entries.
buffer.Sort(static (a, b) => a.Value.CompareTo(b.Value));
return order;
}
finally
{
if (tempArray is not null)
{
factorPool.Return(tempArray, clearArray: false);
}
}
}
for (int i = 0; i < buffer.Length; i++)
{
FactorEntry entry = buffer[i];
ulong primeFactor = entry.Value;
int exponent = entry.Exponent;
for (int iteration = 0; iteration < exponent; iteration++)
{
if ((order % primeFactor) != 0UL)
{
break;
}
ulong reduced = order / primeFactor;
if (Pow2EqualsOneCpu(reduced, prime, divisorData))
{
order = reduced;
continue;
}
break;
}
}
return order;
}
finally
{
if (tempArray is not null)
{
factorPool.Return(tempArray, clearArray: false);
}
}
}
private static bool TryConfirmOrderCpu(ulong prime, ulong order, in MontgomeryDivisorData divisorData, ref PartialFactorResult orderFactors)
{