FFTW : how to prevent breaking aliasing rules?

若如初见. 提交于 2019-12-10 20:43:46

问题


I have a code which uses the std::complex<double> type. From FFTW Manual :

if you have a variable complex<double> *x, you can pass it directly to FFTW via reinterpret_cast<fftw_complex*>(x).

However, when I do this in my code :

tmp_spectrum = reinterpret_cast<std::complex<double>*>(fftw_alloc_complex(conf.spectrumSize()));
plan_bw_temp = fftw_plan_dft_c2r_1d(conf.FFTSize(), reinterpret_cast<fftw_complex*>(tmp_spectrum), tmp_out, FFTW_ESTIMATE);

I get dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]. How to solve this warning ? Thanks !


回答1:


You have three options here:

  • Just create a fftw_complex when you need one: fftw_plan_dft_c2r_1d(conf.FFTSize(), fftw_complex(tmp_spectrum.real(), tmp_spectrum.imag()), tmp_out, FFTW_ESTIMATE);
  • Don't use the C++ language's complex type in your code, and only use the fftw_complex type.
  • Disable ALL strict-alias optimizations and enforcement in the appropriate translation unit with -fno-strict-aliasing. Silencing just the warning is not safe as it might result in broken code.


来源:https://stackoverflow.com/questions/18408673/fftw-how-to-prevent-breaking-aliasing-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!