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
+68 -66

No files matched your search

@@ -27,34 +27,41 @@ public class MutableUInt128MultiplyAddBenchmarks
return ((UInt128)high << 64) | low;
}
/// <summary>
/// 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);
value.MultiplyAdd(Input.Multiplier, Input.Addend);
return value.ToUInt128();
}
private static void MultiplyAdd(ref MutableUInt128 source, ulong multiplier, ulong addend)
{
source.Mul(multiplier);
source.Add(addend);
}
/// <summary>
/// Baseline MultiplyAdd implementation that chains Multiply and Add; measured 3.370 ns with mixed operands,
/// 3.361 ns when both limbs and inputs are near their maxima, and 3.368 ns with tiny operands.
/// </summary>
/// <remarks>
/// Observed means: MixedOperands 3.370 ns, NearMaxOperands 3.361 ns, TinyOperands 3.368 ns.
/// </remarks>
[Benchmark(Baseline = true)]
public MutableUInt128 MultiplyAddChained()
{
MutableUInt128 value = new(Input.InitialValue);
MultiplyAdd(ref value, Input.Multiplier, Input.Addend);
return value;
}
/// <summary>
/// Fully inlined MultiplyAdd implementation that kept all arithmetic inside the method; landed at 4.11 ns on mixed operands,
/// 4.25 ns on near-max inputs, and 4.27 ns on tiny operands.
/// 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 4.109 ns, NearMaxOperands 4.253 ns, TinyOperands 4.271 ns.
/// Observed means: MixedOperands 3.175 ns, NearMaxOperands 3.177 ns, TinyOperands 3.178 ns.
/// Status: Implemented
/// </remarks>
[Benchmark]
public UInt128 MultiplyAddFullyInlined()
public MutableUInt128 MultiplyAddFullyInlined()
{
MutableUInt128 value = new(Input.InitialValue);
value.MultiplyAddInline(Input.Multiplier, Input.Addend);
return value.ToUInt128();
value.MulAdd(Input.Multiplier, Input.Addend);
return value;
}
public readonly record struct MultiplyAddInput(UInt128 InitialValue, ulong Multiplier, ulong Addend, string Name)