annotate src/fftw-3.3.8/simd-support/simd-neon.h @ 82:d0c2a83c1364

Add FFTW 3.3.8 source, and a Linux build
author Chris Cannam
date Tue, 19 Nov 2019 14:52:55 +0000
parents
children
rev   line source
Chris@82 1 /*
Chris@82 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@82 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@82 4 *
Chris@82 5 * Double-precision support added by Romain Dolbeau.
Chris@82 6 * Romain Dolbeau hereby places his modifications in the public domain.
Chris@82 7 *
Chris@82 8 * This program is free software; you can redistribute it and/or modify
Chris@82 9 * it under the terms of the GNU General Public License as published by
Chris@82 10 * the Free Software Foundation; either version 2 of the License, or
Chris@82 11 * (at your option) any later version.
Chris@82 12 *
Chris@82 13 * This program is distributed in the hope that it will be useful,
Chris@82 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@82 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@82 16 * GNU General Public License for more details.
Chris@82 17 *
Chris@82 18 * You should have received a copy of the GNU General Public License
Chris@82 19 * along with this program; if not, write to the Free Software
Chris@82 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@82 21 *
Chris@82 22 */
Chris@82 23
Chris@82 24 #if !defined(FFTW_SINGLE) && !defined( __aarch64__)
Chris@82 25 #error "NEON only works in single precision on 32 bits ARM"
Chris@82 26 #endif
Chris@82 27 #if defined(FFTW_LDOUBLE) || defined(FFTW_QUAD)
Chris@82 28 #error "NEON only works in single or double precision"
Chris@82 29 #endif
Chris@82 30
Chris@82 31 #ifdef FFTW_SINGLE
Chris@82 32 # define DS(d,s) s /* single-precision option */
Chris@82 33 # define SUFF(name) name ## _f32
Chris@82 34 #else
Chris@82 35 # define DS(d,s) d /* double-precision option */
Chris@82 36 # define SUFF(name) name ## _f64
Chris@82 37 #endif
Chris@82 38
Chris@82 39 /* define these unconditionally, because they are used by
Chris@82 40 taint.c which is compiled without neon */
Chris@82 41 #define SIMD_SUFFIX _neon /* for renaming */
Chris@82 42 #define VL DS(1,2) /* SIMD complex vector length */
Chris@82 43 #define SIMD_VSTRIDE_OKA(x) DS(1,((x) == 2))
Chris@82 44 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
Chris@82 45
Chris@82 46 #if defined(__GNUC__) && !defined(__ARM_NEON__) && !defined(__ARM_NEON)
Chris@82 47 #error "compiling simd-neon.h requires -mfpu=neon or equivalent"
Chris@82 48 #endif
Chris@82 49
Chris@82 50 #include <arm_neon.h>
Chris@82 51
Chris@82 52 /* FIXME: I am not sure whether this code assumes little-endian
Chris@82 53 ordering. VLIT may or may not be wrong for big-endian systems. */
Chris@82 54 typedef DS(float64x2_t, float32x4_t) V;
Chris@82 55
Chris@82 56 #ifdef FFTW_SINGLE
Chris@82 57 # define VLIT(x0, x1) {x0, x1, x0, x1}
Chris@82 58 #else
Chris@82 59 # define VLIT(x0, x1) {x0, x1}
Chris@82 60 #endif
Chris@82 61 #define LDK(x) x
Chris@82 62 #define DVK(var, val) const V var = VLIT(val, val)
Chris@82 63
Chris@82 64 /* NEON has FMA, but a three-operand FMA is not too useful
Chris@82 65 for FFT purposes. We normally compute
Chris@82 66
Chris@82 67 t0=a+b*c
Chris@82 68 t1=a-b*c
Chris@82 69
Chris@82 70 In a three-operand instruction set this translates into
Chris@82 71
Chris@82 72 t0=a
Chris@82 73 t0+=b*c
Chris@82 74 t1=a
Chris@82 75 t1-=b*c
Chris@82 76
Chris@82 77 At least one move must be implemented, negating the advantage of
Chris@82 78 the FMA in the first place. At least some versions of gcc generate
Chris@82 79 both moves. So we are better off generating t=b*c;t0=a+t;t1=a-t;*/
Chris@82 80 #if ARCH_PREFERS_FMA
Chris@82 81 #warning "--enable-fma on NEON is probably a bad idea (see source code)"
Chris@82 82 #endif
Chris@82 83
Chris@82 84 #define VADD(a, b) SUFF(vaddq)(a, b)
Chris@82 85 #define VSUB(a, b) SUFF(vsubq)(a, b)
Chris@82 86 #define VMUL(a, b) SUFF(vmulq)(a, b)
Chris@82 87 #define VFMA(a, b, c) SUFF(vmlaq)(c, a, b) /* a*b+c */
Chris@82 88 #define VFNMS(a, b, c) SUFF(vmlsq)(c, a, b) /* FNMS=-(a*b-c) in powerpc terminology; MLS=c-a*b
Chris@82 89 in ARM terminology */
Chris@82 90 #define VFMS(a, b, c) VSUB(VMUL(a, b), c) /* FMS=a*b-c in powerpc terminology; no equivalent
Chris@82 91 arm instruction (?) */
Chris@82 92
Chris@82 93 #define STOREH(a, v) SUFF(vst1)((a), SUFF(vget_high)(v))
Chris@82 94 #define STOREL(a, v) SUFF(vst1)((a), SUFF(vget_low)(v))
Chris@82 95
Chris@82 96 static inline V LDA(const R *x, INT ivs, const R *aligned_like)
Chris@82 97 {
Chris@82 98 (void) aligned_like; /* UNUSED */
Chris@82 99 return SUFF(vld1q)(x);
Chris@82 100 }
Chris@82 101 static inline void STA(R *x, V v, INT ovs, const R *aligned_like)
Chris@82 102 {
Chris@82 103 (void) aligned_like; /* UNUSED */
Chris@82 104 SUFF(vst1q)(x, v);
Chris@82 105 }
Chris@82 106
Chris@82 107
Chris@82 108 #ifdef FFTW_SINGLE
Chris@82 109 static inline V LD(const R *x, INT ivs, const R *aligned_like)
Chris@82 110 {
Chris@82 111 (void) aligned_like; /* UNUSED */
Chris@82 112 return SUFF(vcombine)(SUFF(vld1)(x), SUFF(vld1)((x + ivs)));
Chris@82 113 }
Chris@82 114 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
Chris@82 115 {
Chris@82 116 (void) aligned_like; /* UNUSED */
Chris@82 117 /* WARNING: the extra_iter hack depends upon store-low occurring
Chris@82 118 after store-high */
Chris@82 119 STOREH(x + ovs, v);
Chris@82 120 STOREL(x,v);
Chris@82 121 }
Chris@82 122 #else /* !FFTW_SINGLE */
Chris@82 123 # define LD LDA
Chris@82 124 # define ST STA
Chris@82 125 #endif
Chris@82 126
Chris@82 127 /* 2x2 complex transpose and store */
Chris@82 128 #define STM2 DS(STA,ST)
Chris@82 129 #define STN2(x, v0, v1, ovs) /* nop */
Chris@82 130
Chris@82 131 #ifdef FFTW_SINGLE
Chris@82 132 /* store and 4x4 real transpose */
Chris@82 133 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
Chris@82 134 {
Chris@82 135 (void) aligned_like; /* UNUSED */
Chris@82 136 SUFF(vst1_lane)((x) , SUFF(vget_low)(v), 0);
Chris@82 137 SUFF(vst1_lane)((x + ovs), SUFF(vget_low)(v), 1);
Chris@82 138 SUFF(vst1_lane)((x + 2 * ovs), SUFF(vget_high)(v), 0);
Chris@82 139 SUFF(vst1_lane)((x + 3 * ovs), SUFF(vget_high)(v), 1);
Chris@82 140 }
Chris@82 141 #define STN4(x, v0, v1, v2, v3, ovs) /* use STM4 */
Chris@82 142 #else /* !FFTW_SINGLE */
Chris@82 143 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
Chris@82 144 {
Chris@82 145 (void)aligned_like; /* UNUSED */
Chris@82 146 STOREL(x, v);
Chris@82 147 STOREH(x + ovs, v);
Chris@82 148 }
Chris@82 149 # define STN4(x, v0, v1, v2, v3, ovs) /* nothing */
Chris@82 150 #endif
Chris@82 151
Chris@82 152 #ifdef FFTW_SINGLE
Chris@82 153 #define FLIP_RI(x) SUFF(vrev64q)(x)
Chris@82 154 #else
Chris@82 155 /* FIXME */
Chris@82 156 #define FLIP_RI(x) SUFF(vcombine)(SUFF(vget_high)(x), SUFF(vget_low)(x))
Chris@82 157 #endif
Chris@82 158
Chris@82 159 static inline V VCONJ(V x)
Chris@82 160 {
Chris@82 161 #ifdef FFTW_SINGLE
Chris@82 162 static const uint32x4_t pm = {0, 0x80000000u, 0, 0x80000000u};
Chris@82 163 return vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(x), pm));
Chris@82 164 #else
Chris@82 165 static const uint64x2_t pm = {0, 0x8000000000000000ull};
Chris@82 166 /* Gcc-4.9.2 still does not include vreinterpretq_f64_u64, but simple
Chris@82 167 * casts generate the correct assembly.
Chris@82 168 */
Chris@82 169 return (float64x2_t)(veorq_u64((uint64x2_t)(x), (uint64x2_t)(pm)));
Chris@82 170 #endif
Chris@82 171 }
Chris@82 172
Chris@82 173 static inline V VBYI(V x)
Chris@82 174 {
Chris@82 175 return FLIP_RI(VCONJ(x));
Chris@82 176 }
Chris@82 177
Chris@82 178 static inline V VFMAI(V b, V c)
Chris@82 179 {
Chris@82 180 const V mp = VLIT(-1.0, 1.0);
Chris@82 181 return VFMA(FLIP_RI(b), mp, c);
Chris@82 182 }
Chris@82 183
Chris@82 184 static inline V VFNMSI(V b, V c)
Chris@82 185 {
Chris@82 186 const V mp = VLIT(-1.0, 1.0);
Chris@82 187 return VFNMS(FLIP_RI(b), mp, c);
Chris@82 188 }
Chris@82 189
Chris@82 190 static inline V VFMACONJ(V b, V c)
Chris@82 191 {
Chris@82 192 const V pm = VLIT(1.0, -1.0);
Chris@82 193 return VFMA(b, pm, c);
Chris@82 194 }
Chris@82 195
Chris@82 196 static inline V VFNMSCONJ(V b, V c)
Chris@82 197 {
Chris@82 198 const V pm = VLIT(1.0, -1.0);
Chris@82 199 return VFNMS(b, pm, c);
Chris@82 200 }
Chris@82 201
Chris@82 202 static inline V VFMSCONJ(V b, V c)
Chris@82 203 {
Chris@82 204 return VSUB(VCONJ(b), c);
Chris@82 205 }
Chris@82 206
Chris@82 207 #ifdef FFTW_SINGLE
Chris@82 208 #if 1
Chris@82 209 #define VEXTRACT_REIM(tr, ti, tx) \
Chris@82 210 { \
Chris@82 211 tr = SUFF(vcombine)(SUFF(vdup_lane)(SUFF(vget_low)(tx), 0), \
Chris@82 212 SUFF(vdup_lane)(SUFF(vget_high)(tx), 0)); \
Chris@82 213 ti = SUFF(vcombine)(SUFF(vdup_lane)(SUFF(vget_low)(tx), 1), \
Chris@82 214 SUFF(vdup_lane)(SUFF(vget_high)(tx), 1)); \
Chris@82 215 }
Chris@82 216 #else
Chris@82 217 /* this alternative might be faster in an ideal world, but gcc likes
Chris@82 218 to spill VVV onto the stack */
Chris@82 219 #define VEXTRACT_REIM(tr, ti, tx) \
Chris@82 220 { \
Chris@82 221 float32x4x2_t vvv = SUFF(vtrnq)(tx, tx); \
Chris@82 222 tr = vvv.val[0]; \
Chris@82 223 ti = vvv.val[1]; \
Chris@82 224 }
Chris@82 225 #endif
Chris@82 226 #else
Chris@82 227 #define VEXTRACT_REIM(tr, ti, tx) \
Chris@82 228 { \
Chris@82 229 tr = SUFF(vtrn1q)(tx, tx); \
Chris@82 230 ti = SUFF(vtrn2q)(tx, tx); \
Chris@82 231 }
Chris@82 232 #endif
Chris@82 233
Chris@82 234 static inline V VZMUL(V tx, V sr)
Chris@82 235 {
Chris@82 236 V tr, ti;
Chris@82 237 VEXTRACT_REIM(tr, ti, tx);
Chris@82 238 tr = VMUL(sr, tr);
Chris@82 239 sr = VBYI(sr);
Chris@82 240 return VFMA(ti, sr, tr);
Chris@82 241 }
Chris@82 242
Chris@82 243 static inline V VZMULJ(V tx, V sr)
Chris@82 244 {
Chris@82 245 V tr, ti;
Chris@82 246 VEXTRACT_REIM(tr, ti, tx);
Chris@82 247 tr = VMUL(sr, tr);
Chris@82 248 sr = VBYI(sr);
Chris@82 249 return VFNMS(ti, sr, tr);
Chris@82 250 }
Chris@82 251
Chris@82 252 static inline V VZMULI(V tx, V sr)
Chris@82 253 {
Chris@82 254 V tr, ti;
Chris@82 255 VEXTRACT_REIM(tr, ti, tx);
Chris@82 256 ti = VMUL(ti, sr);
Chris@82 257 sr = VBYI(sr);
Chris@82 258 return VFMS(tr, sr, ti);
Chris@82 259 }
Chris@82 260
Chris@82 261 static inline V VZMULIJ(V tx, V sr)
Chris@82 262 {
Chris@82 263 V tr, ti;
Chris@82 264 VEXTRACT_REIM(tr, ti, tx);
Chris@82 265 ti = VMUL(ti, sr);
Chris@82 266 sr = VBYI(sr);
Chris@82 267 return VFMA(tr, sr, ti);
Chris@82 268 }
Chris@82 269
Chris@82 270 /* twiddle storage #1: compact, slower */
Chris@82 271 #ifdef FFTW_SINGLE
Chris@82 272 #define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
Chris@82 273 #else
Chris@82 274 #define VTW1(v,x) {TW_CEXP, v, x}
Chris@82 275 #endif
Chris@82 276 #define TWVL1 VL
Chris@82 277 static inline V BYTW1(const R *t, V sr)
Chris@82 278 {
Chris@82 279 V tx = LDA(t, 2, 0);
Chris@82 280 return VZMUL(tx, sr);
Chris@82 281 }
Chris@82 282
Chris@82 283 static inline V BYTWJ1(const R *t, V sr)
Chris@82 284 {
Chris@82 285 V tx = LDA(t, 2, 0);
Chris@82 286 return VZMULJ(tx, sr);
Chris@82 287 }
Chris@82 288
Chris@82 289 /* twiddle storage #2: twice the space, faster (when in cache) */
Chris@82 290 #ifdef FFTW_SINGLE
Chris@82 291 # define VTW2(v,x) \
Chris@82 292 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
Chris@82 293 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}
Chris@82 294 #else
Chris@82 295 # define VTW2(v,x) \
Chris@82 296 {TW_COS, v, x}, {TW_COS, v, x}, {TW_SIN, v, -x}, {TW_SIN, v, x}
Chris@82 297 #endif
Chris@82 298 #define TWVL2 (2 * VL)
Chris@82 299
Chris@82 300 static inline V BYTW2(const R *t, V sr)
Chris@82 301 {
Chris@82 302 V si = FLIP_RI(sr);
Chris@82 303 V tr = LDA(t, 2, 0), ti = LDA(t+2*VL, 2, 0);
Chris@82 304 return VFMA(ti, si, VMUL(tr, sr));
Chris@82 305 }
Chris@82 306
Chris@82 307 static inline V BYTWJ2(const R *t, V sr)
Chris@82 308 {
Chris@82 309 V si = FLIP_RI(sr);
Chris@82 310 V tr = LDA(t, 2, 0), ti = LDA(t+2*VL, 2, 0);
Chris@82 311 return VFNMS(ti, si, VMUL(tr, sr));
Chris@82 312 }
Chris@82 313
Chris@82 314 /* twiddle storage #3 */
Chris@82 315 #ifdef FFTW_SINGLE
Chris@82 316 # define VTW3(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
Chris@82 317 #else
Chris@82 318 # define VTW3(v,x) {TW_CEXP, v, x}
Chris@82 319 #endif
Chris@82 320 # define TWVL3 (VL)
Chris@82 321
Chris@82 322 /* twiddle storage for split arrays */
Chris@82 323 #ifdef FFTW_SINGLE
Chris@82 324 # define VTWS(v,x) \
Chris@82 325 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
Chris@82 326 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}
Chris@82 327 #else
Chris@82 328 # define VTWS(v,x) \
Chris@82 329 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_SIN, v, x}, {TW_SIN, v+1, x}
Chris@82 330 #endif
Chris@82 331 #define TWVLS (2 * VL)
Chris@82 332
Chris@82 333 #define VLEAVE() /* nothing */
Chris@82 334
Chris@82 335 #include "simd-common.h"