1 /*
2  * Copyright (c) 2018, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at https://www.aomedia.org/license/software-license. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at https://www.aomedia.org/license/patent-license.
10  */
11 
12 #ifndef AOM_DSP_X86_SYNONYMS_AVX2_H_
13 #define AOM_DSP_X86_SYNONYMS_AVX2_H_
14 
15 #include <immintrin.h>
16 #include <stdio.h>
17 
18 /**
19   * Various reusable shorthands for x86 SIMD intrinsics.
20   *
21   * Intrinsics prefixed with xx_ operate on or return 128bit XMM registers.
22   * Intrinsics prefixed with yy_ operate on or return 256bit YMM registers.
23   */
24 
25 // Loads and stores to do away with the tedium of casting the address
26 // to the right type.
yy_load_256(const void * const a)27 static INLINE __m256i yy_load_256(const void *const a) {
28 #ifdef EB_TEST_SIMD_ALIGN
29     if ((intptr_t)a % 32)
30         SVT_LOG("\n yy_load_256() NOT 32-byte aligned!!!\n");
31 #endif
32     return _mm256_loadu_si256((const __m256i *)a);
33 }
34 
yy_loadu_256(const void * const a)35 static INLINE __m256i yy_loadu_256(const void *const a) {
36     return _mm256_loadu_si256((const __m256i *)a);
37 }
38 
yy_store_256(void * const a,const __m256i v)39 static INLINE void yy_store_256(void *const a, const __m256i v) {
40 #ifdef EB_TEST_SIMD_ALIGN
41     if ((intptr_t)a % 32)
42         SVT_LOG("\n yy_store_256() NOT 32-byte aligned!!!\n");
43 #endif
44     _mm256_storeu_si256((__m256i *)a, v);
45 }
46 
yy_storeu_256(void * const a,const __m256i v)47 static INLINE void yy_storeu_256(void *const a, const __m256i v) {
48     _mm256_storeu_si256((__m256i *)a, v);
49 }
50 
51 // The _mm256_set1_epi64x() intrinsic is undefined for some Visual Studio
52 // compilers. The following function is equivalent to _mm256_set1_epi64x()
53 // acting on a 32-bit integer.
yy_set1_64_from_32i(int32_t a)54 static INLINE __m256i yy_set1_64_from_32i(int32_t a) {
55 #if defined(_MSC_VER) && defined(_M_IX86) && _MSC_VER < 1900
56     return _mm256_set_epi32(0, a, 0, a, 0, a, 0, a);
57 #else
58     return _mm256_set1_epi64x((uint32_t)a);
59 #endif
60 }
61 
62 // Some compilers don't have _mm256_set_m128i defined in immintrin.h. We
63 // therefore define an equivalent function using a different intrinsic.
64 // ([ hi ], [ lo ]) -> [ hi ][ lo ]
yy_set_m128i(__m128i hi,__m128i lo)65 static INLINE __m256i yy_set_m128i(__m128i hi, __m128i lo) {
66     return _mm256_insertf128_si256(_mm256_castsi128_si256(lo), hi, 1);
67 }
68 
yy_roundn_epu16(__m256i v_val_w,int bits)69 static INLINE __m256i yy_roundn_epu16(__m256i v_val_w, int bits) {
70     const __m256i v_s_w = _mm256_srli_epi16(v_val_w, bits - 1);
71     return _mm256_avg_epu16(v_s_w, _mm256_setzero_si256());
72 }
yy_storeu2_128(void * hi,void * lo,const __m256i a)73 static INLINE void yy_storeu2_128(void *hi, void *lo, const __m256i a) {
74     _mm_storeu_si128((__m128i *)hi, _mm256_extracti128_si256(a, 1));
75     _mm_storeu_si128((__m128i *)lo, _mm256_castsi256_si128(a));
76 }
77 
yy_loadu2_128(const void * hi,const void * lo)78 static INLINE __m256i yy_loadu2_128(const void *hi, const void *lo) {
79     __m128i mhi = _mm_loadu_si128((__m128i *)(hi));
80     __m128i mlo = _mm_loadu_si128((__m128i *)(lo));
81     return yy_set_m128i(mhi, mlo);
82 }
83 #endif // AOM_DSP_X86_SYNONYMS_AVX2_H_
84