C/C++

Built-in Function: long __builtin_expect (long exp, long c) indicates compiler to do optimization. It return value is exp, where c is actually the indicator to compiler which value exp most likely will take. For example,

if (__buildin_expect(x,0)) {

doSomthing();

}

would indicate that we do not expect to call foo, since we expect x to be zero, i.e., it is equivalent to 

if (x!=0) {

doSomething();

}

For following code:

if (__builtin_expect(x, 0)) {
    foo();
    ...
} else {
    bar();
    ...
}

Compiler could generate following code

cmp   $x, 0
  jne   _foo
_bar:
  call  bar
  ...
  jmp   after_if
_foo:
  call  foo
  ...
after_if:


In this case,  since programmer expect x equal to 0

Advertisements
C/C++

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s