vaebnkehn
vaebnkehn

Reputation: 113

GCC optimization of __builtin functions used in a macro

If I have a macro like:

#define MAX_SIZE ((1<<18)-1)

I can rest assured that by runtime this math has been done already and MAX_SIZE is a literal.

My question is, what if I use a __builtin function such as:

#define BIT_OFFSET (__builtin_clz(MAX_SIZE))

does this turn into a constant by run time as well?

Upvotes: 2

Views: 745

Answers (1)

Jim Balter
Jim Balter

Reputation: 16424

For some values, some machines, and some versions of some compilers, __builtin_clz(constant) will be evaluated at compile time. But there is no guarantee that it will be evaluated at compile time for all values, all machines, and all compilers. Use gcc -S to see what your compiler does on your target machine.

Upvotes: 4

Related Questions