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)
|
public bool IsPrime(ulong exponent)
|
||||||
{
|
{
|
||||||
// Early rejections aligned with incremental/order sieves, but safe for small p:
|
// Early rejections aligned with incremental/order sieves, but safe for small p:
|
||||||
// - 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();
|
if (
|
||||||
ulong mod5 = exponent.Mod5();
|
(exponent.Mod3() == 0UL && exponent != 3UL) ||
|
||||||
ulong mod7 = exponent.Mod7();
|
(exponent.Mod5() == 0UL && exponent != 5UL) ||
|
||||||
ulong mod11 = exponent.Mod11();
|
(exponent.Mod7() == 0UL && exponent != 7UL) ||
|
||||||
if (
|
(exponent.Mod11() == 0UL && exponent != 11UL)
|
||||||
(mod3 == 0UL && exponent != 3UL) ||
|
)
|
||||||
(mod5 == 0UL && exponent != 5UL) ||
|
|
||||||
(mod7 == 0UL && exponent != 7UL) ||
|
|
||||||
(mod11 == 0UL && exponent != 11UL)
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,17 +97,23 @@ 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,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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,
|
||||||
@@ -318,4 +316,4 @@ public static class UInt128Extensions
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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)]
|
||||||
|
|||||||
@@ -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)]
|
||||||
@@ -647,4 +646,4 @@ public static class ULongExtensions
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user