annotate fft/fftw/fftw-3.3.4/simd-support/simd-neon.h @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 /*
Chris@19 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 4 *
Chris@19 5 * This program is free software; you can redistribute it and/or modify
Chris@19 6 * it under the terms of the GNU General Public License as published by
Chris@19 7 * the Free Software Foundation; either version 2 of the License, or
Chris@19 8 * (at your option) any later version.
Chris@19 9 *
Chris@19 10 * This program is distributed in the hope that it will be useful,
Chris@19 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 13 * GNU General Public License for more details.
Chris@19 14 *
Chris@19 15 * You should have received a copy of the GNU General Public License
Chris@19 16 * along with this program; if not, write to the Free Software
Chris@19 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 18 *
Chris@19 19 */
Chris@19 20
Chris@19 21 #ifndef FFTW_SINGLE
Chris@19 22 #error "NEON only works in single precision"
Chris@19 23 #endif
Chris@19 24
Chris@19 25 /* define these unconditionally, because they are used by
Chris@19 26 taint.c which is compiled without neon */
Chris@19 27 #define SIMD_SUFFIX _neon /* for renaming */
Chris@19 28 #define VL 2 /* SIMD complex vector length */
Chris@19 29 #define SIMD_VSTRIDE_OKA(x) ((x) == 2)
Chris@19 30 #define SIMD_STRIDE_OKPAIR SIMD_STRIDE_OK
Chris@19 31
Chris@19 32 #if defined(__GNUC__) && !defined(__ARM_NEON__)
Chris@19 33 #error "compiling simd-neon.h requires -mfpu=neon or equivalent"
Chris@19 34 #endif
Chris@19 35
Chris@19 36 #include <arm_neon.h>
Chris@19 37
Chris@19 38 /* FIXME: I am not sure whether this code assumes little-endian
Chris@19 39 ordering. VLIT may or may not be wrong for big-endian systems. */
Chris@19 40 typedef float32x4_t V;
Chris@19 41
Chris@19 42 #define VLIT(x0, x1, x2, x3) {x0, x1, x2, x3}
Chris@19 43 #define LDK(x) x
Chris@19 44 #define DVK(var, val) const V var = VLIT(val, val, val, val)
Chris@19 45
Chris@19 46 /* NEON has FMA, but a three-operand FMA is not too useful
Chris@19 47 for FFT purposes. We normally compute
Chris@19 48
Chris@19 49 t0=a+b*c
Chris@19 50 t1=a-b*c
Chris@19 51
Chris@19 52 In a three-operand instruction set this translates into
Chris@19 53
Chris@19 54 t0=a
Chris@19 55 t0+=b*c
Chris@19 56 t1=a
Chris@19 57 t1-=b*c
Chris@19 58
Chris@19 59 At least one move must be implemented, negating the advantage of
Chris@19 60 the FMA in the first place. At least some versions of gcc generate
Chris@19 61 both moves. So we are better off generating t=b*c;t0=a+t;t1=a-t;*/
Chris@19 62 #if HAVE_FMA
Chris@19 63 #warning "--enable-fma on NEON is probably a bad idea (see source code)"
Chris@19 64 #endif
Chris@19 65
Chris@19 66 #define VADD(a, b) vaddq_f32(a, b)
Chris@19 67 #define VSUB(a, b) vsubq_f32(a, b)
Chris@19 68 #define VMUL(a, b) vmulq_f32(a, b)
Chris@19 69 #define VFMA(a, b, c) vmlaq_f32(c, a, b) /* a*b+c */
Chris@19 70 #define VFNMS(a, b, c) vmlsq_f32(c, a, b) /* FNMS=-(a*b-c) in powerpc terminology; MLS=c-a*b
Chris@19 71 in ARM terminology */
Chris@19 72 #define VFMS(a, b, c) VSUB(VMUL(a, b), c) /* FMS=a*b-c in powerpc terminology; no equivalent
Chris@19 73 arm instruction (?) */
Chris@19 74
Chris@19 75 static inline V LDA(const R *x, INT ivs, const R *aligned_like)
Chris@19 76 {
Chris@19 77 (void) aligned_like; /* UNUSED */
Chris@19 78 return vld1q_f32((const float32_t *)x);
Chris@19 79 }
Chris@19 80
Chris@19 81 static inline V LD(const R *x, INT ivs, const R *aligned_like)
Chris@19 82 {
Chris@19 83 (void) aligned_like; /* UNUSED */
Chris@19 84 return vcombine_f32(vld1_f32((float32_t *)x), vld1_f32((float32_t *)(x + ivs)));
Chris@19 85 }
Chris@19 86
Chris@19 87 static inline void STA(R *x, V v, INT ovs, const R *aligned_like)
Chris@19 88 {
Chris@19 89 (void) aligned_like; /* UNUSED */
Chris@19 90 vst1q_f32((float32_t *)x, v);
Chris@19 91 }
Chris@19 92
Chris@19 93 static inline void ST(R *x, V v, INT ovs, const R *aligned_like)
Chris@19 94 {
Chris@19 95 (void) aligned_like; /* UNUSED */
Chris@19 96 /* WARNING: the extra_iter hack depends upon store-low occurring
Chris@19 97 after store-high */
Chris@19 98 vst1_f32((float32_t *)(x + ovs), vget_high_f32(v));
Chris@19 99 vst1_f32((float32_t *)x, vget_low_f32(v));
Chris@19 100 }
Chris@19 101
Chris@19 102 /* 2x2 complex transpose and store */
Chris@19 103 #define STM2 ST
Chris@19 104 #define STN2(x, v0, v1, ovs) /* nop */
Chris@19 105
Chris@19 106 /* store and 4x4 real transpose */
Chris@19 107 static inline void STM4(R *x, V v, INT ovs, const R *aligned_like)
Chris@19 108 {
Chris@19 109 (void) aligned_like; /* UNUSED */
Chris@19 110 vst1_lane_f32((float32_t *)(x) , vget_low_f32(v), 0);
Chris@19 111 vst1_lane_f32((float32_t *)(x + ovs), vget_low_f32(v), 1);
Chris@19 112 vst1_lane_f32((float32_t *)(x + 2 * ovs), vget_high_f32(v), 0);
Chris@19 113 vst1_lane_f32((float32_t *)(x + 3 * ovs), vget_high_f32(v), 1);
Chris@19 114 }
Chris@19 115 #define STN4(x, v0, v1, v2, v3, ovs) /* use STM4 */
Chris@19 116
Chris@19 117 #define FLIP_RI(x) vrev64q_f32(x)
Chris@19 118
Chris@19 119 static inline V VCONJ(V x)
Chris@19 120 {
Chris@19 121 #if 1
Chris@19 122 static const uint32x4_t pm = {0, 0x80000000u, 0, 0x80000000u};
Chris@19 123 return vreinterpretq_f32_u32(veorq_u32(vreinterpretq_u32_f32(x), pm));
Chris@19 124 #else
Chris@19 125 const V pm = VLIT(1.0, -1.0, 1.0, -1.0);
Chris@19 126 return VMUL(x, pm);
Chris@19 127 #endif
Chris@19 128 }
Chris@19 129
Chris@19 130 static inline V VBYI(V x)
Chris@19 131 {
Chris@19 132 return FLIP_RI(VCONJ(x));
Chris@19 133 }
Chris@19 134
Chris@19 135 static inline V VFMAI(V b, V c)
Chris@19 136 {
Chris@19 137 const V mp = VLIT(-1.0, 1.0, -1.0, 1.0);
Chris@19 138 return VFMA(FLIP_RI(b), mp, c);
Chris@19 139 }
Chris@19 140
Chris@19 141 static inline V VFNMSI(V b, V c)
Chris@19 142 {
Chris@19 143 const V mp = VLIT(-1.0, 1.0, -1.0, 1.0);
Chris@19 144 return VFNMS(FLIP_RI(b), mp, c);
Chris@19 145 }
Chris@19 146
Chris@19 147 static inline V VFMACONJ(V b, V c)
Chris@19 148 {
Chris@19 149 const V pm = VLIT(1.0, -1.0, 1.0, -1.0);
Chris@19 150 return VFMA(b, pm, c);
Chris@19 151 }
Chris@19 152
Chris@19 153 static inline V VFNMSCONJ(V b, V c)
Chris@19 154 {
Chris@19 155 const V pm = VLIT(1.0, -1.0, 1.0, -1.0);
Chris@19 156 return VFNMS(b, pm, c);
Chris@19 157 }
Chris@19 158
Chris@19 159 static inline V VFMSCONJ(V b, V c)
Chris@19 160 {
Chris@19 161 return VSUB(VCONJ(b), c);
Chris@19 162 }
Chris@19 163
Chris@19 164 #if 1
Chris@19 165 #define VEXTRACT_REIM(tr, ti, tx) \
Chris@19 166 { \
Chris@19 167 tr = vcombine_f32(vdup_lane_f32(vget_low_f32(tx), 0), \
Chris@19 168 vdup_lane_f32(vget_high_f32(tx), 0)); \
Chris@19 169 ti = vcombine_f32(vdup_lane_f32(vget_low_f32(tx), 1), \
Chris@19 170 vdup_lane_f32(vget_high_f32(tx), 1)); \
Chris@19 171 }
Chris@19 172 #else
Chris@19 173 /* this alternative might be faster in an ideal world, but gcc likes
Chris@19 174 to spill VVV onto the stack */
Chris@19 175 #define VEXTRACT_REIM(tr, ti, tx) \
Chris@19 176 { \
Chris@19 177 float32x4x2_t vvv = vtrnq_f32(tx, tx); \
Chris@19 178 tr = vvv.val[0]; \
Chris@19 179 ti = vvv.val[1]; \
Chris@19 180 }
Chris@19 181 #endif
Chris@19 182
Chris@19 183 static inline V VZMUL(V tx, V sr)
Chris@19 184 {
Chris@19 185 V tr, ti;
Chris@19 186 VEXTRACT_REIM(tr, ti, tx);
Chris@19 187 tr = VMUL(sr, tr);
Chris@19 188 sr = VBYI(sr);
Chris@19 189 return VFMA(ti, sr, tr);
Chris@19 190 }
Chris@19 191
Chris@19 192 static inline V VZMULJ(V tx, V sr)
Chris@19 193 {
Chris@19 194 V tr, ti;
Chris@19 195 VEXTRACT_REIM(tr, ti, tx);
Chris@19 196 tr = VMUL(sr, tr);
Chris@19 197 sr = VBYI(sr);
Chris@19 198 return VFNMS(ti, sr, tr);
Chris@19 199 }
Chris@19 200
Chris@19 201 static inline V VZMULI(V tx, V sr)
Chris@19 202 {
Chris@19 203 V tr, ti;
Chris@19 204 VEXTRACT_REIM(tr, ti, tx);
Chris@19 205 ti = VMUL(ti, sr);
Chris@19 206 sr = VBYI(sr);
Chris@19 207 return VFMS(tr, sr, ti);
Chris@19 208 }
Chris@19 209
Chris@19 210 static inline V VZMULIJ(V tx, V sr)
Chris@19 211 {
Chris@19 212 V tr, ti;
Chris@19 213 VEXTRACT_REIM(tr, ti, tx);
Chris@19 214 ti = VMUL(ti, sr);
Chris@19 215 sr = VBYI(sr);
Chris@19 216 return VFMA(tr, sr, ti);
Chris@19 217 }
Chris@19 218
Chris@19 219 /* twiddle storage #1: compact, slower */
Chris@19 220 #define VTW1(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
Chris@19 221 #define TWVL1 VL
Chris@19 222 static inline V BYTW1(const R *t, V sr)
Chris@19 223 {
Chris@19 224 V tx = LDA(t, 2, 0);
Chris@19 225 return VZMUL(tx, sr);
Chris@19 226 }
Chris@19 227
Chris@19 228 static inline V BYTWJ1(const R *t, V sr)
Chris@19 229 {
Chris@19 230 V tx = LDA(t, 2, 0);
Chris@19 231 return VZMULJ(tx, sr);
Chris@19 232 }
Chris@19 233
Chris@19 234 /* twiddle storage #2: twice the space, faster (when in cache) */
Chris@19 235 # define VTW2(v,x) \
Chris@19 236 {TW_COS, v, x}, {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+1, x}, \
Chris@19 237 {TW_SIN, v, -x}, {TW_SIN, v, x}, {TW_SIN, v+1, -x}, {TW_SIN, v+1, x}
Chris@19 238 #define TWVL2 (2 * VL)
Chris@19 239
Chris@19 240 static inline V BYTW2(const R *t, V sr)
Chris@19 241 {
Chris@19 242 V si = FLIP_RI(sr);
Chris@19 243 V tr = LDA(t, 2, 0), ti = LDA(t+2*VL, 2, 0);
Chris@19 244 return VFMA(ti, si, VMUL(tr, sr));
Chris@19 245 }
Chris@19 246
Chris@19 247 static inline V BYTWJ2(const R *t, V sr)
Chris@19 248 {
Chris@19 249 V si = FLIP_RI(sr);
Chris@19 250 V tr = LDA(t, 2, 0), ti = LDA(t+2*VL, 2, 0);
Chris@19 251 return VFNMS(ti, si, VMUL(tr, sr));
Chris@19 252 }
Chris@19 253
Chris@19 254 /* twiddle storage #3 */
Chris@19 255 # define VTW3(v,x) {TW_CEXP, v, x}, {TW_CEXP, v+1, x}
Chris@19 256 # define TWVL3 (VL)
Chris@19 257
Chris@19 258 /* twiddle storage for split arrays */
Chris@19 259 # define VTWS(v,x) \
Chris@19 260 {TW_COS, v, x}, {TW_COS, v+1, x}, {TW_COS, v+2, x}, {TW_COS, v+3, x}, \
Chris@19 261 {TW_SIN, v, x}, {TW_SIN, v+1, x}, {TW_SIN, v+2, x}, {TW_SIN, v+3, x}
Chris@19 262 #define TWVLS (2 * VL)
Chris@19 263
Chris@19 264 #define VLEAVE() /* nothing */
Chris@19 265
Chris@19 266 #include "simd-common.h"