annotate src/fftw-3.3.5/simd-support/simd-avx2.h @ 168:ceec0dd9ec9c

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