annotate src/fftw-3.3.5/simd-support/simd-neon.h @ 56:af97cad61ff0

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