Browse Source

Implement feedback and update benchmarks

pull/2369/head
James Jackson-South 3 years ago
parent
commit
9e19134d30
  1. 6
      src/ImageSharp/Common/Helpers/Numerics.cs
  2. 14
      tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs
  3. 15
      tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs

6
src/ImageSharp/Common/Helpers/Numerics.cs

@ -474,8 +474,10 @@ internal static class Numerics
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Premultiply(ref Vector4 source) public static void Premultiply(ref Vector4 source)
{ {
Vector4 alpha = PermuteW(source); // Load into a local variable to prevent accessing the source from memory multiple times.
source = WithW(source * alpha, alpha); Vector4 src = source;
Vector4 alpha = PermuteW(src);
source = WithW(src * alpha, alpha);
} }
/// <summary> /// <summary>

14
tests/ImageSharp.Benchmarks/Color/Bulk/PremultiplyVector4.cs

@ -25,7 +25,19 @@ public class PremultiplyVector4
} }
[Benchmark] [Benchmark]
public void Premultiply() => Numerics.Premultiply(Vectors); public void Premultiply()
{
ref Vector4 baseRef = ref MemoryMarshal.GetReference<Vector4>(Vectors);
for (int i = 0; i < Vectors.Length; i++)
{
ref Vector4 v = ref Unsafe.Add(ref baseRef, i);
Numerics.Premultiply(ref v);
}
}
[Benchmark]
public void PremultiplyBulk() => Numerics.Premultiply(Vectors);
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static void Premultiply(ref Vector4 source) private static void Premultiply(ref Vector4 source)

15
tests/ImageSharp.Benchmarks/Color/Bulk/UnPremultiplyVector4.cs

@ -20,12 +20,25 @@ public class UnPremultiplyVector4
for (int i = 0; i < Vectors.Length; i++) for (int i = 0; i < Vectors.Length; i++)
{ {
ref Vector4 v = ref Unsafe.Add(ref baseRef, i); ref Vector4 v = ref Unsafe.Add(ref baseRef, i);
UnPremultiply(ref v); UnPremultiply(ref v);
} }
} }
[Benchmark] [Benchmark]
public void UnPremultiply() => Numerics.UnPremultiply(Vectors); public void UnPremultiply()
{
ref Vector4 baseRef = ref MemoryMarshal.GetReference<Vector4>(Vectors);
for (int i = 0; i < Vectors.Length; i++)
{
ref Vector4 v = ref Unsafe.Add(ref baseRef, i);
Numerics.UnPremultiply(ref v);
}
}
[Benchmark]
public void UnPremultiplyBulk() => Numerics.UnPremultiply(Vectors);
[MethodImpl(InliningOptions.ShortMethod)] [MethodImpl(InliningOptions.ShortMethod)]
private static void UnPremultiply(ref Vector4 source) private static void UnPremultiply(ref Vector4 source)

Loading…
Cancel
Save