OLD | NEW |
---|---|
(Empty) | |
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 | |
11 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_VECTOR_MATH_H_ | |
12 #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_VECTOR_MATH_H_ | |
13 | |
14 #include "webrtc/typedefs.h" | |
aleloi
2017/04/11 14:27:30
Project headers should be after system headers.
| |
15 #if defined(WEBRTC_ARCH_X86_FAMILY) | |
16 #include <emmintrin.h> | |
17 #endif | |
18 #include <math.h> | |
19 #include <algorithm> | |
20 #include <array> | |
21 #include <functional> | |
22 | |
23 #include "webrtc/base/array_view.h" | |
24 #include "webrtc/base/checks.h" | |
25 #include "webrtc/modules/audio_processing/aec3/aec3_common.h" | |
26 | |
27 namespace webrtc { | |
28 namespace aec3 { | |
29 | |
30 // Provides optimizations for mathematical operations based on vectors. | |
31 class VectorMath { | |
32 public: | |
33 explicit VectorMath(Aec3Optimization optimization) | |
34 : optimization_(optimization) {} | |
35 | |
36 // Elementwise square root. | |
37 void Sqrt(rtc::ArrayView<float> x) { | |
38 switch (optimization_) { | |
39 #if defined(WEBRTC_ARCH_X86_FAMILY) | |
40 case Aec3Optimization::kSse2: { | |
41 const size_t kVectorLimit = x.size() >> 2; | |
42 const size_t kRemaining = x.size() - kVectorLimit * 4; | |
aleloi
2017/04/11 14:27:30
I think automatic variables are usually named with
| |
43 | |
44 int j = 0; | |
45 for (size_t k = 0; k < kVectorLimit; ++k, j += 4) { | |
ivoc
2017/04/11 12:03:01
I think the 2 different loop variables are a bit c
peah-webrtc
2017/04/11 13:09:26
Awesome! Thanks!
Done.
| |
46 __m128 g = _mm_loadu_ps(&x[j]); | |
47 g = _mm_sqrt_ps(g); | |
48 _mm_storeu_ps(&x[j], g); | |
49 } | |
50 | |
51 for (size_t k = 0; k < kRemaining; ++k, ++j) { | |
52 x[j] = sqrtf(x[j]); | |
53 } | |
54 } break; | |
55 #endif | |
56 default: | |
57 std::for_each(x.begin(), x.end(), [](float& a) { a = sqrtf(a); }); | |
58 } | |
59 } | |
60 | |
61 // Elementwise vector multiplication z = x * y. | |
62 void Multiply(rtc::ArrayView<const float> x, | |
63 rtc::ArrayView<const float> y, | |
64 rtc::ArrayView<float> z) { | |
65 RTC_DCHECK_EQ(z.size(), x.size()); | |
66 RTC_DCHECK_EQ(z.size(), y.size()); | |
67 switch (optimization_) { | |
68 #if defined(WEBRTC_ARCH_X86_FAMILY) | |
69 case Aec3Optimization::kSse2: { | |
70 const size_t kVectorLimit = x.size() >> 2; | |
71 const size_t kRemaining = x.size() - kVectorLimit * 4; | |
72 | |
73 int j = 0; | |
74 for (size_t k = 0; k < kVectorLimit; ++k, j += 4) { | |
ivoc
2017/04/11 12:03:01
Same here.
peah-webrtc
2017/04/11 13:09:26
Done.
| |
75 const __m128 x_k = _mm_loadu_ps(&x[j]); | |
76 const __m128 y_k = _mm_loadu_ps(&y[j]); | |
77 const __m128 z_k = _mm_mul_ps(x_k, y_k); | |
78 _mm_storeu_ps(&z[j], z_k); | |
79 } | |
80 | |
81 for (size_t k = 0; k < kRemaining; ++k, ++j) { | |
82 z[j] = x[j] * y[j]; | |
83 } | |
84 } break; | |
85 #endif | |
86 default: | |
87 std::transform(x.begin(), x.end(), y.begin(), z.begin(), | |
88 std::multiplies<float>()); | |
aleloi
2017/04/11 14:27:30
Are we sure std::transform doesn't do this kind of
| |
89 } | |
90 } | |
91 | |
92 // Elementwise vector accumulation z += x. | |
93 void Accumulate(rtc::ArrayView<const float> x, rtc::ArrayView<float> z) { | |
94 RTC_DCHECK_EQ(z.size(), x.size()); | |
95 switch (optimization_) { | |
96 #if defined(WEBRTC_ARCH_X86_FAMILY) | |
97 case Aec3Optimization::kSse2: { | |
98 const size_t kVectorLimit = x.size() >> 2; | |
99 const size_t kRemaining = x.size() - kVectorLimit * 4; | |
100 | |
101 int j = 0; | |
102 for (size_t k = 0; k < kVectorLimit; ++k, j += 4) { | |
ivoc
2017/04/11 12:03:01
And here.
peah-webrtc
2017/04/11 13:09:26
Done.
| |
103 const __m128 x_k = _mm_loadu_ps(&x[j]); | |
104 __m128 z_k = _mm_loadu_ps(&z[j]); | |
105 z_k = _mm_add_ps(x_k, z_k); | |
106 _mm_storeu_ps(&z[j], z_k); | |
107 } | |
108 | |
109 for (size_t k = 0; k < kRemaining; ++k, ++j) { | |
110 z[j] += x[j]; | |
111 } | |
112 } break; | |
113 #endif | |
114 default: | |
115 std::transform(x.begin(), x.end(), z.begin(), z.begin(), | |
116 std::plus<float>()); | |
117 } | |
118 } | |
119 | |
120 private: | |
121 Aec3Optimization optimization_; | |
122 }; | |
123 | |
124 } // namespace aec3 | |
125 | |
126 } // namespace webrtc | |
127 | |
128 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC3_VECTOR_MATH_H_ | |
OLD | NEW |