xref: /aosp_15_r20/external/libaom/aom_dsp/x86/convolve_ssse3.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2021, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_DSP_X86_CONVOLVE_SSSE3_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_DSP_X86_CONVOLVE_SSSE3_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include <tmmintrin.h>  // SSSE3
16*77c1e3ccSAndroid Build Coastguard Worker 
shuffle_filter_ssse3(const int16_t * const filter,__m128i * const f)17*77c1e3ccSAndroid Build Coastguard Worker static inline void shuffle_filter_ssse3(const int16_t *const filter,
18*77c1e3ccSAndroid Build Coastguard Worker                                         __m128i *const f) {
19*77c1e3ccSAndroid Build Coastguard Worker   const __m128i f_values = _mm_load_si128((const __m128i *)filter);
20*77c1e3ccSAndroid Build Coastguard Worker   // pack and duplicate the filter values
21*77c1e3ccSAndroid Build Coastguard Worker   f[0] = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0200u));
22*77c1e3ccSAndroid Build Coastguard Worker   f[1] = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0604u));
23*77c1e3ccSAndroid Build Coastguard Worker   f[2] = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0a08u));
24*77c1e3ccSAndroid Build Coastguard Worker   f[3] = _mm_shuffle_epi8(f_values, _mm_set1_epi16(0x0e0cu));
25*77c1e3ccSAndroid Build Coastguard Worker }
26*77c1e3ccSAndroid Build Coastguard Worker 
convolve8_8_ssse3(const __m128i * const s,const __m128i * const f)27*77c1e3ccSAndroid Build Coastguard Worker static inline __m128i convolve8_8_ssse3(const __m128i *const s,
28*77c1e3ccSAndroid Build Coastguard Worker                                         const __m128i *const f) {
29*77c1e3ccSAndroid Build Coastguard Worker   // multiply 2 adjacent elements with the filter and add the result
30*77c1e3ccSAndroid Build Coastguard Worker   const __m128i k_64 = _mm_set1_epi16(1 << 6);
31*77c1e3ccSAndroid Build Coastguard Worker   const __m128i x0 = _mm_maddubs_epi16(s[0], f[0]);
32*77c1e3ccSAndroid Build Coastguard Worker   const __m128i x1 = _mm_maddubs_epi16(s[1], f[1]);
33*77c1e3ccSAndroid Build Coastguard Worker   const __m128i x2 = _mm_maddubs_epi16(s[2], f[2]);
34*77c1e3ccSAndroid Build Coastguard Worker   const __m128i x3 = _mm_maddubs_epi16(s[3], f[3]);
35*77c1e3ccSAndroid Build Coastguard Worker   __m128i sum1, sum2;
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker   // sum the results together, saturating only on the final step
38*77c1e3ccSAndroid Build Coastguard Worker   // adding x0 with x2 and x1 with x3 is the only order that prevents
39*77c1e3ccSAndroid Build Coastguard Worker   // outranges for all filters
40*77c1e3ccSAndroid Build Coastguard Worker   sum1 = _mm_add_epi16(x0, x2);
41*77c1e3ccSAndroid Build Coastguard Worker   sum2 = _mm_add_epi16(x1, x3);
42*77c1e3ccSAndroid Build Coastguard Worker   // add the rounding offset early to avoid another saturated add
43*77c1e3ccSAndroid Build Coastguard Worker   sum1 = _mm_add_epi16(sum1, k_64);
44*77c1e3ccSAndroid Build Coastguard Worker   sum1 = _mm_adds_epi16(sum1, sum2);
45*77c1e3ccSAndroid Build Coastguard Worker   // shift by 7 bit each 16 bit
46*77c1e3ccSAndroid Build Coastguard Worker   sum1 = _mm_srai_epi16(sum1, 7);
47*77c1e3ccSAndroid Build Coastguard Worker   return sum1;
48*77c1e3ccSAndroid Build Coastguard Worker }
49*77c1e3ccSAndroid Build Coastguard Worker 
50*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_DSP_X86_CONVOLVE_SSSE3_H_
51