Consolidate multiply helpers and guard GPU fallbacks

This commit is contained in:
Marek Lemańczyk committed 2025-10-04 21:20:29 +02:00
1 parent 84ebbed98b
commit 6adac6b7a7
10 files changed
+227 -70

No files matched your search

+41
View File
@@ -56,6 +56,47 @@ public static class ULongExtensions
return order;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong MultiplyShiftRight(ulong value, ulong multiplier, int shift)
{
if (shift < 0)
{
throw new ArgumentOutOfRangeException(nameof(shift), "Shift must be non-negative.");
}
UInt128 product = (UInt128)value * multiplier;
return (ulong)(product >> shift);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong MultiplyShiftRightShiftFirst(ulong value, ulong multiplier, int shift)
{
if (shift < 0)
{
throw new ArgumentOutOfRangeException(nameof(shift), "Shift must be non-negative.");
}
if (shift == 0)
{
return MultiplyShiftRight(value, multiplier, 0);
}
if (shift >= 64)
{
return 0UL;
}
ulong high = value >> shift;
ulong mask = (1UL << shift) - 1UL;
ulong low = value & mask;
UInt128 highContribution = (UInt128)high * multiplier;
UInt128 lowContribution = (UInt128)low * multiplier;
UInt128 combined = highContribution + (lowContribution >> shift);
return (ulong)combined;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong FastDiv64(this ulong value, ulong divisor, ulong mul)
{