shun_iwasawa a35b8f
#include "kfc.h"
shun_iwasawa a35b8f
shun_iwasawa a35b8f
/*
shun_iwasawa a35b8f
Copyright (c) 2003-2004, Mark Borgerding
shun_iwasawa a35b8f
shun_iwasawa a35b8f
All rights reserved.
shun_iwasawa a35b8f
shun_iwasawa a35b8f
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
shun_iwasawa a35b8f
    * 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.
shun_iwasawa a35b8f
    * 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.
shun_iwasawa a35b8f
shun_iwasawa a35b8f
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.
shun_iwasawa a35b8f
*/
shun_iwasawa a35b8f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
typedef struct cached_fft *kfc_cfg;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
struct cached_fft
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int nfft;
shun_iwasawa a35b8f
    int inverse;
shun_iwasawa a35b8f
    kiss_fft_cfg cfg;
shun_iwasawa a35b8f
    kfc_cfg next;
shun_iwasawa a35b8f
};
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static kfc_cfg cache_root=NULL;
shun_iwasawa a35b8f
static int ncached=0;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    size_t len;
shun_iwasawa a35b8f
    kfc_cfg  cur=cache_root;
shun_iwasawa a35b8f
    kfc_cfg  prev=NULL;
shun_iwasawa a35b8f
    while ( cur ) {
shun_iwasawa a35b8f
        if ( cur->nfft == nfft && inverse == cur->inverse )
shun_iwasawa a35b8f
            break;/*found the right node*/
shun_iwasawa a35b8f
        prev = cur;
shun_iwasawa a35b8f
        cur = prev->next;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    if (cur== NULL) {
shun_iwasawa a35b8f
        /* no cached node found, need to create a new one*/
shun_iwasawa a35b8f
        kiss_fft_alloc(nfft,inverse,0,&len);
shun_iwasawa a35b8f
#ifdef USE_SIMD
shun_iwasawa a35b8f
        int padding = (16-sizeof(struct cached_fft)) & 15;
shun_iwasawa a35b8f
        // make sure the cfg aligns on a 16 byte boundary
shun_iwasawa a35b8f
        len += padding;
shun_iwasawa a35b8f
#endif
shun_iwasawa a35b8f
        cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len ));
shun_iwasawa a35b8f
        if (cur == NULL)
shun_iwasawa a35b8f
            return NULL;
shun_iwasawa a35b8f
        cur->cfg = (kiss_fft_cfg)(cur+1);
shun_iwasawa a35b8f
#ifdef USE_SIMD
shun_iwasawa a35b8f
        cur->cfg = (kiss_fft_cfg) ((char*)(cur+1)+padding);
shun_iwasawa a35b8f
#endif
shun_iwasawa a35b8f
        kiss_fft_alloc(nfft,inverse,cur->cfg,&len);
shun_iwasawa a35b8f
        cur->nfft=nfft;
shun_iwasawa a35b8f
        cur->inverse=inverse;
shun_iwasawa a35b8f
        cur->next = NULL;
shun_iwasawa a35b8f
        if ( prev )
shun_iwasawa a35b8f
            prev->next = cur;
shun_iwasawa a35b8f
        else
shun_iwasawa a35b8f
            cache_root = cur;
shun_iwasawa a35b8f
        ++ncached;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    return cur->cfg;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
void kfc_cleanup(void)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kfc_cfg  cur=cache_root;
shun_iwasawa a35b8f
    kfc_cfg  next=NULL;
shun_iwasawa a35b8f
    while (cur){
shun_iwasawa a35b8f
        next = cur->next;
shun_iwasawa a35b8f
        free(cur);
shun_iwasawa a35b8f
        cur=next;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    ncached=0;
shun_iwasawa a35b8f
    cache_root = NULL;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kiss_fft( find_cached_fft(nfft,0),fin,fout );
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kiss_fft( find_cached_fft(nfft,1),fin,fout );
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
#ifdef KFC_TEST
shun_iwasawa a35b8f
static void check(int nc)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    if (ncached != nc) {
shun_iwasawa a35b8f
        fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached);
shun_iwasawa a35b8f
        exit(1);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
int main(void)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    kiss_fft_cpx buf1[1024],buf2[1024];
shun_iwasawa a35b8f
    memset(buf1,0,sizeof(buf1));
shun_iwasawa a35b8f
    check(0);
shun_iwasawa a35b8f
    kfc_fft(512,buf1,buf2);
shun_iwasawa a35b8f
    check(1);
shun_iwasawa a35b8f
    kfc_fft(512,buf1,buf2);
shun_iwasawa a35b8f
    check(1);
shun_iwasawa a35b8f
    kfc_ifft(512,buf1,buf2);
shun_iwasawa a35b8f
    check(2);
shun_iwasawa a35b8f
    kfc_cleanup();
shun_iwasawa a35b8f
    check(0);
shun_iwasawa a35b8f
    return 0;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
#endif