media/kiss_fft/_kiss_fft_guts.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial