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