From f78c115f9205c8fef126ba756da62ca79e27a3c9 Mon Sep 17 00:00:00 2001 From: Ralph Tandetzky Date: Thu, 21 Apr 2016 11:41:53 +0200 Subject: [PATCH] Improved code quality. * Replaced int by std::size_t where appropriate. * Made traits member functions static. * Removed redundant data from traits class. * Made local variables const where possible. * Made mutable variables as local as possible. * Removed redundant variables. * Improved white-space formatting for readability (sparingly). --- kissfft.hh | 150 +++++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 74 deletions(-) diff --git a/kissfft.hh b/kissfft.hh index 3ad8554..a38ed73 100644 --- a/kissfft.hh +++ b/kissfft.hh @@ -10,27 +10,29 @@ struct traits { typedef T_scalar scalar_type; typedef std::complex cpx_type; - void fill_twiddles( std::complex * dst ,int nfft,bool inverse) + static void fill_twiddles( cpx_type * dst, + std::size_t nfft, + bool inverse ) { T_scalar phinc = (inverse?2:-2)* acos( (T_scalar) -1) / nfft; - for (int i=0;i(0,i*phinc) ); + for (std::size_t i=0;i > & dst, - int nfft,bool inverse, - std::vector & stageRadix, - std::vector & stageRemainder ) + static void prepare( + std::vector< cpx_type > & _twiddles, + std::size_t nfft, + bool inverse, + std::vector & stageRadix, + std::vector & stageRemainder ) { _twiddles.resize(nfft); fill_twiddles( &_twiddles[0],nfft,inverse); - dst = _twiddles; //factorize //start factoring out 4's, then 2's, then 3,5,7,9,... - int n= nfft; - int p=4; + std::size_t n= nfft; + std::size_t p=4; do { while (n % p) { switch (p) { @@ -39,17 +41,13 @@ struct traits default: p += 2; break; } if (p*p>n) - p=n;// no more factors + p = n;// no more factors } n /= p; stageRadix.push_back(p); stageRemainder.push_back(n); }while(n>1); } - std::vector _twiddles; - - - const cpx_type twiddle(int i) const { return _twiddles[i]; } }; } @@ -64,24 +62,31 @@ class kissfft typedef typename traits_type::scalar_type scalar_type; typedef typename traits_type::cpx_type cpx_type; - kissfft(int nfft,bool inverse,const traits_type & traits=traits_type() ) - :_nfft(nfft),_inverse(inverse),_traits(traits) + kissfft( std::size_t nfft, + bool inverse ) + :_nfft(nfft) + ,_inverse(inverse) { - _traits.prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder); + T_traits::prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder); } - void transform(const cpx_type * src , cpx_type * dst) const + void transform( const cpx_type * src, + cpx_type * dst ) const { kf_work(0, dst, src, 1,1); } private: - void kf_work( int stage,cpx_type * Fout, const cpx_type * f, size_t fstride,size_t in_stride) const + void kf_work( std::size_t stage, + cpx_type * Fout, + const cpx_type * f, + std::size_t fstride, + std::size_t in_stride) const { - int p = _stageRadix[stage]; - int m = _stageRemainder[stage]; - cpx_type * Fout_beg = Fout; - cpx_type * Fout_end = Fout + p*m; + const std::size_t p = _stageRadix[stage]; + const std::size_t m = _stageRemainder[stage]; + cpx_type * const Fout_beg = Fout; + cpx_type * const Fout_end = Fout + p*m; if (m==1) { do{ @@ -121,45 +126,45 @@ class kissfft static scalar_type HALF_OF( const scalar_type & a) { return a*.5;} static void C_MULBYSCALAR(cpx_type & c,const scalar_type & a) {c*=a;} - void kf_bfly2( cpx_type * Fout, const size_t fstride, int m) const + void kf_bfly2( cpx_type * Fout, const size_t fstride, std::size_t m) const { - for (int k=0;k=Norig) twidx-=Norig; + if (twidx>=_nfft) + twidx-=_nfft; + cpx_type t; C_MUL(t,scratchbuf[q] , twiddles[twidx] ); C_ADDTO( Fout[ k ] ,t); } @@ -290,11 +293,10 @@ class kissfft } } - int _nfft; + std::size_t _nfft; bool _inverse; std::vector _twiddles; - std::vector _stageRadix; - std::vector _stageRemainder; - traits_type _traits; + std::vector _stageRadix; + std::vector _stageRemainder; }; #endif