Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

likely/unlikely macro

10 views
Skip to first unread message

Manoj Awasthi

unread,
Nov 24, 2006, 10:56:44 AM11/24/06
to
Hi,

Can someone please tell me the cases in which we can use likely() and unlikely() macros ?
I think this optimization is at the cost of the correctness of code handling.

thanks
-Manoj

--
What is important?  What you want to be true, or what is true?
---------------------------------------------------------------------------
http://awmanoj.blogspot.com

Fernando Apesteguía

unread,
Nov 24, 2006, 11:49:57 AM11/24/06
to
On 11/24/06, Manoj Awasthi <lkml....@gmail.com> wrote:
> Hi,
>
> Can someone please tell me the cases in which we can use likely() and
> unlikely() macros ?

In cases when you are really really sure, that the value of a
comparison is going to be/is not going to be, equal to the value that
you are comparing with.

> I think this optimization is at the cost of the correctness of code
> handling.

Why? AFAIK, it only changes the order of the instructions to avoid
pipeline emptying.

CMIIW

>
> thanks
> -Manoj
>
> --
> What is important? What you want to be true, or what is true?
> ---------------------------------------------------------------------------
> http://awmanoj.blogspot.com

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


Stavros Passas

unread,
Nov 24, 2006, 12:05:38 PM11/24/06
to
On Fri, 2006-11-24 at 17:49 +0100, Fernando Apesteguía wrote:
> On 11/24/06, Manoj Awasthi <lkml....@gmail.com> wrote:
> > Hi,
> >
> > Can someone please tell me the cases in which we can use likely() and
> > unlikely() macros ?
>
> In cases when you are really really sure, that the value of a
> comparison is going to be/is not going to be, equal to the value that
> you are comparing with.
>
> > I think this optimization is at the cost of the correctness of code
> > handling.
>
> Why? AFAIK, it only changes the order of the instructions to avoid
> pipeline emptying.

It depends from processor's architecture which is the change.
If specific instructions for likely/unlikely branches are existed,
compiler use them, if branch instruction have a hint bit,
compiler set it, otherwise most likely path is placed first (actually on
the correct place) to avoid stalls.
Of course, on x86 we haven't special feautures for branches,
so code reordering is used.

Stavros

Stavros Passas

unread,
Nov 24, 2006, 11:18:18 AM11/24/06
to
Hello,

likely and unlikely macros is used to make compiler give
"hints" about conditional instructions(branches usually)
to the processor, if of course processor's architecture supports it.
With these macros, compiler/processor use them for branch prediction
algorithm to minimize the needed pipeline's stalls,
making faster the application from the view of programmer.

Branch prediction hit rate is really hi,if I am not wrong
something like 99%,so it's needed to give hits
on our branches only if we are sure that we can achieve more.
Finally these macros are (usually) used:
-On error handing tests (errors are unlikely to happen)
-On tests that we want to make faster the common path,
and not care if we have higher overhead on the other one.

In terms of correctness, these macros are not needed,
they are only used by optimization reasons. Also their wrong use
cause only worse performance, and never affect correctness.

Stavros Passas

On Fri, 2006-11-24 at 10:56 -0500, Manoj Awasthi wrote:
> Hi,
>
> Can someone please tell me the cases in which we can use likely() and
> unlikely() macros ?
> I think this optimization is at the cost of the correctness of code
> handling.
>
> thanks
> -Manoj
>


--

0 new messages