Trim unreachable MulMod conditionals

This commit is contained in:
Marek Lemańczyk committed 2025-10-20 11:48:10 +02:00
1 parent 94323c2e8c
commit c892e1c30c
2 files changed
+27 -14

No files matched your search

+17 -7
View File
@@ -677,15 +677,25 @@ public static class UInt128Extensions
// return UInt128.Zero; // return UInt128.Zero;
// } // }
value %= modulus; // value and addend already lie below the modulus throughout the divisor-by-divisor CPU flow,
addend %= modulus; // so skip the redundant reductions the old helper performed.
if (addend == UInt128.Zero) // value %= modulus;
{ // addend %= modulus;
return value; // Odd moduli keep 2^delta from landing on zero, leaving this branch inactive in the scan.
} // if (addend == UInt128.Zero)
// {
// return value;
// }
UInt128 threshold = modulus - addend; UInt128 threshold = modulus - addend;
return value >= threshold ? value - threshold : value + addend; if (value >= threshold)
{
return value - threshold;
}
UInt128 sum = value + addend;
// The sum stays below the modulus under the threshold guard above, so no wraparound occurs.
return sum;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
+10 -7
View File
@@ -464,17 +464,20 @@ public static partial class ULongExtensions
// return 0UL; // return 0UL;
// } // }
// The Pollard Rho walks keep both operands below the modulus, so skip re-folding them here.
UInt128 sum = (UInt128)value + addend; UInt128 sum = (UInt128)value + addend;
UInt128 mod = modulus; if (sum < modulus)
if (sum >= mod)
{ {
sum -= mod; return (ulong)sum;
if (sum >= mod)
{
sum -= mod;
}
} }
sum -= modulus;
// After one subtraction the sum always falls below the modulus on this path, making the old guard redundant.
// if (sum >= modulus)
// {
// sum -= modulus;
// }
return (ulong)sum; return (ulong)sum;
} }