Chris@42
|
1 /*
|
Chris@42
|
2 * Copyright (c) 2003, 2007-14 Matteo Frigo
|
Chris@42
|
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
|
Chris@42
|
4 *
|
Chris@42
|
5 * 128-bit AVX2 support by Erik Lindahl, 2015.
|
Chris@42
|
6 * Erik Lindahl hereby places his modifications in the public domain.
|
Chris@42
|
7 *
|
Chris@42
|
8 * This program is free software; you can redistribute it and/or modify
|
Chris@42
|
9 * it under the terms of the GNU General Public License as published by
|
Chris@42
|
10 * the Free Software Foundation; either version 2 of the License, or
|
Chris@42
|
11 * (at your option) any later version.
|
Chris@42
|
12 *
|
Chris@42
|
13 * This program is distributed in the hope that it will be useful,
|
Chris@42
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@42
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@42
|
16 * GNU General Public License for more details.
|
Chris@42
|
17 *
|
Chris@42
|
18 * You should have received a copy of the GNU General Public License
|
Chris@42
|
19 * along with this program; if not, write to the Free Software
|
Chris@42
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Chris@42
|
21 *
|
Chris@42
|
22 */
|
Chris@42
|
23
|
Chris@42
|
24 #if defined(FFTW_LDOUBLE) || defined(FFTW_QUAD)
|
Chris@42
|
25 #error "AVX2 only works in single or double precision"
|
Chris@42
|
26 #endif
|
Chris@42
|
27
|
Chris@42
|
28 #ifdef FFTW_SINGLE
|
Chris@42
|
29 # define DS(d,s) s /* single-precision option */
|
Chris@42
|
30 # define SUFF(name) name ## s
|
Chris@42
|
31 #else
|
Chris@42
|
32 # define DS(d,s) d /* double-precision option */
|
Chris@42
|
33 # define SUFF(name) name ## d
|
Chris@42
|
34 #endif
|
Chris@42
|
35
|
Chris@42
|
36 #define SIMD_SUFFIX _avx2_128 /* for renaming */
|
Chris@42
|
37 #define VL DS(1,2) /* SIMD vector length, in term of complex numbers */
|
Chris@42
|
38 #define SIMD_VSTRIDE_OKA(x) DS(1,((x) == 2))
|
Chris@42
|
39 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
|
Chris@42
|
40
|
Chris@42
|
41 #if defined(__GNUC__) && !defined(__AVX2__) /* sanity check */
|
Chris@42
|
42 #error "compiling simd-avx2-128.h without avx2 support"
|
Chris@42
|
43 #endif
|
Chris@42
|
44
|
Chris@42
|
45 #ifdef _MSC_VER
|
Chris@42
|
46 #ifndef inline
|
Chris@42
|
47 #define inline __inline
|
Chris@42
|
48 #endif
|
Chris@42
|
49 #endif
|
Chris@42
|
50
|
Chris@42
|
51 #include <immintrin.h>
|
Chris@42
|
52
|
Chris@42
|
53 typedef DS(__m128d,__m128) V;
|
Chris@42
|
54 #define VADD SUFF(_mm_add_p)
|
Chris@42
|
55 #define VSUB SUFF(_mm_sub_p)
|
Chris@42
|
56 #define VMUL SUFF(_mm_mul_p)
|
Chris@42
|
57 #define VXOR SUFF(_mm_xor_p)
|
Chris@42
|
58 #define SHUF SUFF(_mm_shuffle_p)
|
Chris@42
|
59 #define VPERM1 SUFF(_mm_permute_p)
|
Chris@42
|
60 #define UNPCKL SUFF(_mm_unpacklo_p)
|
Chris@42
|
61 #define UNPCKH SUFF(_mm_unpackhi_p)
|
Chris@42
|
62
|
Chris@42
|
63 #define SHUFVALS(fp0,fp1,fp2,fp3) \
|
Chris@42
|
64 (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
|
Chris@42
|
65
|
Chris@42
|
66 #define VDUPL(x) DS(_mm_permute_pd(x,0), _mm_moveldup_ps(x))
|
Chris@42
|
67 #define VDUPH(x) DS(_mm_permute_pd(x,3), _mm_movehdup_ps(x))
|
Chris@42
|
68 #define LOADH(addr, val) _mm_loadh_pi(val, (const __m64 *)(addr))
|
Chris@42
|
69 #define LOADL(addr, val) _mm_loadl_pi(val, (const __m64 *)(addr))
|
Chris@42
|
70 #define STOREH(a, v) DS(_mm_storeh_pd(a, v), _mm_storeh_pi((__m64 *)(a), v))
|
Chris@42
|
71 #define STOREL(a, v) DS(_mm_storel_pd(a, v), _mm_storel_pi((__m64 *)(a), v))
|
Chris@42
|
72
|
Chris@42
|
73 #define VLIT(x0, x1) DS(_mm_set_pd(x0, x1), _mm_set_ps(x0, x1, x0, x1))
|
Chris@42
|
74 #define DVK(var, val) V var = VLIT(val, val)
|
Chris@42
|
75 #define LDK(x) x
|
Chris@42
|
76
|
Chris@42
|
77 static inline V LDA(const R *x, INT ivs, const R *aligned_like)
|
Chris@42
|
78 {
|
Chris@42
|
79 (void)aligned_like; /* UNUSED */
|
Chris@42
|
80 (void)ivs; /* UNUSED */
|
Chris@42
|
81 return *(const V *)x;
|
Chris@42
|
82 }
|
Chris@42
|
83
|
Chris@42
|
84 static inline void STA(R *x, V v, INT ovs, const R *aligned_like)
|
Chris@42
|
85 {
|
Chris@42
|
86 (void)aligned_like; /* UNUSED */
|
Chris@42
|
87 (void)ovs; /* UNUSED */
|
Chris@42
|
88 *(V *)x = v;
|
Chris@42
|
89 }
|
Chris@42
|
90
|
Chris@42
|
91 #ifdef FFTW_SINGLE
|
Chris@42
|
92
|
Chris@42
|
93 static inline V LD(const R *x, INT ivs, const R *aligned_like)
|
Chris@42
|
94 {
|
Chris@42
|
95 __m128 l0, l1;
|
Chris@42
|
96 (void)aligned_like; /* UNUSED */
|
Chris@42
|
97 #if defined(__ICC) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 8)
|
Chris@42
|
98 l0 = LOADL(x, SUFF(_mm_undefined_p)());
|
Chris@42
|
99 l1 = LOADL(x + ivs, SUFF(_mm_undefined_p)());
|
Chris@42
|
100 #else
|
Chris@42
|
101 l0 = LOADL(x, l0);
|
Chris@42
|
102 l1 = LOADL(x + ivs, l1);
|
Chris@42
|
103 #endif
|
Chris@42
|
104 return SUFF(_mm_movelh_p)(l0,l1);
|
Chris@42
|
105 }
|
Chris@42
|
106
|
Chris@42
|
107 # ifdef _MSC_VER
|
Chris@42
|
108 # pragma warning(default : 4700)
|
Chris@42
|
109 # pragma runtime_checks("u", restore)
|
Chris@42
|
110 # endif
|
Chris@42
|
111
|
Chris@42
|
112 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
|
Chris@42
|
113 {
|
Chris@42
|
114 (void)aligned_like; /* UNUSED */
|
Chris@42
|
115 /* WARNING: the extra_iter hack depends upon STOREL occurring
|
Chris@42
|
116 after STOREH */
|
Chris@42
|
117 STOREH(x + ovs, v);
|
Chris@42
|
118 STOREL(x, v);
|
Chris@42
|
119 }
|
Chris@42
|
120
|
Chris@42
|
121 #else /* ! FFTW_SINGLE */
|
Chris@42
|
122 # define LD LDA
|
Chris@42
|
123 # define ST STA
|
Chris@42
|
124 #endif
|
Chris@42
|
125
|
Chris@42
|
126 #define STM2 DS(STA,ST)
|
Chris@42
|
127 #define STN2(x, v0, v1, ovs) /* nop */
|
Chris@42
|
128
|
Chris@42
|
129 #ifdef FFTW_SINGLE
|
Chris@42
|
130 # define STM4(x, v, ovs, aligned_like) /* no-op */
|
Chris@42
|
131 /* STN4 is a macro, not a function, thanks to Visual C++ developers
|
Chris@42
|
132 deciding "it would be infrequent that people would want to pass more
|
Chris@42
|
133 than 3 [__m128 parameters] by value." 3 parameters ought to be enough
|
Chris@42
|
134 for anybody. */
|
Chris@42
|
135 # define STN4(x, v0, v1, v2, v3, ovs) \
|
Chris@42
|
136 { \
|
Chris@42
|
137 V xxx0, xxx1, xxx2, xxx3; \
|
Chris@42
|
138 xxx0 = UNPCKL(v0, v2); \
|
Chris@42
|
139 xxx1 = UNPCKH(v0, v2); \
|
Chris@42
|
140 xxx2 = UNPCKL(v1, v3); \
|
Chris@42
|
141 xxx3 = UNPCKH(v1, v3); \
|
Chris@42
|
142 STA(x, UNPCKL(xxx0, xxx2), 0, 0); \
|
Chris@42
|
143 STA(x + ovs, UNPCKH(xxx0, xxx2), 0, 0); \
|
Chris@42
|
144 STA(x + 2 * ovs, UNPCKL(xxx1, xxx3), 0, 0); \
|
Chris@42
|
145 STA(x + 3 * ovs, UNPCKH(xxx1, xxx3), 0, 0); \
|
Chris@42
|
146 }
|
Chris@42
|
147 #else /* !FFTW_SINGLE */
|
Chris@42
|
148 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
|
Chris@42
|
149 {
|
Chris@42
|
150 (void)aligned_like; /* UNUSED */
|
Chris@42
|
151 STOREL(x, v);
|
Chris@42
|
152 STOREH(x + ovs, v);
|
Chris@42
|
153 }
|
Chris@42
|
154 # define STN4(x, v0, v1, v2, v3, ovs) /* nothing */
|
Chris@42
|
155 #endif
|
Chris@42
|
156
|
Chris@42
|
157 static inline V FLIP_RI(V x)
|
Chris@42
|
158 {
|
Chris@42
|
159 return VPERM1(x, DS(1, SHUFVALS(1, 0, 3, 2)));
|
Chris@42
|
160 }
|
Chris@42
|
161
|
Chris@42
|
162 static inline V VCONJ(V x)
|
Chris@42
|
163 {
|
Chris@42
|
164 V pmpm = VLIT(-0.0, 0.0);
|
Chris@42
|
165 return VXOR(pmpm, x);
|
Chris@42
|
166 }
|
Chris@42
|
167
|
Chris@42
|
168 static inline V VBYI(V x)
|
Chris@42
|
169 {
|
Chris@42
|
170 x = VCONJ(x);
|
Chris@42
|
171 x = FLIP_RI(x);
|
Chris@42
|
172 return x;
|
Chris@42
|
173 }
|
Chris@42
|
174
|
Chris@42
|
175 /* FMA support */
|
Chris@42
|
176 #define VFMA(a, b, c) SUFF(_mm_fmadd_p)(a,b,c)
|
Chris@42
|
177 #define VFNMS(a, b, c) SUFF(_mm_fnmadd_p)(a,b,c)
|
Chris@42
|
178 #define VFMS(a, b, c) SUFF(_mm_fmsub_p)(a,b,c)
|
Chris@42
|
179 #define VFMAI(b, c) SUFF(_mm_addsub_p)(c,FLIP_RI(b))
|
Chris@42
|
180 #define VFNMSI(b, c) VSUB(c, VBYI(b))
|
Chris@42
|
181 #define VFMACONJ(b,c) VADD(VCONJ(b),c)
|
Chris@42
|
182 #define VFMSCONJ(b,c) VSUB(VCONJ(b),c)
|
Chris@42
|
183 #define VFNMSCONJ(b,c) SUFF(_mm_addsub_p)(c,b)
|
Chris@42
|
184
|
Chris@42
|
185
|
Chris@42
|
186 static inline V VZMUL(V tx, V sr)
|
Chris@42
|
187 {
|
Chris@42
|
188 V tr = VDUPL(tx);
|
Chris@42
|
189 V ti = VDUPH(tx);
|
Chris@42
|
190 ti = VMUL(ti, FLIP_RI(sr));
|
Chris@42
|
191 return SUFF(_mm_fmaddsub_p)(tr,sr,ti);
|
Chris@42
|
192 }
|
Chris@42
|
193
|
Chris@42
|
194 static inline V VZMULJ(V tx, V sr)
|
Chris@42
|
195 {
|
Chris@42
|
196 V tr = VDUPL(tx);
|
Chris@42
|
197 V ti = VDUPH(tx);
|
Chris@42
|
198 ti = VMUL(ti, FLIP_RI(sr));
|
Chris@42
|
199 return SUFF(_mm_fmsubadd_p)(tr,sr,ti);
|
Chris@42
|
200 }
|
Chris@42
|
201
|
Chris@42
|
202 static inline V VZMULI(V tx, V sr)
|
Chris@42
|
203 {
|
Chris@42
|
204 V tr = VDUPL(tx);
|
Chris@42
|
205 V ti = VDUPH(tx);
|
Chris@42
|
206 ti = VMUL(ti, sr);
|
Chris@42
|
207 sr = VBYI(sr);
|
Chris@42
|
208 return VFMS(tr, sr, ti);
|
Chris@42
|
209 }
|
Chris@42
|
210
|
Chris@42
|
211 static inline V VZMULIJ(V tx, V sr)
|
Chris@42
|
212 {
|
Chris@42
|
213 V tr = VDUPL(tx);
|
Chris@42
|
214 V ti = VDUPH(tx);
|
Chris@42
|
215 tr = VMUL(tr, FLIP_RI(sr));
|
Chris@42
|
216 return SUFF(_mm_fmaddsub_p)(ti,sr,tr);
|
Chris@42
|
217 }
|
Chris@42
|
218
|
Chris@42
|
219 /* twiddle storage #1: compact, slower */
|
Chris@42
|
220 #ifdef FFTW_SINGLE
|
Chris@42
|
221 # define VTW1(v,x) \
|
Chris@42
|
222 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
|
Chris@42
|
223 static inline V BYTW1(const R *t, V sr)
|
Chris@42
|
224 {
|
Chris@42
|
225 const V *twp = (const V *)t;
|
Chris@42
|
226 V tx = twp[0];
|
Chris@42
|
227 V tr = UNPCKL(tx, tx);
|
Chris@42
|
228 V ti = UNPCKH(tx, tx);
|
Chris@42
|
229 ti = VMUL(ti, FLIP_RI(sr));
|
Chris@42
|
230 return SUFF(_mm_fmaddsub_p)(tr,sr,ti);
|
Chris@42
|
231 }
|
Chris@42
|
232 static inline V BYTWJ1(const R *t, V sr)
|
Chris@42
|
233 {
|
Chris@42
|
234 const V *twp = (const V *)t;
|
Chris@42
|
235 V tx = twp[0];
|
Chris@42
|
236 V tr = UNPCKL(tx, tx);
|
Chris@42
|
237 V ti = UNPCKH(tx, tx);
|
Chris@42
|
238 ti = VMUL(ti, FLIP_RI(sr));
|
Chris@42
|
239 return SUFF(_mm_fmsubadd_p)(tr,sr,ti);
|
Chris@42
|
240 }
|
Chris@42
|
241 #else /* !FFTW_SINGLE */
|
Chris@42
|
242 # define VTW1(v,x) {TW_CEXP, v, x}
|
Chris@42
|
243 static inline V BYTW1(const R *t, V sr)
|
Chris@42
|
244 {
|
Chris@42
|
245 V tx = LD(t, 1, t);
|
Chris@42
|
246 return VZMUL(tx, sr);
|
Chris@42
|
247 }
|
Chris@42
|
248 static inline V BYTWJ1(const R *t, V sr)
|
Chris@42
|
249 {
|
Chris@42
|
250 V tx = LD(t, 1, t);
|
Chris@42
|
251 return VZMULJ(tx, sr);
|
Chris@42
|
252 }
|
Chris@42
|
253 #endif
|
Chris@42
|
254 #define TWVL1 (VL)
|
Chris@42
|
255
|
Chris@42
|
256 /* twiddle storage #2: twice the space, faster (when in cache) */
|
Chris@42
|
257 #ifdef FFTW_SINGLE
|
Chris@42
|
258 # define VTW2(v,x) \
|
Chris@42
|
259 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
|
Chris@42
|
260 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}
|
Chris@42
|
261 #else /* !FFTW_SINGLE */
|
Chris@42
|
262 # define VTW2(v,x) \
|
Chris@42
|
263 {TW_COS, v, x}, {TW_COS, v, x}, {TW_SIN, v, -x}, {TW_SIN, v, x}
|
Chris@42
|
264 #endif
|
Chris@42
|
265 #define TWVL2 (2 * VL)
|
Chris@42
|
266 static inline V BYTW2(const R *t, V sr)
|
Chris@42
|
267 {
|
Chris@42
|
268 const V *twp = (const V *)t;
|
Chris@42
|
269 V si = FLIP_RI(sr);
|
Chris@42
|
270 V tr = twp[0], ti = twp[1];
|
Chris@42
|
271 return VFMA(tr, sr, VMUL(ti, si));
|
Chris@42
|
272 }
|
Chris@42
|
273 static inline V BYTWJ2(const R *t, V sr)
|
Chris@42
|
274 {
|
Chris@42
|
275 const V *twp = (const V *)t;
|
Chris@42
|
276 V si = FLIP_RI(sr);
|
Chris@42
|
277 V tr = twp[0], ti = twp[1];
|
Chris@42
|
278 return VFNMS(ti, si, VMUL(tr, sr));
|
Chris@42
|
279 }
|
Chris@42
|
280
|
Chris@42
|
281 /* twiddle storage #3 */
|
Chris@42
|
282 #ifdef FFTW_SINGLE
|
Chris@42
|
283 # define VTW3(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
|
Chris@42
|
284 # define TWVL3 (VL)
|
Chris@42
|
285 #else
|
Chris@42
|
286 # define VTW3(v,x) VTW1(v,x)
|
Chris@42
|
287 # define TWVL3 TWVL1
|
Chris@42
|
288 #endif
|
Chris@42
|
289
|
Chris@42
|
290 /* twiddle storage for split arrays */
|
Chris@42
|
291 #ifdef FFTW_SINGLE
|
Chris@42
|
292 # define VTWS(v,x) \
|
Chris@42
|
293 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
|
Chris@42
|
294 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}
|
Chris@42
|
295 #else
|
Chris@42
|
296 # define VTWS(v,x) \
|
Chris@42
|
297 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
|
Chris@42
|
298 #endif
|
Chris@42
|
299 #define TWVLS (2 * VL)
|
Chris@42
|
300
|
Chris@42
|
301 #define VLEAVE() /* nothing */
|
Chris@42
|
302
|
Chris@42
|
303 #include "simd-common.h"
|