likely and unlikely

Ivan Voras ivoras at freebsd.org
Fri Mar 12 12:18:40 UTC 2010


While looking into branch prediction in gcc I found this post describing 
the "likely" and "unlikely" macros in Linux:

http://kerneltrap.org/node/4705

In short, they are wrappers for the gcc-specific __builtin_expect for 
static branch prediction.

Grepping around I see it is defined and used locally in several places:

amd64/include/xen/xen-os.h:#define likely(x)  __builtin_expect((x),1)
amd64/include/xen/xen-os.h:#define unlikely(x)  __builtin_expect((x),0)
dev/nxge/xge-osdep.h:#define xge_os_unlikely(x)    (x)
gnu/fs/xfs/FreeBSD/xfs_compat.h:#define	likely(x)	__builtin_expect((x), 1)
gnu/fs/xfs/FreeBSD/xfs_compat.h:#define	unlikely(x)	__builtin_expect((x), 0)
i386/include/xen/xen-os.h:#define likely(x)  __builtin_expect((x),1)
i386/include/xen/xen-os.h:#define unlikely(x)  __builtin_expect((x),0)
sun4v/include/cpu.h:#define likely(x)  __builtin_expect((x),1)
sun4v/include/cpu.h:#define unlikely(x)  __builtin_expect((x),0)

Wouldn't it be more convenient to have a single global definition of 
them, under #ifdef __GNUC__ for example in sys/stddef.h ?




More information about the freebsd-arch mailing list