FastLED 3.9.15
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
9#pragma once
10
11/* kiss_fft.h
12 defines kiss_fft_scalar as either short or a float type
13 and defines
14 typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
15#include "kiss_fft.h"
16#include <limits.h>
17
18#define MAXFACTORS 32
19/* e.g. an fft of length 128 has 4 factors
20 as far as kissfft is concerned
21 4*4*4*2
22 */
23
30
31/*
32 Explanation of macros dealing with complex math:
33
34 C_MUL(m,a,b) : m = a*b
35 C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise
36 C_SUB( res, a,b) : res = a - b
37 C_SUBFROM( res , a) : res -= a
38 C_ADDTO( res , a) : res += a
39 * */
40#ifdef FIXED_POINT
41#if (FIXED_POINT==32)
42# define FRACBITS 31
43# define SAMPPROD int64_t
44#define SAMP_MAX 2147483647
45#else
46# define FRACBITS 15
47# define SAMPPROD int32_t
48#define SAMP_MAX 32767
49#endif
50
51#define SAMP_MIN -SAMP_MAX
52
53#if defined(CHECK_OVERFLOW)
54# define CHECK_OVERFLOW_OP(a,op,b) \
55 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
56 fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
57#endif
58
59
60# define smul(a,b) ( (SAMPPROD)(a)*(b) )
61# define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS )
62
63# define S_MUL(a,b) sround( smul(a,b) )
64
65# define C_MUL(m,a,b) \
66 do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \
67 (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0)
68
69# define DIVSCALAR(x,k) \
70 (x) = sround( smul( x, SAMP_MAX/k ) )
71
72# define C_FIXDIV(c,div) \
73 do { DIVSCALAR( (c).r , div); \
74 DIVSCALAR( (c).i , div); }while (0)
75
76# define C_MULBYSCALAR( c, s ) \
77 do{ (c).r = sround( smul( (c).r , s ) ) ;\
78 (c).i = sround( smul( (c).i , s ) ) ; }while(0)
79
80#else /* not FIXED_POINT*/
81
82# define S_MUL(a,b) ( (a)*(b) )
83#define C_MUL(m,a,b) \
84 do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
85 (m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
86# define C_FIXDIV(c,div) /* NOOP */
87# define C_MULBYSCALAR( c, s ) \
88 do{ (c).r *= (s);\
89 (c).i *= (s); }while(0)
90#endif
91
92#ifndef CHECK_OVERFLOW_OP
93# define CHECK_OVERFLOW_OP(a,op,b) /* noop */
94#endif
95
96#define C_ADD( res, a,b)\
97 do { \
98 CHECK_OVERFLOW_OP((a).r,+,(b).r)\
99 CHECK_OVERFLOW_OP((a).i,+,(b).i)\
100 (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \
101 }while(0)
102#define C_SUB( res, a,b)\
103 do { \
104 CHECK_OVERFLOW_OP((a).r,-,(b).r)\
105 CHECK_OVERFLOW_OP((a).i,-,(b).i)\
106 (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \
107 }while(0)
108#define C_ADDTO( res , a)\
109 do { \
110 CHECK_OVERFLOW_OP((res).r,+,(a).r)\
111 CHECK_OVERFLOW_OP((res).i,+,(a).i)\
112 (res).r += (a).r; (res).i += (a).i;\
113 }while(0)
114
115#define C_SUBFROM( 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
123#ifdef FIXED_POINT
124# define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase))
125# define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase))
126# define HALF_OF(x) ((x)>>1)
127#elif defined(USE_SIMD)
128# define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) )
129# define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) )
130# define HALF_OF(x) ((x)*_mm_set1_ps(.5))
131#else
132# define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase)
133# define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase)
134# define HALF_OF(x) ((x)*.5)
135#endif
136
137#define kf_cexp(x,phase) \
138 do{ \
139 (x)->r = KISS_FFT_COS(phase);\
140 (x)->i = KISS_FFT_SIN(phase);\
141 }while(0)
142
143
144/* a debugging function */
145#define pcpx(c)\
146 fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )
147
148
149#ifdef KISS_FFT_USE_ALLOCA
150// define this to allow use of alloca instead of malloc for temporary buffers
151// Temporary buffers are used in two case:
152// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5
153// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform.
154#include <alloca.h>
155#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes)
156#define KISS_FFT_TMP_FREE(ptr)
157#else
158#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes)
159#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr)
160#endif
#define MAXFACTORS
int factors[2 *MAXFACTORS]
kiss_fft_cpx twiddles[1]