Document unreachable MulMod guards

This commit is contained in:
Marek Lemańczyk committed 2025-10-20 11:48:07 +02:00
1 parent 34240ab3e9
commit 94323c2e8c
2 files changed
+26 -26

No files matched your search

+11 -14
View File
@@ -671,10 +671,11 @@ public static class UInt128Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128 AddMod(this UInt128 value, UInt128 addend, UInt128 modulus) public static UInt128 AddMod(this UInt128 value, UInt128 addend, UInt128 modulus)
{ {
if (modulus <= UInt128.One) // The by-divisor CPU path never supplies moduli below 2, so this guard stays disabled.
{ // if (modulus <= UInt128.One)
return UInt128.Zero; // {
} // return UInt128.Zero;
// }
value %= modulus; value %= modulus;
addend %= modulus; addend %= modulus;
@@ -684,21 +685,17 @@ public static class UInt128Extensions
} }
UInt128 threshold = modulus - addend; UInt128 threshold = modulus - addend;
if (value >= threshold) return value >= threshold ? value - threshold : value + addend;
{
return value - threshold;
}
return value + addend;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128 SubtractOneMod(this UInt128 value, UInt128 modulus) public static UInt128 SubtractOneMod(this UInt128 value, UInt128 modulus)
{ {
if (modulus <= UInt128.One) // The divisor scan never observes moduli below 2, so leave this guard documented but inactive.
{ // if (modulus <= UInt128.One)
return UInt128.Zero; // {
} // return UInt128.Zero;
// }
if (value == UInt128.Zero) if (value == UInt128.Zero)
{ {
+15 -12
View File
@@ -458,10 +458,11 @@ public static partial class ULongExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong AddMod64(this ulong value, ulong addend, ulong modulus) public static ulong AddMod64(this ulong value, ulong addend, ulong modulus)
{ {
if (modulus <= 1UL) // The CPU factoring path never presents moduli below 2, so leave this guard documented but disabled.
{ // if (modulus <= 1UL)
return 0UL; // {
} // return 0UL;
// }
UInt128 sum = (UInt128)value + addend; UInt128 sum = (UInt128)value + addend;
UInt128 mod = modulus; UInt128 mod = modulus;
@@ -480,10 +481,11 @@ public static partial class ULongExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong FoldMontgomery(ulong value, ulong modulus) private static ulong FoldMontgomery(ulong value, ulong modulus)
{ {
if (modulus <= 1UL) // Moduli in the divisor scan always exceed 1, so retain the documentation and skip the runtime guard.
{ // if (modulus <= 1UL)
return 0UL; // {
} // return 0UL;
// }
if (value < modulus) if (value < modulus)
{ {
@@ -508,10 +510,11 @@ public static partial class ULongExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong ModPow64(this ulong value, ulong exponent, ulong modulus) public static ulong ModPow64(this ulong value, ulong exponent, ulong modulus)
{ {
if (modulus <= 1UL) // Moduli below 2 do not occur in the even-perfect divisor scans, so keep the guard commented for clarity.
{ // if (modulus <= 1UL)
return 0UL; // {
} // return 0UL;
// }
ulong result = 1UL; ulong result = 1UL;
value = FoldMontgomery(value, modulus); value = FoldMontgomery(value, modulus);