blob: ee75fb38ad4b2b5f36b66561e9b65e02767879a5 [file] [log] [blame]
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "common_audio/fir_filter_sse.h"
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000012
kwibergc2b785d2016-02-24 05:22:32 -080013#include <stdint.h>
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000014#include <string.h>
15#include <xmmintrin.h>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
Karl Wiberg29e7bee2018-03-22 14:11:52 +010018#include "rtc_base/memory/aligned_malloc.h"
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000019
20namespace webrtc {
21
Yves Gerey665174f2018-06-19 15:03:05 +020022FIRFilterSSE2::~FIRFilterSSE2() {}
Patrik Höglundf715c532017-11-17 11:04:15 +010023
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000024FIRFilterSSE2::FIRFilterSSE2(const float* coefficients,
25 size_t coefficients_length,
26 size_t max_input_length)
27 : // Closest higher multiple of four.
28 coefficients_length_((coefficients_length + 3) & ~0x03),
29 state_length_(coefficients_length_ - 1),
30 coefficients_(static_cast<float*>(
31 AlignedMalloc(sizeof(float) * coefficients_length_, 16))),
32 state_(static_cast<float*>(
33 AlignedMalloc(sizeof(float) * (max_input_length + state_length_),
34 16))) {
35 // Add zeros at the end of the coefficients.
36 size_t padding = coefficients_length_ - coefficients_length;
bjornv@webrtc.org3cbd6c22014-09-04 13:21:44 +000037 memset(coefficients_.get(), 0, padding * sizeof(coefficients_[0]));
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000038 // 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 < coefficients_length; ++i) {
41 coefficients_[i + padding] = coefficients[coefficients_length - i - 1];
42 }
Yves Gerey665174f2018-06-19 15:03:05 +020043 memset(state_.get(), 0,
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000044 (max_input_length + state_length_) * sizeof(state_[0]));
45}
46
47void FIRFilterSSE2::Filter(const float* in, size_t length, float* out) {
kwibergb890c95c2016-11-29 05:30:40 -080048 RTC_DCHECK_GT(length, 0);
aluebs@webrtc.org37ca7652014-03-24 10:16:11 +000049
50 memcpy(&state_[state_length_], in, length * sizeof(*in));
51
52 // Convolves the input signal |in| with the filter kernel |coefficients_|
53 // taking into account the previous state.
54 for (size_t i = 0; i < length; ++i) {
55 float* in_ptr = &state_[i];
56 float* coef_ptr = coefficients_.get();
57
58 __m128 m_sum = _mm_setzero_ps();
59 __m128 m_in;
60
61 // Depending on if the pointer is aligned with 16 bytes or not it is loaded
62 // differently.
63 if (reinterpret_cast<uintptr_t>(in_ptr) & 0x0F) {
64 for (size_t j = 0; j < coefficients_length_; j += 4) {
65 m_in = _mm_loadu_ps(in_ptr + j);
66 m_sum = _mm_add_ps(m_sum, _mm_mul_ps(m_in, _mm_load_ps(coef_ptr + j)));
67 }
68 } else {
69 for (size_t j = 0; j < coefficients_length_; j += 4) {
70 m_in = _mm_load_ps(in_ptr + j);
71 m_sum = _mm_add_ps(m_sum, _mm_mul_ps(m_in, _mm_load_ps(coef_ptr + j)));
72 }
73 }
74 m_sum = _mm_add_ps(_mm_movehl_ps(m_sum, m_sum), m_sum);
75 _mm_store_ss(out + i, _mm_add_ss(m_sum, _mm_shuffle_ps(m_sum, m_sum, 1)));
76 }
77
78 // Update current state.
79 memmove(state_.get(), &state_[length], state_length_ * sizeof(state_[0]));
80}
81
82} // namespace webrtc