Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 823
Kurz Avatar asked Jan 07 '19 17:01

Kurz


People also ask

What is __ Builtin_ffs?

Built-in Function: int __builtin_ffs (int x) Returns one plus the index of the least significant 1-bit of x , or if x is zero, returns zero. Built-in Function: int __builtin_clz (unsigned int x) Returns the number of leading 0-bits in x , starting at the most significant bit position.

What is __ Builtin_expect in C?

You can use the __builtin_expect built-in function to indicate that an expression is likely to evaluate to a specified value. The compiler can use this knowledge to direct optimizations. This built-in function is portable with the GNU C/C++ __builtin_expect function.

What is the function of GCC?

GCC allows the declaration of nested functions (a function defined inside another function). The nested function's name will be local to the block where it is defined and can access all the variables of the containing function that are visible at the point of its definition.

What is __ Pretty_function __?

The identifier __PRETTY_FUNCTION__ holds the name of the function pretty printed in a language specific fashion. These names are always the same in a C function, but in a C++ function they may be different. For example, this program: extern "C" { extern int printf (char *, ...


1 Answers

Good news! a __has_builtin was added in GCC 10 (see change notes):

The special operator __has_builtin (operand) may be used in constant integer contexts and in preprocessor ‘#if’ and ‘#elif’ expressions to test whether the symbol named by its operand is recognized as a built-in function by GCC in the current language and conformance mode. It evaluates to a constant integer with a nonzero value if the argument refers to such a function, and to zero otherwise. The operator may also be used in preprocessor ‘#if’ and ‘#elif’ expressions. The __has_builtin operator by itself, without any operand or parentheses, acts as a predefined macro so that support for it can be tested in portable code. Thus, the recommended use of the operator is as follows:

#if defined __has_builtin
#  if __has_builtin (__builtin_object_size)
#    define builtin_object_size(ptr) __builtin_object_size (ptr, 2)
#  endif
#endif
#ifndef builtin_object_size
#  define builtin_object_size(ptr)   ((size_t)-1)
#endif
like image 192
Adam.Er8 Avatar answered Oct 15 '22 02:10

Adam.Er8