mixer_neon.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "config.h"
  2. #include <arm_neon.h>
  3. #include "AL/al.h"
  4. #include "AL/alc.h"
  5. #include "alMain.h"
  6. #include "alu.h"
  7. #include "hrtf.h"
  8. #include "defs.h"
  9. const ALfloat *Resample_lerp_Neon(const InterpState* UNUSED(state),
  10. const ALfloat *restrict src, ALsizei frac, ALint increment,
  11. ALfloat *restrict dst, ALsizei numsamples)
  12. {
  13. const int32x4_t increment4 = vdupq_n_s32(increment*4);
  14. const float32x4_t fracOne4 = vdupq_n_f32(1.0f/FRACTIONONE);
  15. const int32x4_t fracMask4 = vdupq_n_s32(FRACTIONMASK);
  16. alignas(16) ALint pos_[4];
  17. alignas(16) ALsizei frac_[4];
  18. int32x4_t pos4;
  19. int32x4_t frac4;
  20. ALsizei i;
  21. ASSUME(numsamples > 0);
  22. InitiatePositionArrays(frac, increment, frac_, pos_, 4);
  23. frac4 = vld1q_s32(frac_);
  24. pos4 = vld1q_s32(pos_);
  25. for(i = 0;numsamples-i > 3;i += 4)
  26. {
  27. const float32x4_t val1 = (float32x4_t){src[pos_[0]], src[pos_[1]], src[pos_[2]], src[pos_[3]]};
  28. const float32x4_t val2 = (float32x4_t){src[pos_[0]+1], src[pos_[1]+1], src[pos_[2]+1], src[pos_[3]+1]};
  29. /* val1 + (val2-val1)*mu */
  30. const float32x4_t r0 = vsubq_f32(val2, val1);
  31. const float32x4_t mu = vmulq_f32(vcvtq_f32_s32(frac4), fracOne4);
  32. const float32x4_t out = vmlaq_f32(val1, mu, r0);
  33. vst1q_f32(&dst[i], out);
  34. frac4 = vaddq_s32(frac4, increment4);
  35. pos4 = vaddq_s32(pos4, vshrq_n_s32(frac4, FRACTIONBITS));
  36. frac4 = vandq_s32(frac4, fracMask4);
  37. vst1q_s32(pos_, pos4);
  38. }
  39. if(i < numsamples)
  40. {
  41. /* NOTE: These four elements represent the position *after* the last
  42. * four samples, so the lowest element is the next position to
  43. * resample.
  44. */
  45. ALint pos = pos_[0];
  46. frac = vgetq_lane_s32(frac4, 0);
  47. do {
  48. dst[i] = lerp(src[pos], src[pos+1], frac * (1.0f/FRACTIONONE));
  49. frac += increment;
  50. pos += frac>>FRACTIONBITS;
  51. frac &= FRACTIONMASK;
  52. } while(++i < numsamples);
  53. }
  54. return dst;
  55. }
  56. const ALfloat *Resample_bsinc_Neon(const InterpState *state,
  57. const ALfloat *restrict src, ALsizei frac, ALint increment,
  58. ALfloat *restrict dst, ALsizei dstlen)
  59. {
  60. const ALfloat *const filter = state->bsinc.filter;
  61. const float32x4_t sf4 = vdupq_n_f32(state->bsinc.sf);
  62. const ALsizei m = state->bsinc.m;
  63. const float32x4_t *fil, *scd, *phd, *spd;
  64. ALsizei pi, i, j, offset;
  65. float32x4_t r4;
  66. ALfloat pf;
  67. ASSUME(m > 0);
  68. ASSUME(dstlen > 0);
  69. src += state->bsinc.l;
  70. for(i = 0;i < dstlen;i++)
  71. {
  72. // Calculate the phase index and factor.
  73. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  74. pi = frac >> FRAC_PHASE_BITDIFF;
  75. pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
  76. #undef FRAC_PHASE_BITDIFF
  77. offset = m*pi*4;
  78. fil = ASSUME_ALIGNED(filter + offset, 16); offset += m;
  79. scd = ASSUME_ALIGNED(filter + offset, 16); offset += m;
  80. phd = ASSUME_ALIGNED(filter + offset, 16); offset += m;
  81. spd = ASSUME_ALIGNED(filter + offset, 16);
  82. // Apply the scale and phase interpolated filter.
  83. r4 = vdupq_n_f32(0.0f);
  84. {
  85. const float32x4_t pf4 = vdupq_n_f32(pf);
  86. for(j = 0;j < m;j+=4,fil++,scd++,phd++,spd++)
  87. {
  88. /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
  89. const float32x4_t f4 = vmlaq_f32(
  90. vmlaq_f32(*fil, sf4, *scd),
  91. pf4, vmlaq_f32(*phd, sf4, *spd)
  92. );
  93. /* r += f*src */
  94. r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j]));
  95. }
  96. }
  97. r4 = vaddq_f32(r4, vcombine_f32(vrev64_f32(vget_high_f32(r4)),
  98. vrev64_f32(vget_low_f32(r4))));
  99. dst[i] = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0);
  100. frac += increment;
  101. src += frac>>FRACTIONBITS;
  102. frac &= FRACTIONMASK;
  103. }
  104. return dst;
  105. }
  106. static inline void ApplyCoeffs(ALsizei Offset, ALfloat (*restrict Values)[2],
  107. const ALsizei IrSize,
  108. const ALfloat (*restrict Coeffs)[2],
  109. ALfloat left, ALfloat right)
  110. {
  111. ALsizei c;
  112. float32x4_t leftright4;
  113. {
  114. float32x2_t leftright2 = vdup_n_f32(0.0);
  115. leftright2 = vset_lane_f32(left, leftright2, 0);
  116. leftright2 = vset_lane_f32(right, leftright2, 1);
  117. leftright4 = vcombine_f32(leftright2, leftright2);
  118. }
  119. Values = ASSUME_ALIGNED(Values, 16);
  120. Coeffs = ASSUME_ALIGNED(Coeffs, 16);
  121. for(c = 0;c < IrSize;c += 2)
  122. {
  123. const ALsizei o0 = (Offset+c)&HRIR_MASK;
  124. const ALsizei o1 = (o0+1)&HRIR_MASK;
  125. float32x4_t vals = vcombine_f32(vld1_f32((float32_t*)&Values[o0][0]),
  126. vld1_f32((float32_t*)&Values[o1][0]));
  127. float32x4_t coefs = vld1q_f32((float32_t*)&Coeffs[c][0]);
  128. vals = vmlaq_f32(vals, coefs, leftright4);
  129. vst1_f32((float32_t*)&Values[o0][0], vget_low_f32(vals));
  130. vst1_f32((float32_t*)&Values[o1][0], vget_high_f32(vals));
  131. }
  132. }
  133. #define MixHrtf MixHrtf_Neon
  134. #define MixHrtfBlend MixHrtfBlend_Neon
  135. #define MixDirectHrtf MixDirectHrtf_Neon
  136. #include "hrtf_inc.c"
  137. void Mix_Neon(const ALfloat *data, ALsizei OutChans, ALfloat (*restrict OutBuffer)[BUFFERSIZE],
  138. ALfloat *CurrentGains, const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
  139. ALsizei BufferSize)
  140. {
  141. ALfloat gain, delta, step;
  142. float32x4_t gain4;
  143. ALsizei c;
  144. ASSUME(OutChans > 0);
  145. ASSUME(BufferSize > 0);
  146. data = ASSUME_ALIGNED(data, 16);
  147. OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
  148. delta = (Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f;
  149. for(c = 0;c < OutChans;c++)
  150. {
  151. ALsizei pos = 0;
  152. gain = CurrentGains[c];
  153. step = (TargetGains[c] - gain) * delta;
  154. if(fabsf(step) > FLT_EPSILON)
  155. {
  156. ALsizei minsize = mini(BufferSize, Counter);
  157. /* Mix with applying gain steps in aligned multiples of 4. */
  158. if(minsize-pos > 3)
  159. {
  160. float32x4_t step4;
  161. gain4 = vsetq_lane_f32(gain, gain4, 0);
  162. gain4 = vsetq_lane_f32(gain + step, gain4, 1);
  163. gain4 = vsetq_lane_f32(gain + step + step, gain4, 2);
  164. gain4 = vsetq_lane_f32(gain + step + step + step, gain4, 3);
  165. step4 = vdupq_n_f32(step + step + step + step);
  166. do {
  167. const float32x4_t val4 = vld1q_f32(&data[pos]);
  168. float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
  169. dry4 = vmlaq_f32(dry4, val4, gain4);
  170. gain4 = vaddq_f32(gain4, step4);
  171. vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
  172. pos += 4;
  173. } while(minsize-pos > 3);
  174. /* NOTE: gain4 now represents the next four gains after the
  175. * last four mixed samples, so the lowest element represents
  176. * the next gain to apply.
  177. */
  178. gain = vgetq_lane_f32(gain4, 0);
  179. }
  180. /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
  181. for(;pos < minsize;pos++)
  182. {
  183. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  184. gain += step;
  185. }
  186. if(pos == Counter)
  187. gain = TargetGains[c];
  188. CurrentGains[c] = gain;
  189. /* Mix until pos is aligned with 4 or the mix is done. */
  190. minsize = mini(BufferSize, (pos+3)&~3);
  191. for(;pos < minsize;pos++)
  192. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  193. }
  194. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  195. continue;
  196. gain4 = vdupq_n_f32(gain);
  197. for(;BufferSize-pos > 3;pos += 4)
  198. {
  199. const float32x4_t val4 = vld1q_f32(&data[pos]);
  200. float32x4_t dry4 = vld1q_f32(&OutBuffer[c][OutPos+pos]);
  201. dry4 = vmlaq_f32(dry4, val4, gain4);
  202. vst1q_f32(&OutBuffer[c][OutPos+pos], dry4);
  203. }
  204. for(;pos < BufferSize;pos++)
  205. OutBuffer[c][OutPos+pos] += data[pos]*gain;
  206. }
  207. }
  208. void MixRow_Neon(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans, ALsizei InPos, ALsizei BufferSize)
  209. {
  210. float32x4_t gain4;
  211. ALsizei c;
  212. ASSUME(InChans > 0);
  213. ASSUME(BufferSize > 0);
  214. data = ASSUME_ALIGNED(data, 16);
  215. OutBuffer = ASSUME_ALIGNED(OutBuffer, 16);
  216. for(c = 0;c < InChans;c++)
  217. {
  218. ALsizei pos = 0;
  219. ALfloat gain = Gains[c];
  220. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  221. continue;
  222. gain4 = vdupq_n_f32(gain);
  223. for(;BufferSize-pos > 3;pos += 4)
  224. {
  225. const float32x4_t val4 = vld1q_f32(&data[c][InPos+pos]);
  226. float32x4_t dry4 = vld1q_f32(&OutBuffer[pos]);
  227. dry4 = vmlaq_f32(dry4, val4, gain4);
  228. vst1q_f32(&OutBuffer[pos], dry4);
  229. }
  230. for(;pos < BufferSize;pos++)
  231. OutBuffer[pos] += data[c][InPos+pos]*gain;
  232. }
  233. }