annotate src/fftw-3.3.8/simd-support/simd-avx2-128.h @ 167:bd3cc4d1df30

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 19 Nov 2019 14:52:55 +0000
parents
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 * 128-bit AVX2 support by Erik Lindahl, 2015.
cannam@167 6 * Erik Lindahl hereby places his modifications in the public domain.
cannam@167 7 *
cannam@167 8 * This program is free software; you can redistribute it and/or modify
cannam@167 9 * it under the terms of the GNU General Public License as published by
cannam@167 10 * the Free Software Foundation; either version 2 of the License, or
cannam@167 11 * (at your option) any later version.
cannam@167 12 *
cannam@167 13 * This program is distributed in the hope that it will be useful,
cannam@167 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@167 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@167 16 * GNU General Public License for more details.
cannam@167 17 *
cannam@167 18 * You should have received a copy of the GNU General Public License
cannam@167 19 * along with this program; if not, write to the Free Software
cannam@167 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cannam@167 21 *
cannam@167 22 */
cannam@167 23
cannam@167 24 #if defined(FFTW_LDOUBLE) || defined(FFTW_QUAD)
cannam@167 25 #error "AVX2 only works in single or double precision"
cannam@167 26 #endif
cannam@167 27
cannam@167 28 #ifdef FFTW_SINGLE
cannam@167 29 # define DS(d,s) s /* single-precision option */
cannam@167 30 # define SUFF(name) name ## s
cannam@167 31 #else
cannam@167 32 # define DS(d,s) d /* double-precision option */
cannam@167 33 # define SUFF(name) name ## d
cannam@167 34 #endif
cannam@167 35
cannam@167 36 #define SIMD_SUFFIX _avx2_128 /* for renaming */
cannam@167 37 #define VL DS(1,2) /* SIMD vector length, in term of complex numbers */
cannam@167 38 #define SIMD_VSTRIDE_OKA(x) DS(1,((x) == 2))
cannam@167 39 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
cannam@167 40
cannam@167 41 #if defined(__GNUC__) && !defined(__AVX2__) /* sanity check */
cannam@167 42 #error "compiling simd-avx2-128.h without avx2 support"
cannam@167 43 #endif
cannam@167 44
cannam@167 45 #ifdef _MSC_VER
cannam@167 46 #ifndef inline
cannam@167 47 #define inline __inline
cannam@167 48 #endif
cannam@167 49 #endif
cannam@167 50
cannam@167 51 #include <immintrin.h>
cannam@167 52
cannam@167 53 typedef DS(__m128d,__m128) V;
cannam@167 54 #define VADD SUFF(_mm_add_p)
cannam@167 55 #define VSUB SUFF(_mm_sub_p)
cannam@167 56 #define VMUL SUFF(_mm_mul_p)
cannam@167 57 #define VXOR SUFF(_mm_xor_p)
cannam@167 58 #define SHUF SUFF(_mm_shuffle_p)
cannam@167 59 #define VPERM1 SUFF(_mm_permute_p)
cannam@167 60 #define UNPCKL SUFF(_mm_unpacklo_p)
cannam@167 61 #define UNPCKH SUFF(_mm_unpackhi_p)
cannam@167 62
cannam@167 63 #define SHUFVALS(fp0,fp1,fp2,fp3) \
cannam@167 64 (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0)))
cannam@167 65
cannam@167 66 #define VDUPL(x) DS(_mm_permute_pd(x,0), _mm_moveldup_ps(x))
cannam@167 67 #define VDUPH(x) DS(_mm_permute_pd(x,3), _mm_movehdup_ps(x))
cannam@167 68 #define LOADH(addr, val) _mm_loadh_pi(val, (const __m64 *)(addr))
cannam@167 69 #define LOADL(addr, val) _mm_loadl_pi(val, (const __m64 *)(addr))
cannam@167 70 #define STOREH(a, v) DS(_mm_storeh_pd(a, v), _mm_storeh_pi((__m64 *)(a), v))
cannam@167 71 #define STOREL(a, v) DS(_mm_storel_pd(a, v), _mm_storel_pi((__m64 *)(a), v))
cannam@167 72
cannam@167 73 #define VLIT(x0, x1) DS(_mm_set_pd(x0, x1), _mm_set_ps(x0, x1, x0, x1))
cannam@167 74 #define DVK(var, val) V var = VLIT(val, val)
cannam@167 75 #define LDK(x) x
cannam@167 76
cannam@167 77 static inline V LDA(const R *x, INT ivs, const R *aligned_like)
cannam@167 78 {
cannam@167 79 (void)aligned_like; /* UNUSED */
cannam@167 80 (void)ivs; /* UNUSED */
cannam@167 81 return *(const V *)x;
cannam@167 82 }
cannam@167 83
cannam@167 84 static inline void STA(R *x, V v, INT ovs, const R *aligned_like)
cannam@167 85 {
cannam@167 86 (void)aligned_like; /* UNUSED */
cannam@167 87 (void)ovs; /* UNUSED */
cannam@167 88 *(V *)x = v;
cannam@167 89 }
cannam@167 90
cannam@167 91 #ifdef FFTW_SINGLE
cannam@167 92
cannam@167 93 # ifdef _MSC_VER
cannam@167 94 /* Temporarily disable the warning "uninitialized local variable
cannam@167 95 'name' used" and runtime checks for using a variable before it is
cannam@167 96 defined which is erroneously triggered by the LOADL0 / LOADH macros
cannam@167 97 as they only modify VAL partly each. */
cannam@167 98 # ifndef __INTEL_COMPILER
cannam@167 99 # pragma warning(disable : 4700)
cannam@167 100 # pragma runtime_checks("u", off)
cannam@167 101 # endif
cannam@167 102 # endif
cannam@167 103 # ifdef __INTEL_COMPILER
cannam@167 104 # pragma warning(disable : 592)
cannam@167 105 # endif
cannam@167 106
cannam@167 107 static inline V LD(const R *x, INT ivs, const R *aligned_like)
cannam@167 108 {
cannam@167 109 __m128 l0, l1;
cannam@167 110 (void)aligned_like; /* UNUSED */
cannam@167 111 #if defined(__ICC) || (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ > 8)
cannam@167 112 l0 = LOADL(x, SUFF(_mm_undefined_p)());
cannam@167 113 l1 = LOADL(x + ivs, SUFF(_mm_undefined_p)());
cannam@167 114 #else
cannam@167 115 l0 = LOADL(x, l0);
cannam@167 116 l1 = LOADL(x + ivs, l1);
cannam@167 117 #endif
cannam@167 118 return SUFF(_mm_movelh_p)(l0,l1);
cannam@167 119 }
cannam@167 120
cannam@167 121 # ifdef _MSC_VER
cannam@167 122 # ifndef __INTEL_COMPILER
cannam@167 123 # pragma warning(default : 4700)
cannam@167 124 # pragma runtime_checks("u", restore)
cannam@167 125 # endif
cannam@167 126 # endif
cannam@167 127 # ifdef __INTEL_COMPILER
cannam@167 128 # pragma warning(default : 592)
cannam@167 129 # endif
cannam@167 130
cannam@167 131 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
cannam@167 132 {
cannam@167 133 (void)aligned_like; /* UNUSED */
cannam@167 134 /* WARNING: the extra_iter hack depends upon STOREL occurring
cannam@167 135 after STOREH */
cannam@167 136 STOREH(x + ovs, v);
cannam@167 137 STOREL(x, v);
cannam@167 138 }
cannam@167 139
cannam@167 140 #else /* ! FFTW_SINGLE */
cannam@167 141 # define LD LDA
cannam@167 142 # define ST STA
cannam@167 143 #endif
cannam@167 144
cannam@167 145 #define STM2 DS(STA,ST)
cannam@167 146 #define STN2(x, v0, v1, ovs) /* nop */
cannam@167 147
cannam@167 148 #ifdef FFTW_SINGLE
cannam@167 149 # define STM4(x, v, ovs, aligned_like) /* no-op */
cannam@167 150 /* STN4 is a macro, not a function, thanks to Visual C++ developers
cannam@167 151 deciding "it would be infrequent that people would want to pass more
cannam@167 152 than 3 [__m128 parameters] by value." 3 parameters ought to be enough
cannam@167 153 for anybody. */
cannam@167 154 # define STN4(x, v0, v1, v2, v3, ovs) \
cannam@167 155 { \
cannam@167 156 V xxx0, xxx1, xxx2, xxx3; \
cannam@167 157 xxx0 = UNPCKL(v0, v2); \
cannam@167 158 xxx1 = UNPCKH(v0, v2); \
cannam@167 159 xxx2 = UNPCKL(v1, v3); \
cannam@167 160 xxx3 = UNPCKH(v1, v3); \
cannam@167 161 STA(x, UNPCKL(xxx0, xxx2), 0, 0); \
cannam@167 162 STA(x + ovs, UNPCKH(xxx0, xxx2), 0, 0); \
cannam@167 163 STA(x + 2 * ovs, UNPCKL(xxx1, xxx3), 0, 0); \
cannam@167 164 STA(x + 3 * ovs, UNPCKH(xxx1, xxx3), 0, 0); \
cannam@167 165 }
cannam@167 166 #else /* !FFTW_SINGLE */
cannam@167 167 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
cannam@167 168 {
cannam@167 169 (void)aligned_like; /* UNUSED */
cannam@167 170 STOREL(x, v);
cannam@167 171 STOREH(x + ovs, v);
cannam@167 172 }
cannam@167 173 # define STN4(x, v0, v1, v2, v3, ovs) /* nothing */
cannam@167 174 #endif
cannam@167 175
cannam@167 176 static inline V FLIP_RI(V x)
cannam@167 177 {
cannam@167 178 return VPERM1(x, DS(1, SHUFVALS(1, 0, 3, 2)));
cannam@167 179 }
cannam@167 180
cannam@167 181 static inline V VCONJ(V x)
cannam@167 182 {
cannam@167 183 /* Produce a SIMD vector[VL] of (0 + -0i).
cannam@167 184
cannam@167 185 We really want to write this:
cannam@167 186
cannam@167 187 V pmpm = VLIT(-0.0, 0.0);
cannam@167 188
cannam@167 189 but historically some compilers have ignored the distiction
cannam@167 190 between +0 and -0. It looks like 'gcc-8 -fast-math' treats -0
cannam@167 191 as 0 too.
cannam@167 192 */
cannam@167 193 union uvec {
cannam@167 194 unsigned u[4];
cannam@167 195 V v;
cannam@167 196 };
cannam@167 197 static const union uvec pmpm = {
cannam@167 198 #ifdef FFTW_SINGLE
cannam@167 199 { 0x00000000, 0x80000000, 0x00000000, 0x80000000 }
cannam@167 200 #else
cannam@167 201 { 0x00000000, 0x00000000, 0x00000000, 0x80000000 }
cannam@167 202 #endif
cannam@167 203 };
cannam@167 204 return VXOR(pmpm.v, x);
cannam@167 205 }
cannam@167 206
cannam@167 207 static inline V VBYI(V x)
cannam@167 208 {
cannam@167 209 x = VCONJ(x);
cannam@167 210 x = FLIP_RI(x);
cannam@167 211 return x;
cannam@167 212 }
cannam@167 213
cannam@167 214 /* FMA support */
cannam@167 215 #define VFMA(a, b, c) SUFF(_mm_fmadd_p)(a,b,c)
cannam@167 216 #define VFNMS(a, b, c) SUFF(_mm_fnmadd_p)(a,b,c)
cannam@167 217 #define VFMS(a, b, c) SUFF(_mm_fmsub_p)(a,b,c)
cannam@167 218 #define VFMAI(b, c) SUFF(_mm_addsub_p)(c,FLIP_RI(b))
cannam@167 219 #define VFNMSI(b, c) VSUB(c, VBYI(b))
cannam@167 220 #define VFMACONJ(b,c) VADD(VCONJ(b),c)
cannam@167 221 #define VFMSCONJ(b,c) VSUB(VCONJ(b),c)
cannam@167 222 #define VFNMSCONJ(b,c) SUFF(_mm_addsub_p)(c,b)
cannam@167 223
cannam@167 224
cannam@167 225 static inline V VZMUL(V tx, V sr)
cannam@167 226 {
cannam@167 227 V tr = VDUPL(tx);
cannam@167 228 V ti = VDUPH(tx);
cannam@167 229 ti = VMUL(ti, FLIP_RI(sr));
cannam@167 230 return SUFF(_mm_fmaddsub_p)(tr,sr,ti);
cannam@167 231 }
cannam@167 232
cannam@167 233 static inline V VZMULJ(V tx, V sr)
cannam@167 234 {
cannam@167 235 V tr = VDUPL(tx);
cannam@167 236 V ti = VDUPH(tx);
cannam@167 237 ti = VMUL(ti, FLIP_RI(sr));
cannam@167 238 return SUFF(_mm_fmsubadd_p)(tr,sr,ti);
cannam@167 239 }
cannam@167 240
cannam@167 241 static inline V VZMULI(V tx, V sr)
cannam@167 242 {
cannam@167 243 V tr = VDUPL(tx);
cannam@167 244 V ti = VDUPH(tx);
cannam@167 245 ti = VMUL(ti, sr);
cannam@167 246 sr = VBYI(sr);
cannam@167 247 return VFMS(tr, sr, ti);
cannam@167 248 }
cannam@167 249
cannam@167 250 static inline V VZMULIJ(V tx, V sr)
cannam@167 251 {
cannam@167 252 V tr = VDUPL(tx);
cannam@167 253 V ti = VDUPH(tx);
cannam@167 254 tr = VMUL(tr, FLIP_RI(sr));
cannam@167 255 return SUFF(_mm_fmaddsub_p)(ti,sr,tr);
cannam@167 256 }
cannam@167 257
cannam@167 258 /* twiddle storage #1: compact, slower */
cannam@167 259 #ifdef FFTW_SINGLE
cannam@167 260 # define VTW1(v,x) \
cannam@167 261 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
cannam@167 262 static inline V BYTW1(const R *t, V sr)
cannam@167 263 {
cannam@167 264 const V *twp = (const V *)t;
cannam@167 265 V tx = twp[0];
cannam@167 266 V tr = UNPCKL(tx, tx);
cannam@167 267 V ti = UNPCKH(tx, tx);
cannam@167 268 ti = VMUL(ti, FLIP_RI(sr));
cannam@167 269 return SUFF(_mm_fmaddsub_p)(tr,sr,ti);
cannam@167 270 }
cannam@167 271 static inline V BYTWJ1(const R *t, V sr)
cannam@167 272 {
cannam@167 273 const V *twp = (const V *)t;
cannam@167 274 V tx = twp[0];
cannam@167 275 V tr = UNPCKL(tx, tx);
cannam@167 276 V ti = UNPCKH(tx, tx);
cannam@167 277 ti = VMUL(ti, FLIP_RI(sr));
cannam@167 278 return SUFF(_mm_fmsubadd_p)(tr,sr,ti);
cannam@167 279 }
cannam@167 280 #else /* !FFTW_SINGLE */
cannam@167 281 # define VTW1(v,x) {TW_CEXP, v, x}
cannam@167 282 static inline V BYTW1(const R *t, V sr)
cannam@167 283 {
cannam@167 284 V tx = LD(t, 1, t);
cannam@167 285 return VZMUL(tx, sr);
cannam@167 286 }
cannam@167 287 static inline V BYTWJ1(const R *t, V sr)
cannam@167 288 {
cannam@167 289 V tx = LD(t, 1, t);
cannam@167 290 return VZMULJ(tx, sr);
cannam@167 291 }
cannam@167 292 #endif
cannam@167 293 #define TWVL1 (VL)
cannam@167 294
cannam@167 295 /* twiddle storage #2: twice the space, faster (when in cache) */
cannam@167 296 #ifdef FFTW_SINGLE
cannam@167 297 # define VTW2(v,x) \
cannam@167 298 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
cannam@167 299 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}
cannam@167 300 #else /* !FFTW_SINGLE */
cannam@167 301 # define VTW2(v,x) \
cannam@167 302 {TW_COS, v, x}, {TW_COS, v, x}, {TW_SIN, v, -x}, {TW_SIN, v, x}
cannam@167 303 #endif
cannam@167 304 #define TWVL2 (2 * VL)
cannam@167 305 static inline V BYTW2(const R *t, V sr)
cannam@167 306 {
cannam@167 307 const V *twp = (const V *)t;
cannam@167 308 V si = FLIP_RI(sr);
cannam@167 309 V tr = twp[0], ti = twp[1];
cannam@167 310 return VFMA(tr, sr, VMUL(ti, si));
cannam@167 311 }
cannam@167 312 static inline V BYTWJ2(const R *t, V sr)
cannam@167 313 {
cannam@167 314 const V *twp = (const V *)t;
cannam@167 315 V si = FLIP_RI(sr);
cannam@167 316 V tr = twp[0], ti = twp[1];
cannam@167 317 return VFNMS(ti, si, VMUL(tr, sr));
cannam@167 318 }
cannam@167 319
cannam@167 320 /* twiddle storage #3 */
cannam@167 321 #ifdef FFTW_SINGLE
cannam@167 322 # define VTW3(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
cannam@167 323 # define TWVL3 (VL)
cannam@167 324 #else
cannam@167 325 # define VTW3(v,x) VTW1(v,x)
cannam@167 326 # define TWVL3 TWVL1
cannam@167 327 #endif
cannam@167 328
cannam@167 329 /* twiddle storage for split arrays */
cannam@167 330 #ifdef FFTW_SINGLE
cannam@167 331 # define VTWS(v,x) \
cannam@167 332 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
cannam@167 333 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}
cannam@167 334 #else
cannam@167 335 # define VTWS(v,x) \
cannam@167 336 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
cannam@167 337 #endif
cannam@167 338 #define TWVLS (2 * VL)
cannam@167 339
cannam@167 340 #define VLEAVE() /* nothing */
cannam@167 341
cannam@167 342 #include "simd-common.h"