Debian Codegraph @

xbmc/contrib/kissfft/kiss_fft.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved.
3 * This file is part of KISS FFT - https://github.com/mborgerding/kissfft
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 * See COPYING file for more information.
7 */
8
9 #include "_kiss_fft_guts.h"
10 /* The guts header contains all the multiplication and addition macros that are defined for
11 fixed or floating point complex numbers. It also declares the kf_ internal functions.
12 */
13
14 static void kf_bfly2(
15 kiss_fft_cpx * Fout,
16 const size_t fstride,
17 const kiss_fft_cfg st,
18 int m
19 )
20 {
21 kiss_fft_cpx * Fout2;
22 kiss_fft_cpx * tw1 = st->twiddles;
23 kiss_fft_cpx t;
24 Fout2 = Fout + m;
25 do{
26 C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2);
27
28 C_MUL (t, *Fout2 , *tw1);
29 tw1 += fstride;
30 C_SUB( *Fout2 , *Fout , t );
31 C_ADDTO( *Fout , t );
32 ++Fout2;
33 ++Fout;
34 }while (--m);
35 }
36
37 static void kf_bfly4(
38 kiss_fft_cpx * Fout,
39 const size_t fstride,
40 const kiss_fft_cfg st,
41 const size_t m
42 )
43 {
44 kiss_fft_cpx *tw1,*tw2,*tw3;
45 kiss_fft_cpx scratch[6];
46 size_t k=m;
47 const size_t m2=2*m;
48 const size_t m3=3*m;
49
50
51 tw3 = tw2 = tw1 = st->twiddles;
52
53 do {
54 C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4);
55
56 C_MUL(scratch[0],Fout[m] , *tw1 );
57 C_MUL(scratch[1],Fout[m2] , *tw2 );
58 C_MUL(scratch[2],Fout[m3] , *tw3 );
59
60 C_SUB( scratch[5] , *Fout, scratch[1] );
61 C_ADDTO(*Fout, scratch[1]);
62 C_ADD( scratch[3] , scratch[0] , scratch[2] );
63 C_SUB( scratch[4] , scratch[0] , scratch[2] );
64 C_SUB( Fout[m2], *Fout, scratch[3] );
65 tw1 += fstride;
66 tw2 += fstride*2;
67 tw3 += fstride*3;
68 C_ADDTO( *Fout , scratch[3] );
69
70 if(st->inverse) {
71 Fout[m].r = scratch[5].r - scratch[4].i;
72 Fout[m].i = scratch[5].i + scratch[4].r;
73 Fout[m3].r = scratch[5].r + scratch[4].i;
74 Fout[m3].i = scratch[5].i - scratch[4].r;
75 }else{
76 Fout[m].r = scratch[5].r + scratch[4].i;
77 Fout[m].i = scratch[5].i - scratch[4].r;
78 Fout[m3].r = scratch[5].r - scratch[4].i;
79 Fout[m3].i = scratch[5].i + scratch[4].r;
80 }
81 ++Fout;
82 }while(--k);
83 }
84
85 static void kf_bfly3(
86 kiss_fft_cpx * Fout,
87 const size_t fstride,
88 const kiss_fft_cfg st,
89 size_t m
90 )
91 {
92 size_t k=m;
93 const size_t m2 = 2*m;
94 kiss_fft_cpx *tw1,*tw2;
95 kiss_fft_cpx scratch[5];
96 kiss_fft_cpx epi3;
97 epi3 = st->twiddles[fstride*m];
98
99 tw1=tw2=st->twiddles;
100
101 do{
102 C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3);
103
104 C_MUL(scratch[1],Fout[m] , *tw1);
105 C_MUL(scratch[2],Fout[m2] , *tw2);
106
107 C_ADD(scratch[3],scratch[1],scratch[2]);
108 C_SUB(scratch[0],scratch[1],scratch[2]);
109 tw1 += fstride;
110 tw2 += fstride*2;
111
112 Fout[m].r = Fout->r - HALF_OF(scratch[3].r);
113 Fout[m].i = Fout->i - HALF_OF(scratch[3].i);
114
115 C_MULBYSCALAR( scratch[0] , epi3.i );
116
117 C_ADDTO(*Fout,scratch[3]);
118
119 Fout[m2].r = Fout[m].r + scratch[0].i;
120 Fout[m2].i = Fout[m].i - scratch[0].r;
121
122 Fout[m].r -= scratch[0].i;
123 Fout[m].i += scratch[0].r;
124
125 ++Fout;
126 }while(--k);
127 }
128
129 static void kf_bfly5(
130 kiss_fft_cpx * Fout,
131 const size_t fstride,
132 const kiss_fft_cfg st,
133 int m
134 )
135 {
136 kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4;
137 int u;
138 kiss_fft_cpx scratch[13];
139 kiss_fft_cpx * twiddles = st->twiddles;
140 kiss_fft_cpx *tw;
141 kiss_fft_cpx ya,yb;
142 ya = twiddles[fstride*m];
143 yb = twiddles[fstride*2*m];
144
145 Fout0=Fout;
146 Fout1=Fout0+m;
147 Fout2=Fout0+2*m;
148 Fout3=Fout0+3*m;
149 Fout4=Fout0+4*m;
150
151 tw=st->twiddles;
152 for ( u=0; u<m; ++u ) {
153 C_FIXDIV( *Fout0,5); C_FIXDIV( *Fout1,5); C_FIXDIV( *Fout2,5); C_FIXDIV( *Fout3,5); C_FIXDIV( *Fout4,5);
154 scratch[0] = *Fout0;
155
156 C_MUL(scratch[1] ,*Fout1, tw[u*fstride]);
157 C_MUL(scratch[2] ,*Fout2, tw[2*u*fstride]);
158 C_MUL(scratch[3] ,*Fout3, tw[3*u*fstride]);
159 C_MUL(scratch[4] ,*Fout4, tw[4*u*fstride]);
160
161 C_ADD( scratch[7],scratch[1],scratch[4]);
162 C_SUB( scratch[10],scratch[1],scratch[4]);
163 C_ADD( scratch[8],scratch[2],scratch[3]);
164 C_SUB( scratch[9],scratch[2],scratch[3]);
165
166 Fout0->r += scratch[7].r + scratch[8].r;
167 Fout0->i += scratch[7].i + scratch[8].i;
168
169 scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r);
170 scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r);
171
172 scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i);
173 scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i);
174
175 C_SUB(*Fout1,scratch[5],scratch[6]);
176 C_ADD(*Fout4,scratch[5],scratch[6]);
177
178 scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r);
179 scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r);
180 scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i);
181 scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i);
182
183 C_ADD(*Fout2,scratch[11],scratch[12]);
184 C_SUB(*Fout3,scratch[11],scratch[12]);
185
186 ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4;
187 }
188 }
189
190 /* perform the butterfly for one stage of a mixed radix FFT */
191 static void kf_bfly_generic(
192 kiss_fft_cpx * Fout,
193 const size_t fstride,
194 const kiss_fft_cfg st,
195 int m,
196 int p
197 )
198 {
199 int u,k,q1,q;
200 kiss_fft_cpx * twiddles = st->twiddles;
201 kiss_fft_cpx t;
202 int Norig = st->nfft;
203
204 kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p);
205
206 for ( u=0; u<m; ++u ) {
207 k=u;
208 for ( q1=0 ; q1<p ; ++q1 ) {
209 scratch[q1] = Fout[ k ];
210 C_FIXDIV(scratch[q1],p);
211 k += m;
212 }
213
214 k=u;
215 for ( q1=0 ; q1<p ; ++q1 ) {
216 int twidx=0;
217 Fout[ k ] = scratch[0];
218 for (q=1;q<p;++q ) {
219 twidx += fstride * k;
220 if (twidx>=Norig) twidx-=Norig;
221 C_MUL(t,scratch[q] , twiddles[twidx] );
222 C_ADDTO( Fout[ k ] ,t);
223 }
224 k += m;
225 }
226 }
227 KISS_FFT_TMP_FREE(scratch);
228 }
229
230 static
231 void kf_work(
232 kiss_fft_cpx * Fout,
233 const kiss_fft_cpx * f,
234 const size_t fstride,
235 int in_stride,
236 int * factors,
237 const kiss_fft_cfg st
238 )
239 {
240 kiss_fft_cpx * Fout_beg=Fout;
241 const int p=*factors++; /* the radix */
242 const int m=*factors++; /* stage's fft length/p */
243 const kiss_fft_cpx * Fout_end = Fout + p*m;
244
245 #ifdef _OPENMP
246 // use openmp extensions at the
247 // top-level (not recursive)
248 if (fstride==1 && p<=5)
249 {
250 int k;
251
252 // execute the p different work units in different threads
253 # pragma omp parallel for
254 for (k=0;k<p;++k)
255 kf_work( Fout +k*m, f+ fstride*in_stride*k,fstride*p,in_stride,factors,st);
256 // all threads have joined by this point
257
258 switch (p) {
259 case 2: kf_bfly2(Fout,fstride,st,m); break;
260 case 3: kf_bfly3(Fout,fstride,st,m); break;
261 case 4: kf_bfly4(Fout,fstride,st,m); break;
262 case 5: kf_bfly5(Fout,fstride,st,m); break;
263 default: kf_bfly_generic(Fout,fstride,st,m,p); break;
264 }
265 return;
266 }
267 #endif
268
269 if (m==1) {
270 do{
271 *Fout = *f;
272 f += fstride*in_stride;
273 }while(++Fout != Fout_end );
274 }else{
275 do{
276 // recursive call:
277 // DFT of size m*p performed by doing
278 // p instances of smaller DFTs of size m,
279 // each one takes a decimated version of the input
280 kf_work( Fout , f, fstride*p, in_stride, factors,st);
281 f += fstride*in_stride;
282 }while( (Fout += m) != Fout_end );
283 }
284
285 Fout=Fout_beg;
286
287 // recombine the p smaller DFTs
288 switch (p) {
289 case 2: kf_bfly2(Fout,fstride,st,m); break;
290 case 3: kf_bfly3(Fout,fstride,st,m); break;
291 case 4: kf_bfly4(Fout,fstride,st,m); break;
292 case 5: kf_bfly5(Fout,fstride,st,m); break;
293 default: kf_bfly_generic(Fout,fstride,st,m,p); break;
294 }
295 }
296
297 /* facbuf is populated by p1,m1,p2,m2, ...
298 where
299 p[i] * m[i] = m[i-1]
300 m0 = n */
301 static
302 void kf_factor(int n,int * facbuf)
303 {
304 int p=4;
305 double floor_sqrt;
306 floor_sqrt = floor( sqrt((double)n) );
307
308 /*factor out powers of 4, powers of 2, then any remaining primes */
309 do {
310 while (n % p) {
311 switch (p) {
312 case 4: p = 2; break;
313 case 2: p = 3; break;
314 default: p += 2; break;
315 }
316 if (p > floor_sqrt)
317 p = n; /* no more factors, skip to end */
318 }
319 n /= p;
320 *facbuf++ = p;
321 *facbuf++ = n;
322 } while (n > 1);
323 }
324
325 /*
326 *
327 * User-callable function to allocate all necessary storage space for the fft.
328 *
329 * The return value is a contiguous block of memory, allocated with malloc. As such,
330 * It can be freed with free(), rather than a kiss_fft-specific function.
331 * */
332 kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
333 {
334 kiss_fft_cfg st=NULL;
335 size_t memneeded = sizeof(struct kiss_fft_state)
336 + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/
337
338 if ( lenmem==NULL ) {
339 st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
340 }else{
341 if (mem != NULL && *lenmem >= memneeded)
342 st = (kiss_fft_cfg)mem;
343 *lenmem = memneeded;
344 }
345 if (st) {
346 int i;
347 st->nfft=nfft;
348 st->inverse = inverse_fft;
349
350 for (i=0;i<nfft;++i) {
351 const double pi=3.141592653589793238462643383279502884197169399375105820974944;
352 double phase = -2*pi*i / nfft;
353 if (st->inverse)
354 phase *= -1;
355 kf_cexp(st->twiddles+i, phase );
356 }
357
358 kf_factor(nfft,st->factors);
359 }
360 return st;
361 }
362
363
364 void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride)
365 {
366 if (fin == fout) {
367 //NOTE: this is not really an in-place FFT algorithm.
368 //It just performs an out-of-place FFT into a temp buffer
369 kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft);
370 kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
371 memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
372 KISS_FFT_TMP_FREE(tmpbuf);
373 }else{
374 kf_work( fout, fin, 1,in_stride, st->factors,st );
375 }
376 }
377
378 void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
379 {
380 kiss_fft_stride(cfg,fin,fout,1);
381 }
382
383
384 void kiss_fft_cleanup(void)
385 {
386 // nothing needed any more
387 }
388
389 int kiss_fft_next_fast_size(int n)
390 {
391 while(1) {
392 int m=n;
393 while ( (m%2) == 0 ) m/=2;
394 while ( (m%3) == 0 ) m/=3;
395 while ( (m%5) == 0 ) m/=5;
396 if (m<=1)
397 break; /* n is completely factorable by twos, threes, and fives */
398 n++;
399 }
400 return n;
401 }