comparison src/fftw-3.3.3/simd-support/simd-sse2.h @ 10:37bf6b4a2645

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