annotate src/ext/kissfft/_kiss_fft_guts.h @ 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 /*
c@174 2 Copyright (c) 2003-2010, Mark Borgerding
c@174 3
c@174 4 All rights reserved.
c@174 5
c@174 6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
c@174 7
c@174 8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
c@174 9 * 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 10 * 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 11
c@174 12 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 13 */
c@174 14
c@174 15 /* kiss_fft.h
c@174 16 defines kiss_fft_scalar as either short or a float type
c@174 17 and defines
c@174 18 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
c@174 19 #include "kiss_fft.h"
c@174 20 #include <limits.h>
c@174 21
c@174 22 #define MAXFACTORS 32
c@174 23 /* e.g. an fft of length 128 has 4 factors
c@174 24 as far as kissfft is concerned
c@174 25 4*4*4*2
c@174 26 */
c@174 27
c@174 28 struct kiss_fft_state{
c@174 29 int nfft;
c@174 30 int inverse;
c@174 31 int factors[2*MAXFACTORS];
c@174 32 kiss_fft_cpx twiddles[1];
c@174 33 };
c@174 34
c@174 35 /*
c@174 36 Explanation of macros dealing with complex math:
c@174 37
c@174 38 C_MUL(m,a,b) : m = a*b
c@174 39 C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
c@174 40 C_SUB( res, a,b) : res = a - b
c@174 41 C_SUBFROM( res , a) : res -= a
c@174 42 C_ADDTO( res , a) : res += a
c@174 43 * */
c@174 44 #ifdef FIXED_POINT
c@174 45 #if (FIXED_POINT==32)
c@174 46 # define FRACBITS 31
c@174 47 # define SAMPPROD int64_t
c@174 48 #define SAMP_MAX 2147483647
c@174 49 #else
c@174 50 # define FRACBITS 15
c@174 51 # define SAMPPROD int32_t
c@174 52 #define SAMP_MAX 32767
c@174 53 #endif
c@174 54
c@174 55 #define SAMP_MIN -SAMP_MAX
c@174 56
c@174 57 #if defined(CHECK_OVERFLOW)
c@174 58 # define CHECK_OVERFLOW_OP(a,op,b) \
c@174 59 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
c@174 60 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
c@174 61 #endif
c@174 62
c@174 63
c@174 64 # define smul(a,b) ( (SAMPPROD)(a)*(b) )
c@174 65 # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
c@174 66
c@174 67 # define S_MUL(a,b) sround( smul(a,b) )
c@174 68
c@174 69 # define C_MUL(m,a,b) \
c@174 70 do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
c@174 71 (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
c@174 72
c@174 73 # define DIVSCALAR(x,k) \
c@174 74 (x) = sround( smul( x, SAMP_MAX/k ) )
c@174 75
c@174 76 # define C_FIXDIV(c,div) \
c@174 77 do { DIVSCALAR( (c).r , div); \
c@174 78 DIVSCALAR( (c).i , div); }while (0)
c@174 79
c@174 80 # define C_MULBYSCALAR( c, s ) \
c@174 81 do{ (c).r = sround( smul( (c).r , s ) ) ;\
c@174 82 (c).i = sround( smul( (c).i , s ) ) ; }while(0)
c@174 83
c@174 84 #else /* not FIXED_POINT*/
c@174 85
c@174 86 # define S_MUL(a,b) ( (a)*(b) )
c@174 87 #define C_MUL(m,a,b) \
c@174 88 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
c@174 89 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
c@174 90 # define C_FIXDIV(c,div) /* NOOP */
c@174 91 # define C_MULBYSCALAR( c, s ) \
c@174 92 do{ (c).r *= (s);\
c@174 93 (c).i *= (s); }while(0)
c@174 94 #endif
c@174 95
c@174 96 #ifndef CHECK_OVERFLOW_OP
c@174 97 # define CHECK_OVERFLOW_OP(a,op,b) /* noop */
c@174 98 #endif
c@174 99
c@174 100 #define C_ADD( res, a,b)\
c@174 101 do { \
c@174 102 CHECK_OVERFLOW_OP((a).r,+,(b).r)\
c@174 103 CHECK_OVERFLOW_OP((a).i,+,(b).i)\
c@174 104 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
c@174 105 }while(0)
c@174 106 #define C_SUB( res, a,b)\
c@174 107 do { \
c@174 108 CHECK_OVERFLOW_OP((a).r,-,(b).r)\
c@174 109 CHECK_OVERFLOW_OP((a).i,-,(b).i)\
c@174 110 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
c@174 111 }while(0)
c@174 112 #define C_ADDTO( res , a)\
c@174 113 do { \
c@174 114 CHECK_OVERFLOW_OP((res).r,+,(a).r)\
c@174 115 CHECK_OVERFLOW_OP((res).i,+,(a).i)\
c@174 116 (res).r += (a).r; (res).i += (a).i;\
c@174 117 }while(0)
c@174 118
c@174 119 #define C_SUBFROM( res , a)\
c@174 120 do {\
c@174 121 CHECK_OVERFLOW_OP((res).r,-,(a).r)\
c@174 122 CHECK_OVERFLOW_OP((res).i,-,(a).i)\
c@174 123 (res).r -= (a).r; (res).i -= (a).i; \
c@174 124 }while(0)
c@174 125
c@174 126
c@174 127 #ifdef FIXED_POINT
c@174 128 # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
c@174 129 # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
c@174 130 # define HALF_OF(x) ((x)>>1)
c@174 131 #elif defined(USE_SIMD)
c@174 132 # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
c@174 133 # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
c@174 134 # define HALF_OF(x) ((x)*_mm_set1_ps(.5))
c@174 135 #else
c@174 136 # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
c@174 137 # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
c@174 138 # define HALF_OF(x) ((x)*.5)
c@174 139 #endif
c@174 140
c@174 141 #define kf_cexp(x,phase) \
c@174 142 do{ \
c@174 143 (x)->r = KISS_FFT_COS(phase);\
c@174 144 (x)->i = KISS_FFT_SIN(phase);\
c@174 145 }while(0)
c@174 146
c@174 147
c@174 148 /* a debugging function */
c@174 149 #define pcpx(c)\
c@174 150 fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )
c@174 151
c@174 152
c@174 153 #ifdef KISS_FFT_USE_ALLOCA
c@174 154 // define this to allow use of alloca instead of malloc for temporary buffers
c@174 155 // Temporary buffers are used in two case:
c@174 156 // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5
c@174 157 // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform.
c@174 158 #include <alloca.h>
c@174 159 #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes)
c@174 160 #define KISS_FFT_TMP_FREE(ptr)
c@174 161 #else
c@174 162 #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes)
c@174 163 #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr)
c@174 164 #endif