MulAdd optimized

This commit is contained in:
mlemanczyk committed 2025-10-12 20:18:35 +02:00
1 parent 6c4fa7fcf6
commit 0ff93e1d09
3 files changed
+67 -65

No files matched your search

@@ -27,34 +27,41 @@ public class MutableUInt128MultiplyAddBenchmarks
return ((UInt128)high << 64) | low; return ((UInt128)high << 64) | low;
} }
/// <summary> private static void MultiplyAdd(ref MutableUInt128 source, ulong multiplier, ulong addend)
/// Baseline MultiplyAdd implementation that chains Multiply and Add; measured 4.59 ns with mixed operands,
/// 4.68 ns when both limbs and inputs are near their maxima, and 4.49 ns with tiny operands.
/// </summary>
/// <remarks>
/// Observed means: MixedOperands 4.593 ns, NearMaxOperands 4.681 ns, TinyOperands 4.487 ns.
/// </remarks>
[Benchmark(Baseline = true)]
public UInt128 MultiplyAddChained()
{ {
MutableUInt128 value = new(Input.InitialValue); source.Mul(multiplier);
value.MultiplyAdd(Input.Multiplier, Input.Addend); source.Add(addend);
return value.ToUInt128();
} }
/// <summary> /// <summary>
/// Fully inlined MultiplyAdd implementation that kept all arithmetic inside the method; landed at 4.11 ns on mixed operands, /// Baseline MultiplyAdd implementation that chains Multiply and Add; measured 3.370 ns with mixed operands,
/// 4.25 ns on near-max inputs, and 4.27 ns on tiny operands. /// 3.361 ns when both limbs and inputs are near their maxima, and 3.368 ns with tiny operands.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// Observed means: MixedOperands 4.109 ns, NearMaxOperands 4.253 ns, TinyOperands 4.271 ns. /// Observed means: MixedOperands 3.370 ns, NearMaxOperands 3.361 ns, TinyOperands 3.368 ns.
/// </remarks> /// </remarks>
[Benchmark] [Benchmark(Baseline = true)]
public UInt128 MultiplyAddFullyInlined() public MutableUInt128 MultiplyAddChained()
{ {
MutableUInt128 value = new(Input.InitialValue); MutableUInt128 value = new(Input.InitialValue);
value.MultiplyAddInline(Input.Multiplier, Input.Addend); MultiplyAdd(ref value, Input.Multiplier, Input.Addend);
return value.ToUInt128(); return value;
}
/// <summary>
/// Fully inlined MultiplyAdd implementation that kept all arithmetic inside the method; landed at 3.175 ns on mixed operands,
/// 3.177 ns on near-max inputs, and 3.178 ns on tiny operands.
/// </summary>
/// <remarks>
/// Observed means: MixedOperands 3.175 ns, NearMaxOperands 3.177 ns, TinyOperands 3.178 ns.
/// Status: Implemented
/// </remarks>
[Benchmark]
public MutableUInt128 MultiplyAddFullyInlined()
{
MutableUInt128 value = new(Input.InitialValue);
value.MulAdd(Input.Multiplier, Input.Addend);
return value;
} }
public readonly record struct MultiplyAddInput(UInt128 InitialValue, ulong Multiplier, ulong Addend, string Name) public readonly record struct MultiplyAddInput(UInt128 InitialValue, ulong Multiplier, ulong Addend, string Name)
+40 -45
View File
@@ -8,7 +8,7 @@ namespace PerfectNumbers.Core;
/// Mutable helper for 128-bit arithmetic derived from the .NET runtime UInt128 implementation. /// Mutable helper for 128-bit arithmetic derived from the .NET runtime UInt128 implementation.
/// Portions adapted from https://github.com/dotnet/runtime/blob/v8.0.0/src/libraries/System.Private.CoreLib/src/System/UInt128.cs. /// Portions adapted from https://github.com/dotnet/runtime/blob/v8.0.0/src/libraries/System.Private.CoreLib/src/System/UInt128.cs.
/// </summary> /// </summary>
internal struct MutableUInt128 public struct MutableUInt128
{ {
public ulong High; public ulong High;
public ulong Low; public ulong Low;
@@ -78,7 +78,7 @@ internal struct MutableUInt128
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Subtract(ulong value) public void Sub(ulong value)
{ {
ulong low = Low; ulong low = Low;
ulong result = low - value; ulong result = low - value;
@@ -89,7 +89,7 @@ internal struct MutableUInt128
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Subtract(in MutableUInt128 value) public void Sub(in MutableUInt128 value)
{ {
ulong low = Low; ulong low = Low;
ulong result = low - value.Low; ulong result = low - value.Low;
@@ -100,13 +100,13 @@ internal struct MutableUInt128
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Multiply(ulong value) public void Mul(ulong value)
{ {
ulong currentHigh = High; ulong currentHigh = High;
ulong currentLow = Low; ulong currentLow = Low;
ulong carry = Multiply64(currentLow, value, out ulong newLow); ulong carry = Mul64(currentLow, value, out ulong newLow);
Multiply64(currentHigh, value, out ulong shiftedHigh); Mul64(currentHigh, value, out ulong shiftedHigh);
ulong newHigh = shiftedHigh + carry; ulong newHigh = shiftedHigh + carry;
Low = newLow; Low = newLow;
@@ -114,7 +114,7 @@ internal struct MutableUInt128
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ulong Multiply64(ulong left, ulong right, out ulong low) private static ulong Mul64(ulong left, ulong right, out ulong low)
{ {
const ulong Mask32 = 0xFFFF_FFFFUL; const ulong Mask32 = 0xFFFF_FFFFUL;
@@ -136,57 +136,52 @@ internal struct MutableUInt128
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MultiplyAdd(ulong multiplier, ulong addend) public void MulAdd(ulong multiplier, ulong addend)
{
Multiply(multiplier);
Add(addend);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MultiplyAddInline(ulong multiplier, ulong addend)
{ {
const ulong Mask32 = 0xFFFF_FFFFUL; const ulong Mask32 = 0xFFFF_FFFFUL;
ulong multiplierLow = multiplier & Mask32; ulong multiplierLow = multiplier & Mask32;
ulong multiplierHigh = multiplier >> 32; ulong multiplierHigh = multiplier >> 32;
ulong low = Low; ulong product11 = Low;
ulong lowLow = low & Mask32; ulong product01 = product11 & Mask32;
ulong lowHigh = low >> 32; product11 >>= 32;
ulong product00 = lowLow * multiplierLow; ulong product00 = product01 * multiplierLow;
ulong product01 = lowLow * multiplierHigh; product01 *= multiplierHigh;
ulong product10 = lowHigh * multiplierLow; ulong product10 = product11 * multiplierLow;
ulong product11 = lowHigh * multiplierHigh; product11 *= multiplierHigh;
ulong carry = (product00 >> 32) + (product01 & Mask32) + (product10 & Mask32); ulong high = (product00 >> 32) + (product01 & Mask32) + (product10 & Mask32);
ulong lowResult = ((carry & Mask32) << 32) | (product00 & Mask32); product11 = product11 + (product01 >> 32) + (product10 >> 32) + (high >> 32);
ulong highContribution = product11 + (product01 >> 32) + (product10 >> 32) + (carry >> 32); product10 = ((high & Mask32) << 32) | (product00 & Mask32);
ulong high = High; high = High;
ulong highLow = high & Mask32; product00 = high & Mask32;
ulong highHigh = high >> 32; high >>= 32;
ulong product20 = highLow * multiplierLow; product01 = product00 * multiplierLow;
ulong product21 = highLow * multiplierHigh; product00 *= multiplierHigh;
ulong product30 = highHigh * multiplierLow; high *= multiplierLow;
ulong highCarry = (product20 >> 32) + (product21 & Mask32) + (product30 & Mask32); high = (product01 >> 32) + (product00 & Mask32) + (high & Mask32);
ulong shiftedHighLow = ((highCarry & Mask32) << 32) | (product20 & Mask32); product01 = ((high & Mask32) << 32) | (product01 & Mask32);
ulong newLow = lowResult + addend; // Calculate low. We're assigning addend to local variable for a tiny bit better performance.
ulong addCarry = newLow < addend ? 1UL : 0UL; product00 = addend;
high = product10 + product00;
Low = high;
// Calculate high including carry
high = high < product00 ? 1UL : 0UL;
high += product11 + product01;
ulong newHigh = highContribution + shiftedHighLow;
// Discard overflow above 128 bits to match Multiply's truncation semantics. // Discard overflow above 128 bits to match Multiply's truncation semantics.
newHigh += addCarry; High = high;
Low = newLow;
High = newHigh;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong Mod(ulong modulus) public readonly ulong Mod(ulong modulus)
{ {
if (modulus == 0UL) if (modulus == 0UL)
{ {
@@ -214,7 +209,7 @@ internal struct MutableUInt128
{ {
for (int bit = bitCount - 1; bit >= 0; bit--) for (int bit = bitCount - 1; bit >= 0; bit--)
{ {
remainder.ShiftLeftOneBit(); remainder.ShiftLeft();
if (((word >> bit) & 1UL) != 0) if (((word >> bit) & 1UL) != 0)
{ {
remainder.Add(1UL); remainder.Add(1UL);
@@ -225,7 +220,7 @@ internal struct MutableUInt128
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ShiftLeftOneBit() private void ShiftLeft()
{ {
High = (High << 1) | (Low >> 63); High = (High << 1) | (Low >> 63);
Low <<= 1; Low <<= 1;
@@ -236,10 +231,10 @@ internal struct MutableUInt128
{ {
if (High != 0UL || Low >= modulus) if (High != 0UL || Low >= modulus)
{ {
Subtract(modulus); Sub(modulus);
if (High != 0UL || Low >= modulus) if (High != 0UL || Low >= modulus)
{ {
Subtract(modulus); Sub(modulus);
} }
} }
} }
+1 -1
View File
@@ -1327,7 +1327,7 @@ internal static partial class PrimeOrderCalculator
{ {
ref MutableUInt128 polynomial = ref s_pollardRhoPolynomialBuffer; ref MutableUInt128 polynomial = ref s_pollardRhoPolynomialBuffer;
polynomial.Set(x); polynomial.Set(x);
polynomial.Multiply(x); polynomial.Mul(x);
polynomial.Add(c); polynomial.Add(c);
return polynomial.Mod(modulus); return polynomial.Mod(modulus);
} }