https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101301

--- Comment #7 from Segher Boessenkool <segher at gcc dot gnu.org> ---
Assuming that you divide the "default" case into ten pieces of 0.01 each,
some slight corrections:

(In reply to Thomas Koenig from comment #6)
> int foo3 (int n)
> {
>   if (__builtin_expect_with_probability (n >= 55555, 1, 0.55))
>     {
>       if (__builtin_expect_with_probability (n >= 77777, 1, 0.33/0.55))
>         {
>           if (__builtin_expect_with_probability (n == 77777, 1, 0.1/0.33))
>             return 7;
>           if (__builtin_expect_with_probability (n == 88888, 1, 0.1/0.23))
>             return 8;
>           if (__builtin_expect_with_probability (n == 99999, 1, 0.1/0.11))
>             return 9;

0.1/0.13 for that last one.

> 
>           return 0;
>         }
>       else
>         {
>           if (__builtin_expect_with_probability (n == 55555, 1, 0.1/0.22))
>             return 5;
>           if (__builtin_expect_with_probability (n == 66666, 1, 0.1/0.11))
>             return 6;

0.1/0.12

> 
>           return 0;
>         }
>     }
>   else
>     {
>       if (__builtin_expect_with_probability (n >= 33333, 1, 0.22/0.45))
>         {
>           if (__builtin_expect_with_probability (n == 33333, 1, 0.1/0.22))
>             return 3;
>           if (__builtin_expect_with_probability (n == 44444, 1, 0.1/0.11))
>             return 4;

0.1/0.12

> 
>           return 0;
>         }
>       else
>         {
>           if (__builtin_expect_with_probability (n == 11111, 1, 0.1/0.23))
>             return 1;
>           if (__builtin_expect_with_probability (n == 22222, 1, 0.1/0.13))
>             return 2;
> 
>           return 0;
>         }
>     }
> }
> 
> the numbers on POWER9 become
> 
> [tkoenig@gcc135 ~]$ gcc -O3 bench.c a.c
> [tkoenig@gcc135 ~]$ ./a.out
> foo: 7.134855
> foo2: 7.842507
> foo3: 6.624406
> [tkoenig@gcc135 ~]$ gcc -mcpu=native -O3 bench.c a.c
> [tkoenig@gcc135 ~]$ ./a.out
> foo: 6.458520
> foo2: 7.696735
> foo3: 6.196469
> 
> where, on a few runs, the differene betweeh foo and foo3 with -mcpu=native
> sometimes disappears and sometimes is larger (gcc135 is not a benchmark
> machine).
> 
> So, I'd say there some advantage in the compiler not lying to itself :-)

Yeah :-)  Of course in your testing the nine named cases have the same
probability,
which is not very true in practice (but is there any better guess possible),
and
the "default" case has that same probability for GCC (is there a better
estimate
for that, maybe?)

(I expect there just is some typo or thinko somewhere in the pass, fwiw :-) )

Reply via email to