d94d1240d6f957c8b19c59002101f5e37dc27a0c
[ardour.git] / libs / qm-dsp / ext / kissfft / tools / kfc.c
1 #include "kfc.h"
2
3 /*
4 Copyright (c) 2003-2004, Mark Borgerding
5
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
11     * 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.
12     * 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.
13
14 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.
15 */
16
17
18 typedef struct cached_fft *kfc_cfg;
19
20 struct cached_fft
21 {
22     int nfft;
23     int inverse;
24     kiss_fft_cfg cfg;
25     kfc_cfg next;
26 };
27
28 static kfc_cfg cache_root=NULL;
29 static int ncached=0;
30
31 static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
32 {
33     size_t len;
34     kfc_cfg  cur=cache_root;
35     kfc_cfg  prev=NULL;
36     while ( cur ) {
37         if ( cur->nfft == nfft && inverse == cur->inverse )
38             break;/*found the right node*/
39         prev = cur;
40         cur = prev->next;
41     }
42     if (cur== NULL) {
43         /* no cached node found, need to create a new one*/
44         kiss_fft_alloc(nfft,inverse,0,&len);
45 #ifdef USE_SIMD
46         int padding = (16-sizeof(struct cached_fft)) & 15;
47         // make sure the cfg aligns on a 16 byte boundary
48         len += padding;
49 #endif
50         cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len ));
51         if (cur == NULL)
52             return NULL;
53         cur->cfg = (kiss_fft_cfg)(cur+1);
54 #ifdef USE_SIMD
55         cur->cfg = (kiss_fft_cfg) ((char*)(cur+1)+padding);
56 #endif
57         kiss_fft_alloc(nfft,inverse,cur->cfg,&len);
58         cur->nfft=nfft;
59         cur->inverse=inverse;
60         cur->next = NULL;
61         if ( prev )
62             prev->next = cur;
63         else
64             cache_root = cur;
65         ++ncached;
66     }
67     return cur->cfg;
68 }
69
70 void kfc_cleanup(void)
71 {
72     kfc_cfg  cur=cache_root;
73     kfc_cfg  next=NULL;
74     while (cur){
75         next = cur->next;
76         free(cur);
77         cur=next;
78     }
79     ncached=0;
80     cache_root = NULL;
81 }
82 void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
83 {
84     kiss_fft( find_cached_fft(nfft,0),fin,fout );
85 }
86
87 void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
88 {
89     kiss_fft( find_cached_fft(nfft,1),fin,fout );
90 }
91
92 #ifdef KFC_TEST
93 static void check(int nc)
94 {
95     if (ncached != nc) {
96         fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached);
97         exit(1);
98     }
99 }
100
101 int main(void)
102 {
103     kiss_fft_cpx buf1[1024],buf2[1024];
104     memset(buf1,0,sizeof(buf1));
105     check(0);
106     kfc_fft(512,buf1,buf2);
107     check(1);
108     kfc_fft(512,buf1,buf2);
109     check(1);
110     kfc_ifft(512,buf1,buf2);
111     check(2);
112     kfc_cleanup();
113     check(0);
114     return 0;
115 }
116 #endif