From 60c8dbf6d6abb5f68947461981c52ac68f187efb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Sat, 11 Oct 2025 23:13:17 +0200 Subject: [PATCH] Make MutableUInt128 a struct and reuse it in Pollard Rho --- PerfectNumbers.Core/MutableUInt128.cs | 79 +++++++++++++++++---- PerfectNumbers.Core/PrimeOrderCalculator.cs | 11 ++- 2 files changed, 74 insertions(+), 16 deletions(-) diff --git a/PerfectNumbers.Core/MutableUInt128.cs b/PerfectNumbers.Core/MutableUInt128.cs index 7922ec1..1b53b11 100644 --- a/PerfectNumbers.Core/MutableUInt128.cs +++ b/PerfectNumbers.Core/MutableUInt128.cs @@ -1,5 +1,6 @@ using System; using System.Numerics; +using System.Runtime.CompilerServices; namespace PerfectNumbers.Core; @@ -7,17 +8,14 @@ namespace PerfectNumbers.Core; /// 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. /// -internal sealed class MutableUInt128 +internal struct MutableUInt128 { - public ulong High { get; private set; } - public ulong Low { get; private set; } - - public MutableUInt128() - { - } + public ulong High; + public ulong Low; public MutableUInt128(ulong value) { + High = 0UL; Low = value; } @@ -29,23 +27,35 @@ internal sealed class MutableUInt128 public MutableUInt128(UInt128 value) { - Set(value); + High = (ulong)(value >> 64); + Low = (ulong)value; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Set(ulong value) + { + High = 0UL; + Low = value; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Set(UInt128 value) { High = (ulong)(value >> 64); Low = (ulong)value; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Set(ulong high, ulong low) { High = high; Low = low; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public UInt128 ToUInt128() => ((UInt128)High << 64) | Low; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(ulong value) { ulong low = Low + value; @@ -57,6 +67,7 @@ internal sealed class MutableUInt128 Low = low; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(in MutableUInt128 value) { ulong low = Low + value.Low; @@ -67,6 +78,7 @@ internal sealed class MutableUInt128 High = high; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Subtract(ulong value) { ulong low = Low; @@ -77,6 +89,7 @@ internal sealed class MutableUInt128 High -= borrow; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Subtract(in MutableUInt128 value) { ulong low = Low; @@ -87,22 +100,62 @@ internal sealed class MutableUInt128 High = High - value.High - borrow; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Multiply(ulong value) { - ulong high = High; - ulong low = Low; + ulong currentHigh = High; + ulong currentLow = Low; - ulong highContribution = Math.BigMul(low, value, out ulong newLow); - Math.BigMul(high, value, out ulong shiftedHigh); - ulong newHigh = shiftedHigh + highContribution; + ulong carry = Multiply64(currentLow, value, out ulong newLow); + Multiply64(currentHigh, value, out ulong shiftedHigh); + ulong newHigh = shiftedHigh + carry; Low = newLow; High = newHigh; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private static ulong Multiply64(ulong left, ulong right, out ulong low) + { + const ulong Mask32 = 0xFFFF_FFFFUL; + + ulong leftLow = left & Mask32; + ulong leftHigh = left >> 32; + ulong rightLow = right & Mask32; + ulong rightHigh = right >> 32; + + ulong lowLow = leftLow * rightLow; + ulong lowHigh = leftLow * rightHigh; + ulong highLow = leftHigh * rightLow; + ulong highHigh = leftHigh * rightHigh; + + ulong carry = (lowLow >> 32) + (lowHigh & Mask32) + (highLow & Mask32); + ulong high = highHigh + (lowHigh >> 32) + (highLow >> 32) + (carry >> 32); + + low = ((carry & Mask32) << 32) | (lowLow & Mask32); + return high; + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void MultiplyAdd(ulong multiplier, ulong addend) { Multiply(multiplier); Add(addend); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ulong Mod(ulong modulus) + { + if (modulus == 0UL) + { + throw new DivideByZeroException(); + } + + if (High == 0UL) + { + return Low % modulus; + } + + return (ulong)(ToUInt128() % modulus); + } } diff --git a/PerfectNumbers.Core/PrimeOrderCalculator.cs b/PerfectNumbers.Core/PrimeOrderCalculator.cs index 7df88c5..f139cee 100644 --- a/PerfectNumbers.Core/PrimeOrderCalculator.cs +++ b/PerfectNumbers.Core/PrimeOrderCalculator.cs @@ -1,7 +1,6 @@ using System.Buffers; using System.Buffers.Binary; using System.Diagnostics; -using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using PerfectNumbers.Core.Gpu; @@ -73,6 +72,9 @@ internal static partial class PrimeOrderCalculator [ThreadStatic] private static int s_divisorCycleSuppressionDepth; + [ThreadStatic] + private static MutableUInt128 s_pollardRhoPolynomialBuffer; + private static bool IsGpuHeuristicDevice => s_pow2ModeInitialized && s_deviceMode == PrimeOrderHeuristicDevice.Gpu; private readonly struct Pow2ModeScope @@ -1326,8 +1328,11 @@ internal static partial class PrimeOrderCalculator [MethodImpl(MethodImplOptions.AggressiveInlining)] private static ulong AdvancePolynomial(ulong x, ulong c, ulong modulus) { - UInt128 value = (UInt128)x * x + c; - return (ulong)(value % modulus); + ref MutableUInt128 polynomial = ref s_pollardRhoPolynomialBuffer; + polynomial.Set(x); + polynomial.Multiply(x); + polynomial.Add(c); + return polynomial.Mod(modulus); } private static ulong BinaryGcd(ulong a, ulong b)