mirror of https://github.com/SixLabors/ImageSharp
9 changed files with 226 additions and 232 deletions
@ -1,54 +0,0 @@ |
|||||
namespace ImageSharp.Benchmarks.General.Vectorization |
|
||||
{ |
|
||||
using System.Numerics; |
|
||||
|
|
||||
using BenchmarkDotNet.Attributes; |
|
||||
|
|
||||
public class DivFloat |
|
||||
{ |
|
||||
private float[] input; |
|
||||
|
|
||||
private float[] result; |
|
||||
|
|
||||
[Params(32)] |
|
||||
public int InputSize { get; set; } |
|
||||
|
|
||||
private float testValue; |
|
||||
|
|
||||
[GlobalSetup] |
|
||||
public void Setup() |
|
||||
{ |
|
||||
this.input = new float[this.InputSize]; |
|
||||
this.result = new float[this.InputSize]; |
|
||||
this.testValue = 42; |
|
||||
|
|
||||
for (int i = 0; i < this.InputSize; i++) |
|
||||
{ |
|
||||
this.input[i] = (uint)i; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark(Baseline = true)] |
|
||||
public void Standard() |
|
||||
{ |
|
||||
float v = this.testValue; |
|
||||
for (int i = 0; i < this.input.Length; i++) |
|
||||
{ |
|
||||
this.result[i] = this.input[i] / v; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public void Simd() |
|
||||
{ |
|
||||
Vector<float> v = new Vector<float>(this.testValue); |
|
||||
|
|
||||
for (int i = 0; i < this.input.Length; i += Vector<uint>.Count) |
|
||||
{ |
|
||||
Vector<float> a = new Vector<float>(this.input, i); |
|
||||
a = a / v; |
|
||||
a.CopyTo(this.result, i); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,54 +0,0 @@ |
|||||
namespace ImageSharp.Benchmarks.General.Vectorization |
|
||||
{ |
|
||||
using System.Numerics; |
|
||||
|
|
||||
using BenchmarkDotNet.Attributes; |
|
||||
|
|
||||
public class DivUInt32 |
|
||||
{ |
|
||||
private uint[] input; |
|
||||
|
|
||||
private uint[] result; |
|
||||
|
|
||||
[Params(32)] |
|
||||
public int InputSize { get; set; } |
|
||||
|
|
||||
private uint testValue; |
|
||||
|
|
||||
[GlobalSetup] |
|
||||
public void Setup() |
|
||||
{ |
|
||||
this.input = new uint[this.InputSize]; |
|
||||
this.result = new uint[this.InputSize]; |
|
||||
this.testValue = 42; |
|
||||
|
|
||||
for (int i = 0; i < this.InputSize; i++) |
|
||||
{ |
|
||||
this.input[i] = (uint)i; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark(Baseline = true)] |
|
||||
public void Standard() |
|
||||
{ |
|
||||
uint v = this.testValue; |
|
||||
for (int i = 0; i < this.input.Length; i++) |
|
||||
{ |
|
||||
this.result[i] = this.input[i] / v; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public void Simd() |
|
||||
{ |
|
||||
Vector<uint> v = new Vector<uint>(this.testValue); |
|
||||
|
|
||||
for (int i = 0; i < this.input.Length; i += Vector<uint>.Count) |
|
||||
{ |
|
||||
Vector<uint> a = new Vector<uint>(this.input, i); |
|
||||
a = a / v; |
|
||||
a.CopyTo(this.result, i); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,69 @@ |
|||||
|
namespace ImageSharp.Benchmarks.General.Vectorization |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Numerics; |
||||
|
|
||||
|
using BenchmarkDotNet.Attributes; |
||||
|
|
||||
|
public class DivFloat : SIMDBenchmarkBase<float>.Divide |
||||
|
{ |
||||
|
protected override float GetTestValue() => 42; |
||||
|
|
||||
|
[Benchmark(Baseline = true)] |
||||
|
public void Standard() |
||||
|
{ |
||||
|
float v = this.testValue; |
||||
|
for (int i = 0; i < this.input.Length; i++) |
||||
|
{ |
||||
|
this.result[i] = this.input[i] / v; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class Divide : SIMDBenchmarkBase<uint>.Divide |
||||
|
{ |
||||
|
protected override uint GetTestValue() => 42; |
||||
|
|
||||
|
[Benchmark(Baseline = true)] |
||||
|
public void Standard() |
||||
|
{ |
||||
|
uint v = this.testValue; |
||||
|
for (int i = 0; i < this.input.Length; i++) |
||||
|
{ |
||||
|
this.result[i] = this.input[i] / v; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class DivInt32 : SIMDBenchmarkBase<int>.Divide |
||||
|
{ |
||||
|
protected override int GetTestValue() => 42; |
||||
|
|
||||
|
[Benchmark(Baseline = true)] |
||||
|
public void Standard() |
||||
|
{ |
||||
|
int v = this.testValue; |
||||
|
for (int i = 0; i < this.input.Length; i++) |
||||
|
{ |
||||
|
this.result[i] = this.input[i] / v; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class DivInt16 : SIMDBenchmarkBase<short>.Divide |
||||
|
{ |
||||
|
protected override short GetTestValue() => 42; |
||||
|
|
||||
|
protected override Vector<short> GetTestVector() => new Vector<short>(new short[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}); |
||||
|
|
||||
|
[Benchmark(Baseline = true)] |
||||
|
public void Standard() |
||||
|
{ |
||||
|
short v = this.testValue; |
||||
|
for (int i = 0; i < this.input.Length; i++) |
||||
|
{ |
||||
|
this.result[i] = (short)(this.input[i] / v); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,67 +0,0 @@ |
|||||
namespace ImageSharp.Benchmarks.General.Vectorization |
|
||||
{ |
|
||||
using System.Numerics; |
|
||||
|
|
||||
using BenchmarkDotNet.Attributes; |
|
||||
|
|
||||
public class MulFloat |
|
||||
{ |
|
||||
private float[] input; |
|
||||
|
|
||||
private float[] result; |
|
||||
|
|
||||
[Params(32)] |
|
||||
public int InputSize { get; set; } |
|
||||
|
|
||||
private float testValue; |
|
||||
|
|
||||
[GlobalSetup] |
|
||||
public void Setup() |
|
||||
{ |
|
||||
this.input = new float[this.InputSize]; |
|
||||
this.result = new float[this.InputSize]; |
|
||||
this.testValue = 42; |
|
||||
|
|
||||
for (int i = 0; i < this.InputSize; i++) |
|
||||
{ |
|
||||
this.input[i] = i; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark(Baseline = true)] |
|
||||
public void Standard() |
|
||||
{ |
|
||||
float v = this.testValue; |
|
||||
for (int i = 0; i < this.input.Length; i++) |
|
||||
{ |
|
||||
this.result[i] = this.input[i] * v; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public void SimdMultiplyByVector() |
|
||||
{ |
|
||||
Vector<float> v = new Vector<float>(this.testValue); |
|
||||
|
|
||||
for (int i = 0; i < this.input.Length; i += Vector<uint>.Count) |
|
||||
{ |
|
||||
Vector<float> a = new Vector<float>(this.input, i); |
|
||||
a = a * v; |
|
||||
a.CopyTo(this.result, i); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public void SimdMultiplyByScalar() |
|
||||
{ |
|
||||
float v = this.testValue; |
|
||||
|
|
||||
for (int i = 0; i < this.input.Length; i += Vector<uint>.Count) |
|
||||
{ |
|
||||
Vector<float> a = new Vector<float>(this.input, i); |
|
||||
a = a * v; |
|
||||
a.CopyTo(this.result, i); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -1,54 +0,0 @@ |
|||||
namespace ImageSharp.Benchmarks.General.Vectorization |
|
||||
{ |
|
||||
using System.Numerics; |
|
||||
|
|
||||
using BenchmarkDotNet.Attributes; |
|
||||
|
|
||||
public class MulUInt32 |
|
||||
{ |
|
||||
private uint[] input; |
|
||||
|
|
||||
private uint[] result; |
|
||||
|
|
||||
[Params(32)] |
|
||||
public int InputSize { get; set; } |
|
||||
|
|
||||
private uint testValue; |
|
||||
|
|
||||
[GlobalSetup] |
|
||||
public void Setup() |
|
||||
{ |
|
||||
this.input = new uint[this.InputSize]; |
|
||||
this.result = new uint[this.InputSize]; |
|
||||
this.testValue = 42; |
|
||||
|
|
||||
for (int i = 0; i < this.InputSize; i++) |
|
||||
{ |
|
||||
this.input[i] = (uint)i; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark(Baseline = true)] |
|
||||
public void Standard() |
|
||||
{ |
|
||||
uint v = this.testValue; |
|
||||
for (int i = 0; i < this.input.Length; i++) |
|
||||
{ |
|
||||
this.result[i] = this.input[i] * v; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
[Benchmark] |
|
||||
public void Simd() |
|
||||
{ |
|
||||
Vector<uint> v = new Vector<uint>(this.testValue); |
|
||||
|
|
||||
for (int i = 0; i < this.input.Length; i += Vector<uint>.Count) |
|
||||
{ |
|
||||
Vector<uint> a = new Vector<uint>(this.input, i); |
|
||||
a = a * v; |
|
||||
a.CopyTo(this.result, i); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,50 @@ |
|||||
|
namespace ImageSharp.Benchmarks.General.Vectorization |
||||
|
{ |
||||
|
using System.Numerics; |
||||
|
using BenchmarkDotNet.Attributes; |
||||
|
|
||||
|
public class MulUInt32 : SIMDBenchmarkBase<uint>.Multiply |
||||
|
{ |
||||
|
protected override uint GetTestValue() => 42u; |
||||
|
|
||||
|
[Benchmark(Baseline = true)] |
||||
|
public void Standard() |
||||
|
{ |
||||
|
uint v = this.testValue; |
||||
|
for (int i = 0; i < this.input.Length; i++) |
||||
|
{ |
||||
|
this.result[i] = this.input[i] * v; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MulInt32 : SIMDBenchmarkBase<int>.Multiply |
||||
|
{ |
||||
|
[Benchmark(Baseline = true)] |
||||
|
public void Standard() |
||||
|
{ |
||||
|
int v = this.testValue; |
||||
|
for (int i = 0; i < this.input.Length; i++) |
||||
|
{ |
||||
|
this.result[i] = this.input[i] * v; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class MulInt16 : SIMDBenchmarkBase<short>.Multiply |
||||
|
{ |
||||
|
protected override short GetTestValue() => 42; |
||||
|
|
||||
|
protected override Vector<short> GetTestVector() => new Vector<short>(new short[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }); |
||||
|
|
||||
|
[Benchmark(Baseline = true)] |
||||
|
public void Standard() |
||||
|
{ |
||||
|
short v = this.testValue; |
||||
|
for (int i = 0; i < this.input.Length; i++) |
||||
|
{ |
||||
|
this.result[i] = (short)(this.input[i] * v); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,70 @@ |
|||||
|
namespace ImageSharp.Benchmarks.General.Vectorization |
||||
|
{ |
||||
|
using System.Numerics; |
||||
|
using System.Runtime.CompilerServices; |
||||
|
|
||||
|
using BenchmarkDotNet.Attributes; |
||||
|
|
||||
|
public abstract class SIMDBenchmarkBase<T> |
||||
|
where T : struct |
||||
|
{ |
||||
|
protected T[] input; |
||||
|
|
||||
|
protected T[] result; |
||||
|
|
||||
|
protected T testValue; |
||||
|
|
||||
|
protected Vector<T> testVector; |
||||
|
|
||||
|
protected virtual T GetTestValue() => default(T); |
||||
|
|
||||
|
protected virtual Vector<T> GetTestVector() => new Vector<T>(this.GetTestValue()); |
||||
|
|
||||
|
|
||||
|
[Params(32)] |
||||
|
public int InputSize { get; set; } |
||||
|
|
||||
|
[GlobalSetup] |
||||
|
public virtual void Setup() |
||||
|
{ |
||||
|
this.input = new T[this.InputSize]; |
||||
|
this.result = new T[this.InputSize]; |
||||
|
this.testValue = this.GetTestValue(); |
||||
|
this.testVector = this.GetTestVector(); |
||||
|
} |
||||
|
|
||||
|
public abstract class Multiply : SIMDBenchmarkBase<T> |
||||
|
{ |
||||
|
[Benchmark] |
||||
|
public void Simd() |
||||
|
{ |
||||
|
Vector<T> v = this.testVector; |
||||
|
|
||||
|
for (int i = 0; i < this.input.Length; i += Vector<uint>.Count) |
||||
|
{ |
||||
|
Vector<T> a = Unsafe.As<T, Vector<T>>(ref this.input[i]); |
||||
|
a = a * v; |
||||
|
Unsafe.As<T, Vector<T>>(ref this.result[i]) = a; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public abstract class Divide : SIMDBenchmarkBase<T> |
||||
|
{ |
||||
|
[Benchmark] |
||||
|
public void Simd() |
||||
|
{ |
||||
|
Vector<T> v = this.testVector; |
||||
|
|
||||
|
for (int i = 0; i < this.input.Length; i += Vector<uint>.Count) |
||||
|
{ |
||||
|
Vector<T> a = Unsafe.As<T, Vector<T>>(ref this.input[i]); |
||||
|
a = a / v; |
||||
|
Unsafe.As<T, Vector<T>>(ref this.result[i]) = a; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue