Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22033

gcc __builtin_expect doesn't seem to generate a correct code

$
0
0

The following two code snippets produces exactly the same assembly code, even though branches are enclosed with different branch predictions.

Let's say that we have test0.c

#define likely(x)       __builtin_expect((x), 1)#define unlikely(x)     __builtin_expect((x), 0)int bar0();int bar1();int bar2();int bar3();int foo(int arg0) {  if (likely(arg0 > 100)) {    return bar0();  } else if (likely(arg0 < -100)) {    return bar1();  } else if (likely(arg0 > 0)) {    return bar2();  } else {    return bar3();  }}

and test1.c

#define likely(x)       __builtin_expect((x), 1)#define unlikely(x)     __builtin_expect((x), 0)int bar0();int bar1();int bar2();int bar3();int foo(int arg0) {  if (unlikely(arg0 > 100)) {    return bar0();  } else if (unlikely(arg0 < -100)) {    return bar1();  } else if (unlikely(arg0 > 0)) {    return bar2();  } else {    return bar3();  }}

As you can see by comparing two snippets, these two have different branch predictions for each branch (likely() vs. unlikely()).

However, when it is compiled from a linux box(ubuntu 12.04 32bit, gcc 4.6.3). These two sources produce virtually same outputs.

$gcc  -c -S -o test0.s test0.c$gcc  -c -S -o test1.s test1.c$ diff test0.s test1.s1c1<   .file   "test0.c"--->   .file   "test1.c"

If anyone can explain this, it will be a big help.Thanks for your help in advance!


Viewing all articles
Browse latest Browse all 22033

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>