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 <immintrin.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <xmmintrin.h>
15 
16 #include "common_audio/resampler/sinc_resampler.h"
17 
18 namespace webrtc {
19 
Convolve_AVX2(const float * input_ptr,const float * k1,const float * k2,double kernel_interpolation_factor)20 float SincResampler::Convolve_AVX2(const float* input_ptr,
21                                    const float* k1,
22                                    const float* k2,
23                                    double kernel_interpolation_factor) {
24   __m256 m_input;
25   __m256 m_sums1 = _mm256_setzero_ps();
26   __m256 m_sums2 = _mm256_setzero_ps();
27 
28   // Based on |input_ptr| alignment, we need to use loadu or load.  Unrolling
29   // these loops has not been tested or benchmarked.
30   bool aligned_input = (reinterpret_cast<uintptr_t>(input_ptr) & 0x1F) == 0;
31   if (!aligned_input) {
32     for (size_t i = 0; i < kKernelSize; i += 8) {
33       m_input = _mm256_loadu_ps(input_ptr + i);
34       m_sums1 = _mm256_fmadd_ps(m_input, _mm256_load_ps(k1 + i), m_sums1);
35       m_sums2 = _mm256_fmadd_ps(m_input, _mm256_load_ps(k2 + i), m_sums2);
36     }
37   } else {
38     for (size_t i = 0; i < kKernelSize; i += 8) {
39       m_input = _mm256_load_ps(input_ptr + i);
40       m_sums1 = _mm256_fmadd_ps(m_input, _mm256_load_ps(k1 + i), m_sums1);
41       m_sums2 = _mm256_fmadd_ps(m_input, _mm256_load_ps(k2 + i), m_sums2);
42     }
43   }
44 
45   // Linearly interpolate the two "convolutions".
46   __m128 m128_sums1 = _mm_add_ps(_mm256_extractf128_ps(m_sums1, 0),
47                                  _mm256_extractf128_ps(m_sums1, 1));
48   __m128 m128_sums2 = _mm_add_ps(_mm256_extractf128_ps(m_sums2, 0),
49                                  _mm256_extractf128_ps(m_sums2, 1));
50   m128_sums1 = _mm_mul_ps(
51       m128_sums1,
52       _mm_set_ps1(static_cast<float>(1.0 - kernel_interpolation_factor)));
53   m128_sums2 = _mm_mul_ps(
54       m128_sums2, _mm_set_ps1(static_cast<float>(kernel_interpolation_factor)));
55   m128_sums1 = _mm_add_ps(m128_sums1, m128_sums2);
56 
57   // Sum components together.
58   float result;
59   m128_sums2 = _mm_add_ps(_mm_movehl_ps(m128_sums1, m128_sums1), m128_sums1);
60   _mm_store_ss(&result, _mm_add_ss(m128_sums2,
61                                    _mm_shuffle_ps(m128_sums2, m128_sums2, 1)));
62 
63   return result;
64 }
65 
66 }  // namespace webrtc
67