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