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