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