BigInteger-friendly steppers added. MersenneResidueStepper converted to struct. Manual residue tracking replaced with steppers. Modulo operations replaced with steppers.

This commit is contained in:
mlemanczyk committed 2025-12-07 19:16:45 +01:00
1 parent fbf074ade0
commit 6887111128
9 files changed
+238 -421

No files matched your search

@@ -3,52 +3,34 @@ using PerfectNumbers.Core.Gpu;
namespace PerfectNumbers.Core;
internal struct MersenneDivisorResidueStepper
[method: MethodImpl(MethodImplOptions.AggressiveInlining)]
internal struct MersenneDivisorResidueStepper(ulong prime, in GpuUInt128 step, in GpuUInt128 firstDivisor)
{
private const ushort DecimalMaskWhenLastIsSeven = (1 << 3) | (1 << 7) | (1 << 9);
private const ushort DecimalMaskOtherwise = (1 << 1) | (1 << 3) | (1 << 9);
private readonly ushort decimalMask;
private readonly byte step10;
private readonly byte step8;
private readonly byte step5;
private readonly byte step3;
private readonly byte step7;
private readonly byte step11;
private readonly ushort decimalMask = (prime & 3UL) == 3UL ? DecimalMaskWhenLastIsSeven : DecimalMaskOtherwise;
private readonly byte step10 = ComputeModulo(step, 10, multiplier: 6);
private readonly byte step8 = (byte)(step.Low % 8UL);
private readonly byte step5 = ComputeModulo(step, 5, multiplier: 1);
private readonly byte step3 = ComputeModulo(step, 3, multiplier: 1);
private readonly byte step7 = ComputeModulo(step, 7, multiplier: 2);
private readonly byte step11 = ComputeModulo(step, 11, multiplier: 5);
private readonly byte step13 = ComputeModulo(step, 13, multiplier: 3);
private readonly byte step17 = ComputeModulo(step, 17, multiplier: 1);
private readonly byte step19 = ComputeModulo(step, 19, multiplier: 17);
public byte Remainder10 { get; private set; }
private byte Remainder10 = ComputeModulo(firstDivisor, 10, multiplier: 6);
private byte Remainder8 = (byte)(firstDivisor.Low % 8UL);
private byte Remainder5 = ComputeModulo(firstDivisor, 5, multiplier: 1);
private byte Remainder3 = ComputeModulo(firstDivisor, 3, multiplier: 1);
private byte Remainder7 = ComputeModulo(firstDivisor, 7, multiplier: 2);
private byte Remainder11 = ComputeModulo(firstDivisor, 11, multiplier: 5);
private byte Remainder13 = ComputeModulo(firstDivisor, 13, multiplier: 3);
private byte Remainder17 = ComputeModulo(firstDivisor, 17, multiplier: 1);
private byte Remainder19 = ComputeModulo(firstDivisor, 19, multiplier: 17);
public byte Remainder8 { get; private set; }
public byte Remainder5 { get; private set; }
public byte Remainder3 { get; private set; }
public byte Remainder7 { get; private set; }
public byte Remainder11 { get; private set; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MersenneDivisorResidueStepper(ulong prime, in GpuUInt128 step, in GpuUInt128 firstDivisor)
{
decimalMask = (prime & 3UL) == 3UL ? DecimalMaskWhenLastIsSeven : DecimalMaskOtherwise;
step10 = ComputeModulo(step, 10, multiplier: 6);
step8 = (byte)(step.Low % 8UL);
step5 = ComputeModulo(step, 5, multiplier: 1);
step3 = ComputeModulo(step, 3, multiplier: 1);
step7 = ComputeModulo(step, 7, multiplier: 2);
step11 = ComputeModulo(step, 11, multiplier: 5);
Remainder10 = ComputeModulo(firstDivisor, 10, multiplier: 6);
Remainder8 = (byte)(firstDivisor.Low % 8UL);
Remainder5 = ComputeModulo(firstDivisor, 5, multiplier: 1);
Remainder3 = ComputeModulo(firstDivisor, 3, multiplier: 1);
Remainder7 = ComputeModulo(firstDivisor, 7, multiplier: 2);
Remainder11 = ComputeModulo(firstDivisor, 11, multiplier: 5);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly bool IsAdmissible()
{
if (((decimalMask >> Remainder10) & 1) == 0)
@@ -61,7 +43,7 @@ internal struct MersenneDivisorResidueStepper
return false;
}
if (Remainder3 == 0 || Remainder5 == 0 || Remainder7 == 0 || Remainder11 == 0)
if (Remainder3 == 0 || Remainder5 == 0 || Remainder7 == 0 || Remainder11 == 0 || Remainder13 == 0 || Remainder17 == 0 || Remainder19 == 0)
{
return false;
}
@@ -78,6 +60,9 @@ internal struct MersenneDivisorResidueStepper
Remainder3 = AddMod(Remainder3, step3, 3);
Remainder7 = AddMod(Remainder7, step7, 7);
Remainder11 = AddMod(Remainder11, step11, 11);
Remainder13 = AddMod(Remainder13, step13, 13);
Remainder17 = AddMod(Remainder17, step17, 17);
Remainder19 = AddMod(Remainder19, step19, 19);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]