Chris@10: /* Chris@10: * Copyright (c) 2003, 2007-11 Matteo Frigo Chris@10: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology Chris@10: * Chris@10: * This program is free software; you can redistribute it and/or modify Chris@10: * it under the terms of the GNU General Public License as published by Chris@10: * the Free Software Foundation; either version 2 of the License, or Chris@10: * (at your option) any later version. Chris@10: * Chris@10: * This program is distributed in the hope that it will be useful, Chris@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@10: * GNU General Public License for more details. Chris@10: * Chris@10: * You should have received a copy of the GNU General Public License Chris@10: * along with this program; if not, write to the Free Software Chris@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@10: * Chris@10: */ Chris@10: Chris@10: #if defined(FFTW_LDOUBLE) || defined(FFTW_QUAD) Chris@10: #error "AVX only works in single or double precision" Chris@10: #endif Chris@10: Chris@10: #ifdef FFTW_SINGLE Chris@10: # define DS(d,s) s /* single-precision option */ Chris@10: # define SUFF(name) name ## s Chris@10: #else Chris@10: # define DS(d,s) d /* double-precision option */ Chris@10: # define SUFF(name) name ## d Chris@10: #endif Chris@10: Chris@10: #define SIMD_SUFFIX _avx /* for renaming */ Chris@10: #define VL DS(2, 4) /* SIMD complex vector length */ Chris@10: #define SIMD_VSTRIDE_OKA(x) ((x) == 2) Chris@10: #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK Chris@10: Chris@10: #if defined(__GNUC__) && !defined(__AVX__) /* sanity check */ Chris@10: #error "compiling simd-avx.h without -mavx" Chris@10: #endif Chris@10: Chris@10: #ifdef _MSC_VER Chris@10: #ifndef inline Chris@10: #define inline __inline Chris@10: #endif Chris@10: #endif Chris@10: Chris@10: #include Chris@10: Chris@10: typedef DS(__m256d, __m256) V; Chris@10: #define VADD SUFF(_mm256_add_p) Chris@10: #define VSUB SUFF(_mm256_sub_p) Chris@10: #define VMUL SUFF(_mm256_mul_p) Chris@10: #define VXOR SUFF(_mm256_xor_p) Chris@10: #define VSHUF SUFF(_mm256_shuffle_p) Chris@10: Chris@10: #define SHUFVALD(fp0,fp1) \ Chris@10: (((fp1) << 3) | ((fp0) << 2) | ((fp1) << 1) | ((fp0))) Chris@10: #define SHUFVALS(fp0,fp1,fp2,fp3) \ Chris@10: (((fp3) << 6) | ((fp2) << 4) | ((fp1) << 2) | ((fp0))) Chris@10: Chris@10: #define VDUPL(x) DS(_mm256_unpacklo_pd(x, x), VSHUF(x, x, SHUFVALS(0, 0, 2, 2))) Chris@10: #define VDUPH(x) DS(_mm256_unpackhi_pd(x, x), VSHUF(x, x, SHUFVALS(1, 1, 3, 3))) Chris@10: Chris@10: #define VLIT(x0, x1) DS(_mm256_set_pd(x0, x1, x0, x1), _mm256_set_ps(x0, x1, x0, x1, x0, x1, x0, x1)) Chris@10: #define DVK(var, val) V var = VLIT(val, val) Chris@10: #define LDK(x) x Chris@10: Chris@10: static inline V LDA(const R *x, INT ivs, const R *aligned_like) Chris@10: { Chris@10: (void)aligned_like; /* UNUSED */ Chris@10: (void)ivs; /* UNUSED */ Chris@10: return SUFF(_mm256_loadu_p)(x); Chris@10: } Chris@10: Chris@10: static inline void STA(R *x, V v, INT ovs, const R *aligned_like) Chris@10: { Chris@10: (void)aligned_like; /* UNUSED */ Chris@10: (void)ovs; /* UNUSED */ Chris@10: SUFF(_mm256_storeu_p)(x, v); Chris@10: } Chris@10: Chris@10: #if FFTW_SINGLE Chris@10: Chris@10: #define LOADH(addr, val) _mm_loadh_pi(val, (const __m64 *)(addr)) Chris@10: #define LOADL(addr, val) _mm_loadl_pi(val, (const __m64 *)(addr)) Chris@10: #define STOREH(addr, val) _mm_storeh_pi((__m64 *)(addr), val) Chris@10: #define STOREL(addr, val) _mm_storel_pi((__m64 *)(addr), val) Chris@10: Chris@10: /* it seems like the only AVX way to store 4 complex floats is to Chris@10: extract two pairs of complex floats into two __m128 registers, and Chris@10: then use SSE-like half-stores. Similarly, to load 4 complex Chris@10: floats, we load two pairs of complex floats into two __m128 Chris@10: registers, and then pack the two __m128 registers into one __m256 Chris@10: value. */ Chris@10: static inline V LD(const R *x, INT ivs, const R *aligned_like) Chris@10: { Chris@10: __m128 l, h; Chris@10: V v; Chris@10: (void)aligned_like; /* UNUSED */ Chris@10: l = LOADL(x, l); Chris@10: l = LOADH(x + ivs, l); Chris@10: h = LOADL(x + 2*ivs, h); Chris@10: h = LOADH(x + 3*ivs, h); Chris@10: v = _mm256_castps128_ps256(l); Chris@10: v = _mm256_insertf128_ps(v, h, 1); Chris@10: return v; Chris@10: } Chris@10: Chris@10: static inline void ST(R *x, V v, INT ovs, const R *aligned_like) Chris@10: { Chris@10: __m128 h = _mm256_extractf128_ps(v, 1); Chris@10: __m128 l = _mm256_castps256_ps128(v); Chris@10: (void)aligned_like; /* UNUSED */ Chris@10: /* WARNING: the extra_iter hack depends upon STOREL occurring Chris@10: after STOREH */ Chris@10: STOREH(x + 3*ovs, h); Chris@10: STOREL(x + 2*ovs, h); Chris@10: STOREH(x + ovs, l); Chris@10: STOREL(x, l); Chris@10: } Chris@10: Chris@10: #define STM2(x, v, ovs, aligned_like) /* no-op */ Chris@10: static inline void STN2(R *x, V v0, V v1, INT ovs) Chris@10: { Chris@10: V x0 = VSHUF(v0, v1, SHUFVALS(0, 1, 0, 1)); Chris@10: V x1 = VSHUF(v0, v1, SHUFVALS(2, 3, 2, 3)); Chris@10: __m128 h0 = _mm256_extractf128_ps(x0, 1); Chris@10: __m128 l0 = _mm256_castps256_ps128(x0); Chris@10: __m128 h1 = _mm256_extractf128_ps(x1, 1); Chris@10: __m128 l1 = _mm256_castps256_ps128(x1); Chris@10: *(__m128 *)(x + 3*ovs) = h1; Chris@10: *(__m128 *)(x + 2*ovs) = h0; Chris@10: *(__m128 *)(x + 1*ovs) = l1; Chris@10: *(__m128 *)(x + 0*ovs) = l0; Chris@10: } Chris@10: Chris@10: #define STM4(x, v, ovs, aligned_like) /* no-op */ Chris@10: #define STN4(x, v0, v1, v2, v3, ovs) \ Chris@10: { \ Chris@10: V xxx0, xxx1, xxx2, xxx3; \ Chris@10: V yyy0, yyy1, yyy2, yyy3; \ Chris@10: xxx0 = _mm256_unpacklo_ps(v0, v2); \ Chris@10: xxx1 = _mm256_unpackhi_ps(v0, v2); \ Chris@10: xxx2 = _mm256_unpacklo_ps(v1, v3); \ Chris@10: xxx3 = _mm256_unpackhi_ps(v1, v3); \ Chris@10: yyy0 = _mm256_unpacklo_ps(xxx0, xxx2); \ Chris@10: yyy1 = _mm256_unpackhi_ps(xxx0, xxx2); \ Chris@10: yyy2 = _mm256_unpacklo_ps(xxx1, xxx3); \ Chris@10: yyy3 = _mm256_unpackhi_ps(xxx1, xxx3); \ Chris@10: *(__m128 *)(x + 0 * ovs) = _mm256_castps256_ps128(yyy0); \ Chris@10: *(__m128 *)(x + 4 * ovs) = _mm256_extractf128_ps(yyy0, 1); \ Chris@10: *(__m128 *)(x + 1 * ovs) = _mm256_castps256_ps128(yyy1); \ Chris@10: *(__m128 *)(x + 5 * ovs) = _mm256_extractf128_ps(yyy1, 1); \ Chris@10: *(__m128 *)(x + 2 * ovs) = _mm256_castps256_ps128(yyy2); \ Chris@10: *(__m128 *)(x + 6 * ovs) = _mm256_extractf128_ps(yyy2, 1); \ Chris@10: *(__m128 *)(x + 3 * ovs) = _mm256_castps256_ps128(yyy3); \ Chris@10: *(__m128 *)(x + 7 * ovs) = _mm256_extractf128_ps(yyy3, 1); \ Chris@10: } Chris@10: Chris@10: #else Chris@10: static inline __m128d VMOVAPD_LD(const R *x) Chris@10: { Chris@10: /* gcc-4.6 miscompiles the combination _mm256_castpd128_pd256(VMOVAPD_LD(x)) Chris@10: into a 256-bit vmovapd, which requires 32-byte aligment instead of Chris@10: 16-byte alignment. Chris@10: Chris@10: Force the use of vmovapd via asm until compilers stabilize. Chris@10: */ Chris@10: #if defined(__GNUC__) Chris@10: __m128d var; Chris@10: __asm__("vmovapd %1, %0\n" : "=x"(var) : "m"(x[0])); Chris@10: return var; Chris@10: #else Chris@10: return *(const __m128d *)x; Chris@10: #endif Chris@10: } Chris@10: Chris@10: static inline V LD(const R *x, INT ivs, const R *aligned_like) Chris@10: { Chris@10: V var; Chris@10: (void)aligned_like; /* UNUSED */ Chris@10: var = _mm256_castpd128_pd256(VMOVAPD_LD(x)); Chris@10: var = _mm256_insertf128_pd(var, *(const __m128d *)(x+ivs), 1); Chris@10: return var; Chris@10: } Chris@10: Chris@10: static inline void ST(R *x, V v, INT ovs, const R *aligned_like) Chris@10: { Chris@10: (void)aligned_like; /* UNUSED */ Chris@10: /* WARNING: the extra_iter hack depends upon the store of the low Chris@10: part occurring after the store of the high part */ Chris@10: *(__m128d *)(x + ovs) = _mm256_extractf128_pd(v, 1); Chris@10: *(__m128d *)x = _mm256_castpd256_pd128(v); Chris@10: } Chris@10: Chris@10: Chris@10: #define STM2 ST Chris@10: #define STN2(x, v0, v1, ovs) /* nop */ Chris@10: #define STM4(x, v, ovs, aligned_like) /* no-op */ Chris@10: Chris@10: /* STN4 is a macro, not a function, thanks to Visual C++ developers Chris@10: deciding "it would be infrequent that people would want to pass more Chris@10: than 3 [__m128 parameters] by value." Even though the comment Chris@10: was made about __m128 parameters, it appears to apply to __m256 Chris@10: parameters as well. */ Chris@10: #define STN4(x, v0, v1, v2, v3, ovs) \ Chris@10: { \ Chris@10: V xxx0, xxx1, xxx2, xxx3; \ Chris@10: xxx0 = _mm256_unpacklo_pd(v0, v1); \ Chris@10: xxx1 = _mm256_unpackhi_pd(v0, v1); \ Chris@10: xxx2 = _mm256_unpacklo_pd(v2, v3); \ Chris@10: xxx3 = _mm256_unpackhi_pd(v2, v3); \ Chris@10: STA(x, _mm256_permute2f128_pd(xxx0, xxx2, 0x20), 0, 0); \ Chris@10: STA(x + ovs, _mm256_permute2f128_pd(xxx1, xxx3, 0x20), 0, 0); \ Chris@10: STA(x + 2 * ovs, _mm256_permute2f128_pd(xxx0, xxx2, 0x31), 0, 0); \ Chris@10: STA(x + 3 * ovs, _mm256_permute2f128_pd(xxx1, xxx3, 0x31), 0, 0); \ Chris@10: } Chris@10: #endif Chris@10: Chris@10: static inline V FLIP_RI(V x) Chris@10: { Chris@10: return VSHUF(x, x, Chris@10: DS(SHUFVALD(1, 0), Chris@10: SHUFVALS(1, 0, 3, 2))); Chris@10: } Chris@10: Chris@10: static inline V VCONJ(V x) Chris@10: { Chris@10: V pmpm = VLIT(-0.0, 0.0); Chris@10: return VXOR(pmpm, x); Chris@10: } Chris@10: Chris@10: static inline V VBYI(V x) Chris@10: { Chris@10: return FLIP_RI(VCONJ(x)); Chris@10: } Chris@10: Chris@10: /* FMA support */ Chris@10: #define VFMA(a, b, c) VADD(c, VMUL(a, b)) Chris@10: #define VFNMS(a, b, c) VSUB(c, VMUL(a, b)) Chris@10: #define VFMS(a, b, c) VSUB(VMUL(a, b), c) Chris@10: #define VFMAI(b, c) VADD(c, VBYI(b)) Chris@10: #define VFNMSI(b, c) VSUB(c, VBYI(b)) Chris@10: #define VFMACONJ(b,c) VADD(VCONJ(b),c) Chris@10: #define VFMSCONJ(b,c) VSUB(VCONJ(b),c) Chris@10: #define VFNMSCONJ(b,c) VSUB(c, VCONJ(b)) Chris@10: Chris@10: static inline V VZMUL(V tx, V sr) Chris@10: { Chris@10: V tr = VDUPL(tx); Chris@10: V ti = VDUPH(tx); Chris@10: tr = VMUL(sr, tr); Chris@10: sr = VBYI(sr); Chris@10: return VFMA(ti, sr, tr); Chris@10: } Chris@10: Chris@10: static inline V VZMULJ(V tx, V sr) Chris@10: { Chris@10: V tr = VDUPL(tx); Chris@10: V ti = VDUPH(tx); Chris@10: tr = VMUL(sr, tr); Chris@10: sr = VBYI(sr); Chris@10: return VFNMS(ti, sr, tr); Chris@10: } Chris@10: Chris@10: static inline V VZMULI(V tx, V sr) Chris@10: { Chris@10: V tr = VDUPL(tx); Chris@10: V ti = VDUPH(tx); Chris@10: ti = VMUL(ti, sr); Chris@10: sr = VBYI(sr); Chris@10: return VFMS(tr, sr, ti); Chris@10: } Chris@10: Chris@10: static inline V VZMULIJ(V tx, V sr) Chris@10: { Chris@10: V tr = VDUPL(tx); Chris@10: V ti = VDUPH(tx); Chris@10: ti = VMUL(ti, sr); Chris@10: sr = VBYI(sr); Chris@10: return VFMA(tr, sr, ti); Chris@10: } Chris@10: Chris@10: /* twiddle storage #1: compact, slower */ Chris@10: #ifdef FFTW_SINGLE Chris@10: # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}, {TW_CEXP, v+2, x}, {TW_CEXP, v+3, x} Chris@10: #else Chris@10: # define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x} Chris@10: #endif Chris@10: #define TWVL1 (VL) Chris@10: Chris@10: static inline V BYTW1(const R *t, V sr) Chris@10: { Chris@10: return VZMUL(LDA(t, 2, t), sr); Chris@10: } Chris@10: Chris@10: static inline V BYTWJ1(const R *t, V sr) Chris@10: { Chris@10: return VZMULJ(LDA(t, 2, t), sr); Chris@10: } Chris@10: Chris@10: /* twiddle storage #2: twice the space, faster (when in cache) */ Chris@10: #ifdef FFTW_SINGLE Chris@10: # define VTW2(v,x) \ Chris@10: {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \ Chris@10: {TW_COS, v+2, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, {TW_COS, v+3, x}, \ Chris@10: {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}, \ Chris@10: {TW_SIN, v+2, -x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, -x}, {TW_SIN, v+3, x} Chris@10: #else Chris@10: # define VTW2(v,x) \ Chris@10: {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \ Chris@10: {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x} Chris@10: #endif Chris@10: #define TWVL2 (2 * VL) Chris@10: Chris@10: static inline V BYTW2(const R *t, V sr) Chris@10: { Chris@10: const V *twp = (const V *)t; Chris@10: V si = FLIP_RI(sr); Chris@10: V tr = twp[0], ti = twp[1]; Chris@10: return VFMA(tr, sr, VMUL(ti, si)); Chris@10: } Chris@10: Chris@10: static inline V BYTWJ2(const R *t, V sr) Chris@10: { Chris@10: const V *twp = (const V *)t; Chris@10: V si = FLIP_RI(sr); Chris@10: V tr = twp[0], ti = twp[1]; Chris@10: return VFNMS(ti, si, VMUL(tr, sr)); Chris@10: } Chris@10: Chris@10: /* twiddle storage #3 */ Chris@10: #define VTW3 VTW1 Chris@10: #define TWVL3 TWVL1 Chris@10: Chris@10: /* twiddle storage for split arrays */ Chris@10: #ifdef FFTW_SINGLE Chris@10: # define VTWS(v,x) \ Chris@10: {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \ Chris@10: {TW_COS, v+4, x}, {TW_COS, v+5, x}, {TW_COS, v+6, x}, {TW_COS, v+7, x}, \ Chris@10: {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}, \ Chris@10: {TW_SIN, v+4, x}, {TW_SIN, v+5, x}, {TW_SIN, v+6, x}, {TW_SIN, v+7, x} Chris@10: #else Chris@10: # define VTWS(v,x) \ Chris@10: {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \ Chris@10: {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x} Chris@10: #endif Chris@10: #define TWVLS (2 * VL) Chris@10: Chris@10: Chris@10: /* Use VZEROUPPER to avoid the penalty of switching from AVX to SSE. Chris@10: See Intel Optimization Manual (April 2011, version 248966), Section Chris@10: 11.3 */ Chris@10: #define VLEAVE _mm256_zeroupper Chris@10: Chris@10: #include "simd-common.h"