Split MulHigh extensions into CPU and GPU variants

This commit is contained in:
Marek Lemańczyk committed 2025-10-26 22:42:21 +01:00
1 parent 3d458fc6da
commit 0e1212aa1c
7 files changed
+32 -16

No files matched your search

@@ -89,7 +89,7 @@ public class Mul64Benchmarks
{
ulong leftLow = (ulong)left;
ulong rightLow = (ulong)right;
return ((UInt128)(leftLow * (ulong)(right >> 64) + leftLow.MulHigh(rightLow)) << 64) | (UInt128)(leftLow * rightLow);
return ((UInt128)(leftLow * (ulong)(right >> 64) + leftLow.MulHighCpu(rightLow)) << 64) | (UInt128)(leftLow * rightLow);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -3,6 +3,7 @@ using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using PerfectNumbers.Core;
using PerfectNumbers.Core.Gpu;
namespace EvenPerfectBitScanner.Benchmarks;
@@ -36,10 +37,7 @@ public class MulHighBenchmarks
[Benchmark(Baseline = true)]
public ulong CurrentImplementation()
{
// TODO: Replace ULongExtensions.MulHigh with the UInt128-based helper on CPU paths; it was
// 11–14× faster in the latest benchmarks while the GPU-specific variant remains available
// separately.
return ULongExtensions.MulHigh(Input.X, Input.Y);
return ULongExtensions.MulHighCpu(Input.X, Input.Y);
}
/// <summary>
@@ -107,7 +105,9 @@ public class MulHighBenchmarks
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong MulHighWithGpuUInt128(ulong x, ulong y)
{
return ULongExtensions.MulHighGpuCompatible(x, y);
GpuUInt128 product = new(x);
GpuUInt128.Mul64(ref product, 0UL, y);
return product.High;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -240,7 +240,7 @@ public class MulMod64Benchmarks
ulong left = a % modulus;
ulong right = b % modulus;
ulong low = left * right;
ulong high = left.MulHigh(right);
ulong high = left.MulHighCpu(right);
UInt128 product = ((UInt128)high << 64) | low;
return (ulong)(product % modulus);
}