Skip to content

Instantly share code, notes, and snippets.

@airekans
Created January 5, 2015 10:24
Show Gist options
  • Save airekans/b5b5cc3f87be253e88c2 to your computer and use it in GitHub Desktop.
Save airekans/b5b5cc3f87be253e88c2 to your computer and use it in GitHub Desktop.
test __builtin_expect in GCC
// Use the following command to compile, -O2 is necessary
// g++ test_builtin_expect_asm.cpp -O2 -S -o test_builtin_expect_asm.S
// You can see that the branch expected will not jump.
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
int main(int argc, char *argv[])
{
int a;
/* Get the value from somewhere GCC can't optimize */
a = atoi (argv[1]);
if (likely (a == 2))
{
a++;
a += argc + atoi(argv[1]);
}
else
{
a--;
a += argc + 5;
printf ("%d\n", a);
}
printf ("%d\n", a);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment