1 /*
2 * Copyright (c) 2020 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 #include "common_audio/fir_filter_avx2.h"
12
13 #include <immintrin.h>
14 #include <stdint.h>
15 #include <string.h>
16 #include <xmmintrin.h>
17
18 #include "rtc_base/checks.h"
19 #include "rtc_base/memory/aligned_malloc.h"
20
21 namespace webrtc {
22
FIRFilterAVX2(const float * unaligned_coefficients,size_t unaligned_coefficients_length,size_t max_input_length)23 FIRFilterAVX2::FIRFilterAVX2(const float* unaligned_coefficients,
24 size_t unaligned_coefficients_length,
25 size_t max_input_length)
26 : // Closest higher multiple of eight.
27 coefficients_length_((unaligned_coefficients_length + 7) & ~0x07),
28 state_length_(coefficients_length_ - 1),
29 coefficients_(static_cast<float*>(
30 AlignedMalloc(sizeof(float) * coefficients_length_, 32))),
31 state_(static_cast<float*>(
32 AlignedMalloc(sizeof(float) * (max_input_length + state_length_),
33 32))) {
34 // Add zeros at the end of the coefficients.
35 RTC_DCHECK_GE(coefficients_length_, unaligned_coefficients_length);
36 size_t padding = coefficients_length_ - unaligned_coefficients_length;
37 memset(coefficients_.get(), 0, padding * sizeof(coefficients_[0]));
38 // The coefficients are reversed to compensate for the order in which the
39 // input samples are acquired (most recent last).
40 for (size_t i = 0; i < unaligned_coefficients_length; ++i) {
41 coefficients_[i + padding] =
42 unaligned_coefficients[unaligned_coefficients_length - i - 1];
43 }
44 memset(state_.get(), 0,
45 (max_input_length + state_length_) * sizeof(state_[0]));
46 }
47
48 FIRFilterAVX2::~FIRFilterAVX2() = default;
49
Filter(const float * in,size_t length,float * out)50 void FIRFilterAVX2::Filter(const float* in, size_t length, float* out) {
51 RTC_DCHECK_GT(length, 0);
52
53 memcpy(&state_[state_length_], in, length * sizeof(*in));
54
55 // Convolves the input signal `in` with the filter kernel `coefficients_`
56 // taking into account the previous state.
57 for (size_t i = 0; i < length; ++i) {
58 float* in_ptr = &state_[i];
59 float* coef_ptr = coefficients_.get();
60
61 __m256 m_sum = _mm256_setzero_ps();
62 __m256 m_in;
63
64 // Depending on if the pointer is aligned with 32 bytes or not it is loaded
65 // differently.
66 if (reinterpret_cast<uintptr_t>(in_ptr) & 0x1F) {
67 for (size_t j = 0; j < coefficients_length_; j += 8) {
68 m_in = _mm256_loadu_ps(in_ptr + j);
69 m_sum = _mm256_fmadd_ps(m_in, _mm256_load_ps(coef_ptr + j), m_sum);
70 }
71 } else {
72 for (size_t j = 0; j < coefficients_length_; j += 8) {
73 m_in = _mm256_load_ps(in_ptr + j);
74 m_sum = _mm256_fmadd_ps(m_in, _mm256_load_ps(coef_ptr + j), m_sum);
75 }
76 }
77 __m128 m128_sum = _mm_add_ps(_mm256_extractf128_ps(m_sum, 0),
78 _mm256_extractf128_ps(m_sum, 1));
79 m128_sum = _mm_add_ps(_mm_movehl_ps(m128_sum, m128_sum), m128_sum);
80 _mm_store_ss(out + i,
81 _mm_add_ss(m128_sum, _mm_shuffle_ps(m128_sum, m128_sum, 1)));
82 }
83
84 // Update current state.
85 memmove(state_.get(), &state_[length], state_length_ * sizeof(state_[0]));
86 }
87
88 } // namespace webrtc
89