Reapply modulo helpers and inline fast remainders
This commit is contained in:
1 parent
5db0c4fa95
commit
ca866ffc5b
43 files changed
+982
-134
No files matched your search
@@ -4,57 +4,74 @@ namespace PerfectNumbers.Core;
|
||||
|
||||
public static class UIntExtensions
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod3(this uint value) => PerfectNumbersMath.FastRemainder3(value);
|
||||
private static readonly byte[] Mod6Lookup = { 0, 3, 4, 1, 2, 5 };
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod5(this uint value) => PerfectNumbersMath.FastRemainder5(value);
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod3(this uint value)
|
||||
{
|
||||
uint quotient = (uint)(((ulong)value * 0xAAAAAAABUL) >> 33);
|
||||
return value - quotient * 3U;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod7(this uint value)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod5(this uint value)
|
||||
{
|
||||
uint quotient = (uint)(((ulong)value * 0xCCCCCCCDUL) >> 34);
|
||||
return value - quotient * 5U;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod6(this uint value)
|
||||
{
|
||||
uint key = (value.Mod3() << 1) | (value & 1U);
|
||||
return Mod6Lookup[(int)key];
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod7(this uint value)
|
||||
{
|
||||
uint remainder = 0U;
|
||||
uint temp = value;
|
||||
|
||||
while (temp != 0U)
|
||||
{
|
||||
uint remainder = 0U;
|
||||
uint temp = value;
|
||||
|
||||
while (temp != 0U)
|
||||
{
|
||||
remainder += temp & 7U;
|
||||
temp >>= 3;
|
||||
if (remainder >= 7U)
|
||||
{
|
||||
remainder -= 7U;
|
||||
}
|
||||
}
|
||||
|
||||
return remainder >= 7U ? remainder - 7U : remainder;
|
||||
remainder += temp & 7U;
|
||||
temp >>= 3;
|
||||
if (remainder >= 7U)
|
||||
{
|
||||
remainder -= 7U;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod8(this uint value) => value & 7U;
|
||||
return remainder >= 7U ? remainder - 7U : remainder;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod10(this uint value)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod8(this uint value) => value & 7U;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod10(this uint value)
|
||||
{
|
||||
return value - (uint)(((ulong)value * 0xCCCCCCCDUL) >> 35) * 10U;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod11(this uint value)
|
||||
{
|
||||
uint remainder = 0U;
|
||||
uint temp = value;
|
||||
|
||||
while (temp != 0U)
|
||||
{
|
||||
return value - (uint)(((ulong)value * 0xCCCCCCCDUL) >> 35) * 10U;
|
||||
remainder += temp & 1023U;
|
||||
temp >>= 10;
|
||||
remainder -= 11U * (remainder / 11U);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod11(this uint value)
|
||||
{
|
||||
uint remainder = 0U;
|
||||
uint temp = value;
|
||||
return remainder;
|
||||
}
|
||||
|
||||
while (temp != 0U)
|
||||
{
|
||||
remainder += temp & 1023U;
|
||||
temp >>= 10;
|
||||
remainder -= 11U * (remainder / 11U);
|
||||
}
|
||||
|
||||
return remainder;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod128(this uint value) => value & 127U;
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static uint Mod128(this uint value) => value & 127U;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user