Inline single-use modulo helpers

This commit is contained in:
Marek Lemańczyk committed 2025-09-22 19:54:54 +02:00
1 parent 3598f88aaf
commit 3b8a10a20b
5 files changed
+17 -19

No files matched your search

@@ -8,15 +8,11 @@ public class MersenneNumberLucasLehmerCpuTester
// - If 3 | p and p != 3, then 7 | M_p -> composite. // - If 3 | p and p != 3, then 7 | M_p -> composite.
// - If p ≡ 1 (mod 4) and p shares a factor with (p-1), reject fast. // - If p ≡ 1 (mod 4) and p shares a factor with (p-1), reject fast.
// - If p is divisible by divisors specific to numbers ending with 1 or 7, reject fast. // - If p is divisible by divisors specific to numbers ending with 1 or 7, reject fast.
ulong mod3 = exponent.Mod3();
ulong mod5 = exponent.Mod5();
ulong mod7 = exponent.Mod7();
ulong mod11 = exponent.Mod11();
if ( if (
(mod3 == 0UL && exponent != 3UL) || (exponent.Mod3() == 0UL && exponent != 3UL) ||
(mod5 == 0UL && exponent != 5UL) || (exponent.Mod5() == 0UL && exponent != 5UL) ||
(mod7 == 0UL && exponent != 7UL) || (exponent.Mod7() == 0UL && exponent != 7UL) ||
(mod11 == 0UL && exponent != 11UL) (exponent.Mod11() == 0UL && exponent != 11UL)
) )
{ {
return false; return false;
+10 -4
View File
@@ -100,14 +100,20 @@ public static class PrimesGenerator
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsAllowedForLastOne(uint prime) private static bool IsAllowedForLastOne(uint prime)
{ {
uint mod10 = prime % 10U; return (prime % 10U) switch
return mod10 == 1U || mod10 == 3U || mod10 == 9U || prime == 7U || prime == 11U; {
1U or 3U or 9U => true,
_ => prime == 7U || prime == 11U,
};
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsAllowedForLastSeven(uint prime) private static bool IsAllowedForLastSeven(uint prime)
{ {
uint mod10 = prime % 10U; return (prime % 10U) switch
return mod10 == 3U || mod10 == 7U || mod10 == 9U || prime == 11U; {
3U or 7U or 9U => true,
_ => prime == 11U,
};
} }
} }
+1 -3
View File
@@ -241,9 +241,7 @@ public static class UInt128Extensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Mod6(this UInt128 value) public static ulong Mod6(this UInt128 value)
{ {
ulong mod3 = value.Mod3(); return ((value.Mod3() << 1) | ((ulong)value & 1UL)) switch
ulong key = (mod3 << 1) | ((ulong)value & 1UL);
return key switch
{ {
0UL => 0UL, 0UL => 0UL,
1UL => 3UL, 1UL => 3UL,
+1 -2
View File
@@ -23,8 +23,7 @@ public static class UIntExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Mod6(this uint value) public static uint Mod6(this uint value)
{ {
uint key = (value.Mod3() << 1) | (value & 1U); return Mod6Lookup[(int)(((value.Mod3() << 1) | (value & 1U)))];
return Mod6Lookup[(int)key];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
+1 -2
View File
@@ -221,8 +221,7 @@ public static class ULongExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Mod6(this ulong value) public static ulong Mod6(this ulong value)
{ {
ulong key = (value.Mod3() << 1) | (value & 1UL); return Mod6Lookup[(int)(((value.Mod3() << 1) | (value & 1UL)))];
return Mod6Lookup[(int)key];
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]