mixer_sse.c 7.8 KB

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