10

On GCC, we enable -ffast-math to speed up floating point calculations. But as we rely on proper behavior of NaN and Inf floating point values, we also turn on -fno-finite-math-only, so that optimization which assume values aren't NaN/Inf

For MSVC, the "equivalent" to -ffast-math is apparently /fp:fast. However, like GCC's -ffast-math, it also includes the optimizations which assume that Nan/Inf aren't present. (Critically, is appears tests like std::isnan() aren't guaranteed to give "accurate" results.)

Is there an option for MSVC C++ compilation which allows you to take advantage of most of the /fp:fast optimizations, but still treats NaN and Inf values "properly"? (Or at the very least, guarantees that tests like std::isnan()/std::isinf() will detect NaN/Inf if they do happen to be generated.)

R.M.
  • 3,461
  • 1
  • 21
  • 41
  • Most likely you won't find such a switch. As far as I know, each switch added to the compiler doubles testing efforts on Microsoft side. That's why they are quite reluctant to adding new switches now. – stgatilov Jan 10 '17 at 16:58

1 Answers1

2
guarantees that tests like std::isnan()/std::isinf()

Unlike GCC, MSVC (CL RC 19) doesn't actually optimize out std::isnan on the /fp:fast setting:

Another alternative that will never be optimized out is to call the C99 isnan or the MSVC intrinsic _isnanf. Or roll your own nan test against a known bit-mask, which can be generated with std::numeric_limits::quiet_NaN.

See: https://godbolt.org/g/YdZJq5

Mikhail
  • 7,749
  • 11
  • 62
  • 136