mixer_sse.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #include "config.h"
  2. #include <xmmintrin.h>
  3. #include "AL/al.h"
  4. #include "AL/alc.h"
  5. #include "alMain.h"
  6. #include "alu.h"
  7. #include "alSource.h"
  8. #include "alAuxEffectSlot.h"
  9. #include "mixer_defs.h"
  10. const ALfloat *Resample_bsinc32_SSE(const InterpState *state, const ALfloat *restrict src,
  11. ALsizei frac, ALint increment, ALfloat *restrict dst,
  12. ALsizei dstlen)
  13. {
  14. const __m128 sf4 = _mm_set1_ps(state->bsinc.sf);
  15. const ALsizei m = state->bsinc.m;
  16. const ALfloat *fil, *scd, *phd, *spd;
  17. ALsizei pi, i, j;
  18. ALfloat pf;
  19. __m128 r4;
  20. src += state->bsinc.l;
  21. for(i = 0;i < dstlen;i++)
  22. {
  23. // Calculate the phase index and factor.
  24. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  25. pi = frac >> FRAC_PHASE_BITDIFF;
  26. pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
  27. #undef FRAC_PHASE_BITDIFF
  28. fil = ASSUME_ALIGNED(state->bsinc.coeffs[pi].filter, 16);
  29. scd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].scDelta, 16);
  30. phd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].phDelta, 16);
  31. spd = ASSUME_ALIGNED(state->bsinc.coeffs[pi].spDelta, 16);
  32. // Apply the scale and phase interpolated filter.
  33. r4 = _mm_setzero_ps();
  34. {
  35. const __m128 pf4 = _mm_set1_ps(pf);
  36. #define LD4(x) _mm_load_ps(x)
  37. #define ULD4(x) _mm_loadu_ps(x)
  38. #define MLA4(x, y, z) _mm_add_ps(x, _mm_mul_ps(y, z))
  39. for(j = 0;j < m;j+=4)
  40. {
  41. /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
  42. const __m128 f4 = MLA4(MLA4(LD4(&fil[j]), sf4, LD4(&scd[j])),
  43. pf4, MLA4(LD4(&phd[j]), sf4, LD4(&spd[j]))
  44. );
  45. /* r += f*src */
  46. r4 = MLA4(r4, f4, ULD4(&src[j]));
  47. }
  48. #undef MLA4
  49. #undef ULD4
  50. #undef LD4
  51. }
  52. r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
  53. r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
  54. dst[i] = _mm_cvtss_f32(r4);
  55. frac += increment;
  56. src += frac>>FRACTIONBITS;
  57. frac &= FRACTIONMASK;
  58. }
  59. return dst;
  60. }
  61. static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
  62. const ALsizei IrSize,
  63. const ALfloat (*restrict Coeffs)[2],
  64. ALfloat left, ALfloat right)
  65. {
  66. const __m128 lrlr = _mm_setr_ps(left, right, left, right);
  67. __m128 vals = _mm_setzero_ps();
  68. __m128 coeffs;
  69. ALsizei i;
  70. Values = ASSUME_ALIGNED(Values, 16);
  71. Coeffs = ASSUME_ALIGNED(Coeffs, 16);
  72. if((Offset&1))
  73. {
  74. const ALsizei o0 = Offset&HRIR_MASK;
  75. const ALsizei o1 = (Offset+IrSize-1)&HRIR_MASK;
  76. __m128 imp0, imp1;
  77. coeffs = _mm_load_ps(&Coeffs[0][0]);
  78. vals = _mm_loadl_pi(vals, (__m64*)&Values[o0][0]);
  79. imp0 = _mm_mul_ps(lrlr, coeffs);
  80. vals = _mm_add_ps(imp0, vals);
  81. _mm_storel_pi((__m64*)&Values[o0][0], vals);
  82. for(i = 1;i < IrSize-1;i += 2)
  83. {
  84. const ALsizei o2 = (Offset+i)&HRIR_MASK;
  85. coeffs = _mm_load_ps(&Coeffs[i+1][0]);
  86. vals = _mm_load_ps(&Values[o2][0]);
  87. imp1 = _mm_mul_ps(lrlr, coeffs);
  88. imp0 = _mm_shuffle_ps(imp0, imp1, _MM_SHUFFLE(1, 0, 3, 2));
  89. vals = _mm_add_ps(imp0, vals);
  90. _mm_store_ps(&Values[o2][0], vals);
  91. imp0 = imp1;
  92. }
  93. vals = _mm_loadl_pi(vals, (__m64*)&Values[o1][0]);
  94. imp0 = _mm_movehl_ps(imp0, imp0);
  95. vals = _mm_add_ps(imp0, vals);
  96. _mm_storel_pi((__m64*)&Values[o1][0], vals);
  97. }
  98. else
  99. {
  100. for(i = 0;i < IrSize;i += 2)
  101. {
  102. const ALsizei o = (Offset + i)&HRIR_MASK;
  103. coeffs = _mm_load_ps(&Coeffs[i][0]);
  104. vals = _mm_load_ps(&Values[o][0]);
  105. vals = _mm_add_ps(vals, _mm_mul_ps(lrlr, coeffs));
  106. _mm_store_ps(&Values[o][0], vals);
  107. }
  108. }
  109. }
  110. #define MixHrtf MixHrtf_SSE
  111. #define MixHrtfBlend MixHrtfBlend_SSE
  112. #define MixDirectHrtf MixDirectHrtf_SSE
  113. #include "mixer_inc.c"
  114. #undef MixHrtf
  115. void Mix_SSE(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
  116. ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
  117. ALsizei BufferSize)
  118. {
  119. ALfloat gain, delta, step;
  120. __m128 gain4;
  121. ALsizei c;
  122. delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
  123. for(c = 0;c < OutChans;c++)
  124. {
  125. ALsizei pos = 0;
  126. gain = CurrentGains[c];
  127. step = (TargetGains[c] - gain) * delta;
  128. if(fabsf(step) > FLT_EPSILON)
  129. {
  130. ALsizei minsize = mini(BufferSize, Counter);
  131. /* Mix with applying gain steps in aligned multiples of 4. */
  132. if(minsize-pos > 3)
  133. {
  134. __m128 step4;
  135. gain4 = _mm_setr_ps(
  136. gain,
  137. gain + step,
  138. gain + step + step,
  139. gain + step + step + step
  140. );
  141. step4 = _mm_set1_ps(step + step + step + step);
  142. do {
  143. const __m128 val4 = _mm_load_ps(&data[pos]);
  144. __m128 dry4 = _mm_load_ps(&OutBuffer[c][OutPos+pos]);
  145. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  146. gain4 = _mm_add_ps(gain4, step4);
  147. _mm_store_ps(&OutBuffer[c][OutPos+pos], dry4);
  148. pos += 4;
  149. } while(minsize-pos > 3);
  150. /* NOTE: gain4 now represents the next four gains after the
  151. * last four mixed samples, so the lowest element represents
  152. * the next gain to apply.
  153. */
  154. gain = _mm_cvtss_f32(gain4);
  155. }
  156. /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
  157. for(;pos < minsize;pos++)
  158. {
  159. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  160. gain += step;
  161. }
  162. if(pos == Counter)
  163. gain = TargetGains[c];
  164. CurrentGains[c] = gain;
  165. /* Mix until pos is aligned with 4 or the mix is done. */
  166. minsize = mini(BufferSize, (pos+3)&~3);
  167. for(;pos < minsize;pos++)
  168. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  169. }
  170. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  171. continue;
  172. gain4 = _mm_set1_ps(gain);
  173. for(;BufferSize-pos > 3;pos += 4)
  174. {
  175. const __m128 val4 = _mm_load_ps(&data[pos]);
  176. __m128 dry4 = _mm_load_ps(&OutBuffer[c][OutPos+pos]);
  177. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  178. _mm_store_ps(&OutBuffer[c][OutPos+pos], dry4);
  179. }
  180. for(;pos < BufferSize;pos++)
  181. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  182. }
  183. }
  184. void MixRow_SSE(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
  185. {
  186. __m128 gain4;
  187. ALsizei c;
  188. for(c = 0;c < InChans;c++)
  189. {
  190. ALsizei pos = 0;
  191. ALfloat gain = Gains[c];
  192. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  193. continue;
  194. gain4 = _mm_set1_ps(gain);
  195. for(;BufferSize-pos > 3;pos += 4)
  196. {
  197. const __m128 val4 = _mm_load_ps(&data[c][InPos+pos]);
  198. __m128 dry4 = _mm_load_ps(&OutBuffer[pos]);
  199. dry4 = _mm_add_ps(dry4, _mm_mul_ps(val4, gain4));
  200. _mm_store_ps(&OutBuffer[pos], dry4);
  201. }
  202. for(;pos < BufferSize;pos++)
  203. OutBuffer[pos] += data[c][InPos+pos]*gain;
  204. }
  205. }