Search code examples
cgccmacrosbuilt-in

How to check __builtin_ function is available on gcc


I need to know is there a method for gcc to check presence of those awesome __builtin_MY_DESIRED_FUNCTIONs

For example, I'd like to use __builtin_nan and be sure it is available for my program and it won't fail during compilation time.

I'll be more specific: on clang there is __has_builtin "checker" so we can write smth like

#if __has_builtin(__builtin_nan)

But I can't find analog for gcc.

And probably I can rely just on gcc, like "Oh, I'm on gcc now, just let's assume all of those __builtin_ are here like in example below..."

#if __GNUC__
double mynan = __builtin_nan("0");
#endif

And probably it will work, till someone put this "-fno-builtin" compilation flag.


Solution

  • No, you will have to use __GNUC__ and __GNUC_MINOR__ (and __GNUC_PATCHLEVEL__ if you use such gcc versions) to test for each release specific builtin function (gcc releases can be found here)

    For example:

    /* __builtin_mul_overflow_p added in gcc 7.4 */
    #if (__GNUC__ > 7) || \
             ((__GNUC__ == 7) && (__GNUC_MINOR__ > 3))
    #define BUILTIN_MUL_OVERFLOW_EXIST
    #endif
    
    #ifdef BUILTIN_MUL_OVERFLOW_EXIST
    int c = __builtin_mul_overflow_p (3, 2, 3) ? 0 : 3 * 2;
    #endif
    

    And there is an open bug for exactly what you are asking about, in here.