forked from RMC-14/RMC-14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStereoToMonoBenchmark.cs
72 lines (62 loc) · 1.84 KB
/
StereoToMonoBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Runtime.Intrinsics.X86;
using BenchmarkDotNet.Attributes;
using Robust.Shared.Analyzers;
namespace Content.Benchmarks
{
[Virtual]
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));
}
}
}
}
}