Mercurial > hg > sv-dependency-builds
comparison src/fftw-3.3.5/simd-support/simd-avx.h @ 42:2cd0e3b3e1fd
Current fftw source
author | Chris Cannam |
---|---|
date | Tue, 18 Oct 2016 13:40:26 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
41:481f5f8c5634 | 42:2cd0e3b3e1fd |
---|---|
1 /* | |
2 * Copyright (c) 2003, 2007-14 Matteo Frigo | |
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology | |
4 * | |
5 * Improvements to 256-bit AVX by Erik Lindahl, 2015. | |
6 * Erik Lindahl places his modifications in the public domain. | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 * | |
22 */ | |
23 | |
24 #if defined(FFTW_LDOUBLE) || defined(FFTW_QUAD) | |
25 #error "AVX only works in single or double precision" | |
26 #endif | |
27 | |
28 #ifdef FFTW_SINGLE | |
29 # define DS(d,s) s /* single-precision option */ | |
30 # define SUFF(name) name ## s | |
31 #else | |
32 # define DS(d,s) d /* double-precision option */ | |
33 # define SUFF(name) name ## d | |
34 #endif | |
35 | |
36 #define SIMD_SUFFIX _avx /* for renaming */ | |
37 #define VL DS(2, 4) /* SIMD complex vector length */ | |
38 #define SIMD_VSTRIDE_OKA(x) ((x) == 2) | |
39 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK | |
40 | |
41 #if defined(__GNUC__) && !defined(__AVX__) /* sanity check */ | |
42 #error "compiling simd-avx.h without -mavx" | |
43 #endif | |
44 | |
45 #ifdef _MSC_VER | |
46 #ifndef inline | |
47 #define inline __inline | |
48 #endif | |
49 #endif | |
50 | |
51 #include <immintrin.h> | |
52 | |
53 typedef DS(__m256d, __m256) V; | |
54 #define VADD SUFF(_mm256_add_p) | |
55 #define VSUB SUFF(_mm256_sub_p) | |
56 #define VMUL SUFF(_mm256_mul_p) | |
57 #define VXOR SUFF(_mm256_xor_p) | |
58 #define VSHUF SUFF(_mm256_shuffle_p) | |
59 #define VPERM1 SUFF(_mm256_permute_p) | |
60 | |
61 #define SHUFVALD(fp0,fp1) \ | |
62 (((fp1) << 3) | ((fp0) << 2) | ((fp1) << 1) | ((fp0))) | |
63 #define SHUFVALS(fp0,fp1,fp2,fp3) \ | |
64 (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0))) | |
65 | |
66 #define VDUPL(x) DS(_mm256_movedup_pd(x), _mm256_moveldup_ps(x)) | |
67 #define VDUPH(x) DS(_mm256_permute_pd(x,SHUFVALD(1,1)), _mm256_movehdup_ps(x)) | |
68 | |
69 #define VLIT(x0, x1) DS(_mm256_set_pd(x0, x1, x0, x1), _mm256_set_ps(x0, x1, x0, x1, x0, x1, x0, x1)) | |
70 #define DVK(var, val) V var = VLIT(val, val) | |
71 #define LDK(x) x | |
72 | |
73 static inline V LDA(const R *x, INT ivs, const R *aligned_like) | |
74 { | |
75 (void)aligned_like; /* UNUSED */ | |
76 (void)ivs; /* UNUSED */ | |
77 return SUFF(_mm256_loadu_p)(x); | |
78 } | |
79 | |
80 static inline void STA(R *x, V v, INT ovs, const R *aligned_like) | |
81 { | |
82 (void)aligned_like; /* UNUSED */ | |
83 (void)ovs; /* UNUSED */ | |
84 SUFF(_mm256_storeu_p)(x, v); | |
85 } | |
86 | |
87 #if FFTW_SINGLE | |
88 | |
89 #define LOADH(addr, val) _mm_loadh_pi(val, (const __m64 *)(addr)) | |
90 #define LOADL(addr, val) _mm_loadl_pi(val, (const __m64 *)(addr)) | |
91 #define STOREH(addr, val) _mm_storeh_pi((__m64 *)(addr), val) | |
92 #define STOREL(addr, val) _mm_storel_pi((__m64 *)(addr), val) | |
93 | |
94 /* it seems like the only AVX way to store 4 complex floats is to | |
95 extract two pairs of complex floats into two __m128 registers, and | |
96 then use SSE-like half-stores. Similarly, to load 4 complex | |
97 floats, we load two pairs of complex floats into two __m128 | |
98 registers, and then pack the two __m128 registers into one __m256 | |
99 value. */ | |
100 static inline V LD(const R *x, INT ivs, const R *aligned_like) | |
101 { | |
102 __m128 l0, l1, h0, h1; | |
103 (void)aligned_like; /* UNUSED */ | |
104 #if defined(__ICC) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 8) | |
105 l0 = LOADL(x, SUFF(_mm_undefined_p)()); | |
106 l1 = LOADL(x + ivs, SUFF(_mm_undefined_p)()); | |
107 h0 = LOADL(x + 2*ivs, SUFF(_mm_undefined_p)()); | |
108 h1 = LOADL(x + 3*ivs, SUFF(_mm_undefined_p)()); | |
109 #else | |
110 l0 = LOADL(x, l0); | |
111 l1 = LOADL(x + ivs, l1); | |
112 h0 = LOADL(x + 2*ivs, h0); | |
113 h1 = LOADL(x + 3*ivs, h1); | |
114 #endif | |
115 l0 = SUFF(_mm_movelh_p)(l0,l1); | |
116 h0 = SUFF(_mm_movelh_p)(h0,h1); | |
117 return _mm256_insertf128_ps(_mm256_castps128_ps256(l0), h0, 1); | |
118 } | |
119 | |
120 static inline void ST(R *x, V v, INT ovs, const R *aligned_like) | |
121 { | |
122 __m128 h = _mm256_extractf128_ps(v, 1); | |
123 __m128 l = _mm256_castps256_ps128(v); | |
124 (void)aligned_like; /* UNUSED */ | |
125 /* WARNING: the extra_iter hack depends upon STOREL occurring | |
126 after STOREH */ | |
127 STOREH(x + 3*ovs, h); | |
128 STOREL(x + 2*ovs, h); | |
129 STOREH(x + ovs, l); | |
130 STOREL(x, l); | |
131 } | |
132 | |
133 #define STM2(x, v, ovs, aligned_like) /* no-op */ | |
134 static inline void STN2(R *x, V v0, V v1, INT ovs) | |
135 { | |
136 V x0 = VSHUF(v0, v1, SHUFVALS(0, 1, 0, 1)); | |
137 V x1 = VSHUF(v0, v1, SHUFVALS(2, 3, 2, 3)); | |
138 __m128 h0 = _mm256_extractf128_ps(x0, 1); | |
139 __m128 l0 = _mm256_castps256_ps128(x0); | |
140 __m128 h1 = _mm256_extractf128_ps(x1, 1); | |
141 __m128 l1 = _mm256_castps256_ps128(x1); | |
142 | |
143 *(__m128 *)(x + 3*ovs) = h1; | |
144 *(__m128 *)(x + 2*ovs) = h0; | |
145 *(__m128 *)(x + 1*ovs) = l1; | |
146 *(__m128 *)(x + 0*ovs) = l0; | |
147 } | |
148 | |
149 #define STM4(x, v, ovs, aligned_like) /* no-op */ | |
150 #define STN4(x, v0, v1, v2, v3, ovs) \ | |
151 { \ | |
152 V xxx0, xxx1, xxx2, xxx3; \ | |
153 V yyy0, yyy1, yyy2, yyy3; \ | |
154 xxx0 = _mm256_unpacklo_ps(v0, v2); \ | |
155 xxx1 = _mm256_unpackhi_ps(v0, v2); \ | |
156 xxx2 = _mm256_unpacklo_ps(v1, v3); \ | |
157 xxx3 = _mm256_unpackhi_ps(v1, v3); \ | |
158 yyy0 = _mm256_unpacklo_ps(xxx0, xxx2); \ | |
159 yyy1 = _mm256_unpackhi_ps(xxx0, xxx2); \ | |
160 yyy2 = _mm256_unpacklo_ps(xxx1, xxx3); \ | |
161 yyy3 = _mm256_unpackhi_ps(xxx1, xxx3); \ | |
162 *(__m128 *)(x + 0 * ovs) = _mm256_castps256_ps128(yyy0); \ | |
163 *(__m128 *)(x + 4 * ovs) = _mm256_extractf128_ps(yyy0, 1); \ | |
164 *(__m128 *)(x + 1 * ovs) = _mm256_castps256_ps128(yyy1); \ | |
165 *(__m128 *)(x + 5 * ovs) = _mm256_extractf128_ps(yyy1, 1); \ | |
166 *(__m128 *)(x + 2 * ovs) = _mm256_castps256_ps128(yyy2); \ | |
167 *(__m128 *)(x + 6 * ovs) = _mm256_extractf128_ps(yyy2, 1); \ | |
168 *(__m128 *)(x + 3 * ovs) = _mm256_castps256_ps128(yyy3); \ | |
169 *(__m128 *)(x + 7 * ovs) = _mm256_extractf128_ps(yyy3, 1); \ | |
170 } | |
171 | |
172 #else | |
173 static inline __m128d VMOVAPD_LD(const R *x) | |
174 { | |
175 /* gcc-4.6 miscompiles the combination _mm256_castpd128_pd256(VMOVAPD_LD(x)) | |
176 into a 256-bit vmovapd, which requires 32-byte aligment instead of | |
177 16-byte alignment. | |
178 | |
179 Force the use of vmovapd via asm until compilers stabilize. | |
180 */ | |
181 #if defined(__GNUC__) | |
182 __m128d var; | |
183 __asm__("vmovapd %1, %0\n" : "=x"(var) : "m"(x[0])); | |
184 return var; | |
185 #else | |
186 return *(const __m128d *)x; | |
187 #endif | |
188 } | |
189 | |
190 static inline V LD(const R *x, INT ivs, const R *aligned_like) | |
191 { | |
192 V var; | |
193 (void)aligned_like; /* UNUSED */ | |
194 var = _mm256_castpd128_pd256(VMOVAPD_LD(x)); | |
195 var = _mm256_insertf128_pd(var, *(const __m128d *)(x+ivs), 1); | |
196 return var; | |
197 } | |
198 | |
199 static inline void ST(R *x, V v, INT ovs, const R *aligned_like) | |
200 { | |
201 (void)aligned_like; /* UNUSED */ | |
202 /* WARNING: the extra_iter hack depends upon the store of the low | |
203 part occurring after the store of the high part */ | |
204 *(__m128d *)(x + ovs) = _mm256_extractf128_pd(v, 1); | |
205 *(__m128d *)x = _mm256_castpd256_pd128(v); | |
206 } | |
207 | |
208 | |
209 #define STM2 ST | |
210 #define STN2(x, v0, v1, ovs) /* nop */ | |
211 #define STM4(x, v, ovs, aligned_like) /* no-op */ | |
212 | |
213 /* STN4 is a macro, not a function, thanks to Visual C++ developers | |
214 deciding "it would be infrequent that people would want to pass more | |
215 than 3 [__m128 parameters] by value." Even though the comment | |
216 was made about __m128 parameters, it appears to apply to __m256 | |
217 parameters as well. */ | |
218 #define STN4(x, v0, v1, v2, v3, ovs) \ | |
219 { \ | |
220 V xxx0, xxx1, xxx2, xxx3; \ | |
221 xxx0 = _mm256_unpacklo_pd(v0, v1); \ | |
222 xxx1 = _mm256_unpackhi_pd(v0, v1); \ | |
223 xxx2 = _mm256_unpacklo_pd(v2, v3); \ | |
224 xxx3 = _mm256_unpackhi_pd(v2, v3); \ | |
225 STA(x, _mm256_permute2f128_pd(xxx0, xxx2, 0x20), 0, 0); \ | |
226 STA(x + ovs, _mm256_permute2f128_pd(xxx1, xxx3, 0x20), 0, 0); \ | |
227 STA(x + 2 * ovs, _mm256_permute2f128_pd(xxx0, xxx2, 0x31), 0, 0); \ | |
228 STA(x + 3 * ovs, _mm256_permute2f128_pd(xxx1, xxx3, 0x31), 0, 0); \ | |
229 } | |
230 #endif | |
231 | |
232 static inline V FLIP_RI(V x) | |
233 { | |
234 return VPERM1(x, | |
235 DS(SHUFVALD(1, 0), | |
236 SHUFVALS(1, 0, 3, 2))); | |
237 } | |
238 | |
239 static inline V VCONJ(V x) | |
240 { | |
241 V pmpm = VLIT(-0.0, 0.0); | |
242 return VXOR(pmpm, x); | |
243 } | |
244 | |
245 static inline V VBYI(V x) | |
246 { | |
247 return FLIP_RI(VCONJ(x)); | |
248 } | |
249 | |
250 /* FMA support */ | |
251 #define VFMA(a, b, c) VADD(c, VMUL(a, b)) | |
252 #define VFNMS(a, b, c) VSUB(c, VMUL(a, b)) | |
253 #define VFMS(a, b, c) VSUB(VMUL(a, b), c) | |
254 #define VFMAI(b, c) SUFF(_mm256_addsub_p)(c,FLIP_RI(b)) | |
255 #define VFNMSI(b, c) VSUB(c, VBYI(b)) | |
256 #define VFMACONJ(b,c) VADD(VCONJ(b),c) | |
257 #define VFMSCONJ(b,c) VSUB(VCONJ(b),c) | |
258 #define VFNMSCONJ(b,c) SUFF(_mm256_addsub_p)(c,b) | |
259 | |
260 static inline V VZMUL(V tx, V sr) | |
261 { | |
262 V tr = VDUPL(tx); | |
263 V ti = VDUPH(tx); | |
264 tr = VMUL(tr, sr); | |
265 ti = VMUL(ti, FLIP_RI(sr)); | |
266 return SUFF(_mm256_addsub_p)(tr,ti); | |
267 } | |
268 | |
269 static inline V VZMULJ(V tx, V sr) | |
270 { | |
271 V tr = VDUPL(tx); | |
272 V ti = VDUPH(tx); | |
273 tr = VMUL(tr, sr); | |
274 sr = VBYI(sr); | |
275 return VFNMS(ti, sr, tr); | |
276 } | |
277 | |
278 static inline V VZMULI(V tx, V sr) | |
279 { | |
280 V tr = VDUPL(tx); | |
281 V ti = VDUPH(tx); | |
282 ti = VMUL(ti, sr); | |
283 sr = VBYI(sr); | |
284 return VFMS(tr, sr, ti); | |
285 } | |
286 | |
287 static inline V VZMULIJ(V tx, V sr) | |
288 { | |
289 V tr = VDUPL(tx); | |
290 V ti = VDUPH(tx); | |
291 ti = VMUL(ti, sr); | |
292 tr = VMUL(tr, FLIP_RI(sr)); | |
293 return SUFF(_mm256_addsub_p)(ti,tr); | |
294 } | |
295 | |
296 /* twiddle storage #1: compact, slower */ | |
297 #ifdef FFTW_SINGLE | |
298 # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}, {TW_CEXP, v+2, x}, {TW_CEXP, v+3, x} | |
299 #else | |
300 # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x} | |
301 #endif | |
302 #define TWVL1 (VL) | |
303 | |
304 static inline V BYTW1(const R *t, V sr) | |
305 { | |
306 return VZMUL(LDA(t, 2, t), sr); | |
307 } | |
308 | |
309 static inline V BYTWJ1(const R *t, V sr) | |
310 { | |
311 return VZMULJ(LDA(t, 2, t), sr); | |
312 } | |
313 | |
314 /* twiddle storage #2: twice the space, faster (when in cache) */ | |
315 #ifdef FFTW_SINGLE | |
316 # define VTW2(v,x) \ | |
317 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \ | |
318 {TW_COS, v+2, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, {TW_COS, v+3, x}, \ | |
319 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}, \ | |
320 {TW_SIN, v+2, -x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, -x}, {TW_SIN, v+3, x} | |
321 #else | |
322 # define VTW2(v,x) \ | |
323 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \ | |
324 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x} | |
325 #endif | |
326 #define TWVL2 (2 * VL) | |
327 | |
328 static inline V BYTW2(const R *t, V sr) | |
329 { | |
330 const V *twp = (const V *)t; | |
331 V si = FLIP_RI(sr); | |
332 V tr = twp[0], ti = twp[1]; | |
333 return VFMA(tr, sr, VMUL(ti, si)); | |
334 } | |
335 | |
336 static inline V BYTWJ2(const R *t, V sr) | |
337 { | |
338 const V *twp = (const V *)t; | |
339 V si = FLIP_RI(sr); | |
340 V tr = twp[0], ti = twp[1]; | |
341 return VFNMS(ti, si, VMUL(tr, sr)); | |
342 } | |
343 | |
344 /* twiddle storage #3 */ | |
345 #define VTW3 VTW1 | |
346 #define TWVL3 TWVL1 | |
347 | |
348 /* twiddle storage for split arrays */ | |
349 #ifdef FFTW_SINGLE | |
350 # define VTWS(v,x) \ | |
351 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \ | |
352 {TW_COS, v+4, x}, {TW_COS, v+5, x}, {TW_COS, v+6, x}, {TW_COS, v+7, x}, \ | |
353 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}, \ | |
354 {TW_SIN, v+4, x}, {TW_SIN, v+5, x}, {TW_SIN, v+6, x}, {TW_SIN, v+7, x} | |
355 #else | |
356 # define VTWS(v,x) \ | |
357 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \ | |
358 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x} | |
359 #endif | |
360 #define TWVLS (2 * VL) | |
361 | |
362 | |
363 /* Use VZEROUPPER to avoid the penalty of switching from AVX to SSE. | |
364 See Intel Optimization Manual (April 2011, version 248966), Section | |
365 11.3 */ | |
366 #define VLEAVE _mm256_zeroupper | |
367 | |
368 #include "simd-common.h" |