From 0bdf85730d732d74aa85b7f08d538eb2189ae61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Lema=C5=84czyk?= Date: Mon, 29 Sep 2025 23:28:35 +0200 Subject: [PATCH] Tiny optimization --- PerfectNumbers.Core/Gpu/GpuUInt128.cs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/PerfectNumbers.Core/Gpu/GpuUInt128.cs b/PerfectNumbers.Core/Gpu/GpuUInt128.cs index cf5cfb3..b21731f 100644 --- a/PerfectNumbers.Core/Gpu/GpuUInt128.cs +++ b/PerfectNumbers.Core/Gpu/GpuUInt128.cs @@ -22,7 +22,6 @@ public struct GpuUInt128 : IComparable, IEquatable [MethodImpl(MethodImplOptions.AggressiveInlining)] public GpuUInt128(ulong low) { - High = 0UL; Low = low; } @@ -394,20 +393,25 @@ public struct GpuUInt128 : IComparable, IEquatable ulong w1 = xLow * yHigh; ulong w2 = xHigh * yLow; - ulong carry = - (((xLow * yLow) >> 32) + - (uint)w1 + - (uint)w2) >> 32; - return (xHigh * yHigh) + (w1 >> 32) + (w2 >> 32) + carry; + return (xHigh * yHigh) + (w1 >> 32) + (w2 >> 32) + + ( + ( + ((xLow * yLow) >> 32) + + (uint)w1 + + (uint)w2 + ) >> 32 + ); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Mul64(GpuUInt128 other) { // Multiply this.Low (assumed 64-bit value) by full 128-bit other - ulong operand = Low; - Low = operand * other.Low; - High = operand * other.High + MulHigh(operand, other.Low); + ulong operand = Low, + otherLow = other.Low; + + Low = operand * otherLow; + High = operand * other.High + MulHigh(operand, otherLow); } [MethodImpl(MethodImplOptions.AggressiveInlining)]