🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

272 lines
9.6 KiB

  1. #include "config.h"
  2. #include <xmmintrin.h>
  3. #include <cmath>
  4. #include <limits>
  5. #include "alnumeric.h"
  6. #include "core/bsinc_defs.h"
  7. #include "defs.h"
  8. #include "hrtfbase.h"
  9. struct SSETag;
  10. struct BSincTag;
  11. struct FastBSincTag;
  12. #if defined(__GNUC__) && !defined(__clang__) && !defined(__SSE__)
  13. #pragma GCC target("sse")
  14. #endif
  15. namespace {
  16. constexpr uint FracPhaseBitDiff{MixerFracBits - BSincPhaseBits};
  17. constexpr uint FracPhaseDiffOne{1 << FracPhaseBitDiff};
  18. #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
  19. inline void ApplyCoeffs(float2 *RESTRICT Values, const size_t IrSize, const ConstHrirSpan Coeffs,
  20. const float left, const float right)
  21. {
  22. const __m128 lrlr{_mm_setr_ps(left, right, left, right)};
  23. ASSUME(IrSize >= MinIrLength);
  24. /* This isn't technically correct to test alignment, but it's true for
  25. * systems that support SSE, which is the only one that needs to know the
  26. * alignment of Values (which alternates between 8- and 16-byte aligned).
  27. */
  28. if(!(reinterpret_cast<uintptr_t>(Values)&15))
  29. {
  30. for(size_t i{0};i < IrSize;i += 2)
  31. {
  32. const __m128 coeffs{_mm_load_ps(&Coeffs[i][0])};
  33. __m128 vals{_mm_load_ps(&Values[i][0])};
  34. vals = MLA4(vals, lrlr, coeffs);
  35. _mm_store_ps(&Values[i][0], vals);
  36. }
  37. }
  38. else
  39. {
  40. __m128 imp0, imp1;
  41. __m128 coeffs{_mm_load_ps(&Coeffs[0][0])};
  42. __m128 vals{_mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<__m64*>(&Values[0][0]))};
  43. imp0 = _mm_mul_ps(lrlr, coeffs);
  44. vals = _mm_add_ps(imp0, vals);
  45. _mm_storel_pi(reinterpret_cast<__m64*>(&Values[0][0]), vals);
  46. size_t td{((IrSize+1)>>1) - 1};
  47. size_t i{1};
  48. do {
  49. coeffs = _mm_load_ps(&Coeffs[i+1][0]);
  50. vals = _mm_load_ps(&Values[i][0]);
  51. imp1 = _mm_mul_ps(lrlr, coeffs);
  52. imp0 = _mm_shuffle_ps(imp0, imp1, _MM_SHUFFLE(1, 0, 3, 2));
  53. vals = _mm_add_ps(imp0, vals);
  54. _mm_store_ps(&Values[i][0], vals);
  55. imp0 = imp1;
  56. i += 2;
  57. } while(--td);
  58. vals = _mm_loadl_pi(vals, reinterpret_cast<__m64*>(&Values[i][0]));
  59. imp0 = _mm_movehl_ps(imp0, imp0);
  60. vals = _mm_add_ps(imp0, vals);
  61. _mm_storel_pi(reinterpret_cast<__m64*>(&Values[i][0]), vals);
  62. }
  63. }
  64. } // namespace
  65. template<>
  66. float *Resample_<BSincTag,SSETag>(const InterpState *state, float *RESTRICT src, uint frac,
  67. uint increment, const al::span<float> dst)
  68. {
  69. const float *const filter{state->bsinc.filter};
  70. const __m128 sf4{_mm_set1_ps(state->bsinc.sf)};
  71. const size_t m{state->bsinc.m};
  72. ASSUME(m > 0);
  73. src -= state->bsinc.l;
  74. for(float &out_sample : dst)
  75. {
  76. // Calculate the phase index and factor.
  77. const uint pi{frac >> FracPhaseBitDiff};
  78. const float pf{static_cast<float>(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)};
  79. // Apply the scale and phase interpolated filter.
  80. __m128 r4{_mm_setzero_ps()};
  81. {
  82. const __m128 pf4{_mm_set1_ps(pf)};
  83. const float *RESTRICT fil{filter + m*pi*2};
  84. const float *RESTRICT phd{fil + m};
  85. const float *RESTRICT scd{fil + BSincPhaseCount*2*m};
  86. const float *RESTRICT spd{scd + m};
  87. size_t td{m >> 2};
  88. size_t j{0u};
  89. do {
  90. /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
  91. const __m128 f4 = MLA4(
  92. MLA4(_mm_load_ps(&fil[j]), sf4, _mm_load_ps(&scd[j])),
  93. pf4, MLA4(_mm_load_ps(&phd[j]), sf4, _mm_load_ps(&spd[j])));
  94. /* r += f*src */
  95. r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j]));
  96. j += 4;
  97. } while(--td);
  98. }
  99. r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
  100. r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
  101. out_sample = _mm_cvtss_f32(r4);
  102. frac += increment;
  103. src += frac>>MixerFracBits;
  104. frac &= MixerFracMask;
  105. }
  106. return dst.data();
  107. }
  108. template<>
  109. float *Resample_<FastBSincTag,SSETag>(const InterpState *state, float *RESTRICT src, uint frac,
  110. uint increment, const al::span<float> dst)
  111. {
  112. const float *const filter{state->bsinc.filter};
  113. const size_t m{state->bsinc.m};
  114. ASSUME(m > 0);
  115. src -= state->bsinc.l;
  116. for(float &out_sample : dst)
  117. {
  118. // Calculate the phase index and factor.
  119. const uint pi{frac >> FracPhaseBitDiff};
  120. const float pf{static_cast<float>(frac & (FracPhaseDiffOne-1)) * (1.0f/FracPhaseDiffOne)};
  121. // Apply the phase interpolated filter.
  122. __m128 r4{_mm_setzero_ps()};
  123. {
  124. const __m128 pf4{_mm_set1_ps(pf)};
  125. const float *RESTRICT fil{filter + m*pi*2};
  126. const float *RESTRICT phd{fil + m};
  127. size_t td{m >> 2};
  128. size_t j{0u};
  129. do {
  130. /* f = fil + pf*phd */
  131. const __m128 f4 = MLA4(_mm_load_ps(&fil[j]), pf4, _mm_load_ps(&phd[j]));
  132. /* r += f*src */
  133. r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j]));
  134. j += 4;
  135. } while(--td);
  136. }
  137. r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
  138. r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
  139. out_sample = _mm_cvtss_f32(r4);
  140. frac += increment;
  141. src += frac>>MixerFracBits;
  142. frac &= MixerFracMask;
  143. }
  144. return dst.data();
  145. }
  146. template<>
  147. void MixHrtf_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
  148. const MixHrtfFilter *hrtfparams, const size_t BufferSize)
  149. { MixHrtfBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, hrtfparams, BufferSize); }
  150. template<>
  151. void MixHrtfBlend_<SSETag>(const float *InSamples, float2 *AccumSamples, const uint IrSize,
  152. const HrtfFilter *oldparams, const MixHrtfFilter *newparams, const size_t BufferSize)
  153. {
  154. MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams,
  155. BufferSize);
  156. }
  157. template<>
  158. void MixDirectHrtf_<SSETag>(const FloatBufferSpan LeftOut, const FloatBufferSpan RightOut,
  159. const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples,
  160. float *TempBuf, HrtfChannelState *ChanState, const size_t IrSize, const size_t BufferSize)
  161. {
  162. MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, TempBuf, ChanState,
  163. IrSize, BufferSize);
  164. }
  165. template<>
  166. void Mix_<SSETag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
  167. float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos)
  168. {
  169. const float delta{(Counter > 0) ? 1.0f / static_cast<float>(Counter) : 0.0f};
  170. const auto min_len = minz(Counter, InSamples.size());
  171. const auto aligned_len = minz((min_len+3) & ~size_t{3}, InSamples.size()) - min_len;
  172. for(FloatBufferLine &output : OutBuffer)
  173. {
  174. float *RESTRICT dst{al::assume_aligned<16>(output.data()+OutPos)};
  175. float gain{*CurrentGains};
  176. const float step{(*TargetGains-gain) * delta};
  177. size_t pos{0};
  178. if(!(std::abs(step) > std::numeric_limits<float>::epsilon()))
  179. gain = *TargetGains;
  180. else
  181. {
  182. float step_count{0.0f};
  183. /* Mix with applying gain steps in aligned multiples of 4. */
  184. if(size_t todo{min_len >> 2})
  185. {
  186. const __m128 four4{_mm_set1_ps(4.0f)};
  187. const __m128 step4{_mm_set1_ps(step)};
  188. const __m128 gain4{_mm_set1_ps(gain)};
  189. __m128 step_count4{_mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f)};
  190. do {
  191. const __m128 val4{_mm_load_ps(&InSamples[pos])};
  192. __m128 dry4{_mm_load_ps(&dst[pos])};
  193. /* dry += val * (gain + step*step_count) */
  194. dry4 = MLA4(dry4, val4, MLA4(gain4, step4, step_count4));
  195. _mm_store_ps(&dst[pos], dry4);
  196. step_count4 = _mm_add_ps(step_count4, four4);
  197. pos += 4;
  198. } while(--todo);
  199. /* NOTE: step_count4 now represents the next four counts after
  200. * the last four mixed samples, so the lowest element
  201. * represents the next step count to apply.
  202. */
  203. step_count = _mm_cvtss_f32(step_count4);
  204. }
  205. /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
  206. for(size_t leftover{min_len&3};leftover;++pos,--leftover)
  207. {
  208. dst[pos] += InSamples[pos] * (gain + step*step_count);
  209. step_count += 1.0f;
  210. }
  211. if(pos == Counter)
  212. gain = *TargetGains;
  213. else
  214. gain += step*step_count;
  215. /* Mix until pos is aligned with 4 or the mix is done. */
  216. for(size_t leftover{aligned_len&3};leftover;++pos,--leftover)
  217. dst[pos] += InSamples[pos] * gain;
  218. }
  219. *CurrentGains = gain;
  220. ++CurrentGains;
  221. ++TargetGains;
  222. if(!(std::abs(gain) > GainSilenceThreshold))
  223. continue;
  224. if(size_t todo{(InSamples.size()-pos) >> 2})
  225. {
  226. const __m128 gain4{_mm_set1_ps(gain)};
  227. do {
  228. const __m128 val4{_mm_load_ps(&InSamples[pos])};
  229. __m128 dry4{_mm_load_ps(&dst[pos])};
  230. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  231. _mm_store_ps(&dst[pos], dry4);
  232. pos += 4;
  233. } while(--todo);
  234. }
  235. for(size_t leftover{(InSamples.size()-pos)&3};leftover;++pos,--leftover)
  236. dst[pos] += InSamples[pos] * gain;
  237. }
  238. }