Adjust CPU Pow2 Montgomery helper for small divisors

This commit is contained in:
Marek Lemańczyk committed 2025-10-20 17:03:36 +02:00
1 parent c726d6de6b
commit 798ae455b1
1 file changed
+7 -6
+7 -6
View File
@@ -16,12 +16,13 @@ public static partial class ULongExtensions
// return keepMontgomery ? divisor.MontgomeryOne : 1UL % modulus;
// }
// There is no point in having extra branch, because in real use-cases it'll barely ever hit.
// Out of all 3_358_944_545 Pow2MontgomeryModWindowedCpu calls, we hit Pow2MontgomeryModSingleBit only 772 times.
// if (exponent <= Pow2WindowFallbackThreshold)
// {
// return Pow2MontgomeryModSingleBit(exponent, divisor, keepMontgomery);
// }
// Small divisors come from the precomputed <= 4,000,000 snapshot and tiny rotation counts
// appear when cycle reductions trigger. Route both cases through the single-bit ladder so the
// CPU path avoids renting the window tables when a cheaper method is available.
if (exponent <= Pow2WindowFallbackThreshold || modulus <= (ulong)PerfectNumberConstants.MaxQForDivisorCycles)
{
return Pow2MontgomeryModSingleBit(exponent, divisor, keepMontgomery);
}
int bitLength = GetPortableBitLengthCpu(exponent);
int windowSize = GetWindowSizeCpu(bitLength);