comparison src/fftw-3.3.5/simd-support/simd-avx512.h @ 127:7867fa7e1b6b

Current fftw source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:40:26 +0100
parents
children
comparison
equal deleted inserted replaced
126:4a7071416412 127:7867fa7e1b6b
1 /*
2 * Copyright (c) 2003, 2007-11 Matteo Frigo
3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
4 *
5 * AVX-512 support implemented by Romain Dolbeau.
6 * Romain Dolbeau hereby places his modifications in the public domain.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27
28 #if defined(FFTW_LDOUBLE) || defined(FFTW_QUAD)
29 #error "AVX-512 vector instructions only works in single or double precision"
30 #endif
31
32 #ifdef FFTW_SINGLE
33 # define DS(d,s) s /* single-precision option */
34 # define SUFF(name) name ## _ps
35 # define SCAL(x) x ## f
36 #else /* !FFTW_SINGLE */
37 # define DS(d,s) d /* double-precision option */
38 # define SUFF(name) name ## _pd
39 # define SCAL(x) x
40 #endif /* FFTW_SINGLE */
41
42 #define SIMD_SUFFIX _avx512 /* for renaming */
43 #define VL DS(4, 8) /* SIMD complex vector length */
44 #define SIMD_VSTRIDE_OKA(x) ((x) == 2)
45 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
46
47 #if defined(__GNUC__) && !defined(__AVX512F__) /* sanity check */
48 #error "compiling simd-avx512.h without avx-512f support"
49 #endif
50
51 #if !defined(HAVE_FMA)
52 #warning "You should probably enable FMAs with --enable-fma for AVX-512"
53 #endif
54
55 #if !defined(HAVE_AVX2)
56 #warning "You should probably enable AVX2 with --enable-avx2 for AVX-512"
57 #endif
58
59 #include <immintrin.h>
60
61 typedef DS(__m512d, __m512) V;
62
63 #define VLIT(re, im) DS(SUFF(_mm512_setr)(im, re, im, re, im, re, im, re),SUFF(_mm512_setr)(im, re, im, re, im, re, im, re, im, re, im, re, im, re, im, re))
64 #define VLIT1(val) SUFF(_mm512_set1)(val)
65 #define LDK(x) x
66 #define DVK(var, val) V var = VLIT1(val)
67 #define VZERO SUFF(_mm512_setzero)()
68
69 #define VDUPL(x) DS(_mm512_movedup_pd(x),_mm512_moveldup_ps(x))
70 #define VDUPH(x) DS(_mm512_unpackhi_pd(x, x),_mm512_movehdup_ps(x))
71 #define FLIP_RI(x) SUFF(_mm512_shuffle)(x, x, DS(0x55,0xB1))
72 #define VCONJ(x) SUFF(_mm512_fmsubadd)(VZERO, VZERO, x)
73 static inline V VBYI(V x)
74 {
75 return FLIP_RI(VCONJ(x));
76 }
77
78 #define VADD(a,b) SUFF(_mm512_add)(a,b)
79 #define VSUB(a,b) SUFF(_mm512_sub)(a,b)
80 #define VMUL(a,b) SUFF(_mm512_mul)(a,b)
81 #define VFMA(a, b, c) SUFF(_mm512_fmadd)(a, b, c)
82 #define VFMS(a, b, c) SUFF(_mm512_fmsub)(a, b, c)
83 #define VFNMS(a, b, c) SUFF(_mm512_fnmadd)(a, b, c)
84 #define VFMAI(b, c) SUFF(_mm512_fmaddsub)(VLIT1(1.), c, FLIP_RI(b))
85 #define VFNMSI(b, c) SUFF(_mm512_fmsubadd)(VLIT1(1.), c, FLIP_RI(b))
86 #define VFMACONJ(b,c) SUFF(_mm512_fmsubadd)(VLIT1(1.), c, b)
87 #define VFMSCONJ(b,c) SUFF(_mm512_fmsubadd)(VLIT1(-1.), c, b)
88 #define VFNMSCONJ(b,c) SUFF(_mm512_fmaddsub)(VLIT1(1.), c, b)
89
90 static inline V LDA(const R *x, INT ivs, const R *aligned_like) {
91 (void)aligned_like; /* UNUSED */
92 (void)ivs; /* UNUSED */
93 return SUFF(_mm512_load)(x);
94 }
95 static inline void STA(R *x, V v, INT ovs, const R *aligned_like) {
96 (void)aligned_like; /* UNUSED */
97 (void)ovs; /* UNUSED */
98 SUFF(_mm512_store)(x, v);
99 }
100
101 #if FFTW_SINGLE
102
103 static inline V LDu(const R *x, INT ivs, const R *aligned_like)
104 {
105 (void)aligned_like; /* UNUSED */
106 __m512i index = _mm512_set_epi32(7 * ivs + 1, 7 * ivs,
107 6 * ivs + 1, 6 * ivs,
108 5 * ivs + 1, 5 * ivs,
109 4 * ivs + 1, 4 * ivs,
110 3 * ivs + 1, 3 * ivs,
111 2 * ivs + 1, 2 * ivs,
112 1 * ivs + 1, 1 * ivs,
113 0 * ivs + 1, 0 * ivs);
114
115 return _mm512_i32gather_ps(index, x, 4);
116 }
117
118 static inline void STu(R *x, V v, INT ovs, const R *aligned_like)
119 {
120 (void)aligned_like; /* UNUSED */
121 __m512i index = _mm512_set_epi32(7 * ovs + 1, 7 * ovs,
122 6 * ovs + 1, 6 * ovs,
123 5 * ovs + 1, 5 * ovs,
124 4 * ovs + 1, 4 * ovs,
125 3 * ovs + 1, 3 * ovs,
126 2 * ovs + 1, 2 * ovs,
127 1 * ovs + 1, 1 * ovs,
128 0 * ovs + 1, 0 * ovs);
129
130 _mm512_i32scatter_ps(x, index, v, 4);
131 }
132
133 #else /* !FFTW_SINGLE */
134
135 static inline V LDu(const R *x, INT ivs, const R *aligned_like)
136 {
137 (void)aligned_like; /* UNUSED */
138 __m256i index = _mm256_set_epi32(3 * ivs + 1, 3 * ivs,
139 2 * ivs + 1, 2 * ivs,
140 1 * ivs + 1, 1 * ivs,
141 0 * ivs + 1, 0 * ivs);
142
143 return _mm512_i32gather_pd(index, x, 8);
144 }
145
146 static inline void STu(R *x, V v, INT ovs, const R *aligned_like)
147 {
148 (void)aligned_like; /* UNUSED */
149 __m256i index = _mm256_set_epi32(3 * ovs + 1, 3 * ovs,
150 2 * ovs + 1, 2 * ovs,
151 1 * ovs + 1, 1 * ovs,
152 0 * ovs + 1, 0 * ovs);
153
154 _mm512_i32scatter_pd(x, index, v, 8);
155 }
156
157 #endif /* FFTW_SINGLE */
158
159 #define LD LDu
160 #define ST STu
161
162 #ifdef FFTW_SINGLE
163 #define STM2(x, v, ovs, a) ST(x, v, ovs, a)
164 #define STN2(x, v0, v1, ovs) /* nop */
165
166 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
167 {
168 (void)aligned_like; /* UNUSED */
169 __m512i index = _mm512_set_epi32(15 * ovs, 14 * ovs,
170 13 * ovs, 12 * ovs,
171 11 * ovs, 10 * ovs,
172 9 * ovs, 8 * ovs,
173 7 * ovs, 6 * ovs,
174 5 * ovs, 4 * ovs,
175 3 * ovs, 2 * ovs,
176 1 * ovs, 0 * ovs);
177
178 _mm512_i32scatter_ps(x, index, v, 4);
179 }
180 #define STN4(x, v0, v1, v2, v3, ovs) /* no-op */
181 #else /* !FFTW_SINGLE */
182 #define STM2(x, v, ovs, a) ST(x, v, ovs, a)
183 #define STN2(x, v0, v1, ovs) /* nop */
184
185 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
186 {
187 (void)aligned_like; /* UNUSED */
188 __m256i index = _mm256_set_epi32(7 * ovs, 6 * ovs,
189 5 * ovs, 4 * ovs,
190 3 * ovs, 2 * ovs,
191 1 * ovs, 0 * ovs);
192
193 _mm512_i32scatter_pd(x, index, v, 8);
194 }
195 #define STN4(x, v0, v1, v2, v3, ovs) /* no-op */
196 #endif /* FFTW_SINGLE */
197
198 static inline V VZMUL(V tx, V sr)
199 {
200 /* V tr = VDUPL(tx); */
201 /* V ti = VDUPH(tx); */
202 /* tr = VMUL(sr, tr); */
203 /* sr = VBYI(sr); */
204 /* return VFMA(ti, sr, tr); */
205 return SUFF(_mm512_fmaddsub)(sr, VDUPL(tx), VMUL(FLIP_RI(sr), VDUPH(tx)));
206 }
207
208 static inline V VZMULJ(V tx, V sr)
209 {
210 /* V tr = VDUPL(tx); */
211 /* V ti = VDUPH(tx); */
212 /* tr = VMUL(sr, tr); */
213 /* sr = VBYI(sr); */
214 /* return VFNMS(ti, sr, tr); */
215 return SUFF(_mm512_fmsubadd)(sr, VDUPL(tx), VMUL(FLIP_RI(sr), VDUPH(tx)));
216 }
217
218 static inline V VZMULI(V tx, V sr)
219 {
220 V tr = VDUPL(tx);
221 V ti = VDUPH(tx);
222 ti = VMUL(ti, sr);
223 sr = VBYI(sr);
224 return VFMS(tr, sr, ti);
225 /* return SUFF(_mm512_addsub)(SUFF(_mm512_fnmadd)(sr, VDUPH(tx), VZERO), VMUL(FLIP_RI(sr), VDUPL(tx))); */
226 }
227
228 static inline V VZMULIJ(V tx, V sr)
229 {
230 /* V tr = VDUPL(tx); */
231 /* V ti = VDUPH(tx); */
232 /* ti = VMUL(ti, sr); */
233 /* sr = VBYI(sr); */
234 /* return VFMA(tr, sr, ti); */
235 return SUFF(_mm512_fmaddsub)(sr, VDUPH(tx), VMUL(FLIP_RI(sr), VDUPL(tx)));
236 }
237
238 /* twiddle storage #1: compact, slower */
239 #ifdef FFTW_SINGLE
240 # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}, {TW_CEXP, v+2, x}, {TW_CEXP, v+3, x}, {TW_CEXP, v+4, x}, {TW_CEXP, v+5, x}, {TW_CEXP, v+6, x}, {TW_CEXP, v+7, x}
241 #else /* !FFTW_SINGLE */
242 # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}, {TW_CEXP, v+2, x}, {TW_CEXP, v+3, x}
243 #endif /* FFTW_SINGLE */
244 #define TWVL1 (VL)
245
246 static inline V BYTW1(const R *t, V sr)
247 {
248 return VZMUL(LDA(t, 2, t), sr);
249 }
250
251 static inline V BYTWJ1(const R *t, V sr)
252 {
253 return VZMULJ(LDA(t, 2, t), sr);
254 }
255
256 /* twiddle storage #2: twice the space, faster (when in cache) */
257 #ifdef FFTW_SINGLE
258 # define VTW2(v,x) \
259 {TW_COS, v , x}, {TW_COS, v , x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
260 {TW_COS, v+2, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, {TW_COS, v+3, x}, \
261 {TW_COS, v+4, x}, {TW_COS, v+4, x}, {TW_COS, v+5, x}, {TW_COS, v+5, x}, \
262 {TW_COS, v+6, x}, {TW_COS, v+6, x}, {TW_COS, v+7, x}, {TW_COS, v+7, x}, \
263 {TW_SIN, v , -x}, {TW_SIN, v , x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}, \
264 {TW_SIN, v+2, -x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, -x}, {TW_SIN, v+3, x}, \
265 {TW_SIN, v+4, -x}, {TW_SIN, v+4, x}, {TW_SIN, v+5, -x}, {TW_SIN, v+5, x}, \
266 {TW_SIN, v+6, -x}, {TW_SIN, v+6, x}, {TW_SIN, v+7, -x}, {TW_SIN, v+7, x}
267 #else /* !FFTW_SINGLE */
268 # define VTW2(v,x) \
269 {TW_COS, v , x}, {TW_COS, v , x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
270 {TW_COS, v+2, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, {TW_COS, v+3, x}, \
271 {TW_SIN, v , -x}, {TW_SIN, v , x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}, \
272 {TW_SIN, v+2, -x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, -x}, {TW_SIN, v+3, x}
273 #endif /* FFTW_SINGLE */
274 #define TWVL2 (2 * VL)
275
276 static inline V BYTW2(const R *t, V sr)
277 {
278 const V *twp = (const V *)t;
279 V si = FLIP_RI(sr);
280 V tr = twp[0], ti = twp[1];
281 /* V tr = LD(t, 2, t), ti = LD(t + VL, 2, t + VL); */
282 return VFMA(tr, sr, VMUL(ti, si));
283 }
284
285 static inline V BYTWJ2(const R *t, V sr)
286 {
287 const V *twp = (const V *)t;
288 V si = FLIP_RI(sr);
289 V tr = twp[0], ti = twp[1];
290 /* V tr = LD(t, 2, t), ti = LD(t + VL, 2, t + VL); */
291 return VFNMS(ti, si, VMUL(tr, sr));
292 }
293
294 /* twiddle storage #3 */
295 #define VTW3(v,x) VTW1(v,x)
296 #define TWVL3 TWVL1
297
298 /* twiddle storage for split arrays */
299 #ifdef FFTW_SINGLE
300 # define VTWS(v,x) \
301 {TW_COS, v , x}, {TW_COS, v+1 , x}, {TW_COS, v+2 , x}, {TW_COS, v+3 , x}, \
302 {TW_COS, v+4 , x}, {TW_COS, v+5 , x}, {TW_COS, v+6 , x}, {TW_COS, v+7 , x}, \
303 {TW_COS, v+8 , x}, {TW_COS, v+9 , x}, {TW_COS, v+10, x}, {TW_COS, v+11, x}, \
304 {TW_COS, v+12, x}, {TW_COS, v+13, x}, {TW_COS, v+14, x}, {TW_COS, v+15, x}, \
305 {TW_SIN, v , x}, {TW_SIN, v+1 , x}, {TW_SIN, v+2 , x}, {TW_SIN, v+3 , x}, \
306 {TW_SIN, v+4 , x}, {TW_SIN, v+5 , x}, {TW_SIN, v+6 , x}, {TW_SIN, v+7 , x}, \
307 {TW_SIN, v+8 , x}, {TW_SIN, v+9 , x}, {TW_SIN, v+10, x}, {TW_SIN, v+11, x}, \
308 {TW_SIN, v+12, x}, {TW_SIN, v+13, x}, {TW_SIN, v+14, x}, {TW_SIN, v+15, x}
309 #else /* !FFTW_SINGLE */
310 # define VTWS(v,x) \
311 {TW_COS, v , x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
312 {TW_COS, v+4, x}, {TW_COS, v+5, x}, {TW_COS, v+6, x}, {TW_COS, v+7, x}, \
313 {TW_SIN, v , x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}, \
314 {TW_SIN, v+4, x}, {TW_SIN, v+5, x}, {TW_SIN, v+6, x}, {TW_SIN, v+7, x}
315 #endif /* FFTW_SINGLE */
316 #define TWVLS (2 * VL)
317
318 #define VLEAVE() /* nothing */
319
320 #include "simd-common.h"