Skip to content

Commit 63a6f80

Browse files
committed
Allow setting a suffix for constants and trigonometric functions
In order to use constants or trigonometric functions with a type other than double, a suffix ('f' for float or 'l' for long double) has to be used in C. This commit adds a preprocessor macro 'kiss_fft_suffix' which can be set to either 'f' or 'l' and which will be added to floating point constants and to the trigonometric functions (sin and cos). Without this suffix, the code will use a too high precision for float and a too low precision for long double.
1 parent 1efe720 commit 63a6f80

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

_kiss_fft_guts.h

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,46 @@ struct kiss_fft_state{
119119
}while(0)
120120

121121

122+
// If kiss_fft_suffix is not provided, determine it automatically based on kiss_fft_scalar
123+
#if !defined (kiss_fft_suffix) && !defined (kiss_fft_suffix_empty)
124+
#ifdef FIXED_POINT
125+
#undef kiss_fft_suffix
126+
#define kiss_fft_suffix_empty 1
127+
#endif
128+
129+
#define KISS_X_float_X 1
130+
#define KISS_X__X 2
131+
#define KISS_X_long_X 3
132+
#define KISS_concat2(a, b, c) a##b##c
133+
#define KISS_concat(a, b, c) KISS_concat2(a, b, c)
134+
135+
#define double // This is because otherwise KISS_concat(KISS_X_, long double, _X) would produce two tokens which would break the preprocessor comparison
136+
137+
#if KISS_concat(KISS_X_, kiss_fft_scalar, _X) == KISS_X_float_X // float
138+
#define kiss_fft_suffix f
139+
#elif KISS_concat(KISS_X_, kiss_fft_scalar, _X) == KISS_X__X // double
140+
#undef kiss_fft_suffix
141+
#define kiss_fft_suffix_empty 1
142+
#elif KISS_concat(KISS_X_, kiss_fft_scalar, _X) == KISS_X_long_X // long double
143+
#define kiss_fft_suffix l
144+
#else
145+
#undef kiss_fft_suffix
146+
#define kiss_fft_suffix_empty 1
147+
#warning "Unknown kiss_fft_scalar type"
148+
#endif
149+
150+
#undef double
151+
#endif
152+
153+
154+
#ifdef kiss_fft_suffix_empty
155+
#define KISS_ADD_SUFFIX(x) x
156+
#else
157+
#define KISS_ADD_SUFFIX2(x, y) x##y
158+
#define KISS_ADD_SUFFIX1(x, y) KISS_ADD_SUFFIX2(x, y)
159+
#define KISS_ADD_SUFFIX(x) KISS_ADD_SUFFIX1(x, kiss_fft_suffix)
160+
#endif
161+
122162
#ifdef FIXED_POINT
123163
# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
124164
# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
@@ -128,9 +168,9 @@ struct kiss_fft_state{
128168
# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
129169
# define HALF_OF(x) ((x)*_mm_set1_ps(.5))
130170
#else
131-
# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
132-
# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
133-
# define HALF_OF(x) ((x)*.5)
171+
# define KISS_FFT_COS(phase) (kiss_fft_scalar) KISS_ADD_SUFFIX(cos)(phase)
172+
# define KISS_FFT_SIN(phase) (kiss_fft_scalar) KISS_ADD_SUFFIX(sin)(phase)
173+
# define HALF_OF(x) ((x)*KISS_ADD_SUFFIX(.5))
134174
#endif
135175

136176
#define kf_cexp(x,phase) \

kiss_fft.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,8 @@ kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem
349349
st->inverse = inverse_fft;
350350

351351
for (i=0;i<nfft;++i) {
352-
const double pi=3.141592653589793238462643383279502884197169399375105820974944;
353-
double phase = -2*pi*i / nfft;
352+
const kiss_fft_scalar pi=KISS_ADD_SUFFIX(3.141592653589793238462643383279502884197169399375105820974944);
353+
kiss_fft_scalar phase = -2*pi*i / nfft;
354354
if (st->inverse)
355355
phase *= -1;
356356
kf_cexp(st->twiddles+i, phase );

tools/kiss_fftr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenme
4949
kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
5050

5151
for (i = 0; i < nfft/2; ++i) {
52-
double phase =
53-
-3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
52+
kiss_fft_scalar phase =
53+
KISS_ADD_SUFFIX(-3.14159265358979323846264338327) * ((kiss_fft_scalar) (i+1) / nfft + .5);
5454
if (inverse_fft)
5555
phase *= -1;
5656
kf_cexp (st->super_twiddles+i,phase);

0 commit comments

Comments
 (0)