diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs index 151145e12..ee33cf210 100644 --- a/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs @@ -24,7 +24,7 @@ namespace ImageSharp.Benchmarks.General.Vectorization for (int i = 0; i < this.InputSize; i++) { - this.input[i] = (uint)i; + this.input[i] = i; } } @@ -39,7 +39,7 @@ namespace ImageSharp.Benchmarks.General.Vectorization } [Benchmark] - public void Simd() + public void SimdMultiplyByVector() { Vector v = new Vector(this.testValue); @@ -50,5 +50,18 @@ namespace ImageSharp.Benchmarks.General.Vectorization a.CopyTo(this.result, i); } } + + [Benchmark] + public void SimdMultiplyByScalar() + { + float v = this.testValue; + + for (int i = 0; i < this.input.Length; i += Vector.Count) + { + Vector a = new Vector(this.input, i); + a = a * v; + a.CopyTo(this.result, i); + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs new file mode 100644 index 000000000..93f4095e9 --- /dev/null +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs @@ -0,0 +1,90 @@ +namespace ImageSharp.Benchmarks.General.Vectorization +{ + using System.Numerics; + using System.Runtime.CompilerServices; + + using BenchmarkDotNet.Attributes; + + /// + /// This benchmark compares different methods for fetching memory data into + /// checking if JIT has limitations. Normally SIMD acceleration should be here for all methods. + /// + public class VectorFetching + { + private float testValue; + + private float[] data; + + [Params(64)] + public int InputSize { get; set; } + + [Setup] + public void Setup() + { + this.data = new float[this.InputSize]; + this.testValue = 42; + + for (int i = 0; i < this.InputSize; i++) + { + this.data[i] = i; + } + } + + [Benchmark(Baseline = true)] + public void Baseline() + { + float v = this.testValue; + for (int i = 0; i < this.data.Length; i++) + { + this.data[i] = this.data[i] * v; + } + } + + [Benchmark] + public void FetchWithVectorConstructor() + { + Vector v = new Vector(this.testValue); + + for (int i = 0; i < this.data.Length; i += Vector.Count) + { + Vector a = new Vector(this.data, i); + a = a * v; + a.CopyTo(this.data, i); + } + } + + [Benchmark] + public void FetchWithUnsafeCast() + { + Vector v = new Vector(this.testValue); + ref Vector start = ref Unsafe.As>(ref this.data[0]); + + int n = this.InputSize / Vector.Count; + + for (int i = 0; i < n; i++) + { + ref Vector p = ref Unsafe.Add(ref start, i); + + Vector a = p; + a = a * v; + + p = a; + } + } + + [Benchmark] + public void FetchWithUnsafeCastNoTempVector() + { + Vector v = new Vector(this.testValue); + ref Vector start = ref Unsafe.As>(ref this.data[0]); + + int n = this.InputSize / Vector.Count; + + for (int i = 0; i < n; i++) + { + ref Vector a = ref Unsafe.Add(ref start, i); + a = a * v; + } + } + } +} \ No newline at end of file