likely-unlikely

learning sample of likely() and unlikely() compiler hints

荒凉一梦 提交于 2019-12-18 11:54:15
问题 How can I demonstrate for students the usability of likely and unlikely compiler hints ( __builtin_expect )? Can you write an sample code, which will be several times faster with these hints comparing the code without hints. 回答1: Here is the one I use, a really inefficient implementation of the Fibonacci numbers: #include <stdio.h> #include <inttypes.h> #include <time.h> #include <assert.h> #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) uint64_t fib

gcc likely unlikely macro usage

本小妞迷上赌 提交于 2019-12-08 15:11:35
问题 I am writing a critical piece of code with roughly the following logic if(expression is true){ //do something with extremely low latency before the nuke blows up. This branch is entered rarely, but it is the most important case }else{ //do unimportant thing that doesnt really matter } I am thinking to use likely() macro around the expression, so when it hits the important branch, I get minimum latency. My question is that the usage is really opposite of the macro name suggest because I am

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

删除回忆录丶 提交于 2019-11-30 06:42:10
问题 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. 回答1: 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

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

别说谁变了你拦得住时间么 提交于 2019-11-28 21:12:31
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. Tomas 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

likely/unlikely equivalent for MSVC

江枫思渺然 提交于 2019-11-27 18:42:09
GCC compiler supports __builtin_expect statement that is used to define likely and unlikely macros. eg. #define likely(expr) (__builtin_expect(!!(expr), 1)) #define unlikely(expr) (__builtin_expect(!!(expr), 0)) Is there an equivalent statement for the Microsoft Visual C compiler, or something equivalent ? DigitalRoss I say just punt There is nothing like it. There is __assume() , but don't use it, it's a different kind of optimizer directive. Really, the reason the gnu builtin is wrapped in a macro is so you can just get rid of it automatically if __GNUC__ is not defined. There isn't anything

likely/unlikely equivalent for MSVC

时光怂恿深爱的人放手 提交于 2019-11-27 04:18:35
问题 GCC compiler supports __builtin_expect statement that is used to define likely and unlikely macros. eg. #define likely(expr) (__builtin_expect(!!(expr), 1)) #define unlikely(expr) (__builtin_expect(!!(expr), 0)) Is there an equivalent statement for the Microsoft Visual C compiler, or something equivalent ? 回答1: I say just punt There is nothing like it. There is __assume(), but don't use it, it's a different kind of optimizer directive. Really, the reason the gnu builtin is wrapped in a macro

How do the likely/unlikely macros in the Linux kernel work and what is their benefit?

安稳与你 提交于 2019-11-25 23:42:54
问题 I\'ve been digging through some parts of the Linux kernel, and found calls like this: if (unlikely(fd < 0)) { /* Do something */ } or if (likely(!err)) { /* Do something */ } I\'ve found the definition of them: #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) I know that they are for optimization, but how do they work? And how much performance/size decrease can be expected from using them? And is it worth the hassle (and losing the portability probably) at