Codegen190kzTests.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Round 190kz (0.1.30, perf-first, codegen): runtime CPU dispatch. The release builds keep the x86-64 baseline (SSE2);
  2. // the hottest loops also have AVX2 variants picked at run time (src/core/CpuFeatures.h). Each variant must render
  3. // exactly what the SSE2 loops render: these tests run the same work at the SSE2 level and at the CPU's own level
  4. // (cpu::setLevelForTest) and compare every output bit. On a CPU without AVX2 both runs take the SSE2 loops (the test
  5. // then only checks the level logic).
  6. #include "core/CpuFeatures.h"
  7. TEST (codegen190kz_cpu_level_caps_and_restores)
  8. {
  9. using namespace lynx::cpu;
  10. const auto hw = hardwareLevel();
  11. std::printf (" CPU dispatch level: hardware %s, in use %s\n", levelName (hw), levelName (level()));
  12. CHECK (level() <= hw);
  13. setLevelForTest (sse2);
  14. CHECK (level() == (hw < sse2 ? hw : sse2));
  15. CHECK (! hasAvx2());
  16. setLevelForTest (avx2 + 5); // never above the hardware
  17. CHECK (level() <= hw);
  18. setLevelForTest (-1);
  19. CHECK (level() <= hw);
  20. #if LYNX_CPU_DISPATCH
  21. CHECK (hw >= sse2); // every x86-64 CPU has SSE2
  22. #endif
  23. }
  24. namespace codegen190kz
  25. {
  26. // one render at a capped dispatch level (-1: the CPU's own)
  27. template <typename F>
  28. std::uint64_t atLevel (int cap, F&& render)
  29. {
  30. lynx::cpu::setLevelForTest (cap);
  31. const auto h = render();
  32. lynx::cpu::setLevelForTest (-1);
  33. return h;
  34. }
  35. } // namespace codegen190kz
  36. // Signalsmith Stretch's spectral loops and FFT passes (third_party/signalsmith: eight bands / values at a time with
  37. // AVX2): the real-time pitch shifter at factor 1 (the identity map: Thief's follow-tempo shifters), transpositions
  38. // down and up (the mapped and the random paths), mono and stereo, both analysis blocks; and the offline stretch.
  39. TEST (codegen190kz_signalsmith_avx2_renders_the_sse2_bits)
  40. {
  41. using lynx::StretchMethod;
  42. struct Case
  43. {
  44. double rate;
  45. StretchMethod method;
  46. bool mono;
  47. double factor;
  48. };
  49. const Case cases[] = {
  50. { 44100.0, StretchMethod::general, false, 1.0 },
  51. { 44100.0, StretchMethod::general, true, 1.0 },
  52. { 48000.0, StretchMethod::transients, false, 1.0 },
  53. { 44100.0, StretchMethod::general, true, 0.5 },
  54. { 44100.0, StretchMethod::general, false, 0.9438743126816935 },
  55. { 44100.0, StretchMethod::general, false, 1.0594630943592953 },
  56. { 44100.0, StretchMethod::general, true, 1.4983070768766815 },
  57. { 44100.0, StretchMethod::general, false, 2.378414230005442 },
  58. { 48000.0, StretchMethod::transients, false, 0.7491535384383408 },
  59. { 48000.0, StretchMethod::transients, true, 3.174802103936399 },
  60. };
  61. int same = 0, total = 0;
  62. for (const auto& c : cases)
  63. {
  64. double e0 = 0.0, e1 = 0.0;
  65. const auto a = codegen190kz::atLevel (lynx::cpu::sse2, [&] { return perf190kr::shifterRender (c.rate, c.method, c.mono, c.factor, e0); });
  66. const auto b = codegen190kz::atLevel (-1, [&] { return perf190kr::shifterRender (c.rate, c.method, c.mono, c.factor, e1); });
  67. ++total;
  68. if (a == b)
  69. ++same;
  70. else
  71. std::printf (" shifter %.0f Hz %s factor %.4f: sse2 %016llx, %s %016llx\n", c.rate, c.mono ? "mono" : "stereo", c.factor,
  72. (unsigned long long) a, lynx::cpu::levelName (lynx::cpu::level()), (unsigned long long) b);
  73. CHECK (e0 > 1.0 && std::isfinite (e0));
  74. }
  75. for (const auto& [mono, ratio] : { std::pair { false, 1.37 }, std::pair { true, 0.6 }, std::pair { false, 2.6 } })
  76. {
  77. double e0 = 0.0, e1 = 0.0;
  78. const auto a = codegen190kz::atLevel (lynx::cpu::sse2, [&] { return perf190kr::offlineRender (mono, ratio, e0); });
  79. const auto b = codegen190kz::atLevel (-1, [&] { return perf190kr::offlineRender (mono, ratio, e1); });
  80. ++total;
  81. same += a == b ? 1 : 0;
  82. }
  83. std::printf (" Signalsmith Stretch: %d of %d renders identical at sse2 and %s\n", same, total,
  84. lynx::cpu::levelName (lynx::cpu::level()));
  85. CHECK (same == total);
  86. }
  87. // Parametric EQ 2's band cascade and Maximus's biquad cascades (the AVX2 copies: the same SSE2 operations, VEX-encoded)
  88. // against round 190kr's plain-loop references, at the SSE2 level and at the CPU's own level
  89. TEST (codegen190kz_peq2_and_maximus_cascades_avx2_match_the_plain_loops)
  90. {
  91. int ok[2] = { 0, 0 };
  92. constexpr int seeds = 60;
  93. for (int lv = 0; lv < 2; ++lv)
  94. {
  95. lynx::cpu::setLevelForTest (lv == 0 ? lynx::cpu::sse2 : -1);
  96. for (unsigned seed = 1; seed <= (unsigned) seeds; ++seed)
  97. ok[lv] += (lynx::peqfl::stagesMatchReference (seed * 2246822519u) ? 1 : 0)
  98. + (lynx::hlfx::kernelsMatchReference (seed * 2654435761u) ? 1 : 0);
  99. lynx::cpu::setLevelForTest (-1);
  100. }
  101. std::printf (" PEQ2 / Maximus cascades: sse2 %d / %d, %s %d / %d match the plain loops\n", ok[0], 2 * seeds,
  102. lynx::cpu::levelName (lynx::cpu::level()), ok[1], 2 * seeds);
  103. CHECK (ok[0] == 2 * seeds);
  104. CHECK (ok[1] == 2 * seeds);
  105. }