From d555e157f66cfb25b4bbd6057654d79c27ca63ce Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sun, 12 Oct 2025 18:23:06 +0200 Subject: [PATCH] Calculation switched to typical O(N) --- .../Gpu/PrimeOrderGpuHeuristics.cs | 88 +++++++++---------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs b/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs index db03894..b403218 100644 --- a/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs +++ b/PerfectNumbers.Core/Gpu/PrimeOrderGpuHeuristics.cs @@ -1812,56 +1812,56 @@ internal static partial class PrimeOrderGpuHeuristics { ulong exponent = exponents[index]; ulong modulus = divisor.Modulus; - ulong nPrime = divisor.NPrime; - ulong result = divisor.MontgomeryOne; - ulong baseValue = divisor.MontgomeryTwo; - ulong remainingExponent = exponent; + // ulong nPrime = divisor.NPrime; + // ulong result = divisor.MontgomeryOne; + // ulong baseValue = divisor.MontgomeryTwo; + // ulong remainingExponent = exponent; - if (remainingExponent == 0UL) - { - remainders[index] = result.MontgomeryMultiply(1UL, modulus, nPrime); - return; - } + // if (remainingExponent == 0UL) + // { + // remainders[index] = result.MontgomeryMultiply(1UL, modulus, nPrime); + // return; + // } - while (remainingExponent != 0UL) - { - if ((remainingExponent & 1UL) != 0UL) - { - result = result.MontgomeryMultiply(baseValue, modulus, nPrime); - } + // while (remainingExponent != 0UL) + // { + // if ((remainingExponent & 1UL) != 0UL) + // { + // result = result.MontgomeryMultiply(baseValue, modulus, nPrime); + // } - remainingExponent >>= 1; - if (remainingExponent == 0UL) - { - break; - } + // remainingExponent >>= 1; + // if (remainingExponent == 0UL) + // { + // break; + // } - // Reusing baseValue to hold base^2 so we avoid allocating another register for the squared term. - baseValue = baseValue.MontgomeryMultiply(baseValue, modulus, nPrime); - } + // // Reusing baseValue to hold base^2 so we avoid allocating another register for the squared term. + // baseValue = baseValue.MontgomeryMultiply(baseValue, modulus, nPrime); + // } - remainders[index] = result.MontgomeryMultiply(1UL, modulus, nPrime); + // remainders[index] = result.MontgomeryMultiply(1UL, modulus, nPrime); - // Plain modular exponentiation without Montgomery reduction kept for reference. - // ulong plainResult = 1UL % modulus; - // ulong plainBase = 2UL % modulus; - // ulong plainExponent = exponent; - // while (plainExponent != 0UL) - // { - // if ((plainExponent & 1UL) != 0UL) - // { - // plainResult = (plainResult * plainBase) % modulus; - // } - // - // plainExponent >>= 1; - // if (plainExponent == 0UL) - // { - // break; - // } - // - // plainBase = (plainBase * plainBase) % modulus; - // } - // remainders[index] = plainResult; + // Plain modular exponentiation without Montgomery reduction kept for reference. + ulong plainResult = 1UL;// % modulus; + ulong plainBase = 2UL;// % modulus; + while (exponent != 0UL) + { + if ((exponent & 1UL) != 0UL) + { + plainResult = (plainResult * plainBase) % modulus; + } + + exponent >>= 1; + if (exponent == 0UL) + { + break; + } + + plainBase = (plainBase * plainBase) % modulus; + } + + remainders[index] = plainResult; } private static Action, GpuUInt128, ArrayView1D> GetPow2ModWideKernel(Accelerator accelerator)