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 <immintrin.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.
xx_loadl_32(const void * a)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
xx_loadl_64(const void * a)37 static INLINE __m128i xx_loadl_64(const void *a) {
38 return _mm_loadl_epi64((const __m128i *)a);
39 }
40
xx_load_128(const void * a)41 static INLINE __m128i xx_load_128(const void *a) {
42 return _mm_load_si128((const __m128i *)a);
43 }
44
xx_loadu_128(const void * a)45 static INLINE __m128i xx_loadu_128(const void *a) {
46 return _mm_loadu_si128((const __m128i *)a);
47 }
48
xx_storel_32(void * const a,const __m128i v)49 static INLINE void xx_storel_32(void *const a, const __m128i v) {
50 const int val = _mm_cvtsi128_si32(v);
51 memcpy(a, &val, sizeof(val));
52 }
53
xx_storel_64(void * const a,const __m128i v)54 static INLINE void xx_storel_64(void *const a, const __m128i v) {
55 _mm_storel_epi64((__m128i *)a, v);
56 }
57
xx_store_128(void * const a,const __m128i v)58 static INLINE void xx_store_128(void *const a, const __m128i v) {
59 _mm_store_si128((__m128i *)a, v);
60 }
61
xx_storeu_128(void * const a,const __m128i v)62 static INLINE void xx_storeu_128(void *const a, const __m128i v) {
63 _mm_storeu_si128((__m128i *)a, v);
64 }
65
66 // The _mm_set_epi64x() intrinsic is undefined for some Visual Studio
67 // compilers. The following function is equivalent to _mm_set_epi64x()
68 // acting on 32-bit integers.
xx_set_64_from_32i(int32_t e1,int32_t e0)69 static INLINE __m128i xx_set_64_from_32i(int32_t e1, int32_t e0) {
70 #if defined(_MSC_VER) && _MSC_VER < 1900
71 return _mm_set_epi32(0, e1, 0, e0);
72 #else
73 return _mm_set_epi64x((uint32_t)e1, (uint32_t)e0);
74 #endif
75 }
76
77 // The _mm_set1_epi64x() intrinsic is undefined for some Visual Studio
78 // compilers. The following function is equivalent to _mm_set1_epi64x()
79 // acting on a 32-bit integer.
xx_set1_64_from_32i(int32_t a)80 static INLINE __m128i xx_set1_64_from_32i(int32_t a) {
81 #if defined(_MSC_VER) && _MSC_VER < 1900
82 return _mm_set_epi32(0, a, 0, a);
83 #else
84 return _mm_set1_epi64x((uint32_t)a);
85 #endif
86 }
87
xx_round_epu16(__m128i v_val_w)88 static INLINE __m128i xx_round_epu16(__m128i v_val_w) {
89 return _mm_avg_epu16(v_val_w, _mm_setzero_si128());
90 }
91
xx_roundn_epu16(__m128i v_val_w,int bits)92 static INLINE __m128i xx_roundn_epu16(__m128i v_val_w, int bits) {
93 const __m128i v_s_w = _mm_srli_epi16(v_val_w, bits - 1);
94 return _mm_avg_epu16(v_s_w, _mm_setzero_si128());
95 }
96
xx_roundn_epu32(__m128i v_val_d,int bits)97 static INLINE __m128i xx_roundn_epu32(__m128i v_val_d, int bits) {
98 const __m128i v_bias_d = _mm_set1_epi32((1 << bits) >> 1);
99 const __m128i v_tmp_d = _mm_add_epi32(v_val_d, v_bias_d);
100 return _mm_srli_epi32(v_tmp_d, bits);
101 }
102
103 // This is equivalent to ROUND_POWER_OF_TWO(v_val_d, bits)
xx_roundn_epi32_unsigned(__m128i v_val_d,int bits)104 static INLINE __m128i xx_roundn_epi32_unsigned(__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_srai_epi32(v_tmp_d, bits);
108 }
109
xx_roundn_epi16(__m128i v_val_d,int bits)110 static INLINE __m128i xx_roundn_epi16(__m128i v_val_d, int bits) {
111 const __m128i v_bias_d = _mm_set1_epi16((1 << bits) >> 1);
112 const __m128i v_sign_d = _mm_srai_epi16(v_val_d, 15);
113 const __m128i v_tmp_d =
114 _mm_add_epi16(_mm_add_epi16(v_val_d, v_bias_d), v_sign_d);
115 return _mm_srai_epi16(v_tmp_d, bits);
116 }
117
118 #endif // AOM_AOM_DSP_X86_SYNONYMS_H_
119