Mercurial > hg > peckfft
annotate peck_fftr.c @ 4:2d6c49fcafcb
neon2 and neon4 support
author | Peter Meerwald <p.meerwald@bct-electronic.com> |
---|---|
date | Fri, 16 Sep 2011 14:04:19 +0200 |
parents | 3b31bd44a09f |
children | fee54f1878f7 |
rev | line source |
---|---|
0 | 1 /* |
2 Copyright (c) 2003-2004, Mark Borgerding | |
3 | |
4 All rights reserved. | |
5 | |
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | |
7 | |
8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
9 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | |
10 * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. | |
11 | |
12 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
13 */ | |
14 | |
15 #include "peck_fftr.h" | |
16 #include "_peck_fft_guts.h" | |
17 | |
18 struct peck_fftr_state{ | |
19 peck_fft_cfg substate; | |
20 peck_fft_cpx *tmpbuf; | |
21 peck_fft_cpx *super_twiddles; | |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
22 #if USE_SIMD == SIMD_SSE2 |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
23 void *pad; |
0 | 24 #endif |
25 }; | |
26 | |
27 peck_fftr_cfg peck_fftr_alloc(int nfft, int inverse_fft, void *mem, size_t *lenmem) { | |
28 int i; | |
29 peck_fftr_cfg st = NULL; | |
30 size_t subsize, memneeded; | |
31 | |
32 if (nfft & 1) { | |
33 fprintf(stderr, "Real FFT must be even.\n"); | |
34 return NULL; | |
35 } | |
36 nfft >>= 1; | |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
37 peck_fft_alloc(nfft, inverse_fft, NULL, &subsize); |
0 | 38 |
39 memneeded = sizeof(struct peck_fftr_state) + subsize + sizeof(peck_fft_cpx) * (nfft * 3 / 2); | |
40 if (lenmem == NULL) { | |
41 st = (peck_fftr_cfg) PECK_FFT_MALLOC(memneeded); | |
42 } else { | |
43 if (*lenmem >= memneeded) | |
44 st = (peck_fftr_cfg) mem; | |
45 *lenmem = memneeded; | |
46 } | |
47 if (!st) | |
48 return NULL; | |
49 | |
50 st->substate = (peck_fft_cfg) (st + 1); /* just beyond peck_fftr_state struct */ | |
51 st->tmpbuf = (peck_fft_cpx *) (((char *) st->substate) + subsize); | |
52 st->super_twiddles = st->tmpbuf + nfft; | |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
53 |
0 | 54 peck_fft_alloc(nfft, inverse_fft, st->substate, &subsize); |
55 | |
56 for (i = 0; i < nfft/2; ++i) { | |
57 float phase = | |
58 -3.14159265359f * ((float) (i+1) / nfft + 0.5f); | |
59 if (inverse_fft) | |
60 phase *= -1; | |
3 | 61 kf_cexp(st->super_twiddles+i, phase); |
0 | 62 } |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
63 |
0 | 64 return st; |
65 } | |
66 | |
67 void peck_fftr(peck_fftr_cfg st, const peck_fft_scalar *timedata, peck_fft_cpx *freqdata) { | |
68 /* Input buffer timedata is stored row-wise */ | |
69 int k, ncfft; | |
70 peck_fft_cpx fpnk, fpk, f1k, f2k, tw, tdc; | |
71 | |
72 if (st->substate->inverse) { | |
73 fprintf(stderr, "peck_fft usage error: improper alloc\n"); | |
74 exit(EXIT_FAILURE); | |
75 } | |
76 | |
77 ncfft = st->substate->nfft; | |
78 | |
79 /* Perform the parallel FFT of two real signals packed in real,imag */ | |
80 peck_fft(st->substate, (const peck_fft_cpx*)timedata, st->tmpbuf); | |
81 /* The real part of the DC element of the frequency spectrum in st->tmpbuf | |
82 * contains the sum of the even-numbered elements of the input time sequence. | |
83 * The imag part is the sum of the odd-numbered elements. | |
84 * | |
85 * The sum of tdc.r and tdc.i is the sum of the input time sequence, | |
86 * yielding DC of the input time sequence. | |
87 * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1,... | |
88 * yielding the Nyquist bin of input time sequence. | |
89 */ | |
90 | |
91 tdc.r = st->tmpbuf[0].r; | |
92 tdc.i = st->tmpbuf[0].i; | |
93 C_FIXDIV(tdc,2); | |
94 CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i); | |
95 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); | |
96 freqdata[0].r = tdc.r + tdc.i; | |
97 freqdata[ncfft].r = tdc.r - tdc.i; | |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
98 #if USE_SIMD == SIMD_SSE2 |
0 | 99 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
100 #elif USE_SIMD == SIMD_NEON4 |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
101 freqdata[ncfft].i = freqdata[0].i = vdupq_n_f32(0.0f); |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
102 #elif USE_SIMD == SIMD_NEON2 |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
103 freqdata[ncfft].i = freqdata[0].i = vdup_n_f32(0.0f); |
0 | 104 #else |
105 freqdata[ncfft].i = freqdata[0].i = 0; | |
106 #endif | |
107 | |
108 for (k = 1; k <= ncfft/2; ++k) { | |
109 fpk = st->tmpbuf[k]; | |
110 fpnk.r = st->tmpbuf[ncfft-k].r; | |
111 fpnk.i = - st->tmpbuf[ncfft-k].i; | |
112 C_FIXDIV(fpk, 2); | |
113 C_FIXDIV(fpnk, 2); | |
114 | |
115 C_ADD(f1k, fpk, fpnk); | |
116 C_SUB(f2k, fpk, fpnk); | |
117 C_MUL(tw, f2k, st->super_twiddles[k-1]); | |
118 | |
119 freqdata[k].r = HALF_OF(f1k.r + tw.r); | |
120 freqdata[k].i = HALF_OF(f1k.i + tw.i); | |
121 freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r); | |
122 freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i); | |
123 } | |
124 } | |
125 | |
126 void peck_fftri(peck_fftr_cfg st,const peck_fft_cpx *freqdata,peck_fft_scalar *timedata) { | |
127 /* input buffer timedata is stored row-wise */ | |
128 int k, ncfft; | |
129 | |
130 if (st->substate->inverse == 0) { | |
131 fprintf (stderr, "peck_fft usage error: improper alloc\n"); | |
132 exit(EXIT_FAILURE); | |
133 } | |
134 | |
135 ncfft = st->substate->nfft; | |
136 | |
137 st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; | |
138 st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; | |
139 C_FIXDIV(st->tmpbuf[0], 2); | |
140 | |
141 for (k = 1; k <= ncfft / 2; ++k) { | |
142 peck_fft_cpx fk, fnkc, fek, fok, tmp; | |
143 fk = freqdata[k]; | |
144 fnkc.r = freqdata[ncfft - k].r; | |
145 fnkc.i = -freqdata[ncfft - k].i; | |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
146 C_FIXDIV(fk, 2); |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
147 C_FIXDIV(fnkc, 2); |
0 | 148 |
149 C_ADD(fek, fk, fnkc); | |
150 C_SUB(tmp, fk, fnkc); | |
151 C_MUL(fok, tmp, st->super_twiddles[k-1]); | |
152 C_ADD(st->tmpbuf[k], fek, fok); | |
153 C_SUB(st->tmpbuf[ncfft - k], fek, fok); | |
4
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
154 #if USE_SIMD == SIMD_SSE2 |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
155 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0f); |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
156 #elif USE_SIMD == SIMD_NEON4 |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
157 st->tmpbuf[ncfft - k].i *= vdupq_n_f32(-1.0f); |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
158 #elif USE_SIMD == SIMD_NEON2 |
2d6c49fcafcb
neon2 and neon4 support
Peter Meerwald <p.meerwald@bct-electronic.com>
parents:
3
diff
changeset
|
159 st->tmpbuf[ncfft - k].i *= vdup_n_f32(-1.0f); |
0 | 160 #else |
161 st->tmpbuf[ncfft - k].i *= -1; | |
162 #endif | |
163 } | |
164 peck_fft(st->substate, st->tmpbuf, (peck_fft_cpx *) timedata); | |
165 } |