🛠️🐜 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.

263 lines
9.2 KiB

  1. #include "config.h"
  2. #include <xmmintrin.h>
  3. #include <limits>
  4. #include "AL/al.h"
  5. #include "AL/alc.h"
  6. #include "alMain.h"
  7. #include "alu.h"
  8. #include "alSource.h"
  9. #include "alAuxEffectSlot.h"
  10. #include "defs.h"
  11. #include "hrtfbase.h"
  12. template<>
  13. const ALfloat *Resample_<BSincTag,SSETag>(const InterpState *state, const ALfloat *RESTRICT src,
  14. ALsizei frac, ALint increment, ALfloat *RESTRICT dst, ALsizei dstlen)
  15. {
  16. const ALfloat *const filter{state->bsinc.filter};
  17. const __m128 sf4{_mm_set1_ps(state->bsinc.sf)};
  18. const ALsizei m{state->bsinc.m};
  19. ASSUME(m > 0);
  20. ASSUME(dstlen > 0);
  21. ASSUME(increment > 0);
  22. ASSUME(frac >= 0);
  23. src -= state->bsinc.l;
  24. for(ALsizei i{0};i < dstlen;i++)
  25. {
  26. // Calculate the phase index and factor.
  27. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  28. const ALsizei pi{frac >> FRAC_PHASE_BITDIFF};
  29. const ALfloat pf{(frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF))};
  30. #undef FRAC_PHASE_BITDIFF
  31. ALsizei offset{m*pi*4};
  32. const __m128 *fil{reinterpret_cast<const __m128*>(filter + offset)}; offset += m;
  33. const __m128 *scd{reinterpret_cast<const __m128*>(filter + offset)}; offset += m;
  34. const __m128 *phd{reinterpret_cast<const __m128*>(filter + offset)}; offset += m;
  35. const __m128 *spd{reinterpret_cast<const __m128*>(filter + offset)};
  36. // Apply the scale and phase interpolated filter.
  37. __m128 r4{_mm_setzero_ps()};
  38. {
  39. const ALsizei count{m >> 2};
  40. const __m128 pf4{_mm_set1_ps(pf)};
  41. ASSUME(count > 0);
  42. #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
  43. for(ALsizei j{0};j < count;j++)
  44. {
  45. /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
  46. const __m128 f4 = MLA4(
  47. MLA4(fil[j], sf4, scd[j]),
  48. pf4, MLA4(phd[j], sf4, spd[j])
  49. );
  50. /* r += f*src */
  51. r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j*4]));
  52. }
  53. #undef MLA4
  54. }
  55. r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
  56. r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
  57. dst[i] = _mm_cvtss_f32(r4);
  58. frac += increment;
  59. src += frac>>FRACTIONBITS;
  60. frac &= FRACTIONMASK;
  61. }
  62. return dst;
  63. }
  64. static inline void ApplyCoeffs(ALsizei Offset, float2 *RESTRICT Values, const ALsizei IrSize,
  65. const HrirArray<ALfloat> &Coeffs, const ALfloat left, const ALfloat right)
  66. {
  67. const __m128 lrlr{_mm_setr_ps(left, right, left, right)};
  68. ASSUME(IrSize >= 2);
  69. if((Offset&1))
  70. {
  71. __m128 imp0, imp1;
  72. __m128 coeffs{_mm_load_ps(&Coeffs[0][0])};
  73. __m128 vals{_mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<__m64*>(&Values[0][0]))};
  74. imp0 = _mm_mul_ps(lrlr, coeffs);
  75. vals = _mm_add_ps(imp0, vals);
  76. _mm_storel_pi(reinterpret_cast<__m64*>(&Values[0][0]), vals);
  77. ALsizei i{1};
  78. for(;i < IrSize-1;i += 2)
  79. {
  80. coeffs = _mm_load_ps(&Coeffs[i+1][0]);
  81. vals = _mm_load_ps(&Values[i][0]);
  82. imp1 = _mm_mul_ps(lrlr, coeffs);
  83. imp0 = _mm_shuffle_ps(imp0, imp1, _MM_SHUFFLE(1, 0, 3, 2));
  84. vals = _mm_add_ps(imp0, vals);
  85. _mm_store_ps(&Values[i][0], vals);
  86. imp0 = imp1;
  87. }
  88. vals = _mm_loadl_pi(vals, reinterpret_cast<__m64*>(&Values[i][0]));
  89. imp0 = _mm_movehl_ps(imp0, imp0);
  90. vals = _mm_add_ps(imp0, vals);
  91. _mm_storel_pi(reinterpret_cast<__m64*>(&Values[i][0]), vals);
  92. }
  93. else
  94. {
  95. for(ALsizei i{0};i < IrSize;i += 2)
  96. {
  97. __m128 coeffs{_mm_load_ps(&Coeffs[i][0])};
  98. __m128 vals{_mm_load_ps(&Values[i][0])};
  99. vals = _mm_add_ps(vals, _mm_mul_ps(lrlr, coeffs));
  100. _mm_store_ps(&Values[i][0], vals);
  101. }
  102. }
  103. }
  104. template<>
  105. void MixHrtf_<SSETag>(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut, const ALfloat *data,
  106. float2 *RESTRICT AccumSamples, const ALsizei OutPos, const ALsizei IrSize,
  107. MixHrtfParams *hrtfparams, const ALsizei BufferSize)
  108. {
  109. MixHrtfBase<ApplyCoeffs>(LeftOut, RightOut, data, AccumSamples, OutPos, IrSize, hrtfparams,
  110. BufferSize);
  111. }
  112. template<>
  113. void MixHrtfBlend_<SSETag>(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut,
  114. const ALfloat *data, float2 *RESTRICT AccumSamples, const ALsizei OutPos, const ALsizei IrSize,
  115. const HrtfParams *oldparams, MixHrtfParams *newparams, const ALsizei BufferSize)
  116. {
  117. MixHrtfBlendBase<ApplyCoeffs>(LeftOut, RightOut, data, AccumSamples, OutPos, IrSize, oldparams,
  118. newparams, BufferSize);
  119. }
  120. template<>
  121. void MixDirectHrtf_<SSETag>(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut,
  122. const ALfloat (*data)[BUFFERSIZE], float2 *RESTRICT AccumSamples, DirectHrtfState *State,
  123. const ALsizei NumChans, const ALsizei BufferSize)
  124. {
  125. MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, data, AccumSamples, State, NumChans,
  126. BufferSize);
  127. }
  128. template<>
  129. void Mix_<SSETag>(const ALfloat *data, const ALsizei OutChans, ALfloat (*OutBuffer)[BUFFERSIZE],
  130. ALfloat *CurrentGains, const ALfloat *TargetGains, const ALsizei Counter, const ALsizei OutPos,
  131. const ALsizei BufferSize)
  132. {
  133. ASSUME(OutChans > 0);
  134. ASSUME(BufferSize > 0);
  135. const ALfloat delta{(Counter > 0) ? 1.0f / static_cast<ALfloat>(Counter) : 0.0f};
  136. for(ALsizei c{0};c < OutChans;c++)
  137. {
  138. ALfloat *RESTRICT dst{al::assume_aligned<16>(&OutBuffer[c][OutPos])};
  139. ALsizei pos{0};
  140. ALfloat gain{CurrentGains[c]};
  141. const ALfloat diff{TargetGains[c] - gain};
  142. if(std::fabs(diff) > std::numeric_limits<float>::epsilon())
  143. {
  144. ALsizei minsize{mini(BufferSize, Counter)};
  145. const ALfloat step{diff * delta};
  146. ALfloat step_count{0.0f};
  147. /* Mix with applying gain steps in aligned multiples of 4. */
  148. if(LIKELY(minsize > 3))
  149. {
  150. const __m128 four4{_mm_set1_ps(4.0f)};
  151. const __m128 step4{_mm_set1_ps(step)};
  152. const __m128 gain4{_mm_set1_ps(gain)};
  153. __m128 step_count4{_mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f)};
  154. ALsizei todo{minsize >> 2};
  155. do {
  156. const __m128 val4{_mm_load_ps(&data[pos])};
  157. __m128 dry4{_mm_load_ps(&dst[pos])};
  158. #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
  159. /* dry += val * (gain + step*step_count) */
  160. dry4 = MLA4(dry4, val4, MLA4(gain4, step4, step_count4));
  161. #undef MLA4
  162. _mm_store_ps(&dst[pos], dry4);
  163. step_count4 = _mm_add_ps(step_count4, four4);
  164. pos += 4;
  165. } while(--todo);
  166. /* NOTE: step_count4 now represents the next four counts after
  167. * the last four mixed samples, so the lowest element
  168. * represents the next step count to apply.
  169. */
  170. step_count = _mm_cvtss_f32(step_count4);
  171. }
  172. /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
  173. for(;pos < minsize;pos++)
  174. {
  175. dst[pos] += data[pos]*(gain + step*step_count);
  176. step_count += 1.0f;
  177. }
  178. if(pos == Counter)
  179. gain = TargetGains[c];
  180. else
  181. gain += step*step_count;
  182. CurrentGains[c] = gain;
  183. /* Mix until pos is aligned with 4 or the mix is done. */
  184. minsize = mini(BufferSize, (pos+3)&~3);
  185. for(;pos < minsize;pos++)
  186. dst[pos] += data[pos]*gain;
  187. }
  188. if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
  189. continue;
  190. if(LIKELY(BufferSize-pos > 3))
  191. {
  192. ALsizei todo{(BufferSize-pos) >> 2};
  193. const __m128 gain4{_mm_set1_ps(gain)};
  194. do {
  195. const __m128 val4{_mm_load_ps(&data[pos])};
  196. __m128 dry4{_mm_load_ps(&dst[pos])};
  197. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  198. _mm_store_ps(&dst[pos], dry4);
  199. pos += 4;
  200. } while(--todo);
  201. }
  202. for(;pos < BufferSize;pos++)
  203. dst[pos] += data[pos]*gain;
  204. }
  205. }
  206. template<>
  207. void MixRow_<SSETag>(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*data)[BUFFERSIZE],
  208. const ALsizei InChans, const ALsizei InPos, const ALsizei BufferSize)
  209. {
  210. ASSUME(InChans > 0);
  211. ASSUME(BufferSize > 0);
  212. for(ALsizei c{0};c < InChans;c++)
  213. {
  214. const ALfloat *RESTRICT src{al::assume_aligned<16>(&data[c][InPos])};
  215. const ALfloat gain{Gains[c]};
  216. if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
  217. continue;
  218. ALsizei pos{0};
  219. if(LIKELY(BufferSize > 3))
  220. {
  221. ALsizei todo{BufferSize >> 2};
  222. const __m128 gain4 = _mm_set1_ps(gain);
  223. do {
  224. const __m128 val4{_mm_load_ps(&src[pos])};
  225. __m128 dry4{_mm_load_ps(&OutBuffer[pos])};
  226. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  227. _mm_store_ps(&OutBuffer[pos], dry4);
  228. pos += 4;
  229. } while(--todo);
  230. }
  231. for(;pos < BufferSize;pos++)
  232. OutBuffer[pos] += src[pos]*gain;
  233. }
  234. }