Small refactoring
This commit is contained in:
1 parent
412ce4fd50
commit
2c2bc62cb2
3 files changed
+80
-37
No files matched your search
@@ -12,11 +12,11 @@ public sealed class MersenneResidueAutomaton
|
||||
private readonly UInt128 _step; // 2*p
|
||||
private UInt128 _currentQ; // current q value
|
||||
|
||||
// Cached residues for current q
|
||||
public ulong Mod10R { get; private set; }
|
||||
public ulong Mod8R { get; private set; }
|
||||
public ulong Mod3R { get; private set; }
|
||||
public ulong Mod5R { get; private set; }
|
||||
// Cached residues for current q
|
||||
public ulong Mod10R;
|
||||
public ulong Mod8R;
|
||||
public ulong Mod3R;
|
||||
public ulong Mod5R;
|
||||
|
||||
// Cached residues for step (2*p mod m)
|
||||
private readonly ulong _step10;
|
||||
@@ -27,18 +27,18 @@ public sealed class MersenneResidueAutomaton
|
||||
public MersenneResidueAutomaton(ulong exponent)
|
||||
{
|
||||
_step = (UInt128)exponent << 1; // 2 * p
|
||||
_currentQ = _step + 1UL; // start at k = 1
|
||||
_currentQ = _step + UInt128.One; // start at k = 1
|
||||
|
||||
// init residues
|
||||
// init residues
|
||||
Mod10R = _currentQ.Mod10();
|
||||
Mod8R = Mod8(_currentQ);
|
||||
Mod3R = Mod3(_currentQ);
|
||||
Mod5R = Mod5(_currentQ);
|
||||
Mod8R = _currentQ.Mod8();
|
||||
Mod3R = _currentQ.Mod3();
|
||||
Mod5R = _currentQ.Mod5();
|
||||
|
||||
_step10 = _step.Mod10().Mod10();
|
||||
_step8 = (ulong)(_step & 7UL); // % 8
|
||||
_step3 = Mod3(_step);
|
||||
_step5 = Mod5(_step);
|
||||
_step8 = _step.Mod8();
|
||||
_step3 = _step.Mod3();
|
||||
_step5 = _step.Mod5();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -71,29 +71,6 @@ public sealed class MersenneResidueAutomaton
|
||||
}
|
||||
Mod5R = r5;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong Mod8(UInt128 value) => (ulong)value & 7UL;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong Mod3(UInt128 value)
|
||||
{
|
||||
// 2^64 ≡ 1 (mod 3)
|
||||
ulong high = (ulong)(value >> 64);
|
||||
ulong low = (ulong)value;
|
||||
ulong rem = (low % 3UL) + (high % 3UL);
|
||||
return rem >= 3UL ? rem - 3UL : rem;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static ulong Mod5(UInt128 value)
|
||||
{
|
||||
// 2^64 ≡ 1 (mod 5)
|
||||
ulong high = (ulong)(value >> 64);
|
||||
ulong low = (ulong)value;
|
||||
ulong rem = (low % 5UL) + (high % 5UL);
|
||||
return rem >= 5UL ? rem - 5UL : rem;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user