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