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
+129 -6

No files matched your search

+108
View File
@@ -0,0 +1,108 @@
using System;
using System.Numerics;
namespace PerfectNumbers.Core;
/// <summary>
/// Mutable helper for 128-bit arithmetic derived from the .NET runtime UInt128 implementation.
/// Portions adapted from https://github.com/dotnet/runtime/blob/v8.0.0/src/libraries/System.Private.CoreLib/src/System/UInt128.cs.
/// </summary>
internal sealed class MutableUInt128
{
public ulong High { get; private set; }
public ulong Low { get; private set; }
public MutableUInt128()
{
}
public MutableUInt128(ulong value)
{
Low = value;
}
public MutableUInt128(ulong high, ulong low)
{
High = high;
Low = low;
}
public MutableUInt128(UInt128 value)
{
Set(value);
}
public void Set(UInt128 value)
{
High = (ulong)(value >> 64);
Low = (ulong)value;
}
public void Set(ulong high, ulong low)
{
High = high;
Low = low;
}
public UInt128 ToUInt128() => ((UInt128)High << 64) | Low;
public void Add(ulong value)
{
ulong low = Low + value;
if (low < value)
{
High++;
}
Low = low;
}
public void Add(in MutableUInt128 value)
{
ulong low = Low + value.Low;
ulong carry = low < Low ? 1UL : 0UL;
ulong high = High + value.High + carry;
Low = low;
High = high;
}
public void Subtract(ulong value)
{
ulong low = Low;
ulong result = low - value;
ulong borrow = result > low ? 1UL : 0UL;
Low = result;
High -= borrow;
}
public void Subtract(in MutableUInt128 value)
{
ulong low = Low;
ulong result = low - value.Low;
ulong borrow = low < value.Low ? 1UL : 0UL;
Low = result;
High = High - value.High - borrow;
}
public void Multiply(ulong value)
{
ulong high = High;
ulong low = Low;
ulong highContribution = Math.BigMul(low, value, out ulong newLow);
Math.BigMul(high, value, out ulong shiftedHigh);
ulong newHigh = shiftedHigh + highContribution;
Low = newLow;
High = newHigh;
}
public void MultiplyAdd(ulong multiplier, ulong addend)
{
Multiply(multiplier);
Add(addend);
}
}
+21 -6
View File
@@ -318,6 +318,8 @@ internal static partial class PrimeOrderCalculator
private static ArrayPool<FactorEntry>? _factorPool; private static ArrayPool<FactorEntry>? _factorPool;
private static ArrayPool<FactorEntry> FactorPool => _factorPool ??= ArrayPool<FactorEntry>.Create(); private static ArrayPool<FactorEntry> FactorPool => _factorPool ??= ArrayPool<FactorEntry>.Create();
private const int FactorEntryStackThreshold = 32;
[ThreadStatic] [ThreadStatic]
private static ArrayPool<ulong>? _ulongPool; private static ArrayPool<ulong>? _ulongPool;
private static ArrayPool<ulong> ULongPool => _ulongPool ??= ArrayPool<ulong>.Create(); private static ArrayPool<ulong> ULongPool => _ulongPool ??= ArrayPool<ulong>.Create();
@@ -331,8 +333,19 @@ internal static partial class PrimeOrderCalculator
{ {
ReadOnlySpan<FactorEntry> factorSpan = factors.Factors; ReadOnlySpan<FactorEntry> factorSpan = factors.Factors;
int length = factors.Count; int length = factors.Count;
tempArray = factorPool.Rent(length + 1); int capacity = length + 1;
Span<FactorEntry> buffer = tempArray;
Span<FactorEntry> buffer;
if (capacity <= FactorEntryStackThreshold)
{
buffer = stackalloc FactorEntry[capacity];
}
else
{
tempArray = factorPool.Rent(capacity);
buffer = tempArray.AsSpan(0, capacity);
}
factorSpan.CopyTo(buffer); factorSpan.CopyTo(buffer);
bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factors.Cofactor); bool isPrime = Open.Numeric.Primes.Prime.Numbers.IsPrime(factors.Cofactor);
@@ -343,12 +356,14 @@ internal static partial class PrimeOrderCalculator
length++; length++;
} }
buffer.Slice(0, length).Sort(static (a, b) => a.Value.CompareTo(b.Value)); buffer = buffer.Slice(0, length); // Reuse the same span restricted to populated entries.
buffer.Sort(static (a, b) => a.Value.CompareTo(b.Value));
for (int i = 0; i < length; i++) for (int i = 0; i < buffer.Length; i++)
{ {
ulong primeFactor = buffer[i].Value; FactorEntry entry = buffer[i];
int exponent = buffer[i].Exponent; ulong primeFactor = entry.Value;
int exponent = entry.Exponent;
for (int iteration = 0; iteration < exponent; iteration++) for (int iteration = 0; iteration < exponent; iteration++)
{ {
if ((order % primeFactor) != 0UL) if ((order % primeFactor) != 0UL)