Re: help : likely

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



raja wrote:

Hi,
In kernel code the macros likely() and unlikely() are frequently used.Would you please tell me wht those macros will actually perform?.
thanking you,
raja

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


Hi Raja,
likely() and unlikely() are directives to the compiler (gcc in this case), used to tell it what the usual outcome of the conditional will be. Using this knowledge, the compiler can better optimize the code.

For example, error checks/asserts do not occur too commonly... like:

if (nodeptr == NULL)
    BUG();

The above code is an example assert. for the most part, if you programmed it correctly, nodeptr will never be NULL. You know this, so u can help out the compiler a bit.

if (unlikely(nodeptr == NULL))
    BUG();

this will tell the compiler that the equality condition is the rare case and to optimize the code for the common case, which is nodeptr != NULL.

Similarly, using likely(),

if (likely(nodeptr != NULL)) {
    /* do stuff */
}
else
    BUG();

Hope that helps...
ciao
-rahul


--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux