FFTW : how to prevent breaking aliasing rules?

272 Views Asked by At

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

There are 1 best solutions below

1
On

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.