problem about __builtin_expect()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

    The following C code call __builtin_expect() is to decalre that
the branch condition is more probable.

int sgn(int x, int y)
{
     int s;

     if (__builtin_expect(x < 0, 1))
          s = -1;
     else
          s = 1;
     return s;
}

Compiled by gcc, we can get its asm code as follows:

1  _sgn:
2          pushl   %ebp
3          movl    %esp, %ebp
4          subl    $16, %esp
5          movl    8(%ebp), %eax
6          shrl    $31, %eax
7          testl   %eax, %eax
8          je      L2
9          movl    $-1, -4(%ebp)
10        jmp     L3
11  L2:
12        movl    $1, -4(%ebp)
13  L3:
14        movl    -4(%ebp), %eax
15        leave
16        ret

Now that I think that (x < 0) is more likely, gcc put the instruction
'movl $-1, -4(%ebp)' immediately after conditional branch instruction
'je L2'. But, with more conderation about this code, the goal of
improvement in instruction prefetching successfully can not be
achieved similarly. While executing 'je', the CPU is meant to prefetch
the next 'movl'. Even if this prefecthing is sucessful, the following
instruction 'jmp' will cause another failure of prefecthing
definitely. In fact, for a 'if' structure it is seems that the 'jmp'
insturction is inevitable regrardless of how the gcc arrage the code.
Therefore, as a whole, there is no improvement in instruction
prefetching. It is more likely that my opinion is limited somewhere
and the __builtin_expect() should play its role. So, I expect some
explanation about this problem.


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux