synonyms.h (4514B)
1 /* 2 * Copyright (c) 2016, 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_H_ 13 #define AOM_AOM_DSP_X86_SYNONYMS_H_ 14 15 #include <emmintrin.h> 16 #include <string.h> 17 18 #include "config/aom_config.h" 19 20 #include "aom/aom_integer.h" 21 22 /** 23 * Various reusable shorthands for x86 SIMD intrinsics. 24 * 25 * Intrinsics prefixed with xx_ operate on or return 128bit XMM registers. 26 * Intrinsics prefixed with yy_ operate on or return 256bit YMM registers. 27 */ 28 29 // Loads and stores to do away with the tedium of casting the address 30 // to the right type. 31 static inline __m128i xx_loadl_32(const void *a) { 32 int val; 33 memcpy(&val, a, sizeof(val)); 34 return _mm_cvtsi32_si128(val); 35 } 36 37 static inline __m128i xx_loadl_64(const void *a) { 38 return _mm_loadl_epi64((const __m128i *)a); 39 } 40 41 static inline __m128i xx_load_128(const void *a) { 42 return _mm_load_si128((const __m128i *)a); 43 } 44 45 static inline __m128i xx_loadu_128(const void *a) { 46 return _mm_loadu_si128((const __m128i *)a); 47 } 48 49 // _mm_loadu_si64 has been introduced in GCC 9, reimplement the function 50 // manually on older compilers. 51 #if !defined(__clang__) && __GNUC_MAJOR__ < 9 52 static inline __m128i xx_loadu_2x64(const void *hi, const void *lo) { 53 __m64 hi_, lo_; 54 memcpy(&hi_, hi, sizeof(hi_)); 55 memcpy(&lo_, lo, sizeof(lo_)); 56 return _mm_set_epi64(hi_, lo_); 57 } 58 #else 59 // Load 64 bits from each of hi and low, and pack into an SSE register 60 // Since directly loading as `int64_t`s and using _mm_set_epi64 may violate 61 // the strict aliasing rule, this takes a different approach 62 static inline __m128i xx_loadu_2x64(const void *hi, const void *lo) { 63 return _mm_unpacklo_epi64(_mm_loadl_epi64((const __m128i *)lo), 64 _mm_loadl_epi64((const __m128i *)hi)); 65 } 66 #endif 67 68 static inline void xx_storel_32(void *const a, const __m128i v) { 69 const int val = _mm_cvtsi128_si32(v); 70 memcpy(a, &val, sizeof(val)); 71 } 72 73 static inline void xx_storel_64(void *const a, const __m128i v) { 74 _mm_storel_epi64((__m128i *)a, v); 75 } 76 77 static inline void xx_store_128(void *const a, const __m128i v) { 78 _mm_store_si128((__m128i *)a, v); 79 } 80 81 static inline void xx_storeu_128(void *const a, const __m128i v) { 82 _mm_storeu_si128((__m128i *)a, v); 83 } 84 85 // Fill an SSE register using an interleaved pair of values, ie. set the 86 // 8 channels to {a, b, a, b, a, b, a, b}, using the same channel ordering 87 // as when a register is stored to / loaded from memory. 88 // 89 // This is useful for rearranging filter kernels for use with the _mm_madd_epi16 90 // instruction 91 static inline __m128i xx_set2_epi16(int16_t a, int16_t b) { 92 return _mm_setr_epi16(a, b, a, b, a, b, a, b); 93 } 94 95 static inline __m128i xx_round_epu16(__m128i v_val_w) { 96 return _mm_avg_epu16(v_val_w, _mm_setzero_si128()); 97 } 98 99 static inline __m128i xx_roundn_epu16(__m128i v_val_w, int bits) { 100 const __m128i v_s_w = _mm_srli_epi16(v_val_w, bits - 1); 101 return _mm_avg_epu16(v_s_w, _mm_setzero_si128()); 102 } 103 104 static inline __m128i xx_roundn_epu32(__m128i v_val_d, int bits) { 105 const __m128i v_bias_d = _mm_set1_epi32((1 << bits) >> 1); 106 const __m128i v_tmp_d = _mm_add_epi32(v_val_d, v_bias_d); 107 return _mm_srli_epi32(v_tmp_d, bits); 108 } 109 110 static inline __m128i xx_roundn_epi16_unsigned(__m128i v_val_d, int bits) { 111 const __m128i v_bias_d = _mm_set1_epi16((1 << bits) >> 1); 112 const __m128i v_tmp_d = _mm_add_epi16(v_val_d, v_bias_d); 113 return _mm_srai_epi16(v_tmp_d, bits); 114 } 115 116 // This is equivalent to ROUND_POWER_OF_TWO(v_val_d, bits) 117 static inline __m128i xx_roundn_epi32_unsigned(__m128i v_val_d, int bits) { 118 const __m128i v_bias_d = _mm_set1_epi32((1 << bits) >> 1); 119 const __m128i v_tmp_d = _mm_add_epi32(v_val_d, v_bias_d); 120 return _mm_srai_epi32(v_tmp_d, bits); 121 } 122 123 static inline __m128i xx_roundn_epi16(__m128i v_val_d, int bits) { 124 const __m128i v_bias_d = _mm_set1_epi16((1 << bits) >> 1); 125 const __m128i v_sign_d = _mm_srai_epi16(v_val_d, 15); 126 const __m128i v_tmp_d = 127 _mm_add_epi16(_mm_add_epi16(v_val_d, v_bias_d), v_sign_d); 128 return _mm_srai_epi16(v_tmp_d, bits); 129 } 130 131 #endif // AOM_AOM_DSP_X86_SYNONYMS_H_