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