Use and misuse of __builtin_expect

Denis Efremov efremov@linux.com
Wed Aug 28 15:10:00 GMT 2019


On 8/26/19 3:59 PM, Denis Efremov wrote:
> Hi,
> 
> Is it correct to use __builtin_expect these 2 ways?
> 
> #define likely(x)	__builtin_expect(!!(x), 1)
> #define unlikely(x)	__builtin_expect(!!(x), 0)
> 
> 1) Negation before likely/unlikely.
>    if (!unlikely(cond)) { ... }
>    if (!likely(cond)) { ... }
> 
> I've replaced all !unlikely to likely and vice versa in the Linux
> and obtained different binary results:
> 

Ah, this is incorrect. It's wrong to replace !unlikely to likely
and vice versa. The negation could not be dropped. It's possible
to replace !likely(x) with unlikely(!x).

> 
> 2) As a part of the condition.
>    if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) { ... }
>    if (unlikely(info->thread_notes == 0) || unlikely(view->regsets[0].core_note_type != NT_PRSTATUS)) { ... }
>    if (*s && unlikely(!d_can_lookup(root)) { ... }
> 
> Thanks,
> Denis
> 



More information about the Gcc-help mailing list