annotate constant-q-cpp/src/ext/kissfft/tools/kfc.c @ 372:af71cbdab621 tip

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