Specifically, I am asking about the double '!' in the params of the __built_in.
Is it a double negation, per the 'C' language?
Specifically, I am asking about the double '!' in the params of the __built_in.
Is it a double negation, per the 'C' language?
The !!
is simply two !
operators right next to each other. It's a simple way of converting any non-zero value to 1
, and leaving 0
as-is. (Aka "booleanize" the value). See !! c operator, is a two NOT? for more discussion of the logical operator in general, outside the context of __builtin_expect
.
!!(x)
for __builtin_expect
We want to tell the compiler that x
is likely to be non-zero, not that x
is likely to be exactly 1
. It's the difference between telling the compiler to expect x != 0
vs. x == 1
, which might matter for other code in the same function.
We could equivalently have written __builtin_expect( (x)!=0, 1 )
.
The second arg of __builtin_expect
is a value the compiler should expect the first expression to be equal to. This is not restricted to booleans; see How far does GCC's __builtin_expect go? for uses of expected values other than 0 or 1.