annotate fft/fftw/fftw-3.3.4/simd-support/simd-avx.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 "AVX only works in single or 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 _avx /* for renaming */
Chris@19 34 #define VL DS(2, 4) /* SIMD complex vector length */
Chris@19 35 #define SIMD_VSTRIDE_OKA(x) ((x) == 2)
Chris@19 36 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
Chris@19 37
Chris@19 38 #if defined(__GNUC__) && !defined(__AVX__) /* sanity check */
Chris@19 39 #error "compiling simd-avx.h without -mavx"
Chris@19 40 #endif
Chris@19 41
Chris@19 42 #ifdef _MSC_VER
Chris@19 43 #ifndef inline
Chris@19 44 #define inline __inline
Chris@19 45 #endif
Chris@19 46 #endif
Chris@19 47
Chris@19 48 #include <immintrin.h>
Chris@19 49
Chris@19 50 typedef DS(__m256d, __m256) V;
Chris@19 51 #define VADD SUFF(_mm256_add_p)
Chris@19 52 #define VSUB SUFF(_mm256_sub_p)
Chris@19 53 #define VMUL SUFF(_mm256_mul_p)
Chris@19 54 #define VXOR SUFF(_mm256_xor_p)
Chris@19 55 #define VSHUF SUFF(_mm256_shuffle_p)
Chris@19 56
Chris@19 57 #define SHUFVALD(fp0,fp1) \
Chris@19 58 (((fp1) << 3) | ((fp0) << 2) | ((fp1) << 1) | ((fp0)))
Chris@19 59 #define SHUFVALS(fp0,fp1,fp2,fp3) \
Chris@19 60 (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
Chris@19 61
Chris@19 62 #define VDUPL(x) DS(_mm256_unpacklo_pd(x, x), VSHUF(x, x, SHUFVALS(0, 0, 2, 2)))
Chris@19 63 #define VDUPH(x) DS(_mm256_unpackhi_pd(x, x), VSHUF(x, x, SHUFVALS(1, 1, 3, 3)))
Chris@19 64
Chris@19 65 #define VLIT(x0, x1) DS(_mm256_set_pd(x0, x1, x0, x1), _mm256_set_ps(x0, x1, x0, x1, x0, x1, x0, x1))
Chris@19 66 #define DVK(var, val) V var = VLIT(val, val)
Chris@19 67 #define LDK(x) x
Chris@19 68
Chris@19 69 static inline V LDA(const R *x, INT ivs, const R *aligned_like)
Chris@19 70 {
Chris@19 71 (void)aligned_like; /* UNUSED */
Chris@19 72 (void)ivs; /* UNUSED */
Chris@19 73 return SUFF(_mm256_loadu_p)(x);
Chris@19 74 }
Chris@19 75
Chris@19 76 static inline void STA(R *x, V v, INT ovs, const R *aligned_like)
Chris@19 77 {
Chris@19 78 (void)aligned_like; /* UNUSED */
Chris@19 79 (void)ovs; /* UNUSED */
Chris@19 80 SUFF(_mm256_storeu_p)(x, v);
Chris@19 81 }
Chris@19 82
Chris@19 83 #if FFTW_SINGLE
Chris@19 84
Chris@19 85 #define LOADH(addr, val) _mm_loadh_pi(val, (const __m64 *)(addr))
Chris@19 86 #define LOADL(addr, val) _mm_loadl_pi(val, (const __m64 *)(addr))
Chris@19 87 #define STOREH(addr, val) _mm_storeh_pi((__m64 *)(addr), val)
Chris@19 88 #define STOREL(addr, val) _mm_storel_pi((__m64 *)(addr), val)
Chris@19 89
Chris@19 90 /* it seems like the only AVX way to store 4 complex floats is to
Chris@19 91 extract two pairs of complex floats into two __m128 registers, and
Chris@19 92 then use SSE-like half-stores. Similarly, to load 4 complex
Chris@19 93 floats, we load two pairs of complex floats into two __m128
Chris@19 94 registers, and then pack the two __m128 registers into one __m256
Chris@19 95 value. */
Chris@19 96 static inline V LD(const R *x, INT ivs, const R *aligned_like)
Chris@19 97 {
Chris@19 98 __m128 l, h;
Chris@19 99 V v;
Chris@19 100 (void)aligned_like; /* UNUSED */
Chris@19 101 l = LOADL(x, l);
Chris@19 102 l = LOADH(x + ivs, l);
Chris@19 103 h = LOADL(x + 2*ivs, h);
Chris@19 104 h = LOADH(x + 3*ivs, h);
Chris@19 105 v = _mm256_castps128_ps256(l);
Chris@19 106 v = _mm256_insertf128_ps(v, h, 1);
Chris@19 107 return v;
Chris@19 108 }
Chris@19 109
Chris@19 110 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
Chris@19 111 {
Chris@19 112 __m128 h = _mm256_extractf128_ps(v, 1);
Chris@19 113 __m128 l = _mm256_castps256_ps128(v);
Chris@19 114 (void)aligned_like; /* UNUSED */
Chris@19 115 /* WARNING: the extra_iter hack depends upon STOREL occurring
Chris@19 116 after STOREH */
Chris@19 117 STOREH(x + 3*ovs, h);
Chris@19 118 STOREL(x + 2*ovs, h);
Chris@19 119 STOREH(x + ovs, l);
Chris@19 120 STOREL(x, l);
Chris@19 121 }
Chris@19 122
Chris@19 123 #define STM2(x, v, ovs, aligned_like) /* no-op */
Chris@19 124 static inline void STN2(R *x, V v0, V v1, INT ovs)
Chris@19 125 {
Chris@19 126 V x0 = VSHUF(v0, v1, SHUFVALS(0, 1, 0, 1));
Chris@19 127 V x1 = VSHUF(v0, v1, SHUFVALS(2, 3, 2, 3));
Chris@19 128 __m128 h0 = _mm256_extractf128_ps(x0, 1);
Chris@19 129 __m128 l0 = _mm256_castps256_ps128(x0);
Chris@19 130 __m128 h1 = _mm256_extractf128_ps(x1, 1);
Chris@19 131 __m128 l1 = _mm256_castps256_ps128(x1);
Chris@19 132 *(__m128 *)(x + 3*ovs) = h1;
Chris@19 133 *(__m128 *)(x + 2*ovs) = h0;
Chris@19 134 *(__m128 *)(x + 1*ovs) = l1;
Chris@19 135 *(__m128 *)(x + 0*ovs) = l0;
Chris@19 136 }
Chris@19 137
Chris@19 138 #define STM4(x, v, ovs, aligned_like) /* no-op */
Chris@19 139 #define STN4(x, v0, v1, v2, v3, ovs) \
Chris@19 140 { \
Chris@19 141 V xxx0, xxx1, xxx2, xxx3; \
Chris@19 142 V yyy0, yyy1, yyy2, yyy3; \
Chris@19 143 xxx0 = _mm256_unpacklo_ps(v0, v2); \
Chris@19 144 xxx1 = _mm256_unpackhi_ps(v0, v2); \
Chris@19 145 xxx2 = _mm256_unpacklo_ps(v1, v3); \
Chris@19 146 xxx3 = _mm256_unpackhi_ps(v1, v3); \
Chris@19 147 yyy0 = _mm256_unpacklo_ps(xxx0, xxx2); \
Chris@19 148 yyy1 = _mm256_unpackhi_ps(xxx0, xxx2); \
Chris@19 149 yyy2 = _mm256_unpacklo_ps(xxx1, xxx3); \
Chris@19 150 yyy3 = _mm256_unpackhi_ps(xxx1, xxx3); \
Chris@19 151 *(__m128 *)(x + 0 * ovs) = _mm256_castps256_ps128(yyy0); \
Chris@19 152 *(__m128 *)(x + 4 * ovs) = _mm256_extractf128_ps(yyy0, 1); \
Chris@19 153 *(__m128 *)(x + 1 * ovs) = _mm256_castps256_ps128(yyy1); \
Chris@19 154 *(__m128 *)(x + 5 * ovs) = _mm256_extractf128_ps(yyy1, 1); \
Chris@19 155 *(__m128 *)(x + 2 * ovs) = _mm256_castps256_ps128(yyy2); \
Chris@19 156 *(__m128 *)(x + 6 * ovs) = _mm256_extractf128_ps(yyy2, 1); \
Chris@19 157 *(__m128 *)(x + 3 * ovs) = _mm256_castps256_ps128(yyy3); \
Chris@19 158 *(__m128 *)(x + 7 * ovs) = _mm256_extractf128_ps(yyy3, 1); \
Chris@19 159 }
Chris@19 160
Chris@19 161 #else
Chris@19 162 static inline __m128d VMOVAPD_LD(const R *x)
Chris@19 163 {
Chris@19 164 /* gcc-4.6 miscompiles the combination _mm256_castpd128_pd256(VMOVAPD_LD(x))
Chris@19 165 into a 256-bit vmovapd, which requires 32-byte aligment instead of
Chris@19 166 16-byte alignment.
Chris@19 167
Chris@19 168 Force the use of vmovapd via asm until compilers stabilize.
Chris@19 169 */
Chris@19 170 #if defined(__GNUC__)
Chris@19 171 __m128d var;
Chris@19 172 __asm__("vmovapd %1, %0\n" : "=x"(var) : "m"(x[0]));
Chris@19 173 return var;
Chris@19 174 #else
Chris@19 175 return *(const __m128d *)x;
Chris@19 176 #endif
Chris@19 177 }
Chris@19 178
Chris@19 179 static inline V LD(const R *x, INT ivs, const R *aligned_like)
Chris@19 180 {
Chris@19 181 V var;
Chris@19 182 (void)aligned_like; /* UNUSED */
Chris@19 183 var = _mm256_castpd128_pd256(VMOVAPD_LD(x));
Chris@19 184 var = _mm256_insertf128_pd(var, *(const __m128d *)(x+ivs), 1);
Chris@19 185 return var;
Chris@19 186 }
Chris@19 187
Chris@19 188 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
Chris@19 189 {
Chris@19 190 (void)aligned_like; /* UNUSED */
Chris@19 191 /* WARNING: the extra_iter hack depends upon the store of the low
Chris@19 192 part occurring after the store of the high part */
Chris@19 193 *(__m128d *)(x + ovs) = _mm256_extractf128_pd(v, 1);
Chris@19 194 *(__m128d *)x = _mm256_castpd256_pd128(v);
Chris@19 195 }
Chris@19 196
Chris@19 197
Chris@19 198 #define STM2 ST
Chris@19 199 #define STN2(x, v0, v1, ovs) /* nop */
Chris@19 200 #define STM4(x, v, ovs, aligned_like) /* no-op */
Chris@19 201
Chris@19 202 /* STN4 is a macro, not a function, thanks to Visual C++ developers
Chris@19 203 deciding "it would be infrequent that people would want to pass more
Chris@19 204 than 3 [__m128 parameters] by value." Even though the comment
Chris@19 205 was made about __m128 parameters, it appears to apply to __m256
Chris@19 206 parameters as well. */
Chris@19 207 #define STN4(x, v0, v1, v2, v3, ovs) \
Chris@19 208 { \
Chris@19 209 V xxx0, xxx1, xxx2, xxx3; \
Chris@19 210 xxx0 = _mm256_unpacklo_pd(v0, v1); \
Chris@19 211 xxx1 = _mm256_unpackhi_pd(v0, v1); \
Chris@19 212 xxx2 = _mm256_unpacklo_pd(v2, v3); \
Chris@19 213 xxx3 = _mm256_unpackhi_pd(v2, v3); \
Chris@19 214 STA(x, _mm256_permute2f128_pd(xxx0, xxx2, 0x20), 0, 0); \
Chris@19 215 STA(x + ovs, _mm256_permute2f128_pd(xxx1, xxx3, 0x20), 0, 0); \
Chris@19 216 STA(x + 2 * ovs, _mm256_permute2f128_pd(xxx0, xxx2, 0x31), 0, 0); \
Chris@19 217 STA(x + 3 * ovs, _mm256_permute2f128_pd(xxx1, xxx3, 0x31), 0, 0); \
Chris@19 218 }
Chris@19 219 #endif
Chris@19 220
Chris@19 221 static inline V FLIP_RI(V x)
Chris@19 222 {
Chris@19 223 return VSHUF(x, x,
Chris@19 224 DS(SHUFVALD(1, 0),
Chris@19 225 SHUFVALS(1, 0, 3, 2)));
Chris@19 226 }
Chris@19 227
Chris@19 228 static inline V VCONJ(V x)
Chris@19 229 {
Chris@19 230 V pmpm = VLIT(-0.0, 0.0);
Chris@19 231 return VXOR(pmpm, x);
Chris@19 232 }
Chris@19 233
Chris@19 234 static inline V VBYI(V x)
Chris@19 235 {
Chris@19 236 return FLIP_RI(VCONJ(x));
Chris@19 237 }
Chris@19 238
Chris@19 239 /* FMA support */
Chris@19 240 #define VFMA(a, b, c) VADD(c, VMUL(a, b))
Chris@19 241 #define VFNMS(a, b, c) VSUB(c, VMUL(a, b))
Chris@19 242 #define VFMS(a, b, c) VSUB(VMUL(a, b), c)
Chris@19 243 #define VFMAI(b, c) VADD(c, VBYI(b))
Chris@19 244 #define VFNMSI(b, c) VSUB(c, VBYI(b))
Chris@19 245 #define VFMACONJ(b,c) VADD(VCONJ(b),c)
Chris@19 246 #define VFMSCONJ(b,c) VSUB(VCONJ(b),c)
Chris@19 247 #define VFNMSCONJ(b,c) VSUB(c, VCONJ(b))
Chris@19 248
Chris@19 249 static inline V VZMUL(V tx, V sr)
Chris@19 250 {
Chris@19 251 V tr = VDUPL(tx);
Chris@19 252 V ti = VDUPH(tx);
Chris@19 253 tr = VMUL(sr, tr);
Chris@19 254 sr = VBYI(sr);
Chris@19 255 return VFMA(ti, sr, tr);
Chris@19 256 }
Chris@19 257
Chris@19 258 static inline V VZMULJ(V tx, V sr)
Chris@19 259 {
Chris@19 260 V tr = VDUPL(tx);
Chris@19 261 V ti = VDUPH(tx);
Chris@19 262 tr = VMUL(sr, tr);
Chris@19 263 sr = VBYI(sr);
Chris@19 264 return VFNMS(ti, sr, tr);
Chris@19 265 }
Chris@19 266
Chris@19 267 static inline V VZMULI(V tx, V sr)
Chris@19 268 {
Chris@19 269 V tr = VDUPL(tx);
Chris@19 270 V ti = VDUPH(tx);
Chris@19 271 ti = VMUL(ti, sr);
Chris@19 272 sr = VBYI(sr);
Chris@19 273 return VFMS(tr, sr, ti);
Chris@19 274 }
Chris@19 275
Chris@19 276 static inline V VZMULIJ(V tx, V sr)
Chris@19 277 {
Chris@19 278 V tr = VDUPL(tx);
Chris@19 279 V ti = VDUPH(tx);
Chris@19 280 ti = VMUL(ti, sr);
Chris@19 281 sr = VBYI(sr);
Chris@19 282 return VFMA(tr, sr, ti);
Chris@19 283 }
Chris@19 284
Chris@19 285 /* twiddle storage #1: compact, slower */
Chris@19 286 #ifdef FFTW_SINGLE
Chris@19 287 # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}, {TW_CEXP, v+2, x}, {TW_CEXP, v+3, x}
Chris@19 288 #else
Chris@19 289 # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
Chris@19 290 #endif
Chris@19 291 #define TWVL1 (VL)
Chris@19 292
Chris@19 293 static inline V BYTW1(const R *t, V sr)
Chris@19 294 {
Chris@19 295 return VZMUL(LDA(t, 2, t), sr);
Chris@19 296 }
Chris@19 297
Chris@19 298 static inline V BYTWJ1(const R *t, V sr)
Chris@19 299 {
Chris@19 300 return VZMULJ(LDA(t, 2, t), sr);
Chris@19 301 }
Chris@19 302
Chris@19 303 /* twiddle storage #2: twice the space, faster (when in cache) */
Chris@19 304 #ifdef FFTW_SINGLE
Chris@19 305 # define VTW2(v,x) \
Chris@19 306 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
Chris@19 307 {TW_COS, v+2, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, {TW_COS, v+3, x}, \
Chris@19 308 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}, \
Chris@19 309 {TW_SIN, v+2, -x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, -x}, {TW_SIN, v+3, x}
Chris@19 310 #else
Chris@19 311 # define VTW2(v,x) \
Chris@19 312 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
Chris@19 313 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}
Chris@19 314 #endif
Chris@19 315 #define TWVL2 (2 * VL)
Chris@19 316
Chris@19 317 static inline V BYTW2(const R *t, V sr)
Chris@19 318 {
Chris@19 319 const V *twp = (const V *)t;
Chris@19 320 V si = FLIP_RI(sr);
Chris@19 321 V tr = twp[0], ti = twp[1];
Chris@19 322 return VFMA(tr, sr, VMUL(ti, si));
Chris@19 323 }
Chris@19 324
Chris@19 325 static inline V BYTWJ2(const R *t, V sr)
Chris@19 326 {
Chris@19 327 const V *twp = (const V *)t;
Chris@19 328 V si = FLIP_RI(sr);
Chris@19 329 V tr = twp[0], ti = twp[1];
Chris@19 330 return VFNMS(ti, si, VMUL(tr, sr));
Chris@19 331 }
Chris@19 332
Chris@19 333 /* twiddle storage #3 */
Chris@19 334 #define VTW3 VTW1
Chris@19 335 #define TWVL3 TWVL1
Chris@19 336
Chris@19 337 /* twiddle storage for split arrays */
Chris@19 338 #ifdef FFTW_SINGLE
Chris@19 339 # define VTWS(v,x) \
Chris@19 340 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
Chris@19 341 {TW_COS, v+4, x}, {TW_COS, v+5, x}, {TW_COS, v+6, x}, {TW_COS, v+7, x}, \
Chris@19 342 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}, \
Chris@19 343 {TW_SIN, v+4, x}, {TW_SIN, v+5, x}, {TW_SIN, v+6, x}, {TW_SIN, v+7, x}
Chris@19 344 #else
Chris@19 345 # define VTWS(v,x) \
Chris@19 346 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
Chris@19 347 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}
Chris@19 348 #endif
Chris@19 349 #define TWVLS (2 * VL)
Chris@19 350
Chris@19 351
Chris@19 352 /* Use VZEROUPPER to avoid the penalty of switching from AVX to SSE.
Chris@19 353 See Intel Optimization Manual (April 2011, version 248966), Section
Chris@19 354 11.3 */
Chris@19 355 #define VLEAVE _mm256_zeroupper
Chris@19 356
Chris@19 357 #include "simd-common.h"