The __builtin_expect() Operator

When the GCC extensions setting is on, the compiler recognizes the __builtin_expect() operator. Use this compile-time operator in an if or while statement to specify to the compiler how to generate instructions for branch prediction.

This compile-time operator takes two arguments:

The second argument is the most likely result of the first argument. The following listing shows an example.

Listing: Example for __builtin_expect() operator

void search(int *array, int size, int key)

{

    int i;

    for (i = 0; i < size; ++i)

    {

          /* We expect to find the key rarely. */

          if (__builtin_expect(array[i] == key, 0))

         {

             rescue(i);

         }

    }

}