mixer_neon.c 11 KB

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