mixer_sse.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "config.h"
  2. #include <xmmintrin.h>
  3. #include <limits>
  4. #include "AL/al.h"
  5. #include "AL/alc.h"
  6. #include "alcmain.h"
  7. #include "alu.h"
  8. #include "defs.h"
  9. #include "hrtfbase.h"
  10. namespace {
  11. inline void ApplyCoeffs(float2 *RESTRICT Values, const ALuint IrSize, const HrirArray &Coeffs,
  12. const float left, const float right)
  13. {
  14. const __m128 lrlr{_mm_setr_ps(left, right, left, right)};
  15. ASSUME(IrSize >= 4);
  16. /* This isn't technically correct to test alignment, but it's true for
  17. * systems that support SSE, which is the only one that needs to know the
  18. * alignment of Values (which alternates between 8- and 16-byte aligned).
  19. */
  20. if(reinterpret_cast<intptr_t>(Values)&0x8)
  21. {
  22. __m128 imp0, imp1;
  23. __m128 coeffs{_mm_load_ps(&Coeffs[0][0])};
  24. __m128 vals{_mm_loadl_pi(_mm_setzero_ps(), reinterpret_cast<__m64*>(&Values[0][0]))};
  25. imp0 = _mm_mul_ps(lrlr, coeffs);
  26. vals = _mm_add_ps(imp0, vals);
  27. _mm_storel_pi(reinterpret_cast<__m64*>(&Values[0][0]), vals);
  28. ALuint i{1};
  29. for(;i < IrSize-1;i += 2)
  30. {
  31. coeffs = _mm_load_ps(&Coeffs[i+1][0]);
  32. vals = _mm_load_ps(&Values[i][0]);
  33. imp1 = _mm_mul_ps(lrlr, coeffs);
  34. imp0 = _mm_shuffle_ps(imp0, imp1, _MM_SHUFFLE(1, 0, 3, 2));
  35. vals = _mm_add_ps(imp0, vals);
  36. _mm_store_ps(&Values[i][0], vals);
  37. imp0 = imp1;
  38. }
  39. vals = _mm_loadl_pi(vals, reinterpret_cast<__m64*>(&Values[i][0]));
  40. imp0 = _mm_movehl_ps(imp0, imp0);
  41. vals = _mm_add_ps(imp0, vals);
  42. _mm_storel_pi(reinterpret_cast<__m64*>(&Values[i][0]), vals);
  43. }
  44. else
  45. {
  46. for(ALuint i{0};i < IrSize;i += 2)
  47. {
  48. __m128 coeffs{_mm_load_ps(&Coeffs[i][0])};
  49. __m128 vals{_mm_load_ps(&Values[i][0])};
  50. vals = _mm_add_ps(vals, _mm_mul_ps(lrlr, coeffs));
  51. _mm_store_ps(&Values[i][0], vals);
  52. }
  53. }
  54. }
  55. } // namespace
  56. template<>
  57. const ALfloat *Resample_<BSincTag,SSETag>(const InterpState *state, const ALfloat *RESTRICT src,
  58. ALuint frac, ALuint increment, const al::span<float> dst)
  59. {
  60. const float *const filter{state->bsinc.filter};
  61. const __m128 sf4{_mm_set1_ps(state->bsinc.sf)};
  62. const size_t m{state->bsinc.m};
  63. src -= state->bsinc.l;
  64. for(float &out_sample : dst)
  65. {
  66. // Calculate the phase index and factor.
  67. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  68. const ALuint pi{frac >> FRAC_PHASE_BITDIFF};
  69. const float pf{static_cast<float>(frac & ((1<<FRAC_PHASE_BITDIFF)-1)) *
  70. (1.0f/(1<<FRAC_PHASE_BITDIFF))};
  71. #undef FRAC_PHASE_BITDIFF
  72. // Apply the scale and phase interpolated filter.
  73. __m128 r4{_mm_setzero_ps()};
  74. {
  75. const __m128 pf4{_mm_set1_ps(pf)};
  76. const float *fil{filter + m*pi*4};
  77. const float *phd{fil + m};
  78. const float *scd{phd + m};
  79. const float *spd{scd + m};
  80. size_t td{m >> 2};
  81. size_t j{0u};
  82. #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
  83. do {
  84. /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
  85. const __m128 f4 = MLA4(
  86. MLA4(_mm_load_ps(fil), sf4, _mm_load_ps(scd)),
  87. pf4, MLA4(_mm_load_ps(phd), sf4, _mm_load_ps(spd)));
  88. fil += 4; scd += 4; phd += 4; spd += 4;
  89. /* r += f*src */
  90. r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j]));
  91. j += 4;
  92. } while(--td);
  93. #undef MLA4
  94. }
  95. r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
  96. r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
  97. out_sample = _mm_cvtss_f32(r4);
  98. frac += increment;
  99. src += frac>>FRACTIONBITS;
  100. frac &= FRACTIONMASK;
  101. }
  102. return dst.begin();
  103. }
  104. template<>
  105. const ALfloat *Resample_<FastBSincTag,SSETag>(const InterpState *state,
  106. const ALfloat *RESTRICT src, ALuint frac, ALuint increment, const al::span<float> dst)
  107. {
  108. const float *const filter{state->bsinc.filter};
  109. const size_t m{state->bsinc.m};
  110. src -= state->bsinc.l;
  111. for(float &out_sample : dst)
  112. {
  113. // Calculate the phase index and factor.
  114. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  115. const ALuint pi{frac >> FRAC_PHASE_BITDIFF};
  116. const float pf{static_cast<float>(frac & ((1<<FRAC_PHASE_BITDIFF)-1)) *
  117. (1.0f/(1<<FRAC_PHASE_BITDIFF))};
  118. #undef FRAC_PHASE_BITDIFF
  119. // Apply the phase interpolated filter.
  120. __m128 r4{_mm_setzero_ps()};
  121. {
  122. const __m128 pf4{_mm_set1_ps(pf)};
  123. const float *fil{filter + m*pi*4};
  124. const float *phd{fil + m};
  125. size_t td{m >> 2};
  126. size_t j{0u};
  127. #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
  128. do {
  129. /* f = fil + pf*phd */
  130. const __m128 f4 = MLA4(_mm_load_ps(fil), pf4, _mm_load_ps(phd));
  131. /* r += f*src */
  132. r4 = MLA4(r4, f4, _mm_loadu_ps(&src[j]));
  133. fil += 4; phd += 4; j += 4;
  134. } while(--td);
  135. #undef MLA4
  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>>FRACTIONBITS;
  142. frac &= FRACTIONMASK;
  143. }
  144. return dst.begin();
  145. }
  146. template<>
  147. void MixHrtf_<SSETag>(const float *InSamples, float2 *AccumSamples, const ALuint IrSize,
  148. 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 ALuint IrSize,
  152. const HrtfFilter *oldparams, MixHrtfFilter *newparams, const size_t BufferSize)
  153. {
  154. MixHrtfBlendBase<ApplyCoeffs>(InSamples, AccumSamples, IrSize, oldparams, newparams,
  155. BufferSize);
  156. }
  157. template<>
  158. void MixDirectHrtf_<SSETag>(FloatBufferLine &LeftOut, FloatBufferLine &RightOut,
  159. const al::span<const FloatBufferLine> InSamples, float2 *AccumSamples, DirectHrtfState *State,
  160. const size_t BufferSize)
  161. { MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, InSamples, AccumSamples, State, BufferSize); }
  162. template<>
  163. void Mix_<SSETag>(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutBuffer,
  164. float *CurrentGains, const float *TargetGains, const size_t Counter, const size_t OutPos)
  165. {
  166. const ALfloat delta{(Counter > 0) ? 1.0f / static_cast<ALfloat>(Counter) : 0.0f};
  167. const bool reached_target{InSamples.size() >= Counter};
  168. const auto min_end = reached_target ? InSamples.begin() + Counter : InSamples.end();
  169. const auto aligned_end = minz(static_cast<uintptr_t>(min_end-InSamples.begin()+3) & ~3u,
  170. InSamples.size()) + InSamples.begin();
  171. for(FloatBufferLine &output : OutBuffer)
  172. {
  173. ALfloat *RESTRICT dst{al::assume_aligned<16>(output.data()+OutPos)};
  174. ALfloat gain{*CurrentGains};
  175. const ALfloat diff{*TargetGains - gain};
  176. auto in_iter = InSamples.begin();
  177. if(std::fabs(diff) > std::numeric_limits<float>::epsilon())
  178. {
  179. const ALfloat step{diff * delta};
  180. ALfloat step_count{0.0f};
  181. /* Mix with applying gain steps in aligned multiples of 4. */
  182. if(ptrdiff_t todo{(min_end-in_iter) >> 2})
  183. {
  184. const __m128 four4{_mm_set1_ps(4.0f)};
  185. const __m128 step4{_mm_set1_ps(step)};
  186. const __m128 gain4{_mm_set1_ps(gain)};
  187. __m128 step_count4{_mm_setr_ps(0.0f, 1.0f, 2.0f, 3.0f)};
  188. do {
  189. const __m128 val4{_mm_load_ps(in_iter)};
  190. __m128 dry4{_mm_load_ps(dst)};
  191. #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
  192. /* dry += val * (gain + step*step_count) */
  193. dry4 = MLA4(dry4, val4, MLA4(gain4, step4, step_count4));
  194. #undef MLA4
  195. _mm_store_ps(dst, dry4);
  196. step_count4 = _mm_add_ps(step_count4, four4);
  197. in_iter += 4; dst += 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. while(in_iter != min_end)
  207. {
  208. *(dst++) += *(in_iter++) * (gain + step*step_count);
  209. step_count += 1.0f;
  210. }
  211. if(reached_target)
  212. gain = *TargetGains;
  213. else
  214. gain += step*step_count;
  215. *CurrentGains = gain;
  216. /* Mix until pos is aligned with 4 or the mix is done. */
  217. while(in_iter != aligned_end)
  218. *(dst++) += *(in_iter++) * gain;
  219. }
  220. ++CurrentGains;
  221. ++TargetGains;
  222. if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
  223. continue;
  224. if(ptrdiff_t todo{(InSamples.end()-in_iter) >> 2})
  225. {
  226. const __m128 gain4{_mm_set1_ps(gain)};
  227. do {
  228. const __m128 val4{_mm_load_ps(in_iter)};
  229. __m128 dry4{_mm_load_ps(dst)};
  230. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  231. _mm_store_ps(dst, dry4);
  232. in_iter += 4; dst += 4;
  233. } while(--todo);
  234. }
  235. while(in_iter != InSamples.end())
  236. *(dst++) += *(in_iter++) * gain;
  237. }
  238. }
  239. template<>
  240. void MixRow_<SSETag>(const al::span<float> OutBuffer, const al::span<const float> Gains,
  241. const float *InSamples, const size_t InStride)
  242. {
  243. for(const float gain : Gains)
  244. {
  245. const float *RESTRICT input{InSamples};
  246. InSamples += InStride;
  247. if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
  248. continue;
  249. auto out_iter = OutBuffer.begin();
  250. if(size_t todo{OutBuffer.size() >> 2})
  251. {
  252. const __m128 gain4 = _mm_set1_ps(gain);
  253. do {
  254. const __m128 val4{_mm_load_ps(input)};
  255. __m128 dry4{_mm_load_ps(out_iter)};
  256. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  257. _mm_store_ps(out_iter, dry4);
  258. out_iter += 4; input += 4;
  259. } while(--todo);
  260. }
  261. auto do_mix = [gain](const float cur, const float src) noexcept -> float
  262. { return cur + src*gain; };
  263. std::transform(out_iter, OutBuffer.end(), input, out_iter, do_mix);
  264. }
  265. }