annotate src/ext/kissfft/tools/kfc.c @ 196:da283326bcd3 tip master

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