comparison src/fftw-3.3.8/simd-support/simd-avx2-128.h @ 82:d0c2a83c1364

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam
date Tue, 19 Nov 2019 14:52:55 +0000
parents
children
comparison
equal deleted inserted replaced
81:7029a4916348 82:d0c2a83c1364
1 /*
2 * Copyright (c) 2003, 2007-14 Matteo Frigo
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
4 *
5 * 128-bit AVX2 support by Erik Lindahl, 2015.
6 * Erik Lindahl hereby 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 "AVX2 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 _avx2_128 /* for renaming */
37 #define VL DS(1,2) /* SIMD vector length, in term of complex numbers */
38 #define SIMD_VSTRIDE_OKA(x) DS(1,((x) == 2))
39 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
40
41 #if defined(__GNUC__) && !defined(__AVX2__) /* sanity check */
42 #error "compiling simd-avx2-128.h without avx2 support"
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(__m128d,__m128) V;
54 #define VADD SUFF(_mm_add_p)
55 #define VSUB SUFF(_mm_sub_p)
56 #define VMUL SUFF(_mm_mul_p)
57 #define VXOR SUFF(_mm_xor_p)
58 #define SHUF SUFF(_mm_shuffle_p)
59 #define VPERM1 SUFF(_mm_permute_p)
60 #define UNPCKL SUFF(_mm_unpacklo_p)
61 #define UNPCKH SUFF(_mm_unpackhi_p)
62
63 #define SHUFVALS(fp0,fp1,fp2,fp3) \
64 (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
65
66 #define VDUPL(x) DS(_mm_permute_pd(x,0), _mm_moveldup_ps(x))
67 #define VDUPH(x) DS(_mm_permute_pd(x,3), _mm_movehdup_ps(x))
68 #define LOADH(addr, val) _mm_loadh_pi(val, (const __m64 *)(addr))
69 #define LOADL(addr, val) _mm_loadl_pi(val, (const __m64 *)(addr))
70 #define STOREH(a, v) DS(_mm_storeh_pd(a, v), _mm_storeh_pi((__m64 *)(a), v))
71 #define STOREL(a, v) DS(_mm_storel_pd(a, v), _mm_storel_pi((__m64 *)(a), v))
72
73 #define VLIT(x0, x1) DS(_mm_set_pd(x0, x1), _mm_set_ps(x0, x1, x0, x1))
74 #define DVK(var, val) V var = VLIT(val, val)
75 #define LDK(x) x
76
77 static inline V LDA(const R *x, INT ivs, const R *aligned_like)
78 {
79 (void)aligned_like; /* UNUSED */
80 (void)ivs; /* UNUSED */
81 return *(const V *)x;
82 }
83
84 static inline void STA(R *x, V v, INT ovs, const R *aligned_like)
85 {
86 (void)aligned_like; /* UNUSED */
87 (void)ovs; /* UNUSED */
88 *(V *)x = v;
89 }
90
91 #ifdef FFTW_SINGLE
92
93 # ifdef _MSC_VER
94 /* Temporarily disable the warning "uninitialized local variable
95 'name' used" and runtime checks for using a variable before it is
96 defined which is erroneously triggered by the LOADL0 / LOADH macros
97 as they only modify VAL partly each. */
98 # ifndef __INTEL_COMPILER
99 # pragma warning(disable : 4700)
100 # pragma runtime_checks("u", off)
101 # endif
102 # endif
103 # ifdef __INTEL_COMPILER
104 # pragma warning(disable : 592)
105 # endif
106
107 static inline V LD(const R *x, INT ivs, const R *aligned_like)
108 {
109 __m128 l0, l1;
110 (void)aligned_like; /* UNUSED */
111 #if defined(__ICC) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 8)
112 l0 = LOADL(x, SUFF(_mm_undefined_p)());
113 l1 = LOADL(x + ivs, SUFF(_mm_undefined_p)());
114 #else
115 l0 = LOADL(x, l0);
116 l1 = LOADL(x + ivs, l1);
117 #endif
118 return SUFF(_mm_movelh_p)(l0,l1);
119 }
120
121 # ifdef _MSC_VER
122 # ifndef __INTEL_COMPILER
123 # pragma warning(default : 4700)
124 # pragma runtime_checks("u", restore)
125 # endif
126 # endif
127 # ifdef __INTEL_COMPILER
128 # pragma warning(default : 592)
129 # endif
130
131 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
132 {
133 (void)aligned_like; /* UNUSED */
134 /* WARNING: the extra_iter hack depends upon STOREL occurring
135 after STOREH */
136 STOREH(x + ovs, v);
137 STOREL(x, v);
138 }
139
140 #else /* ! FFTW_SINGLE */
141 # define LD LDA
142 # define ST STA
143 #endif
144
145 #define STM2 DS(STA,ST)
146 #define STN2(x, v0, v1, ovs) /* nop */
147
148 #ifdef FFTW_SINGLE
149 # define STM4(x, v, ovs, aligned_like) /* no-op */
150 /* STN4 is a macro, not a function, thanks to Visual C++ developers
151 deciding "it would be infrequent that people would want to pass more
152 than 3 [__m128 parameters] by value." 3 parameters ought to be enough
153 for anybody. */
154 # define STN4(x, v0, v1, v2, v3, ovs) \
155 { \
156 V xxx0, xxx1, xxx2, xxx3; \
157 xxx0 = UNPCKL(v0, v2); \
158 xxx1 = UNPCKH(v0, v2); \
159 xxx2 = UNPCKL(v1, v3); \
160 xxx3 = UNPCKH(v1, v3); \
161 STA(x, UNPCKL(xxx0, xxx2), 0, 0); \
162 STA(x + ovs, UNPCKH(xxx0, xxx2), 0, 0); \
163 STA(x + 2 * ovs, UNPCKL(xxx1, xxx3), 0, 0); \
164 STA(x + 3 * ovs, UNPCKH(xxx1, xxx3), 0, 0); \
165 }
166 #else /* !FFTW_SINGLE */
167 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
168 {
169 (void)aligned_like; /* UNUSED */
170 STOREL(x, v);
171 STOREH(x + ovs, v);
172 }
173 # define STN4(x, v0, v1, v2, v3, ovs) /* nothing */
174 #endif
175
176 static inline V FLIP_RI(V x)
177 {
178 return VPERM1(x, DS(1, SHUFVALS(1, 0, 3, 2)));
179 }
180
181 static inline V VCONJ(V x)
182 {
183 /* Produce a SIMD vector[VL] of (0 + -0i).
184
185 We really want to write this:
186
187 V pmpm = VLIT(-0.0, 0.0);
188
189 but historically some compilers have ignored the distiction
190 between +0 and -0. It looks like 'gcc-8 -fast-math' treats -0
191 as 0 too.
192 */
193 union uvec {
194 unsigned u[4];
195 V v;
196 };
197 static const union uvec pmpm = {
198 #ifdef FFTW_SINGLE
199 { 0x00000000, 0x80000000, 0x00000000, 0x80000000 }
200 #else
201 { 0x00000000, 0x00000000, 0x00000000, 0x80000000 }
202 #endif
203 };
204 return VXOR(pmpm.v, x);
205 }
206
207 static inline V VBYI(V x)
208 {
209 x = VCONJ(x);
210 x = FLIP_RI(x);
211 return x;
212 }
213
214 /* FMA support */
215 #define VFMA(a, b, c) SUFF(_mm_fmadd_p)(a,b,c)
216 #define VFNMS(a, b, c) SUFF(_mm_fnmadd_p)(a,b,c)
217 #define VFMS(a, b, c) SUFF(_mm_fmsub_p)(a,b,c)
218 #define VFMAI(b, c) SUFF(_mm_addsub_p)(c,FLIP_RI(b))
219 #define VFNMSI(b, c) VSUB(c, VBYI(b))
220 #define VFMACONJ(b,c) VADD(VCONJ(b),c)
221 #define VFMSCONJ(b,c) VSUB(VCONJ(b),c)
222 #define VFNMSCONJ(b,c) SUFF(_mm_addsub_p)(c,b)
223
224
225 static inline V VZMUL(V tx, V sr)
226 {
227 V tr = VDUPL(tx);
228 V ti = VDUPH(tx);
229 ti = VMUL(ti, FLIP_RI(sr));
230 return SUFF(_mm_fmaddsub_p)(tr,sr,ti);
231 }
232
233 static inline V VZMULJ(V tx, V sr)
234 {
235 V tr = VDUPL(tx);
236 V ti = VDUPH(tx);
237 ti = VMUL(ti, FLIP_RI(sr));
238 return SUFF(_mm_fmsubadd_p)(tr,sr,ti);
239 }
240
241 static inline V VZMULI(V tx, V sr)
242 {
243 V tr = VDUPL(tx);
244 V ti = VDUPH(tx);
245 ti = VMUL(ti, sr);
246 sr = VBYI(sr);
247 return VFMS(tr, sr, ti);
248 }
249
250 static inline V VZMULIJ(V tx, V sr)
251 {
252 V tr = VDUPL(tx);
253 V ti = VDUPH(tx);
254 tr = VMUL(tr, FLIP_RI(sr));
255 return SUFF(_mm_fmaddsub_p)(ti,sr,tr);
256 }
257
258 /* twiddle storage #1: compact, slower */
259 #ifdef FFTW_SINGLE
260 # define VTW1(v,x) \
261 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
262 static inline V BYTW1(const R *t, V sr)
263 {
264 const V *twp = (const V *)t;
265 V tx = twp[0];
266 V tr = UNPCKL(tx, tx);
267 V ti = UNPCKH(tx, tx);
268 ti = VMUL(ti, FLIP_RI(sr));
269 return SUFF(_mm_fmaddsub_p)(tr,sr,ti);
270 }
271 static inline V BYTWJ1(const R *t, V sr)
272 {
273 const V *twp = (const V *)t;
274 V tx = twp[0];
275 V tr = UNPCKL(tx, tx);
276 V ti = UNPCKH(tx, tx);
277 ti = VMUL(ti, FLIP_RI(sr));
278 return SUFF(_mm_fmsubadd_p)(tr,sr,ti);
279 }
280 #else /* !FFTW_SINGLE */
281 # define VTW1(v,x) {TW_CEXP, v, x}
282 static inline V BYTW1(const R *t, V sr)
283 {
284 V tx = LD(t, 1, t);
285 return VZMUL(tx, sr);
286 }
287 static inline V BYTWJ1(const R *t, V sr)
288 {
289 V tx = LD(t, 1, t);
290 return VZMULJ(tx, sr);
291 }
292 #endif
293 #define TWVL1 (VL)
294
295 /* twiddle storage #2: twice the space, faster (when in cache) */
296 #ifdef FFTW_SINGLE
297 # define VTW2(v,x) \
298 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
299 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}
300 #else /* !FFTW_SINGLE */
301 # define VTW2(v,x) \
302 {TW_COS, v, x}, {TW_COS, v, x}, {TW_SIN, v, -x}, {TW_SIN, v, x}
303 #endif
304 #define TWVL2 (2 * VL)
305 static inline V BYTW2(const R *t, V sr)
306 {
307 const V *twp = (const V *)t;
308 V si = FLIP_RI(sr);
309 V tr = twp[0], ti = twp[1];
310 return VFMA(tr, sr, VMUL(ti, si));
311 }
312 static inline V BYTWJ2(const R *t, V sr)
313 {
314 const V *twp = (const V *)t;
315 V si = FLIP_RI(sr);
316 V tr = twp[0], ti = twp[1];
317 return VFNMS(ti, si, VMUL(tr, sr));
318 }
319
320 /* twiddle storage #3 */
321 #ifdef FFTW_SINGLE
322 # define VTW3(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
323 # define TWVL3 (VL)
324 #else
325 # define VTW3(v,x) VTW1(v,x)
326 # define TWVL3 TWVL1
327 #endif
328
329 /* twiddle storage for split arrays */
330 #ifdef FFTW_SINGLE
331 # define VTWS(v,x) \
332 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
333 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}
334 #else
335 # define VTWS(v,x) \
336 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
337 #endif
338 #define TWVLS (2 * VL)
339
340 #define VLEAVE() /* nothing */
341
342 #include "simd-common.h"