Inline single-use modulo helpers
This commit is contained in:
1 parent
3598f88aaf
commit
3b8a10a20b
5 files changed
+33
-35
No files matched your search
@@ -4,20 +4,16 @@ public class MersenneNumberLucasLehmerCpuTester
|
||||
{
|
||||
public bool IsPrime(ulong exponent)
|
||||
{
|
||||
// Early rejections aligned with incremental/order sieves, but safe for small p:
|
||||
// - 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 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 (
|
||||
(mod3 == 0UL && exponent != 3UL) ||
|
||||
(mod5 == 0UL && exponent != 5UL) ||
|
||||
(mod7 == 0UL && exponent != 7UL) ||
|
||||
(mod11 == 0UL && exponent != 11UL)
|
||||
)
|
||||
// Early rejections aligned with incremental/order sieves, but safe for small p:
|
||||
// - 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 is divisible by divisors specific to numbers ending with 1 or 7, reject fast.
|
||||
if (
|
||||
(exponent.Mod3() == 0UL && exponent != 3UL) ||
|
||||
(exponent.Mod5() == 0UL && exponent != 5UL) ||
|
||||
(exponent.Mod7() == 0UL && exponent != 7UL) ||
|
||||
(exponent.Mod11() == 0UL && exponent != 11UL)
|
||||
)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -97,17 +97,23 @@ public static class PrimesGenerator
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsAllowedForLastOne(uint prime)
|
||||
{
|
||||
uint mod10 = prime % 10U;
|
||||
return mod10 == 1U || mod10 == 3U || mod10 == 9U || prime == 7U || prime == 11U;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsAllowedForLastOne(uint prime)
|
||||
{
|
||||
return (prime % 10U) switch
|
||||
{
|
||||
1U or 3U or 9U => true,
|
||||
_ => prime == 7U || prime == 11U,
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsAllowedForLastSeven(uint prime)
|
||||
{
|
||||
uint mod10 = prime % 10U;
|
||||
return mod10 == 3U || mod10 == 7U || mod10 == 9U || prime == 11U;
|
||||
}
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool IsAllowedForLastSeven(uint prime)
|
||||
{
|
||||
return (prime % 10U) switch
|
||||
{
|
||||
3U or 7U or 9U => true,
|
||||
_ => prime == 11U,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -241,9 +241,7 @@ public static class UInt128Extensions
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod6(this UInt128 value)
|
||||
{
|
||||
ulong mod3 = value.Mod3();
|
||||
ulong key = (mod3 << 1) | ((ulong)value & 1UL);
|
||||
return key switch
|
||||
return ((value.Mod3() << 1) | ((ulong)value & 1UL)) switch
|
||||
{
|
||||
0UL => 0UL,
|
||||
1UL => 3UL,
|
||||
@@ -318,4 +316,4 @@ public static class UInt128Extensions
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,8 +23,7 @@ public static class UIntExtensions
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod6(this uint value)
|
||||
{
|
||||
uint key = (value.Mod3() << 1) | (value & 1U);
|
||||
return Mod6Lookup[(int)key];
|
||||
return Mod6Lookup[(int)(((value.Mod3() << 1) | (value & 1U)))];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
|
||||
@@ -221,8 +221,7 @@ public static class ULongExtensions
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ulong Mod6(this ulong value)
|
||||
{
|
||||
ulong key = (value.Mod3() << 1) | (value & 1UL);
|
||||
return Mod6Lookup[(int)key];
|
||||
return Mod6Lookup[(int)(((value.Mod3() << 1) | (value & 1UL)))];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -647,4 +646,4 @@ public static class ULongExtensions
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user