summaryrefslogtreecommitdiff
path: root/_kiss_fft_guts.h
diff options
context:
space:
mode:
Diffstat (limited to '_kiss_fft_guts.h')
-rw-r--r--_kiss_fft_guts.h234
1 files changed, 132 insertions, 102 deletions
diff --git a/_kiss_fft_guts.h b/_kiss_fft_guts.h
index 1c62a34..16305af 100644
--- a/_kiss_fft_guts.h
+++ b/_kiss_fft_guts.h
@@ -3,33 +3,49 @@ Copyright (c) 2003-2010, Mark Borgerding
3 3
4All rights reserved. 4All rights reserved.
5 5
6Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6Redistribution and use in source and binary forms, with or without modification,
7 7are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8
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. 9 * Redistributions of source code must retain the above copyright notice,
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. 10this list of conditions and the following disclaimer.
11 11 * Redistributions in binary form must reproduce the above copyright notice,
12THIS 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. 12this list of conditions and the following disclaimer in the documentation and/or
13other materials provided with the distribution.
14 * Neither the author nor the names of any contributors may be used to
15endorse or promote products derived from this software without specific prior
16written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*/ 28*/
14 29
15/* kiss_fft.h 30/* kiss_fft.h
16 defines kiss_fft_scalar as either short or a float type 31 defines kiss_fft_scalar as either short or a float type
17 and defines 32 and defines
18 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ 33 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
19#include "kiss_fft.h"
20#include <limits.h> 34#include <limits.h>
21 35
36#include "kiss_fft.h"
37
22#define MAXFACTORS 32 38#define MAXFACTORS 32
23/* e.g. an fft of length 128 has 4 factors 39/* e.g. an fft of length 128 has 4 factors
24 as far as kissfft is concerned 40 as far as kissfft is concerned
25 4*4*4*2 41 4*4*4*2
26 */ 42 */
27 43
28struct kiss_fft_state{ 44struct kiss_fft_state {
29 int nfft; 45 int nfft;
30 int inverse; 46 int inverse;
31 int factors[2*MAXFACTORS]; 47 int factors[2 * MAXFACTORS];
32 kiss_fft_cpx twiddles[1]; 48 kiss_fft_cpx twiddles[1];
33}; 49};
34 50
35/* 51/*
@@ -42,123 +58,137 @@ struct kiss_fft_state{
42 C_ADDTO( res , a) : res += a 58 C_ADDTO( res , a) : res += a
43 * */ 59 * */
44#ifdef FIXED_POINT 60#ifdef FIXED_POINT
45#if (FIXED_POINT==32) 61#if (FIXED_POINT == 32)
46# define FRACBITS 31 62#define FRACBITS 31
47# define SAMPPROD int64_t 63#define SAMPPROD int64_t
48#define SAMP_MAX 2147483647 64#define SAMP_MAX 2147483647
49#else 65#else
50# define FRACBITS 15 66#define FRACBITS 15
51# define SAMPPROD int32_t 67#define SAMPPROD int32_t
52#define SAMP_MAX 32767 68#define SAMP_MAX 32767
53#endif 69#endif
54 70
55#define SAMP_MIN -SAMP_MAX 71#define SAMP_MIN -SAMP_MAX
56 72
57#if defined(CHECK_OVERFLOW) 73#if defined(CHECK_OVERFLOW)
58# define CHECK_OVERFLOW_OP(a,op,b) \ 74#define CHECK_OVERFLOW_OP(a, op, b) \
59 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ 75 if ((SAMPPROD)(a)op(SAMPPROD)(b) > SAMP_MAX || \
60 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } 76 (SAMPPROD)(a)op(SAMPPROD)(b) < SAMP_MIN) { \
77 fprintf(stderr, \
78 "WARNING:overflow @ " __FILE__ "(%d): (%d " #op " %d) = %ld\n", \
79 __LINE__, (a), (b), (SAMPPROD)(a)op(SAMPPROD)(b)); \
80 }
61#endif 81#endif
62 82
63 83#define smul(a, b) ((SAMPPROD)(a) * (b))
64# define smul(a,b) ( (SAMPPROD)(a)*(b) ) 84#define sround(x) (kiss_fft_scalar)(((x) + (1 << (FRACBITS - 1))) >> FRACBITS)
65# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) 85
66 86#define S_MUL(a, b) sround(smul(a, b))
67# define S_MUL(a,b) sround( smul(a,b) ) 87
68 88#define C_MUL(m, a, b) \
69# define C_MUL(m,a,b) \ 89 do { \
70 do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ 90 (m).r = sround(smul((a).r, (b).r) - smul((a).i, (b).i)); \
71 (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) 91 (m).i = sround(smul((a).r, (b).i) + smul((a).i, (b).r)); \
72 92 } while (0)
73# define DIVSCALAR(x,k) \ 93
74 (x) = sround( smul( x, SAMP_MAX/k ) ) 94#define DIVSCALAR(x, k) (x) = sround(smul(x, SAMP_MAX / k))
75 95
76# define C_FIXDIV(c,div) \ 96#define C_FIXDIV(c, div) \
77 do { DIVSCALAR( (c).r , div); \ 97 do { \
78 DIVSCALAR( (c).i , div); }while (0) 98 DIVSCALAR((c).r, div); \
79 99 DIVSCALAR((c).i, div); \
80# define C_MULBYSCALAR( c, s ) \ 100 } while (0)
81 do{ (c).r = sround( smul( (c).r , s ) ) ;\ 101
82 (c).i = sround( smul( (c).i , s ) ) ; }while(0) 102#define C_MULBYSCALAR(c, s) \
83 103 do { \
84#else /* not FIXED_POINT*/ 104 (c).r = sround(smul((c).r, s)); \
85 105 (c).i = sround(smul((c).i, s)); \
86# define S_MUL(a,b) ( (a)*(b) ) 106 } while (0)
87#define C_MUL(m,a,b) \ 107
88 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ 108#else /* not FIXED_POINT*/
89 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) 109
90# define C_FIXDIV(c,div) /* NOOP */ 110#define S_MUL(a, b) ((a) * (b))
91# define C_MULBYSCALAR( c, s ) \ 111#define C_MUL(m, a, b) \
92 do{ (c).r *= (s);\ 112 do { \
93 (c).i *= (s); }while(0) 113 (m).r = (a).r * (b).r - (a).i * (b).i; \
114 (m).i = (a).r * (b).i + (a).i * (b).r; \
115 } while (0)
116#define C_FIXDIV(c, div) /* NOOP */
117#define C_MULBYSCALAR(c, s) \
118 do { \
119 (c).r *= (s); \
120 (c).i *= (s); \
121 } while (0)
94#endif 122#endif
95 123
96#ifndef CHECK_OVERFLOW_OP 124#ifndef CHECK_OVERFLOW_OP
97# define CHECK_OVERFLOW_OP(a,op,b) /* noop */ 125#define CHECK_OVERFLOW_OP(a, op, b) /* noop */
98#endif 126#endif
99 127
100#define C_ADD( res, a,b)\ 128#define C_ADD(res, a, b) \
101 do { \ 129 do { \
102 CHECK_OVERFLOW_OP((a).r,+,(b).r)\ 130 CHECK_OVERFLOW_OP((a).r, +, (b).r) \
103 CHECK_OVERFLOW_OP((a).i,+,(b).i)\ 131 CHECK_OVERFLOW_OP((a).i, +, (b).i) \
104 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ 132 (res).r = (a).r + (b).r; \
105 }while(0) 133 (res).i = (a).i + (b).i; \
106#define C_SUB( res, a,b)\ 134 } while (0)
107 do { \ 135#define C_SUB(res, a, b) \
108 CHECK_OVERFLOW_OP((a).r,-,(b).r)\ 136 do { \
109 CHECK_OVERFLOW_OP((a).i,-,(b).i)\ 137 CHECK_OVERFLOW_OP((a).r, -, (b).r) \
110 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ 138 CHECK_OVERFLOW_OP((a).i, -, (b).i) \
111 }while(0) 139 (res).r = (a).r - (b).r; \
112#define C_ADDTO( res , a)\ 140 (res).i = (a).i - (b).i; \
113 do { \ 141 } while (0)
114 CHECK_OVERFLOW_OP((res).r,+,(a).r)\ 142#define C_ADDTO(res, a) \
115 CHECK_OVERFLOW_OP((res).i,+,(a).i)\ 143 do { \
116 (res).r += (a).r; (res).i += (a).i;\ 144 CHECK_OVERFLOW_OP((res).r, +, (a).r) \
117 }while(0) 145 CHECK_OVERFLOW_OP((res).i, +, (a).i) \
118 146 (res).r += (a).r; \
119#define C_SUBFROM( res , a)\ 147 (res).i += (a).i; \
120 do {\ 148 } while (0)
121 CHECK_OVERFLOW_OP((res).r,-,(a).r)\ 149
122 CHECK_OVERFLOW_OP((res).i,-,(a).i)\ 150#define C_SUBFROM(res, a) \
123 (res).r -= (a).r; (res).i -= (a).i; \ 151 do { \
124 }while(0) 152 CHECK_OVERFLOW_OP((res).r, -, (a).r) \
125 153 CHECK_OVERFLOW_OP((res).i, -, (a).i) \
154 (res).r -= (a).r; \
155 (res).i -= (a).i; \
156 } while (0)
126 157
127#ifdef FIXED_POINT 158#ifdef FIXED_POINT
128# define KISS_FFT_COS(phase) floorf(.5+SAMP_MAX * cosf (phase)) 159#define KISS_FFT_COS(phase) floorf(.5 + SAMP_MAX * cosf(phase))
129# define KISS_FFT_SIN(phase) floorf(.5+SAMP_MAX * sinf (phase)) 160#define KISS_FFT_SIN(phase) floorf(.5 + SAMP_MAX * sinf(phase))
130# define HALF_OF(x) ((x)>>1) 161#define HALF_OF(x) ((x) >> 1)
131#elif defined(USE_SIMD) 162#elif defined(USE_SIMD)
132# define KISS_FFT_COS(phase) _mm_set1_ps( cosf(phase) ) 163#define KISS_FFT_COS(phase) _mm_set1_ps(cosf(phase))
133# define KISS_FFT_SIN(phase) _mm_set1_ps( sinf(phase) ) 164#define KISS_FFT_SIN(phase) _mm_set1_ps(sinf(phase))
134# define HALF_OF(x) ((x)*_mm_set1_ps(.5)) 165#define HALF_OF(x) ((x)*_mm_set1_ps(.5))
135#else 166#else
136# define KISS_FFT_COS(phase) (kiss_fft_scalar) cosf(phase) 167#define KISS_FFT_COS(phase) (kiss_fft_scalar) cosf(phase)
137# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sinf(phase) 168#define KISS_FFT_SIN(phase) (kiss_fft_scalar) sinf(phase)
138# define HALF_OF(x) ((x)*.5) 169#define HALF_OF(x) ((x)*.5)
139#endif 170#endif
140 171
141#define kf_cexp(x,phase) \ 172#define kf_cexp(x, phase) \
142 do{ \ 173 do { \
143 (x)->r = KISS_FFT_COS(phase);\ 174 (x)->r = KISS_FFT_COS(phase); \
144 (x)->i = KISS_FFT_SIN(phase);\ 175 (x)->i = KISS_FFT_SIN(phase); \
145 }while(0) 176 } while (0)
146
147 177
148/* a debugging function */ 178/* a debugging function */
149#define pcpx(c)\ 179#define pcpx(c) \
150 fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) 180 fprintf(stderr, "%g + %gi\n", (double)((c)->r), (double)((c)->i))
151
152 181
153#ifdef KISS_FFT_USE_ALLOCA 182#ifdef KISS_FFT_USE_ALLOCA
154// define this to allow use of alloca instead of malloc for temporary buffers 183// define this to allow use of alloca instead of malloc for temporary buffers
155// Temporary buffers are used in two case: 184// Temporary buffers are used in two case:
156// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 185// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5
157// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. 186// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an
187// in-place transform.
158#include <alloca.h> 188#include <alloca.h>
159#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) 189#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes)
160#define KISS_FFT_TMP_FREE(ptr) 190#define KISS_FFT_TMP_FREE(ptr)
161#else 191#else
162#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) 192#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes)
163#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) 193#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr)
164#endif 194#endif