1

I was going through the GCC manual http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

I had the following question about builtin_expect

long __builtin_expect (long exp, long c)

Why is the exp of long type when the type should have been bool ?

For example:The kernel defines an macro for converting the expression to bool

define likely(x) __builtin_expect(!!(x), 1)

Then why not define an interface in which exp is bool rather than long ?

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
KodeWarrior
  • 3,538
  • 3
  • 26
  • 40

1 Answers1

0

__builtin_expect returns the value of exp so for consistency it should return it as the same type as exp, too.

The choice of long (if it really still is the case and the documentation is not just lacking behind) is probably historical. What I would expect from such a feature is to leave expression exp completely unchanged in type and value. This feature should only "annotate" the code with a hint to the compiler and not change the semantic if e.g the type of exp would be unsigned long.

BTW, the expression !!(x) in the kernel macro doesn't have type _Bool but is int, and that example is just a particular use case where the exp only may have two outcomes. The feature itself is more general than that.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177