Re: like /unlikely macro

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

 



On 4/15/06, Adrian Bunk <bunk@xxxxxxxxx> wrote:
> On Sat, Apr 15, 2006 at 03:06:09PM +0100, pushparaj vitekar wrote:
>
> > hi
>
> Hi,
>
> >  can any one explain the likely/unlikely macros
> > what is their purpose ,where they should be used?
>
> they give the compiler a strong hint which code path is likely and which
> not.
>
> This is useful for hot paths in the kernel and for macros like
> BUG_ON() (a bug is unlikely, and if you hit it, performance usually
> isn't your biggest problem).
>
> They are _not_ meant for 51:49 situations.
from compiler.h (http://lxr.linux.no/source/include/linux/compiler.h#L60)
60 #define likely(x)       __builtin_expect(!!(x), 1)
Now from info page of gcc,
Built-in Function: long __builtin_expect (long EXP, long C)
     You may use `__builtin_expect' to provide the compiler with branch
     prediction information.  In general, you should prefer to use
     actual profile feedback for this (`-fprofile-arcs'), as
     programmers are notoriously bad at predicting how their programs
     actually perform.  However, there are applications in which this
     data is hard to collect.

     The return value is the value of EXP, which should be an integral
     expression.  The value of C must be a compile-time constant.  The
     semantics of the built-in are that it is expected that EXP == C.
     For example:

          if (__builtin_expect (x, 0))
            foo ();

     would indicate that we do not expect to call `foo', since we
     expect `x' to be zero.  Since you are limited to integral
     expressions for EXP, you should use constructions such as

          if (__builtin_expect (ptr != NULL, 1))
            error ();

     when testing pointer or floating-point values.

HTH
Om

--
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