annotate src/fftw-3.3.5/simd-support/simd-avx.h @ 84:08ae793730bd

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