http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50724
--- Comment #35 from Ethan Tira-Thompson <ejtttje at gmail dot com> 2011-10-18 21:09:07 UTC --- Thanks all for the info! I should have realized there was literally an attribute/pragma called 'optimize' (duh), and it's already in the docs... for some reason I had gotten the impression this was a brand new development (i.e. hadn't been released yet), I should've checked. FYI, the optimize attribute seems to work for fast-math, but interestingly, the pragma doesn't. I've created a new bug 50782 for this. > change RTL optimizers to not do comparison code folding based on > flag_finite_math_only (so you can expand isnan to !(x==x) even with > -ffinite-math-only) What would you say to a solution which allows finite math to optimize the comparisons, but at the cost of explicit classification being full function calls? And of course when finite math is not in effect, everything is inlined as normal. This would allow core computations to be fully optimized and only explicit classification calls would be affected. This presumes the classification calls are less common in order to be a good tradeoff, my intuition is that this is the case. It also allows those of you who want to optimize-away nan checks to continue to do so, just use a (x!=x), and as a bonus this approach will also work consistently between gcc variants. What do you think? This is also easy to implement without touching the compiler source, just apply attributes in libstdc++ to keep the classification calls no-finite-math-only, for example the isnan implementation would be: #if defined(__FINITE_MATH_ONLY__) && __FINITE_MATH_ONLY__ // apply attributes to retain classification functionality template<typename _Tp> inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value, int>::__type isnan(_Tp __f) __attribute__ ((optimize ("no-finite-math-only"),noinline)); #endif template<typename _Tp> inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value, int>::__type isnan(_Tp __f) { return std::__is_integer<_Tp>::__value ? false : __builtin_isnan(__f); } (and obviously similar for fpclassify, isfinite, isinf, and isnormal) I tweaked isnan to short circuit on __is_integer instead of a roundabout promotion to double. If you don't like that it can certainly go back to the promotion style. This works (tested with 4.6.1) because __builtin_isnan is expanded in the isnan context, where the optimize attribute is in effect. If you think that expansion behavior is subject to change (see also bug 50782), we could bump this up to apply to the builtin itself instead of the user function...? As written, this relies on the noinline attribute trumping the inline keyword. We rewrite this to avoid that conflict if needed. (Is the inline keyword superfluous here anyway? Testing it appears so.)