Skip to content

Commit 5ebbc5e

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 5ebbc5e

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

_kiss_fft_guts.h

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

121121

122+
// Normally same as kiss_fft_scalar, but for USE_SIMD this is only a single number
123+
#ifndef kiss_fft_scalar_one
124+
#define kiss_fft_scalar_one kiss_fft_scalar
125+
#endif
126+
127+
128+
// If kiss_fft_suffix is not provided, determine it automatically based on kiss_fft_scalar_one
129+
#if !defined (kiss_fft_suffix) && !defined (kiss_fft_suffix_empty)
130+
#ifdef FIXED_POINT
131+
#undef kiss_fft_suffix
132+
#define kiss_fft_suffix_empty 1
133+
#endif
134+
135+
#define KISS_X_float_X 1
136+
#define KISS_X__X 2
137+
#define KISS_X_long_X 3
138+
#define KISS_concat2(a, b, c) a##b##c
139+
#define KISS_concat(a, b, c) KISS_concat2(a, b, c)
140+
141+
#define double // This is because otherwise KISS_concat(KISS_X_, long double, _X) would produce two tokens which would break the preprocessor comparison
142+
143+
#if KISS_concat(KISS_X_, kiss_fft_scalar_one, _X) == KISS_X_float_X // float
144+
#define kiss_fft_suffix f
145+
#elif KISS_concat(KISS_X_, kiss_fft_scalar_one, _X) == KISS_X__X // double
146+
#undef kiss_fft_suffix
147+
#define kiss_fft_suffix_empty 1
148+
#elif KISS_concat(KISS_X_, kiss_fft_scalar_one, _X) == KISS_X_long_X // long double
149+
#define kiss_fft_suffix l
150+
#else
151+
#undef kiss_fft_suffix
152+
#define kiss_fft_suffix_empty 1
153+
#warning "Unknown kiss_fft_scalar type"
154+
#endif
155+
156+
#undef double
157+
#endif
158+
159+
160+
#ifdef kiss_fft_suffix_empty
161+
#define KISS_ADD_SUFFIX(x) x
162+
#else
163+
#define KISS_ADD_SUFFIX2(x, y) x##y
164+
#define KISS_ADD_SUFFIX1(x, y) KISS_ADD_SUFFIX2(x, y)
165+
#define KISS_ADD_SUFFIX(x) KISS_ADD_SUFFIX1(x, kiss_fft_suffix)
166+
#endif
167+
122168
#ifdef FIXED_POINT
123169
# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
124170
# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
@@ -128,9 +174,9 @@ struct kiss_fft_state{
128174
# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
129175
# define HALF_OF(x) ((x)*_mm_set1_ps(.5))
130176
#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)
177+
# define KISS_FFT_COS(phase) (kiss_fft_scalar) KISS_ADD_SUFFIX(cos)(phase)
178+
# define KISS_FFT_SIN(phase) (kiss_fft_scalar) KISS_ADD_SUFFIX(sin)(phase)
179+
# define HALF_OF(x) ((x)*KISS_ADD_SUFFIX(.5))
134180
#endif
135181

136182
#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_one pi=KISS_ADD_SUFFIX(3.141592653589793238462643383279502884197169399375105820974944);
353+
kiss_fft_scalar_one phase = -2*pi*i / nfft;
354354
if (st->inverse)
355355
phase *= -1;
356356
kf_cexp(st->twiddles+i, phase );

kiss_fft.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ extern "C" {
3535
#ifdef USE_SIMD
3636
# include <xmmintrin.h>
3737
# define kiss_fft_scalar __m128
38+
# define kiss_fft_scalar_one double
39+
//# define kiss_fft_scalar_one float
3840
# ifndef KISS_FFT_MALLOC
3941
# define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16)
4042
# endif
@@ -55,8 +57,10 @@ extern "C" {
5557
#include <stdint.h>
5658
# if (FIXED_POINT == 32)
5759
# define kiss_fft_scalar int32_t
60+
# define kiss_fft_scalar_one double
5861
# else
5962
# define kiss_fft_scalar int16_t
63+
# define kiss_fft_scalar_one double
6064
# endif
6165
#else
6266
# ifndef kiss_fft_scalar

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_one phase =
53+
KISS_ADD_SUFFIX(-3.14159265358979323846264338327) * ((kiss_fft_scalar_one) (i+1) / nfft + .5);
5454
if (inverse_fft)
5555
phase *= -1;
5656
kf_cexp (st->super_twiddles+i,phase);

0 commit comments

Comments
 (0)