xref: /aosp_15_r20/external/libaom/aom_dsp/x86/synonyms_avx2.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2018, 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_SYNONYMS_AVX2_H_
13*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_DSP_X86_SYNONYMS_AVX2_H_
14*77c1e3ccSAndroid Build Coastguard Worker 
15*77c1e3ccSAndroid Build Coastguard Worker #include <immintrin.h>
16*77c1e3ccSAndroid Build Coastguard Worker 
17*77c1e3ccSAndroid Build Coastguard Worker #include "config/aom_config.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_integer.h"
20*77c1e3ccSAndroid Build Coastguard Worker 
21*77c1e3ccSAndroid Build Coastguard Worker /**
22*77c1e3ccSAndroid Build Coastguard Worker  * Various reusable shorthands for x86 SIMD intrinsics.
23*77c1e3ccSAndroid Build Coastguard Worker  *
24*77c1e3ccSAndroid Build Coastguard Worker  * Intrinsics prefixed with xx_ operate on or return 128bit XMM registers.
25*77c1e3ccSAndroid Build Coastguard Worker  * Intrinsics prefixed with yy_ operate on or return 256bit YMM registers.
26*77c1e3ccSAndroid Build Coastguard Worker  */
27*77c1e3ccSAndroid Build Coastguard Worker 
28*77c1e3ccSAndroid Build Coastguard Worker // Loads and stores to do away with the tedium of casting the address
29*77c1e3ccSAndroid Build Coastguard Worker // to the right type.
yy_load_256(const void * a)30*77c1e3ccSAndroid Build Coastguard Worker static inline __m256i yy_load_256(const void *a) {
31*77c1e3ccSAndroid Build Coastguard Worker   return _mm256_load_si256((const __m256i *)a);
32*77c1e3ccSAndroid Build Coastguard Worker }
33*77c1e3ccSAndroid Build Coastguard Worker 
yy_loadu_256(const void * a)34*77c1e3ccSAndroid Build Coastguard Worker static inline __m256i yy_loadu_256(const void *a) {
35*77c1e3ccSAndroid Build Coastguard Worker   return _mm256_loadu_si256((const __m256i *)a);
36*77c1e3ccSAndroid Build Coastguard Worker }
37*77c1e3ccSAndroid Build Coastguard Worker 
yy_store_256(void * const a,const __m256i v)38*77c1e3ccSAndroid Build Coastguard Worker static inline void yy_store_256(void *const a, const __m256i v) {
39*77c1e3ccSAndroid Build Coastguard Worker   _mm256_store_si256((__m256i *)a, v);
40*77c1e3ccSAndroid Build Coastguard Worker }
41*77c1e3ccSAndroid Build Coastguard Worker 
yy_storeu_256(void * const a,const __m256i v)42*77c1e3ccSAndroid Build Coastguard Worker static inline void yy_storeu_256(void *const a, const __m256i v) {
43*77c1e3ccSAndroid Build Coastguard Worker   _mm256_storeu_si256((__m256i *)a, v);
44*77c1e3ccSAndroid Build Coastguard Worker }
45*77c1e3ccSAndroid Build Coastguard Worker 
46*77c1e3ccSAndroid Build Coastguard Worker // Fill an AVX register using an interleaved pair of values, ie. set the
47*77c1e3ccSAndroid Build Coastguard Worker // 16 channels to {a, b} repeated 8 times, using the same channel ordering
48*77c1e3ccSAndroid Build Coastguard Worker // as when a register is stored to / loaded from memory.
49*77c1e3ccSAndroid Build Coastguard Worker //
50*77c1e3ccSAndroid Build Coastguard Worker // This is useful for rearranging filter kernels for use with the _mm_madd_epi16
51*77c1e3ccSAndroid Build Coastguard Worker // instruction
yy_set2_epi16(int16_t a,int16_t b)52*77c1e3ccSAndroid Build Coastguard Worker static inline __m256i yy_set2_epi16(int16_t a, int16_t b) {
53*77c1e3ccSAndroid Build Coastguard Worker   return _mm256_setr_epi16(a, b, a, b, a, b, a, b, a, b, a, b, a, b, a, b);
54*77c1e3ccSAndroid Build Coastguard Worker }
55*77c1e3ccSAndroid Build Coastguard Worker 
56*77c1e3ccSAndroid Build Coastguard Worker // Some compilers don't have _mm256_set_m128i defined in immintrin.h. We
57*77c1e3ccSAndroid Build Coastguard Worker // therefore define an equivalent function using a different intrinsic.
58*77c1e3ccSAndroid Build Coastguard Worker // ([ hi ], [ lo ]) -> [ hi ][ lo ]
yy_set_m128i(__m128i hi,__m128i lo)59*77c1e3ccSAndroid Build Coastguard Worker static inline __m256i yy_set_m128i(__m128i hi, __m128i lo) {
60*77c1e3ccSAndroid Build Coastguard Worker   return _mm256_insertf128_si256(_mm256_castsi128_si256(lo), hi, 1);
61*77c1e3ccSAndroid Build Coastguard Worker }
62*77c1e3ccSAndroid Build Coastguard Worker 
63*77c1e3ccSAndroid Build Coastguard Worker // This behaves similarly to _mm256_set_epi64x(), but avoids undefined
64*77c1e3ccSAndroid Build Coastguard Worker // sanitizer warnings when loading values from unaligned buffers using
65*77c1e3ccSAndroid Build Coastguard Worker // `*(int64_t *)val`.
yy_loadu_4x64(const void * e3,const void * e2,const void * e1,const void * e0)66*77c1e3ccSAndroid Build Coastguard Worker static inline __m256i yy_loadu_4x64(const void *e3, const void *e2,
67*77c1e3ccSAndroid Build Coastguard Worker                                     const void *e1, const void *e0) {
68*77c1e3ccSAndroid Build Coastguard Worker   __m128d v0 = _mm_castsi128_pd(_mm_loadl_epi64((const __m128i *)e0));
69*77c1e3ccSAndroid Build Coastguard Worker   __m128d v01 = _mm_loadh_pd(v0, (const double *)e1);
70*77c1e3ccSAndroid Build Coastguard Worker   __m128d v2 = _mm_castsi128_pd(_mm_loadl_epi64((const __m128i *)e2));
71*77c1e3ccSAndroid Build Coastguard Worker   __m128d v23 = _mm_loadh_pd(v2, (const double *)e3);
72*77c1e3ccSAndroid Build Coastguard Worker   // Note this can be replaced with
73*77c1e3ccSAndroid Build Coastguard Worker   // `_mm256_castpd_si256(_mm256_set_m128d(v23, v01))` if immintrin.h contains
74*77c1e3ccSAndroid Build Coastguard Worker   // _mm256_set_m128d() with all supported compilers. This version is used to
75*77c1e3ccSAndroid Build Coastguard Worker   // match the behavior with yy_set_m128i().
76*77c1e3ccSAndroid Build Coastguard Worker   return yy_set_m128i(_mm_castpd_si128(v23), _mm_castpd_si128(v01));
77*77c1e3ccSAndroid Build Coastguard Worker }
78*77c1e3ccSAndroid Build Coastguard Worker 
yy_loadu2_128(const void * hi,const void * lo)79*77c1e3ccSAndroid Build Coastguard Worker static inline __m256i yy_loadu2_128(const void *hi, const void *lo) {
80*77c1e3ccSAndroid Build Coastguard Worker   __m128i mhi = _mm_loadu_si128((const __m128i *)(hi));
81*77c1e3ccSAndroid Build Coastguard Worker   __m128i mlo = _mm_loadu_si128((const __m128i *)(lo));
82*77c1e3ccSAndroid Build Coastguard Worker   return yy_set_m128i(mhi, mlo);
83*77c1e3ccSAndroid Build Coastguard Worker }
84*77c1e3ccSAndroid Build Coastguard Worker 
yy_storeu2_128(void * hi,void * lo,const __m256i a)85*77c1e3ccSAndroid Build Coastguard Worker static inline void yy_storeu2_128(void *hi, void *lo, const __m256i a) {
86*77c1e3ccSAndroid Build Coastguard Worker   _mm_storeu_si128((__m128i *)hi, _mm256_extracti128_si256(a, 1));
87*77c1e3ccSAndroid Build Coastguard Worker   _mm_storeu_si128((__m128i *)lo, _mm256_castsi256_si128(a));
88*77c1e3ccSAndroid Build Coastguard Worker }
89*77c1e3ccSAndroid Build Coastguard Worker 
yy_roundn_epu16(__m256i v_val_w,int bits)90*77c1e3ccSAndroid Build Coastguard Worker static inline __m256i yy_roundn_epu16(__m256i v_val_w, int bits) {
91*77c1e3ccSAndroid Build Coastguard Worker   const __m256i v_s_w = _mm256_srli_epi16(v_val_w, bits - 1);
92*77c1e3ccSAndroid Build Coastguard Worker   return _mm256_avg_epu16(v_s_w, _mm256_setzero_si256());
93*77c1e3ccSAndroid Build Coastguard Worker }
94*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_DSP_X86_SYNONYMS_AVX2_H_
95