when should one go for likely and unlikely

J

Jorgen Grahn

When should one go for likely and unlikely in c?

You mean the likely() and unlikely() macros which e.g. the Linux
kernel sources have. They expand to gcc-specific things which are
supposed to help generate better code, by telling the compiler which
case it should optimize for:

if(unlikely(weird_error_case)) {
error_handling();
}

I suspect the answer is "rarely, and mostly when profiling tells you
it makes a difference in your target environment".

But I cannot say for sure. I certainly don't want to see them in
/every/ if and while. And of course you cannot implement them in a
portable way, except as do-nothing macros.

/Jorgen
 
G

glen herrmannsfeldt

(snip)
You mean the likely() and unlikely() macros which e.g. the Linux
kernel sources have. They expand to gcc-specific things which are
supposed to help generate better code, by telling the compiler which
case it should optimize for:

The original Fortran had a FREQUENCY statement that allowed one
to specify the relative frequency (ratio) of choices for IF
and computed-GOTO statements, and also an estimate for the number
of iterations of a DO loop. That allowed the compiler to optimize
some a little better than it might otherwise.

-- glen
 
K

Keith Thompson

David Brown said:
Well, in this group we mostly likely C, and many unlikely C++.

Try asking a proper question, and you are more likely to get a proper
answer. If your English is not good, that's okay - but at least write
enough so that we can figure out what you are trying to say.

I believe he's referring to macros named "likely" and "unlikely".
He may not be aware that they're specific to the Linux kernel and
not standard features of C. Given that assumption, there's nothing
*seriously* wrong with the question, though it certainly could have
been phrased more clearly.

Nitin: There are no features in the standard C language or library
called "likely" or "unlikely".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top