Reuse local variables across modulo helpers

This commit is contained in:
Marek Lemańczyk committed 2025-09-22 19:54:59 +02:00
1 parent 3b8a10a20b
commit 762d205e86
3 files changed
+178 -97

No files matched your search

+34 -30
View File
@@ -121,44 +121,48 @@ public static class UInt128Extensions
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Mod10_8_5_3(this UInt128 value, out ulong mod10, out ulong mod8, out ulong mod5, out ulong mod3)
{
UInt128 temp = value;
uint byteSum = 0U;
public static void Mod10_8_5_3(this UInt128 value, out ulong mod10, out ulong mod8, out ulong mod5, out ulong mod3)
{
UInt128 temp = UInt128.Zero;
uint byteSum = 0U;
byte current = 0;
uint accumulator = 0U;
do
{
byteSum += (byte)temp;
temp >>= 8;
}
while (temp != UInt128.Zero);
temp = value;
do
{
current = (byte)temp;
byteSum += current;
temp >>= 8;
}
while (temp != UInt128.Zero);
mod8 = (ulong)value & 7UL;
uint mod5Value = byteSum.Mod5();
uint mod3Value = byteSum.Mod3();
mod8 = (ulong)value & 7UL;
accumulator = byteSum.Mod5();
mod5 = accumulator;
mod10 = (mod8 & 1UL) == 0UL
? mod5Value switch
? accumulator switch
{
0U => 0UL,
1U => 6UL,
2U => 2UL,
3U => 8UL,
_ => 4UL,
}
: mod5Value switch
{
0U => 5UL,
1U => 1UL,
2U => 7UL,
3U => 3UL,
_ => 9UL,
};
2U => 2UL,
3U => 8UL,
_ => 4UL,
}
: accumulator switch
{
0U => 5UL,
1U => 1UL,
2U => 7UL,
3U => 3UL,
_ => 9UL,
};
mod5 = mod5Value;
mod3 = mod3Value;
}
// Reusing accumulator to hold the mod 3 reduction now that the mod 5 value has been stored.
accumulator = byteSum.Mod3();
mod3 = accumulator;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static UInt128 Pow(this UInt128 value, ulong exponent)