vector_math.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_PROCESSING_AEC3_VECTOR_MATH_H_
  11. #define MODULES_AUDIO_PROCESSING_AEC3_VECTOR_MATH_H_
  12. // Defines WEBRTC_ARCH_X86_FAMILY, used below.
  13. #include "rtc_base/system/arch.h"
  14. #if defined(WEBRTC_HAS_NEON)
  15. #include <arm_neon.h>
  16. #endif
  17. #if defined(WEBRTC_ARCH_X86_FAMILY)
  18. #include <emmintrin.h>
  19. #endif
  20. #include <math.h>
  21. #include <algorithm>
  22. #include <array>
  23. #include <functional>
  24. #include "api/array_view.h"
  25. #include "modules/audio_processing/aec3/aec3_common.h"
  26. #include "rtc_base/checks.h"
  27. namespace webrtc {
  28. namespace aec3 {
  29. // Provides optimizations for mathematical operations based on vectors.
  30. class VectorMath {
  31. public:
  32. explicit VectorMath(Aec3Optimization optimization)
  33. : optimization_(optimization) {}
  34. // Elementwise square root.
  35. void SqrtAVX2(rtc::ArrayView<float> x);
  36. void Sqrt(rtc::ArrayView<float> x) {
  37. switch (optimization_) {
  38. #if defined(WEBRTC_ARCH_X86_FAMILY)
  39. case Aec3Optimization::kSse2: {
  40. const int x_size = static_cast<int>(x.size());
  41. const int vector_limit = x_size >> 2;
  42. int j = 0;
  43. for (; j < vector_limit * 4; j += 4) {
  44. __m128 g = _mm_loadu_ps(&x[j]);
  45. g = _mm_sqrt_ps(g);
  46. _mm_storeu_ps(&x[j], g);
  47. }
  48. for (; j < x_size; ++j) {
  49. x[j] = sqrtf(x[j]);
  50. }
  51. } break;
  52. case Aec3Optimization::kAvx2:
  53. SqrtAVX2(x);
  54. break;
  55. #endif
  56. #if defined(WEBRTC_HAS_NEON)
  57. case Aec3Optimization::kNeon: {
  58. const int x_size = static_cast<int>(x.size());
  59. const int vector_limit = x_size >> 2;
  60. int j = 0;
  61. for (; j < vector_limit * 4; j += 4) {
  62. float32x4_t g = vld1q_f32(&x[j]);
  63. #if !defined(WEBRTC_ARCH_ARM64)
  64. float32x4_t y = vrsqrteq_f32(g);
  65. // Code to handle sqrt(0).
  66. // If the input to sqrtf() is zero, a zero will be returned.
  67. // If the input to vrsqrteq_f32() is zero, positive infinity is
  68. // returned.
  69. const uint32x4_t vec_p_inf = vdupq_n_u32(0x7F800000);
  70. // check for divide by zero
  71. const uint32x4_t div_by_zero =
  72. vceqq_u32(vec_p_inf, vreinterpretq_u32_f32(y));
  73. // zero out the positive infinity results
  74. y = vreinterpretq_f32_u32(
  75. vandq_u32(vmvnq_u32(div_by_zero), vreinterpretq_u32_f32(y)));
  76. // from arm documentation
  77. // The Newton-Raphson iteration:
  78. // y[n+1] = y[n] * (3 - d * (y[n] * y[n])) / 2)
  79. // converges to (1/√d) if y0 is the result of VRSQRTE applied to d.
  80. //
  81. // Note: The precision did not improve after 2 iterations.
  82. for (int i = 0; i < 2; i++) {
  83. y = vmulq_f32(vrsqrtsq_f32(vmulq_f32(y, y), g), y);
  84. }
  85. // sqrt(g) = g * 1/sqrt(g)
  86. g = vmulq_f32(g, y);
  87. #else
  88. g = vsqrtq_f32(g);
  89. #endif
  90. vst1q_f32(&x[j], g);
  91. }
  92. for (; j < x_size; ++j) {
  93. x[j] = sqrtf(x[j]);
  94. }
  95. }
  96. #endif
  97. break;
  98. default:
  99. std::for_each(x.begin(), x.end(), [](float& a) { a = sqrtf(a); });
  100. }
  101. }
  102. // Elementwise vector multiplication z = x * y.
  103. void MultiplyAVX2(rtc::ArrayView<const float> x,
  104. rtc::ArrayView<const float> y,
  105. rtc::ArrayView<float> z);
  106. void Multiply(rtc::ArrayView<const float> x,
  107. rtc::ArrayView<const float> y,
  108. rtc::ArrayView<float> z) {
  109. RTC_DCHECK_EQ(z.size(), x.size());
  110. RTC_DCHECK_EQ(z.size(), y.size());
  111. switch (optimization_) {
  112. #if defined(WEBRTC_ARCH_X86_FAMILY)
  113. case Aec3Optimization::kSse2: {
  114. const int x_size = static_cast<int>(x.size());
  115. const int vector_limit = x_size >> 2;
  116. int j = 0;
  117. for (; j < vector_limit * 4; j += 4) {
  118. const __m128 x_j = _mm_loadu_ps(&x[j]);
  119. const __m128 y_j = _mm_loadu_ps(&y[j]);
  120. const __m128 z_j = _mm_mul_ps(x_j, y_j);
  121. _mm_storeu_ps(&z[j], z_j);
  122. }
  123. for (; j < x_size; ++j) {
  124. z[j] = x[j] * y[j];
  125. }
  126. } break;
  127. case Aec3Optimization::kAvx2:
  128. MultiplyAVX2(x, y, z);
  129. break;
  130. #endif
  131. #if defined(WEBRTC_HAS_NEON)
  132. case Aec3Optimization::kNeon: {
  133. const int x_size = static_cast<int>(x.size());
  134. const int vector_limit = x_size >> 2;
  135. int j = 0;
  136. for (; j < vector_limit * 4; j += 4) {
  137. const float32x4_t x_j = vld1q_f32(&x[j]);
  138. const float32x4_t y_j = vld1q_f32(&y[j]);
  139. const float32x4_t z_j = vmulq_f32(x_j, y_j);
  140. vst1q_f32(&z[j], z_j);
  141. }
  142. for (; j < x_size; ++j) {
  143. z[j] = x[j] * y[j];
  144. }
  145. } break;
  146. #endif
  147. default:
  148. std::transform(x.begin(), x.end(), y.begin(), z.begin(),
  149. std::multiplies<float>());
  150. }
  151. }
  152. // Elementwise vector accumulation z += x.
  153. void AccumulateAVX2(rtc::ArrayView<const float> x, rtc::ArrayView<float> z);
  154. void Accumulate(rtc::ArrayView<const float> x, rtc::ArrayView<float> z) {
  155. RTC_DCHECK_EQ(z.size(), x.size());
  156. switch (optimization_) {
  157. #if defined(WEBRTC_ARCH_X86_FAMILY)
  158. case Aec3Optimization::kSse2: {
  159. const int x_size = static_cast<int>(x.size());
  160. const int vector_limit = x_size >> 2;
  161. int j = 0;
  162. for (; j < vector_limit * 4; j += 4) {
  163. const __m128 x_j = _mm_loadu_ps(&x[j]);
  164. __m128 z_j = _mm_loadu_ps(&z[j]);
  165. z_j = _mm_add_ps(x_j, z_j);
  166. _mm_storeu_ps(&z[j], z_j);
  167. }
  168. for (; j < x_size; ++j) {
  169. z[j] += x[j];
  170. }
  171. } break;
  172. case Aec3Optimization::kAvx2:
  173. AccumulateAVX2(x, z);
  174. break;
  175. #endif
  176. #if defined(WEBRTC_HAS_NEON)
  177. case Aec3Optimization::kNeon: {
  178. const int x_size = static_cast<int>(x.size());
  179. const int vector_limit = x_size >> 2;
  180. int j = 0;
  181. for (; j < vector_limit * 4; j += 4) {
  182. const float32x4_t x_j = vld1q_f32(&x[j]);
  183. float32x4_t z_j = vld1q_f32(&z[j]);
  184. z_j = vaddq_f32(z_j, x_j);
  185. vst1q_f32(&z[j], z_j);
  186. }
  187. for (; j < x_size; ++j) {
  188. z[j] += x[j];
  189. }
  190. } break;
  191. #endif
  192. default:
  193. std::transform(x.begin(), x.end(), z.begin(), z.begin(),
  194. std::plus<float>());
  195. }
  196. }
  197. private:
  198. Aec3Optimization optimization_;
  199. };
  200. } // namespace aec3
  201. } // namespace webrtc
  202. #endif // MODULES_AUDIO_PROCESSING_AEC3_VECTOR_MATH_H_