···1515#define _kiss_fft_guts_h
16161717#include "kiss_fft.h"
1818+#include "kiss_fft_log.h"
1819#include <limits.h>
19202021#define MAXFACTORS 32
2121-/* e.g. an fft of length 128 has 4 factors
2222+/* e.g. an fft of length 128 has 4 factors
2223 as far as kissfft is concerned
2324 4*4*4*2
2425 */
···5657#if defined(CHECK_OVERFLOW)
5758# define CHECK_OVERFLOW_OP(a,op,b) \
5859 if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \
5959- fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); }
6060+ KISS_FFT_WARNING("overflow (%d " #op" %d) = %ld", (a),(b),(SAMPPROD)(a) op (SAMPPROD)(b)); }
6061#endif
61626263···146147147148/* a debugging function */
148149#define pcpx(c)\
149149- fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) )
150150+ KISS_FFT_DEBUG("%g + %gi\n",(double)((c)->r),(double)((c)->i))
150151151152152153#ifdef KISS_FFT_USE_ALLOCA
153154// define this to allow use of alloca instead of malloc for temporary buffers
154154-// Temporary buffers are used in two case:
155155+// Temporary buffers are used in two case:
155156// 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5
156157// 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform.
157158#include <alloca.h>
158159#define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes)
159159-#define KISS_FFT_TMP_FREE(ptr)
160160+#define KISS_FFT_TMP_FREE(ptr)
160161#else
161162#define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes)
162163#define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr)
+26-10
kiss_fft.c
···203203 int Norig = st->nfft;
204204205205 kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p);
206206+ if (scratch == NULL){
207207+ KISS_FFT_ERROR("Memory allocation failed.");
208208+ return;
209209+ }
206210207211 for ( u=0; u<m; ++u ) {
208212 k=u;
···244248 const kiss_fft_cpx * Fout_end = Fout + p*m;
245249246250#ifdef _OPENMP
247247- // use openmp extensions at the
251251+ // use openmp extensions at the
248252 // top-level (not recursive)
249253 if (fstride==1 && p<=5 && m!=1)
250254 {
···252256253257 // execute the p different work units in different threads
254258# pragma omp parallel for
255255- for (k=0;k<p;++k)
259259+ for (k=0;k<p;++k)
256260 kf_work( Fout +k*m, f+ fstride*in_stride*k,fstride*p,in_stride,factors,st);
257261 // all threads have joined by this point
258262259263 switch (p) {
260264 case 2: kf_bfly2(Fout,fstride,st,m); break;
261261- case 3: kf_bfly3(Fout,fstride,st,m); break;
265265+ case 3: kf_bfly3(Fout,fstride,st,m); break;
262266 case 4: kf_bfly4(Fout,fstride,st,m); break;
263263- case 5: kf_bfly5(Fout,fstride,st,m); break;
267267+ case 5: kf_bfly5(Fout,fstride,st,m); break;
264268 default: kf_bfly_generic(Fout,fstride,st,m,p); break;
265269 }
266270 return;
···276280 do{
277281 // recursive call:
278282 // DFT of size m*p performed by doing
279279- // p instances of smaller DFTs of size m,
283283+ // p instances of smaller DFTs of size m,
280284 // each one takes a decimated version of the input
281285 kf_work( Fout , f, fstride*p, in_stride, factors,st);
282286 f += fstride*in_stride;
···285289286290 Fout=Fout_beg;
287291288288- // recombine the p smaller DFTs
292292+ // recombine the p smaller DFTs
289293 switch (p) {
290294 case 2: kf_bfly2(Fout,fstride,st,m); break;
291291- case 3: kf_bfly3(Fout,fstride,st,m); break;
295295+ case 3: kf_bfly3(Fout,fstride,st,m); break;
292296 case 4: kf_bfly4(Fout,fstride,st,m); break;
293293- case 5: kf_bfly5(Fout,fstride,st,m); break;
297297+ case 5: kf_bfly5(Fout,fstride,st,m); break;
294298 default: kf_bfly_generic(Fout,fstride,st,m,p); break;
295299 }
296300}
297301298302/* facbuf is populated by p1,m1,p2,m2, ...
299299- where
303303+ where
300304 p[i] * m[i] = m[i-1]
301305 m0 = n */
302302-static
306306+static
303307void kf_factor(int n,int * facbuf)
304308{
305309 int p=4;
···369373 if (fin == fout) {
370374 //NOTE: this is not really an in-place FFT algorithm.
371375 //It just performs an out-of-place FFT into a temp buffer
376376+ if (fout == NULL){
377377+ KISS_FFT_ERROR("fout buffer NULL.");
378378+ return;
379379+ }
380380+372381 kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft);
382382+ if (tmpbuf == NULL){
383383+ KISS_FFT_ERROR("Memory allocation error.");
384384+ return;
385385+ }
386386+387387+388388+373389 kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
374390 memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
375391 KISS_FFT_TMP_FREE(tmpbuf);
+36
kiss_fft_log.h
···11+/*
22+ * Copyright (c) 2003-2010, Mark Borgerding. All rights reserved.
33+ * This file is part of KISS FFT - https://github.com/mborgerding/kissfft
44+ *
55+ * SPDX-License-Identifier: BSD-3-Clause
66+ * See COPYING file for more information.
77+ */
88+99+#ifndef kiss_fft_log_h
1010+#define kiss_fft_log_h
1111+1212+#define ERROR 1
1313+#define WARNING 2
1414+#define INFO 3
1515+#define DEBUG 4
1616+1717+#define STRINGIFY(x) #x
1818+#define TOSTRING(x) STRINGIFY(x)
1919+2020+#if defined(NDEBUG)
2121+# define KISS_FFT_LOG_MSG(severity, ...) ((void)0)
2222+#else
2323+# define KISS_FFT_LOG_MSG(severity, ...) \
2424+ fprintf(stderr, "[" #severity "] " __FILE__ ":" TOSTRING(__LINE__) " "); \
2525+ fprintf(stderr, __VA_ARGS__); \
2626+ fprintf(stderr, "\n")
2727+#endif
2828+2929+#define KISS_FFT_ERROR(...) KISS_FFT_LOG_MSG(ERROR, __VA_ARGS__)
3030+#define KISS_FFT_WARNING(...) KISS_FFT_LOG_MSG(WARNING, __VA_ARGS__)
3131+#define KISS_FFT_INFO(...) KISS_FFT_LOG_MSG(INFO, __VA_ARGS__)
3232+#define KISS_FFT_DEBUG(...) KISS_FFT_LOG_MSG(DEBUG, __VA_ARGS__)
3333+3434+3535+3636+#endif /* kiss_fft_log_h */
+9-9
tools/kiss_fftr.c
···2727 size_t subsize = 0, memneeded;
28282929 if (nfft & 1) {
3030- fprintf(stderr,"Real FFT optimization must be even.\n");
3030+ KISS_FFT_ERROR("Real FFT optimization must be even.");
3131 return NULL;
3232 }
3333 nfft >>= 1;
···6767 kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc;
68686969 if ( st->substate->inverse) {
7070- fprintf(stderr,"kiss fft usage error: improper alloc\n");
7070+ KISS_FFT_ERROR("kiss fft usage error: improper alloc");
7171 exit(1);
7272 }
7373···7979 * contains the sum of the even-numbered elements of the input time sequence
8080 * The imag part is the sum of the odd-numbered elements
8181 *
8282- * The sum of tdc.r and tdc.i is the sum of the input time sequence.
8282+ * The sum of tdc.r and tdc.i is the sum of the input time sequence.
8383 * yielding DC of input time sequence
8484- * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
8484+ * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1...
8585 * yielding Nyquist bin of input time sequence
8686 */
8787-8787+8888 tdc.r = st->tmpbuf[0].r;
8989 tdc.i = st->tmpbuf[0].i;
9090 C_FIXDIV(tdc,2);
···9292 CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i);
9393 freqdata[0].r = tdc.r + tdc.i;
9494 freqdata[ncfft].r = tdc.r - tdc.i;
9595-#ifdef USE_SIMD
9595+#ifdef USE_SIMD
9696 freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0);
9797#else
9898 freqdata[ncfft].i = freqdata[0].i = 0;
9999#endif
100100101101 for ( k=1;k <= ncfft/2 ; ++k ) {
102102- fpk = st->tmpbuf[k];
102102+ fpk = st->tmpbuf[k];
103103 fpnk.r = st->tmpbuf[ncfft-k].r;
104104 fpnk.i = - st->tmpbuf[ncfft-k].i;
105105 C_FIXDIV(fpk,2);
···122122 int k, ncfft;
123123124124 if (st->substate->inverse == 0) {
125125- fprintf (stderr, "kiss fft usage error: improper alloc\n");
125125+ KISS_FFT_ERROR("kiss fft usage error: improper alloc");
126126 exit (1);
127127 }
128128···145145 C_MUL (fok, tmp, st->super_twiddles[k-1]);
146146 C_ADD (st->tmpbuf[k], fek, fok);
147147 C_SUB (st->tmpbuf[ncfft - k], fek, fok);
148148-#ifdef USE_SIMD
148148+#ifdef USE_SIMD
149149 st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0);
150150#else
151151 st->tmpbuf[ncfft - k].i *= -1;