Can likely/unlikely macros be used in user-space code?
Asked Answered
K

3

26

I came across these 2 macros in Linux kernel code. I know they are instructions to compiler (gcc) for optimizations in case of branching. My question is, can we use these macros in user space code? Will it give any optimization? Any example will be very helpful.

Knawel answered 3/11, 2009 at 15:25 Comment(6)
kerneltrap.org/node/4705Cockatrice
duplicate? #110210Sinful
I checked these posts, but both again talks about kernel related stuff. I wanted to know whether same can be used in user code.Knawel
If you are programming for any reasonably powerful processor, you are unlikely to get any performance benefit. Modern dynamic branch predictors are quite good.Kurgan
@Jay I think programmer should not assume power of processor. Dynamic branch detection would be easier if programmer explicitly provide the information.Knawel
@vinit, i create a random vector(from 0-100, branch is bigger than 0 or not) to measure "likely" brings performance gain compared branch prediction of CPU. Result shows it's similar time cost with or without likely.Chlorinate
S
44

Yes they can. In the Linux kernel, they are defined as

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

The __builtin_expect macros are GCC specific macros that use the branch prediction; they tell the processor whether a condition is likely to be true, so that the processor can prefetch instructions on the correct "side" of the branch.

You should wrap the defines in an ifdef to ensure compilation on other compilers:

#ifdef __GNUC__
#define likely(x)       __builtin_expect(!!(x), 1)
#define unlikely(x)     __builtin_expect(!!(x), 0)
#else
#define likely(x)       (x)
#define unlikely(x)     (x)
#endif

It will definitely give you optimizations if you use it for correct branch predictions.

Stickup answered 3/11, 2009 at 15:32 Comment(5)
In the #else part, shouldn't they evaluate to (x) and not empty?Afrikander
which header file contains this definition in user include directories ?Knawel
@tonfa i dont want kernel's header file. I want file which is in /usr/include so that I can include it in my user space code.Knawel
They are only available in the kernels header file (or maybe in other projects). I suggest you create your own header file and add the definitions above. __builtin_expect is a gcc builtin and doesn't need an addition header file.Stickup
You should use !!(x) instead of just (x), to force the parameter to be converted to either 0 or 1.Shylashylock
H
12

Take a look into What Every Programmer Should Know About Memory under "6.2.2 Optimizing Level 1 Instruction Cache Access" - there's a section about exactly this.

Haiku answered 3/11, 2009 at 15:38 Comment(1)
No problem. This is a very enlightening paper, even on a third read :)Haiku
A
4

The likely() and unlikely() macros are pretty names defined in the kernel headers for something that is a real gcc feature

Antitype answered 3/11, 2009 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.