mirror of
https://github.com/mborgerding/kissfft.git
synced 2025-05-27 21:20:27 -04:00
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.
This commit is contained in:
parent
1efe72041e
commit
5ebbc5e618
@ -119,6 +119,52 @@ struct kiss_fft_state{
|
||||
}while(0)
|
||||
|
||||
|
||||
// Normally same as kiss_fft_scalar, but for USE_SIMD this is only a single number
|
||||
#ifndef kiss_fft_scalar_one
|
||||
#define kiss_fft_scalar_one kiss_fft_scalar
|
||||
#endif
|
||||
|
||||
|
||||
// If kiss_fft_suffix is not provided, determine it automatically based on kiss_fft_scalar_one
|
||||
#if !defined (kiss_fft_suffix) && !defined (kiss_fft_suffix_empty)
|
||||
#ifdef FIXED_POINT
|
||||
#undef kiss_fft_suffix
|
||||
#define kiss_fft_suffix_empty 1
|
||||
#endif
|
||||
|
||||
#define KISS_X_float_X 1
|
||||
#define KISS_X__X 2
|
||||
#define KISS_X_long_X 3
|
||||
#define KISS_concat2(a, b, c) a##b##c
|
||||
#define KISS_concat(a, b, c) KISS_concat2(a, b, c)
|
||||
|
||||
#define double // This is because otherwise KISS_concat(KISS_X_, long double, _X) would produce two tokens which would break the preprocessor comparison
|
||||
|
||||
#if KISS_concat(KISS_X_, kiss_fft_scalar_one, _X) == KISS_X_float_X // float
|
||||
#define kiss_fft_suffix f
|
||||
#elif KISS_concat(KISS_X_, kiss_fft_scalar_one, _X) == KISS_X__X // double
|
||||
#undef kiss_fft_suffix
|
||||
#define kiss_fft_suffix_empty 1
|
||||
#elif KISS_concat(KISS_X_, kiss_fft_scalar_one, _X) == KISS_X_long_X // long double
|
||||
#define kiss_fft_suffix l
|
||||
#else
|
||||
#undef kiss_fft_suffix
|
||||
#define kiss_fft_suffix_empty 1
|
||||
#warning "Unknown kiss_fft_scalar type"
|
||||
#endif
|
||||
|
||||
#undef double
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef kiss_fft_suffix_empty
|
||||
#define KISS_ADD_SUFFIX(x) x
|
||||
#else
|
||||
#define KISS_ADD_SUFFIX2(x, y) x##y
|
||||
#define KISS_ADD_SUFFIX1(x, y) KISS_ADD_SUFFIX2(x, y)
|
||||
#define KISS_ADD_SUFFIX(x) KISS_ADD_SUFFIX1(x, kiss_fft_suffix)
|
||||
#endif
|
||||
|
||||
#ifdef FIXED_POINT
|
||||
# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
|
||||
# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
|
||||
@ -128,9 +174,9 @@ struct kiss_fft_state{
|
||||
# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
|
||||
# define HALF_OF(x) ((x)*_mm_set1_ps(.5))
|
||||
#else
|
||||
# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
|
||||
# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
|
||||
# define HALF_OF(x) ((x)*.5)
|
||||
# define KISS_FFT_COS(phase) (kiss_fft_scalar) KISS_ADD_SUFFIX(cos)(phase)
|
||||
# define KISS_FFT_SIN(phase) (kiss_fft_scalar) KISS_ADD_SUFFIX(sin)(phase)
|
||||
# define HALF_OF(x) ((x)*KISS_ADD_SUFFIX(.5))
|
||||
#endif
|
||||
|
||||
#define kf_cexp(x,phase) \
|
||||
|
@ -349,8 +349,8 @@ kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem
|
||||
st->inverse = inverse_fft;
|
||||
|
||||
for (i=0;i<nfft;++i) {
|
||||
const double pi=3.141592653589793238462643383279502884197169399375105820974944;
|
||||
double phase = -2*pi*i / nfft;
|
||||
const kiss_fft_scalar_one pi=KISS_ADD_SUFFIX(3.141592653589793238462643383279502884197169399375105820974944);
|
||||
kiss_fft_scalar_one phase = -2*pi*i / nfft;
|
||||
if (st->inverse)
|
||||
phase *= -1;
|
||||
kf_cexp(st->twiddles+i, phase );
|
||||
|
@ -35,6 +35,8 @@ extern "C" {
|
||||
#ifdef USE_SIMD
|
||||
# include <xmmintrin.h>
|
||||
# define kiss_fft_scalar __m128
|
||||
# define kiss_fft_scalar_one double
|
||||
//# define kiss_fft_scalar_one float
|
||||
# ifndef KISS_FFT_MALLOC
|
||||
# define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16)
|
||||
# endif
|
||||
@ -55,8 +57,10 @@ extern "C" {
|
||||
#include <stdint.h>
|
||||
# if (FIXED_POINT == 32)
|
||||
# define kiss_fft_scalar int32_t
|
||||
# define kiss_fft_scalar_one double
|
||||
# else
|
||||
# define kiss_fft_scalar int16_t
|
||||
# define kiss_fft_scalar_one double
|
||||
# endif
|
||||
#else
|
||||
# ifndef kiss_fft_scalar
|
||||
|
@ -49,8 +49,8 @@ kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenme
|
||||
kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize);
|
||||
|
||||
for (i = 0; i < nfft/2; ++i) {
|
||||
double phase =
|
||||
-3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5);
|
||||
kiss_fft_scalar_one phase =
|
||||
KISS_ADD_SUFFIX(-3.14159265358979323846264338327) * ((kiss_fft_scalar_one) (i+1) / nfft + .5);
|
||||
if (inverse_fft)
|
||||
phase *= -1;
|
||||
kf_cexp (st->super_twiddles+i,phase);
|
||||
|
Loading…
Reference in New Issue
Block a user