🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
11 KiB

  1. #include "config.h"
  2. #include <arm_neon.h>
  3. #include <limits>
  4. #include "AL/al.h"
  5. #include "AL/alc.h"
  6. #include "alMain.h"
  7. #include "alu.h"
  8. #include "hrtf.h"
  9. #include "defs.h"
  10. #include "hrtfbase.h"
  11. template<>
  12. const ALfloat *Resample_<LerpTag,NEONTag>(const InterpState* UNUSED(state),
  13. const ALfloat *RESTRICT src, ALsizei frac, ALint increment,
  14. ALfloat *RESTRICT dst, ALsizei dstlen)
  15. {
  16. const int32x4_t increment4 = vdupq_n_s32(increment*4);
  17. const float32x4_t fracOne4 = vdupq_n_f32(1.0f/FRACTIONONE);
  18. const int32x4_t fracMask4 = vdupq_n_s32(FRACTIONMASK);
  19. alignas(16) ALsizei pos_[4], frac_[4];
  20. int32x4_t pos4, frac4;
  21. ALsizei todo, pos, i;
  22. ASSUME(frac >= 0);
  23. ASSUME(increment > 0);
  24. ASSUME(dstlen > 0);
  25. InitiatePositionArrays(frac, increment, frac_, pos_, 4);
  26. frac4 = vld1q_s32(frac_);
  27. pos4 = vld1q_s32(pos_);
  28. todo = dstlen & ~3;
  29. for(i = 0;i < todo;i += 4)
  30. {
  31. const int pos0 = vgetq_lane_s32(pos4, 0);
  32. const int pos1 = vgetq_lane_s32(pos4, 1);
  33. const int pos2 = vgetq_lane_s32(pos4, 2);
  34. const int pos3 = vgetq_lane_s32(pos4, 3);
  35. const float32x4_t val1 = (float32x4_t){src[pos0], src[pos1], src[pos2], src[pos3]};
  36. const float32x4_t val2 = (float32x4_t){src[pos0+1], src[pos1+1], src[pos2+1], src[pos3+1]};
  37. /* val1 + (val2-val1)*mu */
  38. const float32x4_t r0 = vsubq_f32(val2, val1);
  39. const float32x4_t mu = vmulq_f32(vcvtq_f32_s32(frac4), fracOne4);
  40. const float32x4_t out = vmlaq_f32(val1, mu, r0);
  41. vst1q_f32(&dst[i], out);
  42. frac4 = vaddq_s32(frac4, increment4);
  43. pos4 = vaddq_s32(pos4, vshrq_n_s32(frac4, FRACTIONBITS));
  44. frac4 = vandq_s32(frac4, fracMask4);
  45. }
  46. /* NOTE: These four elements represent the position *after* the last four
  47. * samples, so the lowest element is the next position to resample.
  48. */
  49. pos = vgetq_lane_s32(pos4, 0);
  50. frac = vgetq_lane_s32(frac4, 0);
  51. for(;i < dstlen;++i)
  52. {
  53. dst[i] = lerp(src[pos], src[pos+1], frac * (1.0f/FRACTIONONE));
  54. frac += increment;
  55. pos += frac>>FRACTIONBITS;
  56. frac &= FRACTIONMASK;
  57. }
  58. return dst;
  59. }
  60. template<>
  61. const ALfloat *Resample_<BSincTag,NEONTag>(const InterpState *state, const ALfloat *RESTRICT src,
  62. ALsizei frac, ALint increment, ALfloat *RESTRICT dst, ALsizei dstlen)
  63. {
  64. const ALfloat *const filter = state->bsinc.filter;
  65. const float32x4_t sf4 = vdupq_n_f32(state->bsinc.sf);
  66. const ALsizei m = state->bsinc.m;
  67. const float32x4_t *fil, *scd, *phd, *spd;
  68. ALsizei pi, i, j, offset;
  69. float32x4_t r4;
  70. ALfloat pf;
  71. ASSUME(m > 0);
  72. ASSUME(dstlen > 0);
  73. ASSUME(increment > 0);
  74. ASSUME(frac >= 0);
  75. src -= state->bsinc.l;
  76. for(i = 0;i < dstlen;i++)
  77. {
  78. // Calculate the phase index and factor.
  79. #define FRAC_PHASE_BITDIFF (FRACTIONBITS-BSINC_PHASE_BITS)
  80. pi = frac >> FRAC_PHASE_BITDIFF;
  81. pf = (frac & ((1<<FRAC_PHASE_BITDIFF)-1)) * (1.0f/(1<<FRAC_PHASE_BITDIFF));
  82. #undef FRAC_PHASE_BITDIFF
  83. offset = m*pi*4;
  84. fil = (const float32x4_t*)(filter + offset); offset += m;
  85. scd = (const float32x4_t*)(filter + offset); offset += m;
  86. phd = (const float32x4_t*)(filter + offset); offset += m;
  87. spd = (const float32x4_t*)(filter + offset);
  88. // Apply the scale and phase interpolated filter.
  89. r4 = vdupq_n_f32(0.0f);
  90. {
  91. const ALsizei count = m >> 2;
  92. const float32x4_t pf4 = vdupq_n_f32(pf);
  93. ASSUME(count > 0);
  94. for(j = 0;j < count;j++)
  95. {
  96. /* f = ((fil + sf*scd) + pf*(phd + sf*spd)) */
  97. const float32x4_t f4 = vmlaq_f32(
  98. vmlaq_f32(fil[j], sf4, scd[j]),
  99. pf4, vmlaq_f32(phd[j], sf4, spd[j])
  100. );
  101. /* r += f*src */
  102. r4 = vmlaq_f32(r4, f4, vld1q_f32(&src[j*4]));
  103. }
  104. }
  105. r4 = vaddq_f32(r4, vcombine_f32(vrev64_f32(vget_high_f32(r4)),
  106. vrev64_f32(vget_low_f32(r4))));
  107. dst[i] = vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0);
  108. frac += increment;
  109. src += frac>>FRACTIONBITS;
  110. frac &= FRACTIONMASK;
  111. }
  112. return dst;
  113. }
  114. static inline void ApplyCoeffs(ALsizei /*Offset*/, float2 *RESTRICT Values, const ALsizei IrSize,
  115. const HrirArray<ALfloat> &Coeffs, const ALfloat left, const ALfloat right)
  116. {
  117. ASSUME(IrSize >= 2);
  118. float32x4_t leftright4;
  119. {
  120. float32x2_t leftright2 = vdup_n_f32(0.0);
  121. leftright2 = vset_lane_f32(left, leftright2, 0);
  122. leftright2 = vset_lane_f32(right, leftright2, 1);
  123. leftright4 = vcombine_f32(leftright2, leftright2);
  124. }
  125. for(ALsizei c{0};c < IrSize;c += 2)
  126. {
  127. float32x4_t vals = vcombine_f32(vld1_f32((float32_t*)&Values[c ][0]),
  128. vld1_f32((float32_t*)&Values[c+1][0]));
  129. float32x4_t coefs = vld1q_f32((float32_t*)&Coeffs[c][0]);
  130. vals = vmlaq_f32(vals, coefs, leftright4);
  131. vst1_f32((float32_t*)&Values[c ][0], vget_low_f32(vals));
  132. vst1_f32((float32_t*)&Values[c+1][0], vget_high_f32(vals));
  133. }
  134. }
  135. template<>
  136. void MixHrtf_<NEONTag>(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut, const ALfloat *data,
  137. float2 *RESTRICT AccumSamples, const ALsizei OutPos, const ALsizei IrSize,
  138. MixHrtfParams *hrtfparams, const ALsizei BufferSize)
  139. {
  140. MixHrtfBase<ApplyCoeffs>(LeftOut, RightOut, data, AccumSamples, OutPos, IrSize, hrtfparams,
  141. BufferSize);
  142. }
  143. template<>
  144. void MixHrtfBlend_<NEONTag>(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut,
  145. const ALfloat *data, float2 *RESTRICT AccumSamples, const ALsizei OutPos, const ALsizei IrSize,
  146. const HrtfParams *oldparams, MixHrtfParams *newparams, const ALsizei BufferSize)
  147. {
  148. MixHrtfBlendBase<ApplyCoeffs>(LeftOut, RightOut, data, AccumSamples, OutPos, IrSize, oldparams,
  149. newparams, BufferSize);
  150. }
  151. template<>
  152. void MixDirectHrtf_<NEONTag>(ALfloat *RESTRICT LeftOut, ALfloat *RESTRICT RightOut,
  153. const ALfloat (*data)[BUFFERSIZE], float2 *RESTRICT AccumSamples, DirectHrtfState *State,
  154. const ALsizei NumChans, const ALsizei BufferSize)
  155. {
  156. MixDirectHrtfBase<ApplyCoeffs>(LeftOut, RightOut, data, AccumSamples, State, NumChans,
  157. BufferSize);
  158. }
  159. template<>
  160. void Mix_<NEONTag>(const ALfloat *data, const ALsizei OutChans, ALfloat (*OutBuffer)[BUFFERSIZE],
  161. ALfloat *CurrentGains, const ALfloat *TargetGains, const ALsizei Counter, const ALsizei OutPos,
  162. const ALsizei BufferSize)
  163. {
  164. ASSUME(OutChans > 0);
  165. ASSUME(BufferSize > 0);
  166. const ALfloat delta{(Counter > 0) ? 1.0f/(ALfloat)Counter : 0.0f};
  167. for(ALsizei c{0};c < OutChans;c++)
  168. {
  169. ALfloat *RESTRICT dst{al::assume_aligned<16>(&OutBuffer[c][OutPos])};
  170. ALsizei pos{0};
  171. ALfloat gain{CurrentGains[c]};
  172. const ALfloat diff{TargetGains[c] - gain};
  173. if(std::fabs(diff) > std::numeric_limits<float>::epsilon())
  174. {
  175. ALsizei minsize{mini(BufferSize, Counter)};
  176. const ALfloat step{diff * delta};
  177. ALfloat step_count{0.0f};
  178. /* Mix with applying gain steps in aligned multiples of 4. */
  179. if(LIKELY(minsize > 3))
  180. {
  181. const float32x4_t four4{vdupq_n_f32(4.0f)};
  182. const float32x4_t step4{vdupq_n_f32(step)};
  183. const float32x4_t gain4{vdupq_n_f32(gain)};
  184. float32x4_t step_count4{vsetq_lane_f32(0.0f,
  185. vsetq_lane_f32(1.0f,
  186. vsetq_lane_f32(2.0f,
  187. vsetq_lane_f32(3.0f, vdupq_n_f32(0.0f), 3),
  188. 2), 1), 0
  189. )};
  190. ALsizei todo{minsize >> 2};
  191. do {
  192. const float32x4_t val4 = vld1q_f32(&data[pos]);
  193. float32x4_t dry4 = vld1q_f32(&dst[pos]);
  194. dry4 = vmlaq_f32(dry4, val4, vmlaq_f32(gain4, step4, step_count4));
  195. step_count4 = vaddq_f32(step_count4, four4);
  196. vst1q_f32(&dst[pos], dry4);
  197. pos += 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 = vgetq_lane_f32(step_count4, 0);
  204. }
  205. /* Mix with applying left over gain steps that aren't aligned multiples of 4. */
  206. for(;pos < minsize;pos++)
  207. {
  208. dst[pos] += data[pos]*(gain + step*step_count);
  209. step_count += 1.0f;
  210. }
  211. if(pos == Counter)
  212. gain = TargetGains[c];
  213. else
  214. gain += step*step_count;
  215. CurrentGains[c] = gain;
  216. /* Mix until pos is aligned with 4 or the mix is done. */
  217. minsize = mini(BufferSize, (pos+3)&~3);
  218. for(;pos < minsize;pos++)
  219. dst[pos] += data[pos]*gain;
  220. }
  221. if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
  222. continue;
  223. if(LIKELY(BufferSize-pos > 3))
  224. {
  225. ALsizei todo{(BufferSize-pos) >> 2};
  226. const float32x4_t gain4 = vdupq_n_f32(gain);
  227. do {
  228. const float32x4_t val4 = vld1q_f32(&data[pos]);
  229. float32x4_t dry4 = vld1q_f32(&dst[pos]);
  230. dry4 = vmlaq_f32(dry4, val4, gain4);
  231. vst1q_f32(&dst[pos], dry4);
  232. pos += 4;
  233. } while(--todo);
  234. }
  235. for(;pos < BufferSize;pos++)
  236. dst[pos] += data[pos]*gain;
  237. }
  238. }
  239. template<>
  240. void MixRow_<NEONTag>(ALfloat *OutBuffer, const ALfloat *Gains, const ALfloat (*data)[BUFFERSIZE],
  241. const ALsizei InChans, const ALsizei InPos, const ALsizei BufferSize)
  242. {
  243. ASSUME(InChans > 0);
  244. ASSUME(BufferSize > 0);
  245. for(ALsizei c{0};c < InChans;c++)
  246. {
  247. const ALfloat *RESTRICT src{al::assume_aligned<16>(&data[c][InPos])};
  248. ALsizei pos{0};
  249. const ALfloat gain{Gains[c]};
  250. if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
  251. continue;
  252. if(LIKELY(BufferSize > 3))
  253. {
  254. ALsizei todo{BufferSize >> 2};
  255. float32x4_t gain4{vdupq_n_f32(gain)};
  256. do {
  257. const float32x4_t val4 = vld1q_f32(&src[pos]);
  258. float32x4_t dry4 = vld1q_f32(&OutBuffer[pos]);
  259. dry4 = vmlaq_f32(dry4, val4, gain4);
  260. vst1q_f32(&OutBuffer[pos], dry4);
  261. pos += 4;
  262. } while(--todo);
  263. }
  264. for(;pos < BufferSize;pos++)
  265. OutBuffer[pos] += src[pos]*gain;
  266. }
  267. }