GCC supports the function __builtin_expect(long exp, long c)
to provide this kind of feature. You can check the documentation here.
Where exp
is the condition used and c
is the expected value. For example in you case you would want
if (__builtin_expect(normal, 1))
Because of the awkward syntax this is usually used by defining two custom macros like
#define likely(x) __builtin_expect (!!(x), 1)
#define unlikely(x) __builtin_expect (!!(x), 0)
just to ease the task.
Mind that:
- this is non standard
- a compiler/cpu branch predictor are likely more skilled than you in deciding such things so this could be a premature micro-optimization