1
0

StereoToMonoBenchmark.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System.Runtime.Intrinsics.X86;
  2. using BenchmarkDotNet.Attributes;
  3. using Robust.Shared.Analyzers;
  4. namespace Content.Benchmarks
  5. {
  6. [Virtual]
  7. public class StereoToMonoBenchmark
  8. {
  9. [Params(128, 256, 512)]
  10. public int N { get; set; }
  11. private short[] _input;
  12. private short[] _output;
  13. [GlobalSetup]
  14. public void Setup()
  15. {
  16. _input = new short[N * 2];
  17. _output = new short[N];
  18. }
  19. [Benchmark]
  20. public void BenchSimple()
  21. {
  22. var l = N;
  23. for (var j = 0; j < l; j++)
  24. {
  25. var k = j + l;
  26. _output[j] = (short) ((_input[k] + _input[j]) / 2);
  27. }
  28. }
  29. [Benchmark]
  30. public unsafe void BenchSse()
  31. {
  32. var l = N;
  33. fixed (short* iPtr = _input)
  34. fixed (short* oPtr = _output)
  35. {
  36. for (var j = 0; j < l; j += 8)
  37. {
  38. var k = j + l;
  39. var jV = Sse2.ShiftRightArithmetic(Sse2.LoadVector128(iPtr + j), 1);
  40. var kV = Sse2.ShiftRightArithmetic(Sse2.LoadVector128(iPtr + k), 1);
  41. Sse2.Store(j + oPtr, Sse2.Add(jV, kV));
  42. }
  43. }
  44. }
  45. [Benchmark]
  46. public unsafe void BenchAvx2()
  47. {
  48. var l = N;
  49. fixed (short* iPtr = _input)
  50. fixed (short* oPtr = _output)
  51. {
  52. for (var j = 0; j < l; j += 16)
  53. {
  54. var k = j + l;
  55. var jV = Avx2.ShiftRightArithmetic(Avx.LoadVector256(iPtr + j), 1);
  56. var kV = Avx2.ShiftRightArithmetic(Avx.LoadVector256(iPtr + k), 1);
  57. Avx.Store(j + oPtr, Avx2.Add(jV, kV));
  58. }
  59. }
  60. }
  61. }
  62. }