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