Why does GCC implement isnan() more efficiently for C++ than C <math.h>?

前端 未结 1 1425
醉梦人生
醉梦人生 2020-12-13 17:22

Here\'s my code:

int f(double x)
{
  return isnan(x);
}

If I #include I get this assembly:

xorl          


        
相关标签:
1条回答
  • 2020-12-13 18:05

    Looking at <cmath> for libstdc++ shipped with gcc 4.9 you get this:

      constexpr bool
      isnan(double __x)
      { return __builtin_isnan(__x); }
    

    A constexpr function could be aggressively inlined and, of course, the function just delegates the work over to __builtin_isnan.

    The <math.h> header doesn't use __builtin_isnan, rather it uses an __isnan implementation which is kind of long to paste here but it's lines 430 of math.h on my machine™. Since the C99 standard requires using a macro for isnan et al (section 7.12 of the C99 standard) the 'function' is defined as follows:

    #define isnan(x) (sizeof (x) == sizeof (float) ? __isnanf (x)   \
      : sizeof (x) == sizeof (double) ? __isnan (x) \
      : __isnanl (x))
    

    However, I see no reason why it can't use __builtin_isnan instead of __isnan so I suspect it's an oversight. As Marc Glisse points out in the comments, there is a relevant bug report for a similar issue using isinf instead of isnan.

    0 讨论(0)
提交回复
热议问题