This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
__builtin_expect usage and g++
- From: Anders Fugmann <afu at fugmann dot dhs dot org>
- To: gcc <gcc at gcc dot gnu dot org>
- Date: Tue, 25 Nov 2003 20:58:53 +0100
- Subject: __builtin_expect usage and g++
Hi,
While trying to squeeze the last cycle out of a program, I stumbled over
that using __builtin_expect yields worse code than when not using it.
Tests on the subject reveild that the problem only existed when
compiling with the g++ compiler. The gcc compiler did, as expected,
yield better code (in terms of runtime).
The program below exposes the irregularity:
#if 1
#define LIKELY(expression) (__builtin_expect(!!(expression), 1))
#define UNLIKELY(expression) (__builtin_expect(!!(expression), 0))
#else
#define LIKELY(expression) (expression)
#define UNLIKELY(expression) (expression)
#endif
int main(void)
{
unsigned int i,j;
for(i = 0; i < (1 << 30); i++) {
if (UNLIKELY(i%10 == 0)) {
for (j=0; j<10; j++) {
++i;
}
}
if (LIKELY(i%5 != 0)) {
++i;
}
}
}
When compiling this program with
g++ -O3 -o test test.c, the result is (best case of all runs):
afu@marvin:~$ time ./test
real 0m4.246s
user 0m4.205s
sys 0m0.008s
However setting #if 0, gives the result (worst case):
afu@marvin:~$ time ./test
real 0m4.131s
user 0m4.094s
sys 0m0.009s
Is this to be expected when compiling c++ programs, and are there other
issues to be aware of?
Btw. The compiler is:
gcc (GCC) 3.3.2 (Debian)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
Machine: Athlon 1.4 GHz
Regards
Anders Fugmann