phase_shifter.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #ifndef PHASE_SHIFTER_H
  2. #define PHASE_SHIFTER_H
  3. #include "config_simd.h"
  4. #if HAVE_SSE_INTRINSICS
  5. #include <xmmintrin.h>
  6. #elif HAVE_NEON
  7. #include <arm_neon.h>
  8. #endif
  9. #include <array>
  10. #include <cmath>
  11. #include <cstddef>
  12. #include "alnumbers.h"
  13. #include "alspan.h"
  14. #include "opthelpers.h"
  15. /* Implements a wide-band +90 degree phase-shift. Note that this should be
  16. * given one sample less of a delay (FilterSize/2 - 1) compared to the direct
  17. * signal delay (FilterSize/2) to properly align.
  18. */
  19. template<std::size_t FilterSize>
  20. struct SIMDALIGN PhaseShifterT {
  21. static_assert(FilterSize >= 16, "FilterSize needs to be at least 16");
  22. static_assert((FilterSize&(FilterSize-1)) == 0, "FilterSize needs to be power-of-two");
  23. alignas(16) std::array<float,FilterSize/2> mCoeffs{};
  24. PhaseShifterT()
  25. {
  26. /* Every other coefficient is 0, so we only need to calculate and store
  27. * the non-0 terms and double-step over the input to apply it. The
  28. * calculated coefficients are in reverse to make applying in the time-
  29. * domain more efficient.
  30. */
  31. for(std::size_t i{0};i < FilterSize/2;++i)
  32. {
  33. const auto k = static_cast<int>(i*2 + 1) - int{FilterSize/2};
  34. /* Calculate the Blackman window value for this coefficient. */
  35. const auto w = 2.0*al::numbers::pi/double{FilterSize} * static_cast<double>(i*2 + 1);
  36. const auto window = 0.3635819 - 0.4891775*std::cos(w) + 0.1365995*std::cos(2.0*w)
  37. - 0.0106411*std::cos(3.0*w);
  38. const auto pk = al::numbers::pi * static_cast<double>(k);
  39. mCoeffs[i] = static_cast<float>(window * (1.0-std::cos(pk)) / pk);
  40. }
  41. }
  42. void process(const al::span<float> dst, const al::span<const float> src) const;
  43. private:
  44. #if HAVE_NEON
  45. static auto load4(float32_t a, float32_t b, float32_t c, float32_t d)
  46. {
  47. float32x4_t ret{vmovq_n_f32(a)};
  48. ret = vsetq_lane_f32(b, ret, 1);
  49. ret = vsetq_lane_f32(c, ret, 2);
  50. ret = vsetq_lane_f32(d, ret, 3);
  51. return ret;
  52. }
  53. static void vtranspose4(float32x4_t &x0, float32x4_t &x1, float32x4_t &x2, float32x4_t &x3)
  54. {
  55. float32x4x2_t t0_{vzipq_f32(x0, x2)};
  56. float32x4x2_t t1_{vzipq_f32(x1, x3)};
  57. float32x4x2_t u0_{vzipq_f32(t0_.val[0], t1_.val[0])};
  58. float32x4x2_t u1_{vzipq_f32(t0_.val[1], t1_.val[1])};
  59. x0 = u0_.val[0];
  60. x1 = u0_.val[1];
  61. x2 = u1_.val[0];
  62. x3 = u1_.val[1];
  63. }
  64. #endif
  65. };
  66. template<std::size_t S>
  67. NOINLINE inline
  68. void PhaseShifterT<S>::process(const al::span<float> dst, const al::span<const float> src) const
  69. {
  70. auto in = src.begin();
  71. #if HAVE_SSE_INTRINSICS
  72. if(const std::size_t todo{dst.size()>>2})
  73. {
  74. auto out = al::span{reinterpret_cast<__m128*>(dst.data()), todo};
  75. std::generate(out.begin(), out.end(), [&in,this]
  76. {
  77. __m128 r0{_mm_setzero_ps()};
  78. __m128 r1{_mm_setzero_ps()};
  79. __m128 r2{_mm_setzero_ps()};
  80. __m128 r3{_mm_setzero_ps()};
  81. for(std::size_t j{0};j < mCoeffs.size();j+=4)
  82. {
  83. const __m128 coeffs{_mm_load_ps(&mCoeffs[j])};
  84. const __m128 s0{_mm_loadu_ps(&in[j*2])};
  85. const __m128 s1{_mm_loadu_ps(&in[j*2 + 4])};
  86. const __m128 s2{_mm_movehl_ps(_mm_movelh_ps(s1, s1), s0)};
  87. const __m128 s3{_mm_loadh_pi(_mm_movehl_ps(s1, s1),
  88. reinterpret_cast<const __m64*>(&in[j*2 + 8]))};
  89. __m128 s{_mm_shuffle_ps(s0, s1, _MM_SHUFFLE(2, 0, 2, 0))};
  90. r0 = _mm_add_ps(r0, _mm_mul_ps(s, coeffs));
  91. s = _mm_shuffle_ps(s0, s1, _MM_SHUFFLE(3, 1, 3, 1));
  92. r1 = _mm_add_ps(r1, _mm_mul_ps(s, coeffs));
  93. s = _mm_shuffle_ps(s2, s3, _MM_SHUFFLE(2, 0, 2, 0));
  94. r2 = _mm_add_ps(r2, _mm_mul_ps(s, coeffs));
  95. s = _mm_shuffle_ps(s2, s3, _MM_SHUFFLE(3, 1, 3, 1));
  96. r3 = _mm_add_ps(r3, _mm_mul_ps(s, coeffs));
  97. }
  98. in += 4;
  99. _MM_TRANSPOSE4_PS(r0, r1, r2, r3);
  100. return _mm_add_ps(_mm_add_ps(r0, r1), _mm_add_ps(r2, r3));
  101. });
  102. }
  103. if(const std::size_t todo{dst.size()&3})
  104. {
  105. auto out = dst.last(todo);
  106. std::generate(out.begin(), out.end(), [&in,this]
  107. {
  108. __m128 r4{_mm_setzero_ps()};
  109. for(std::size_t j{0};j < mCoeffs.size();j+=4)
  110. {
  111. const __m128 coeffs{_mm_load_ps(&mCoeffs[j])};
  112. const __m128 s{_mm_setr_ps(in[j*2], in[j*2 + 2], in[j*2 + 4], in[j*2 + 6])};
  113. r4 = _mm_add_ps(r4, _mm_mul_ps(s, coeffs));
  114. }
  115. ++in;
  116. r4 = _mm_add_ps(r4, _mm_shuffle_ps(r4, r4, _MM_SHUFFLE(0, 1, 2, 3)));
  117. r4 = _mm_add_ps(r4, _mm_movehl_ps(r4, r4));
  118. return _mm_cvtss_f32(r4);
  119. });
  120. }
  121. #elif HAVE_NEON
  122. if(const std::size_t todo{dst.size()>>2})
  123. {
  124. auto out = al::span{reinterpret_cast<float32x4_t*>(dst.data()), todo};
  125. std::generate(out.begin(), out.end(), [&in,this]
  126. {
  127. float32x4_t r0{vdupq_n_f32(0.0f)};
  128. float32x4_t r1{vdupq_n_f32(0.0f)};
  129. float32x4_t r2{vdupq_n_f32(0.0f)};
  130. float32x4_t r3{vdupq_n_f32(0.0f)};
  131. for(std::size_t j{0};j < mCoeffs.size();j+=4)
  132. {
  133. const float32x4_t coeffs{vld1q_f32(&mCoeffs[j])};
  134. const float32x4_t s0{vld1q_f32(&in[j*2])};
  135. const float32x4_t s1{vld1q_f32(&in[j*2 + 4])};
  136. const float32x4_t s2{vcombine_f32(vget_high_f32(s0), vget_low_f32(s1))};
  137. const float32x4_t s3{vcombine_f32(vget_high_f32(s1), vld1_f32(&in[j*2 + 8]))};
  138. const float32x4x2_t values0{vuzpq_f32(s0, s1)};
  139. const float32x4x2_t values1{vuzpq_f32(s2, s3)};
  140. r0 = vmlaq_f32(r0, values0.val[0], coeffs);
  141. r1 = vmlaq_f32(r1, values0.val[1], coeffs);
  142. r2 = vmlaq_f32(r2, values1.val[0], coeffs);
  143. r3 = vmlaq_f32(r3, values1.val[1], coeffs);
  144. }
  145. in += 4;
  146. vtranspose4(r0, r1, r2, r3);
  147. return vaddq_f32(vaddq_f32(r0, r1), vaddq_f32(r2, r3));
  148. });
  149. }
  150. if(const std::size_t todo{dst.size()&3})
  151. {
  152. auto out = dst.last(todo);
  153. std::generate(out.begin(), out.end(), [&in,this]
  154. {
  155. float32x4_t r4{vdupq_n_f32(0.0f)};
  156. for(std::size_t j{0};j < mCoeffs.size();j+=4)
  157. {
  158. const float32x4_t coeffs{vld1q_f32(&mCoeffs[j])};
  159. const float32x4_t s{load4(in[j*2], in[j*2 + 2], in[j*2 + 4], in[j*2 + 6])};
  160. r4 = vmlaq_f32(r4, s, coeffs);
  161. }
  162. ++in;
  163. r4 = vaddq_f32(r4, vrev64q_f32(r4));
  164. return vget_lane_f32(vadd_f32(vget_low_f32(r4), vget_high_f32(r4)), 0);
  165. });
  166. }
  167. #else
  168. std::generate(dst.begin(), dst.end(), [&in,this]
  169. {
  170. float ret{0.0f};
  171. for(std::size_t j{0};j < mCoeffs.size();++j)
  172. ret += in[j*2] * mCoeffs[j];
  173. ++in;
  174. return ret;
  175. });
  176. #endif
  177. }
  178. #endif /* PHASE_SHIFTER_H */