annotate ext/kissfft/tools/kfc.c @ 206:335be766a54d

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