Chris@10
|
1 /*
|
Chris@10
|
2 * Copyright (c) 2003, 2007-11 Matteo Frigo
|
Chris@10
|
3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
|
Chris@10
|
4 *
|
Chris@10
|
5 * This program is free software; you can redistribute it and/or modify
|
Chris@10
|
6 * it under the terms of the GNU General Public License as published by
|
Chris@10
|
7 * the Free Software Foundation; either version 2 of the License, or
|
Chris@10
|
8 * (at your option) any later version.
|
Chris@10
|
9 *
|
Chris@10
|
10 * This program is distributed in the hope that it will be useful,
|
Chris@10
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@10
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@10
|
13 * GNU General Public License for more details.
|
Chris@10
|
14 *
|
Chris@10
|
15 * You should have received a copy of the GNU General Public License
|
Chris@10
|
16 * along with this program; if not, write to the Free Software
|
Chris@10
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Chris@10
|
18 *
|
Chris@10
|
19 */
|
Chris@10
|
20
|
Chris@10
|
21 #if defined(FFTW_LDOUBLE) || defined(FFTW_QUAD)
|
Chris@10
|
22 # error "SSE/SSE2 only works in single/double precision"
|
Chris@10
|
23 #endif
|
Chris@10
|
24
|
Chris@10
|
25 #ifdef FFTW_SINGLE
|
Chris@10
|
26 # define DS(d,s) s /* single-precision option */
|
Chris@10
|
27 # define SUFF(name) name ## s
|
Chris@10
|
28 #else
|
Chris@10
|
29 # define DS(d,s) d /* double-precision option */
|
Chris@10
|
30 # define SUFF(name) name ## d
|
Chris@10
|
31 #endif
|
Chris@10
|
32
|
Chris@10
|
33 #define SIMD_SUFFIX _sse2 /* for renaming */
|
Chris@10
|
34 #define VL DS(1,2) /* SIMD vector length, in term of complex numbers */
|
Chris@10
|
35 #define SIMD_VSTRIDE_OKA(x) DS(1,((x) == 2))
|
Chris@10
|
36 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
|
Chris@10
|
37
|
Chris@10
|
38 #if defined(__GNUC__) && !defined(FFTW_SINGLE) && !defined(__SSE2__)
|
Chris@10
|
39 # error "compiling simd-sse2.h in double precision without -msse2"
|
Chris@10
|
40 #elif defined(__GNUC__) && defined(FFTW_SINGLE) && !defined(__SSE__)
|
Chris@10
|
41 # error "compiling simd-sse2.h in single precision without -msse"
|
Chris@10
|
42 #endif
|
Chris@10
|
43
|
Chris@10
|
44 #ifdef _MSC_VER
|
Chris@10
|
45 #ifndef inline
|
Chris@10
|
46 #define inline __inline
|
Chris@10
|
47 #endif
|
Chris@10
|
48 #endif
|
Chris@10
|
49
|
Chris@10
|
50 /* some versions of glibc's sys/cdefs.h define __inline to be empty,
|
Chris@10
|
51 which is wrong because emmintrin.h defines several inline
|
Chris@10
|
52 procedures */
|
Chris@10
|
53 #ifndef _MSC_VER
|
Chris@10
|
54 #undef __inline
|
Chris@10
|
55 #endif
|
Chris@10
|
56
|
Chris@10
|
57 #ifdef FFTW_SINGLE
|
Chris@10
|
58 # include <xmmintrin.h>
|
Chris@10
|
59 #else
|
Chris@10
|
60 # include <emmintrin.h>
|
Chris@10
|
61 #endif
|
Chris@10
|
62
|
Chris@10
|
63 typedef DS(__m128d,__m128) V;
|
Chris@10
|
64 #define VADD SUFF(_mm_add_p)
|
Chris@10
|
65 #define VSUB SUFF(_mm_sub_p)
|
Chris@10
|
66 #define VMUL SUFF(_mm_mul_p)
|
Chris@10
|
67 #define VXOR SUFF(_mm_xor_p)
|
Chris@10
|
68 #define SHUF SUFF(_mm_shuffle_p)
|
Chris@10
|
69 #define UNPCKL SUFF(_mm_unpacklo_p)
|
Chris@10
|
70 #define UNPCKH SUFF(_mm_unpackhi_p)
|
Chris@10
|
71
|
Chris@10
|
72 #define SHUFVALS(fp0,fp1,fp2,fp3) \
|
Chris@10
|
73 (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
|
Chris@10
|
74
|
Chris@10
|
75 #define VDUPL(x) DS(UNPCKL(x, x), SHUF(x, x, SHUFVALS(0, 0, 2, 2)))
|
Chris@10
|
76 #define VDUPH(x) DS(UNPCKH(x, x), SHUF(x, x, SHUFVALS(1, 1, 3, 3)))
|
Chris@10
|
77 #define STOREH(a, v) DS(_mm_storeh_pd(a, v), _mm_storeh_pi((__m64 *)(a), v))
|
Chris@10
|
78 #define STOREL(a, v) DS(_mm_storel_pd(a, v), _mm_storel_pi((__m64 *)(a), v))
|
Chris@10
|
79
|
Chris@10
|
80
|
Chris@10
|
81 #ifdef __GNUC__
|
Chris@10
|
82 /*
|
Chris@10
|
83 * gcc-3.3 generates slow code for mm_set_ps (write all elements to
|
Chris@10
|
84 * the stack and load __m128 from the stack).
|
Chris@10
|
85 *
|
Chris@10
|
86 * gcc-3.[34] generates slow code for mm_set_ps1 (load into low element
|
Chris@10
|
87 * and shuffle).
|
Chris@10
|
88 *
|
Chris@10
|
89 * This hack forces gcc to generate a constant __m128 at compile time.
|
Chris@10
|
90 */
|
Chris@10
|
91 union rvec {
|
Chris@10
|
92 R r[DS(2,4)];
|
Chris@10
|
93 V v;
|
Chris@10
|
94 };
|
Chris@10
|
95
|
Chris@10
|
96 # ifdef FFTW_SINGLE
|
Chris@10
|
97 # define DVK(var, val) V var = __extension__ ({ \
|
Chris@10
|
98 static const union rvec _var = { {val,val,val,val} }; _var.v; })
|
Chris@10
|
99 # else
|
Chris@10
|
100 # define DVK(var, val) V var = __extension__ ({ \
|
Chris@10
|
101 static const union rvec _var = { {val,val} }; _var.v; })
|
Chris@10
|
102 # endif
|
Chris@10
|
103 # define LDK(x) x
|
Chris@10
|
104 #else
|
Chris@10
|
105 # define DVK(var, val) const R var = K(val)
|
Chris@10
|
106 # define LDK(x) DS(_mm_set1_pd,_mm_set_ps1)(x)
|
Chris@10
|
107 #endif
|
Chris@10
|
108
|
Chris@10
|
109 union uvec {
|
Chris@10
|
110 unsigned u[4];
|
Chris@10
|
111 V v;
|
Chris@10
|
112 };
|
Chris@10
|
113
|
Chris@10
|
114 static inline V LDA(const R *x, INT ivs, const R *aligned_like)
|
Chris@10
|
115 {
|
Chris@10
|
116 (void)aligned_like; /* UNUSED */
|
Chris@10
|
117 (void)ivs; /* UNUSED */
|
Chris@10
|
118 return *(const V *)x;
|
Chris@10
|
119 }
|
Chris@10
|
120
|
Chris@10
|
121 static inline void STA(R *x, V v, INT ovs, const R *aligned_like)
|
Chris@10
|
122 {
|
Chris@10
|
123 (void)aligned_like; /* UNUSED */
|
Chris@10
|
124 (void)ovs; /* UNUSED */
|
Chris@10
|
125 *(V *)x = v;
|
Chris@10
|
126 }
|
Chris@10
|
127
|
Chris@10
|
128 #ifdef FFTW_SINGLE
|
Chris@10
|
129
|
Chris@10
|
130 # ifdef _MSC_VER
|
Chris@10
|
131 /* Temporarily disable the warning "uninitialized local variable
|
Chris@10
|
132 'name' used" and runtime checks for using a variable before it is
|
Chris@10
|
133 defined which is erroneously triggered by the LOADL0 / LOADH macros
|
Chris@10
|
134 as they only modify VAL partly each. */
|
Chris@10
|
135 # pragma warning(disable : 4700)
|
Chris@10
|
136 # pragma runtime_checks("u", off)
|
Chris@10
|
137 # endif
|
Chris@10
|
138
|
Chris@10
|
139 static inline V LD(const R *x, INT ivs, const R *aligned_like)
|
Chris@10
|
140 {
|
Chris@10
|
141 V var;
|
Chris@10
|
142 (void)aligned_like; /* UNUSED */
|
Chris@10
|
143 # ifdef __GNUC__
|
Chris@10
|
144 /* We use inline asm because gcc-3.x generates slow code for
|
Chris@10
|
145 _mm_loadh_pi(). gcc-3.x insists upon having an existing variable for
|
Chris@10
|
146 VAL, which is however never used. Thus, it generates code to move
|
Chris@10
|
147 values in and out the variable. Worse still, gcc-4.0 stores VAL on
|
Chris@10
|
148 the stack, causing valgrind to complain about uninitialized reads. */
|
Chris@10
|
149 __asm__("movlps %1, %0\n\tmovhps %2, %0"
|
Chris@10
|
150 : "=x"(var) : "m"(x[0]), "m"(x[ivs]));
|
Chris@10
|
151 # else
|
Chris@10
|
152 # define LOADH(addr, val) _mm_loadh_pi(val, (const __m64 *)(addr))
|
Chris@10
|
153 # define LOADL0(addr, val) _mm_loadl_pi(val, (const __m64 *)(addr))
|
Chris@10
|
154 var = LOADL0(x, var);
|
Chris@10
|
155 var = LOADH(x + ivs, var);
|
Chris@10
|
156 # endif
|
Chris@10
|
157 return var;
|
Chris@10
|
158 }
|
Chris@10
|
159
|
Chris@10
|
160 # ifdef _MSC_VER
|
Chris@10
|
161 # pragma warning(default : 4700)
|
Chris@10
|
162 # pragma runtime_checks("u", restore)
|
Chris@10
|
163 # endif
|
Chris@10
|
164
|
Chris@10
|
165 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
|
Chris@10
|
166 {
|
Chris@10
|
167 (void)aligned_like; /* UNUSED */
|
Chris@10
|
168 /* WARNING: the extra_iter hack depends upon STOREL occurring
|
Chris@10
|
169 after STOREH */
|
Chris@10
|
170 STOREH(x + ovs, v);
|
Chris@10
|
171 STOREL(x, v);
|
Chris@10
|
172 }
|
Chris@10
|
173
|
Chris@10
|
174 #else /* ! FFTW_SINGLE */
|
Chris@10
|
175 # define LD LDA
|
Chris@10
|
176 # define ST STA
|
Chris@10
|
177 #endif
|
Chris@10
|
178
|
Chris@10
|
179 #define STM2 DS(STA,ST)
|
Chris@10
|
180 #define STN2(x, v0, v1, ovs) /* nop */
|
Chris@10
|
181
|
Chris@10
|
182 #ifdef FFTW_SINGLE
|
Chris@10
|
183 # define STM4(x, v, ovs, aligned_like) /* no-op */
|
Chris@10
|
184 /* STN4 is a macro, not a function, thanks to Visual C++ developers
|
Chris@10
|
185 deciding "it would be infrequent that people would want to pass more
|
Chris@10
|
186 than 3 [__m128 parameters] by value." 3 parameters ought to be enough
|
Chris@10
|
187 for anybody. */
|
Chris@10
|
188 # define STN4(x, v0, v1, v2, v3, ovs) \
|
Chris@10
|
189 { \
|
Chris@10
|
190 V xxx0, xxx1, xxx2, xxx3; \
|
Chris@10
|
191 xxx0 = UNPCKL(v0, v2); \
|
Chris@10
|
192 xxx1 = UNPCKH(v0, v2); \
|
Chris@10
|
193 xxx2 = UNPCKL(v1, v3); \
|
Chris@10
|
194 xxx3 = UNPCKH(v1, v3); \
|
Chris@10
|
195 STA(x, UNPCKL(xxx0, xxx2), 0, 0); \
|
Chris@10
|
196 STA(x + ovs, UNPCKH(xxx0, xxx2), 0, 0); \
|
Chris@10
|
197 STA(x + 2 * ovs, UNPCKL(xxx1, xxx3), 0, 0); \
|
Chris@10
|
198 STA(x + 3 * ovs, UNPCKH(xxx1, xxx3), 0, 0); \
|
Chris@10
|
199 }
|
Chris@10
|
200 #else /* !FFTW_SINGLE */
|
Chris@10
|
201 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
|
Chris@10
|
202 {
|
Chris@10
|
203 (void)aligned_like; /* UNUSED */
|
Chris@10
|
204 STOREL(x, v);
|
Chris@10
|
205 STOREH(x + ovs, v);
|
Chris@10
|
206 }
|
Chris@10
|
207 # define STN4(x, v0, v1, v2, v3, ovs) /* nothing */
|
Chris@10
|
208 #endif
|
Chris@10
|
209
|
Chris@10
|
210 static inline V FLIP_RI(V x)
|
Chris@10
|
211 {
|
Chris@10
|
212 return SHUF(x, x, DS(1, SHUFVALS(1, 0, 3, 2)));
|
Chris@10
|
213 }
|
Chris@10
|
214
|
Chris@10
|
215 extern const union uvec X(sse2_pm);
|
Chris@10
|
216 static inline V VCONJ(V x)
|
Chris@10
|
217 {
|
Chris@10
|
218 return VXOR(X(sse2_pm).v, x);
|
Chris@10
|
219 }
|
Chris@10
|
220
|
Chris@10
|
221 static inline V VBYI(V x)
|
Chris@10
|
222 {
|
Chris@10
|
223 x = VCONJ(x);
|
Chris@10
|
224 x = FLIP_RI(x);
|
Chris@10
|
225 return x;
|
Chris@10
|
226 }
|
Chris@10
|
227
|
Chris@10
|
228 /* FMA support */
|
Chris@10
|
229 #define VFMA(a, b, c) VADD(c, VMUL(a, b))
|
Chris@10
|
230 #define VFNMS(a, b, c) VSUB(c, VMUL(a, b))
|
Chris@10
|
231 #define VFMS(a, b, c) VSUB(VMUL(a, b), c)
|
Chris@10
|
232 #define VFMAI(b, c) VADD(c, VBYI(b))
|
Chris@10
|
233 #define VFNMSI(b, c) VSUB(c, VBYI(b))
|
Chris@10
|
234 #define VFMACONJ(b,c) VADD(VCONJ(b),c)
|
Chris@10
|
235 #define VFMSCONJ(b,c) VSUB(VCONJ(b),c)
|
Chris@10
|
236 #define VFNMSCONJ(b,c) VSUB(c, VCONJ(b))
|
Chris@10
|
237
|
Chris@10
|
238 static inline V VZMUL(V tx, V sr)
|
Chris@10
|
239 {
|
Chris@10
|
240 V tr = VDUPL(tx);
|
Chris@10
|
241 V ti = VDUPH(tx);
|
Chris@10
|
242 tr = VMUL(sr, tr);
|
Chris@10
|
243 sr = VBYI(sr);
|
Chris@10
|
244 return VFMA(ti, sr, tr);
|
Chris@10
|
245 }
|
Chris@10
|
246
|
Chris@10
|
247 static inline V VZMULJ(V tx, V sr)
|
Chris@10
|
248 {
|
Chris@10
|
249 V tr = VDUPL(tx);
|
Chris@10
|
250 V ti = VDUPH(tx);
|
Chris@10
|
251 tr = VMUL(sr, tr);
|
Chris@10
|
252 sr = VBYI(sr);
|
Chris@10
|
253 return VFNMS(ti, sr, tr);
|
Chris@10
|
254 }
|
Chris@10
|
255
|
Chris@10
|
256 static inline V VZMULI(V tx, V sr)
|
Chris@10
|
257 {
|
Chris@10
|
258 V tr = VDUPL(tx);
|
Chris@10
|
259 V ti = VDUPH(tx);
|
Chris@10
|
260 ti = VMUL(ti, sr);
|
Chris@10
|
261 sr = VBYI(sr);
|
Chris@10
|
262 return VFMS(tr, sr, ti);
|
Chris@10
|
263 }
|
Chris@10
|
264
|
Chris@10
|
265 static inline V VZMULIJ(V tx, V sr)
|
Chris@10
|
266 {
|
Chris@10
|
267 V tr = VDUPL(tx);
|
Chris@10
|
268 V ti = VDUPH(tx);
|
Chris@10
|
269 ti = VMUL(ti, sr);
|
Chris@10
|
270 sr = VBYI(sr);
|
Chris@10
|
271 return VFMA(tr, sr, ti);
|
Chris@10
|
272 }
|
Chris@10
|
273
|
Chris@10
|
274 /* twiddle storage #1: compact, slower */
|
Chris@10
|
275 #ifdef FFTW_SINGLE
|
Chris@10
|
276 # define VTW1(v,x) \
|
Chris@10
|
277 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
|
Chris@10
|
278 static inline V BYTW1(const R *t, V sr)
|
Chris@10
|
279 {
|
Chris@10
|
280 const V *twp = (const V *)t;
|
Chris@10
|
281 V tx = twp[0];
|
Chris@10
|
282 V tr = UNPCKL(tx, tx);
|
Chris@10
|
283 V ti = UNPCKH(tx, tx);
|
Chris@10
|
284 tr = VMUL(tr, sr);
|
Chris@10
|
285 sr = VBYI(sr);
|
Chris@10
|
286 return VFMA(ti, sr, tr);
|
Chris@10
|
287 }
|
Chris@10
|
288 static inline V BYTWJ1(const R *t, V sr)
|
Chris@10
|
289 {
|
Chris@10
|
290 const V *twp = (const V *)t;
|
Chris@10
|
291 V tx = twp[0];
|
Chris@10
|
292 V tr = UNPCKL(tx, tx);
|
Chris@10
|
293 V ti = UNPCKH(tx, tx);
|
Chris@10
|
294 tr = VMUL(tr, sr);
|
Chris@10
|
295 sr = VBYI(sr);
|
Chris@10
|
296 return VFNMS(ti, sr, tr);
|
Chris@10
|
297 }
|
Chris@10
|
298 #else /* !FFTW_SINGLE */
|
Chris@10
|
299 # define VTW1(v,x) {TW_CEXP, v, x}
|
Chris@10
|
300 static inline V BYTW1(const R *t, V sr)
|
Chris@10
|
301 {
|
Chris@10
|
302 V tx = LD(t, 1, t);
|
Chris@10
|
303 return VZMUL(tx, sr);
|
Chris@10
|
304 }
|
Chris@10
|
305 static inline V BYTWJ1(const R *t, V sr)
|
Chris@10
|
306 {
|
Chris@10
|
307 V tx = LD(t, 1, t);
|
Chris@10
|
308 return VZMULJ(tx, sr);
|
Chris@10
|
309 }
|
Chris@10
|
310 #endif
|
Chris@10
|
311 #define TWVL1 (VL)
|
Chris@10
|
312
|
Chris@10
|
313 /* twiddle storage #2: twice the space, faster (when in cache) */
|
Chris@10
|
314 #ifdef FFTW_SINGLE
|
Chris@10
|
315 # define VTW2(v,x) \
|
Chris@10
|
316 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
|
Chris@10
|
317 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}
|
Chris@10
|
318 #else /* !FFTW_SINGLE */
|
Chris@10
|
319 # define VTW2(v,x) \
|
Chris@10
|
320 {TW_COS, v, x}, {TW_COS, v, x}, {TW_SIN, v, -x}, {TW_SIN, v, x}
|
Chris@10
|
321 #endif
|
Chris@10
|
322 #define TWVL2 (2 * VL)
|
Chris@10
|
323 static inline V BYTW2(const R *t, V sr)
|
Chris@10
|
324 {
|
Chris@10
|
325 const V *twp = (const V *)t;
|
Chris@10
|
326 V si = FLIP_RI(sr);
|
Chris@10
|
327 V tr = twp[0], ti = twp[1];
|
Chris@10
|
328 return VFMA(tr, sr, VMUL(ti, si));
|
Chris@10
|
329 }
|
Chris@10
|
330 static inline V BYTWJ2(const R *t, V sr)
|
Chris@10
|
331 {
|
Chris@10
|
332 const V *twp = (const V *)t;
|
Chris@10
|
333 V si = FLIP_RI(sr);
|
Chris@10
|
334 V tr = twp[0], ti = twp[1];
|
Chris@10
|
335 return VFNMS(ti, si, VMUL(tr, sr));
|
Chris@10
|
336 }
|
Chris@10
|
337
|
Chris@10
|
338 /* twiddle storage #3 */
|
Chris@10
|
339 #ifdef FFTW_SINGLE
|
Chris@10
|
340 # define VTW3(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
|
Chris@10
|
341 # define TWVL3 (VL)
|
Chris@10
|
342 #else
|
Chris@10
|
343 # define VTW3(v,x) VTW1(v,x)
|
Chris@10
|
344 # define TWVL3 TWVL1
|
Chris@10
|
345 #endif
|
Chris@10
|
346
|
Chris@10
|
347 /* twiddle storage for split arrays */
|
Chris@10
|
348 #ifdef FFTW_SINGLE
|
Chris@10
|
349 # define VTWS(v,x) \
|
Chris@10
|
350 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
|
Chris@10
|
351 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}
|
Chris@10
|
352 #else
|
Chris@10
|
353 # define VTWS(v,x) \
|
Chris@10
|
354 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
|
Chris@10
|
355 #endif
|
Chris@10
|
356 #define TWVLS (2 * VL)
|
Chris@10
|
357
|
Chris@10
|
358 #define VLEAVE() /* nothing */
|
Chris@10
|
359
|
Chris@10
|
360 #include "simd-common.h"
|