Version
menu_open
link
Wwise SDK 2019.2.15
AkSimdAvx2.h
Go to the documentation of this file.
1 /*******************************************************************************
2 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
3 released in source code form as part of the SDK installer package.
4 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Version: <VERSION> Build: <BUILDNUMBER>
25  Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
26 *******************************************************************************/
27 
28 // AkSimdAvx2.h
29 
30 /// \file
31 /// AKSIMD - AVX2 implementation
32 
33 #ifndef _AK_SIMD_AVX2_H_
34 #define _AK_SIMD_AVX2_H_
35 
38 
39 #if !defined(__AVX2__)
40 #error "Inclusion of AkSimdAvx2.h requires AVX2 instruction sets to be defined on platform"
41 #endif
42 
44 
45 
46 ////////////////////////////////////////////////////////////////////////
47 /// @name AKSIMD arithmetic
48 //@{
49 
50 /// Cross-platform SIMD multiplication of 8 complex data elements with interleaved real and imaginary parts,
51 /// and taking advantage of fused-multiply-add instructions
52 static AkForceInline AKSIMD_V8F32 AKSIMD_COMPLEXMUL_AVX2(const AKSIMD_V8F32 cIn1, const AKSIMD_V8F32 cIn2)
53 {
54  __m256 real1Ext = _mm256_moveldup_ps(cIn1); // reals extended (a3, a3, a2, a2, a1, a1, a0, a0)
55  __m256 in2Shuf = _mm256_shuffle_ps(cIn2, cIn2, 0xB1); // shuf multiplicand (c3, d3, c2, d2, c1, d1, c0, d0)
56  __m256 imag1Ext = _mm256_movehdup_ps(cIn1); // multiplier imag (b3, b3, b2, b2, b1, b1, b0, b0)
57  __m256 temp = _mm256_mul_ps(imag1Ext, in2Shuf); // temp (b3c3, b3d3, b2c2, b2d2, b1c1, b1d1, b0c0, b0d0)
58  __m256 out = _mm256_fmaddsub_ps(real1Ext, cIn2, temp); // final (a3d3+b3c3, a3c3-b3d3, a2d2+b2c2, a2c2-b2d2, a1d1+b1c1, a1c1-b1d1, a0d0+b0c0, a0c0-b0d0)
59  return out;
60 }
61 
62 //@}
63 ////////////////////////////////////////////////////////////////////////
64 
65 ////////////////////////////////////////////////////////////////////////
66 /// @name AKSIMD shuffling
67 //@{
68 
69 /// For each 8b value in a, move it to the designated location in each 128b lane specified by the
70 /// corresponding control byte in b (or, if the control byte is >=16, set the dest to zero) (see _mm_shuffle_epi8)
71 #define AKSIMD_SHUFFLEB_V8I32(a, b) _mm256_shuffle_epi8(a, b)
72 
73 /// For each 16b integer, select one of the values from a and b using the provided control mask - if the
74 /// nth bit is false, the nth value from a will be selected; if true, the value from b will be selected.
75 /// (the mask applies to each 128b lane identically)
76 #define AKSIMD_BLEND_V16I16(a, b, i) _mm256_blend_epi16(a, b, i)
77 
78 #define AKSIMD_INSERT_V2I128( a, m128, idx) _mm256_inserti128_si256(a, m128, idx)
79 
80 /// For each 128b lane, select one of the four input 128b lanes across a and b,
81 /// based on the mask i. AKSIMD_SHUFFLE can still be directly used as a control
82 #define AKSIMD_PERMUTE_2X128_V8I32( a, b, i ) _mm256_permute2x128_si256(a, b, i)
83 
84 /// Selects the lower of each of the 128b lanes in a and b to be the result ( B A ), ( D C ) -> ( C A )
85 #define AKSIMD_DEINTERLEAVELANES_LO_V8I32( a, b ) AKSIMD_PERMUTE_2X128_V8I32(a, b, AKSIMD_PERMUTE128(2, 0))
86 
87 /// Selects the higher of each of the 128b lanes in a and b to be the result ( B A ), ( D C) -> ( D B )
88 #define AKSIMD_DEINTERLEAVELANES_HI_V8I32( a, b ) AKSIMD_PERMUTE_2X128_V8I32(a, b, AKSIMD_PERMUTE128(3, 1))
89 
90 //@}
91 ////////////////////////////////////////////////////////////////////////
92 
93 ////////////////////////////////////////////////////////////////////////
94 /// @name AKSIMD conversion
95 //@{
96 
97 /// Converts the eight signed 16b integer values of a to signed 32-bit integer values
98 #define AKSIMD_CONVERT_V8I16_TO_V8I32( __vec__ ) _mm256_cvtepi16_epi32( (__vec__) )
99 
100 //@}
101 ////////////////////////////////////////////////////////////////////////
102 
103 ////////////////////////////////////////////////////////////////////////
104 /// @name AKSIMD integer arithmetic
105 //@{
106 
107 /// Adds the eight integer values of a and b
108 #define AKSIMD_ADD_V8I32( a, b ) _mm256_add_epi32( a, b )
109 
110 #define AKSIMD_CMPLT_V8I32( a, b ) _mm256_cmpgt_epi32( b, a )
111 #define AKSIMD_CMPGT_V8I32( a, b ) _mm256_cmpgt_epi32( a, b )
112 #define AKSIMD_OR_V8I32( a, b ) _mm256_or_si256(a,b)
113 #define AKSIMD_XOR_V8I32( a, b ) _mm256_xor_si256(a,b)
114 #define AKSIMD_SUB_V8I32( a, b ) _mm256_sub_epi32(a,b)
115 
116 /// Computes the bitwise AND of the 256-bit value in a and the
117 /// 256-bit value in b (see _mm_and_si128)
118 #define AKSIMD_AND_V8I32( __a__, __b__ ) _mm256_and_si256( (__a__), (__b__) )
119 
120 /// Multiplies each 32-bit int value of a by b and returns the lower 32b of the result (no overflow or clamp)
121 #define AKSIMD_MULLO_V8I32( a , b) _mm256_mullo_epi32(a, b)
122 
123 /// Multiplies the low 16bits of a by b and stores it in V8I32 (no overflow)
124 #define AKSIMD_MULLO16_V8I32( a , b) _mm256_mullo_epi16(a, b)
125 
126 /// Subtracts each 16b integer of a by b
127 #define AKSIMD_SUB_V16I16( a, b ) _mm256_sub_epi16( a, b )
128 
129 /// Compares the 16 signed 16-bit integers in a and the 16 signed
130 /// 16-bit integers in b for greater than (see _mm_cmpgt_epi16)
131 #define AKSIMD_CMPGT_V16I16( __a__, __b__ ) _mm256_cmpgt_epi16( (__a__), (__b__) )
132 //@}
133 ////////////////////////////////////////////////////////////////////////
134 
135 ////////////////////////////////////////////////////////////////////////
136 /// @name AKSIMD packing / unpacking
137 //@{
138 
139 /// Interleaves the lower 4 signed or unsigned 16-bit integers in each lane of a
140 /// with the lower 4 signed or unsigned 16-bit integers in each lane of b
141 /// (see _mm_unpacklo_epi16)
142 #define AKSIMD_UNPACKLO_VECTOR16I16( a, b ) _mm256_unpacklo_epi16( a, b )
143 
144 /// Interleaves the upper 8 signed or unsigned 16-bit integers in each lane of a
145 /// with the upper 8 signed or unsigned 16-bit integers in each lane of b
146 /// (see _mm_unpackhi_epi16)
147 #define AKSIMD_UNPACKHI_VECTOR16I16( a, b ) _mm256_unpackhi_epi16( a, b )
148 
149 /// Packs the 8 signed 32-bit integers from a and b into 16 signed 16-bit
150 /// integers and saturates (see _mm_packs_epi32)
151 #define AKSIMD_PACKS_V8I32( a, b ) _mm256_packs_epi32( a, b )
152 
153 //@}
154 ////////////////////////////////////////////////////////////////////////
155 
156 ////////////////////////////////////////////////////////////////////////
157 /// @name AKSIMD shifting
158 //@{
159 
160 /// Shifts the 8 signed or unsigned 32-bit integers in a left by
161 /// in_shiftBy bits while shifting in zeros (see _mm_slli_epi32)
162 #define AKSIMD_SHIFTLEFT_V8I32( __vec__, __shiftBy__ ) \
163  _mm256_slli_epi32( (__vec__), (__shiftBy__) )
164 
165 /// Shifts the 8 signed 32-bit integers in a right by in_shiftBy
166 /// bits while shifting in the sign bit (see _mm_srai_epi32)
167 #define AKSIMD_SHIFTRIGHTARITH_V8I32( __vec__, __shiftBy__ ) \
168  _mm256_srai_epi32( (__vec__), (__shiftBy__) )
169 
170 //@}
171 ////////////////////////////////////////////////////////////////////////
172 
173 ////////////////////////////////////////////////////////////////////////
174 /// @name AKSIMD gather
175 //@{
176 
177 /// To use these, provide a base_ptr, and an expression that calculates an
178 /// array index for the provided base_ptr. The expression can be a lambda,
179 /// such as follows:
180 /// AKSIMD_V8I32 viData = AKSIMD_GATHER_EPI32(src, [uIndex, uStep](int i)
181 /// { return (uIndex + uStep * i); });
182 /// This tends to perform better than a native VGATHER on most CPUs
183 
184 template <typename T, typename Function>
185 inline AKSIMD_V8I32 AKSIMD_GATHER_EPI32(const T* __restrict base_ptr, Function expr)
186 {
187  __m256i vals = _mm256_setzero_si256();
188  __m128i valsTemp[2] = { _mm_setzero_si128(),_mm_setzero_si128() };
189 #define _GATHER_SIM_FETCH(_x) \
190  {\
191  AkInt32 val = *(AkInt32*)(base_ptr + expr(_x)); \
192  valsTemp[_x/4] = _mm_insert_epi32(valsTemp[_x/4], val, _x%4);\
193  }
194 
203 #undef _GATHER_SIM_FETCH
204  vals = _mm256_setr_m128i(valsTemp[0], valsTemp[1]);
205  return vals;
206 }
207 
208 template <typename T, typename Function>
209 inline AKSIMD_V8I32 AKSIMD_GATHER_EPI64(const T* base_ptr, Function expr)
210 {
211  __m256i vals = _mm256_setzero_si256();
212  __m128i valsTemp[2] = { _mm_setzero_si128(),_mm_setzero_si128() };
213 #define _GATHER_SIM_FETCH(_x) \
214  {\
215  AkInt64 val = *(AkInt64*)(base_ptr + expr(_x)); \
216  valsTemp[_x/2] = _mm_insert_epi64(valsTemp[_x/2], val, _x%2);\
217  }
218 
223 #undef _GATHER_SIM_FETCH
224  vals = _mm256_setr_m128i(valsTemp[0], valsTemp[1]);
225  return vals;
226 }
227 
228 template <typename T, typename Function>
229 inline AKSIMD_V8F32 AKSIMD_GATHER_PS(const T* base_ptr, Function expr)
230 {
231  return _mm256_castsi256_ps(AKSIMD_GATHER_EPI32(base_ptr, expr));
232 }
233 
234 template <typename T, typename Function>
235 inline AKSIMD_V8F32 AKSIMD_GATHER_PD(const T* base_ptr, Function expr)
236 {
237  return _mm256_castsi256_pd(AKSIMD_GATHER_EPI64(base_ptr, expr));
238 }
239 
240 //@}
241 ////////////////////////////////////////////////////////////////////////
242 
243 
244 #endif //_AK_SIMD_AVX2_H_
AKSIMD_V8F32 AKSIMD_GATHER_PD(const T *base_ptr, Function expr)
Definition: AkSimdAvx2.h:235
static AkForceInline AKSIMD_V8F32 AKSIMD_COMPLEXMUL_AVX2(const AKSIMD_V8F32 cIn1, const AKSIMD_V8F32 cIn2)
Definition: AkSimdAvx2.h:52
AKSIMD_V8F32 AKSIMD_GATHER_PS(const T *base_ptr, Function expr)
Definition: AkSimdAvx2.h:229
#define _GATHER_SIM_FETCH(_x)
AKSIMD_V8I32 AKSIMD_GATHER_EPI32(const T *__restrict base_ptr, Function expr)
Definition: AkSimdAvx2.h:185
AKSIMD_V8I32 AKSIMD_GATHER_EPI64(const T *base_ptr, Function expr)
Definition: AkSimdAvx2.h:209
#define AkForceInline
Definition: AkTypes.h:62

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise