Kernel’s macro: likely() and unlikely()

Linux kernel has a optimized way to check for validity of conditions. They are likely(x) and unlikely(x) defined macros.

#define likely(x)      __builtin_expect(!!(x), 1)
#define unlikely(x)    __builtin_expect(!!(x), 0)

This may see little confuse, but the general ideal that I keep in mind is: likely(x) means “I expect x to be true”, and unlikely(x) means “I expect x to be false”. This helps me when I use these macros. I’m not sure how much these bultin macros can improve performance, but as we can see in many piece of kernel’s code, I think they can help a lot. (I’m quite newbie in kernel programming :))

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s