[PATCH 2/7] Add 'likely' and 'unlikely' macros

Julien Cristau jcristau at debian.org
Mon May 11 23:29:12 PDT 2015


On Mon, May 11, 2015 at 21:23:53 -0700, Keith Packard wrote:

> These two macros provide hints to the compiler about common code paths
> to help it optimize a bit better.
> 
> Signed-off-by: Keith Packard <keithp at keithp.com>
> ---
>  include/misc.h | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/include/misc.h b/include/misc.h
> index 9b1c03a..74889f5 100644
> --- a/include/misc.h
> +++ b/include/misc.h
> @@ -291,6 +291,14 @@ version_compare(uint32_t a_major, uint32_t a_minor,
>  #define SwapRestL(stuff) \
>      SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff))
>  
> +#if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
> +#define likely(expr) (__builtin_expect (!!(expr), 1))
> +#define unlikely(expr) (__builtin_expect (!!(expr), 0))
> +#else
> +#define likely(expr) (expr)
> +#define unlikely(expr) (expr)
> +#endif
> +
>  #if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
>  void __attribute__ ((error("wrong sized variable passed to swap")))
>  wrong_size(void);

<X11/Xfuncproto.h> defines _X_LIKELY and _X_UNLIKELY, are those not good
enough?

Cheers,
Julien


More information about the xorg-devel mailing list