From b9e59fc8d457975e9edf7fd0e39b6b1305f90f95 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Tue, 5 May 2020 23:58:56 +0200 Subject: [PATCH] Audio processing benchmarks. --- Content.Benchmarks/Program.cs | 2 +- Content.Benchmarks/StereoToMonoBenchmark.cs | 70 +++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Content.Benchmarks/StereoToMonoBenchmark.cs diff --git a/Content.Benchmarks/Program.cs b/Content.Benchmarks/Program.cs index 741a4efb33..7eb128092d 100644 --- a/Content.Benchmarks/Program.cs +++ b/Content.Benchmarks/Program.cs @@ -6,7 +6,7 @@ namespace Content.Benchmarks { public static void Main(string[] args) { - BenchmarkRunner.Run(); + BenchmarkRunner.Run(); } } } diff --git a/Content.Benchmarks/StereoToMonoBenchmark.cs b/Content.Benchmarks/StereoToMonoBenchmark.cs new file mode 100644 index 0000000000..89dec8a3dd --- /dev/null +++ b/Content.Benchmarks/StereoToMonoBenchmark.cs @@ -0,0 +1,70 @@ +using System.Runtime.Intrinsics.X86; +using BenchmarkDotNet.Attributes; + +namespace Content.Benchmarks +{ + public class StereoToMonoBenchmark + { + [Params(128, 256, 512)] + public int N { get; set; } + + private short[] _input; + private short[] _output; + + [GlobalSetup] + public void Setup() + { + _input = new short[N * 2]; + _output = new short[N]; + } + + [Benchmark] + public void BenchSimple() + { + var l = N; + for (var j = 0; j < l; j++) + { + var k = j + l; + _output[j] = (short) ((_input[k] + _input[j]) / 2); + } + } + + [Benchmark] + public unsafe void BenchSse() + { + var l = N; + fixed (short* iPtr = _input) + fixed (short* oPtr = _output) + { + for (var j = 0; j < l; j += 8) + { + var k = j + l; + + var jV = Sse2.ShiftRightArithmetic(Sse2.LoadVector128(iPtr + j), 1); + var kV = Sse2.ShiftRightArithmetic(Sse2.LoadVector128(iPtr + k), 1); + + Sse2.Store(j + oPtr, Sse2.Add(jV, kV)); + } + } + } + + [Benchmark] + public unsafe void BenchAvx2() + { + var l = N; + fixed (short* iPtr = _input) + fixed (short* oPtr = _output) + { + for (var j = 0; j < l; j += 16) + { + var k = j + l; + + var jV = Avx2.ShiftRightArithmetic(Avx.LoadVector256(iPtr + j), 1); + var kV = Avx2.ShiftRightArithmetic(Avx.LoadVector256(iPtr + k), 1); + + Avx.Store(j + oPtr, Avx2.Add(jV, kV)); + } + } + } + } +}