annotate fft/fftw/fftw-3.3.4/simd-support/simd-sse2.h @ 40:223f770b5341 kissfft-double tip

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