annotate src/fftw-3.3.5/simd-support/simd-avx.h @ 148:b4bfdf10c4b3

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