This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

x86 code generation question


Hello,

Before I forget to mention, I am not currently subscribed to this mailing 
list, so please CC any replies to me, if necessary.

I've been looking at creating a rather simple macro, call it sel(x, y, s), 
that simply returns x when s == 0, or y when s == 1. The easy and 
straightforward way to write this macro is, of course:
  #define sel(x, y, s) ((s) ? (y) : (x))
But then again, if I am not mistaken, this kind of construction will cause 
the processor to jump, when selecting based on s. So, after a bit of 
thought, we can write the same macro differently, using only straight-line 
code:
  #define sel(x, y, s) ((x) + (((y) - (x)) & (-(s))))
Ah, that should be better.

But this is where I am not exactly sure what is going on. In some code, I 
have a line that says:
  y = sel(3, 7, (x > 11));
When I compile the simple macro with "gcc -Wall -S test.c", I get assembly 
that uses jumps, exactly as expected:
       cmpl $11, -4(%ebp)
       jle .L2
       movl $7, -12(%ebp)
       jmp .L3
  .L2: movl $3, -12(%ebp)
  .L3: ... rest of code
When I compile the complex version of the sel macro, I get the very same 
code.
Additionally, when I compile both of these macros with
"gcc -Wall -O -S test.c", they again generate the same code, which is now 
straight-lined.

Thus, my question: Why would the second macro cause gcc to use the exact 
same code as the first, for both optimization levels. It seems that the 
more complex expression of the sel macro should preclude gcc from using 
jumps, given that it was already written straight-line. I know I'm missing 
something important in my understanding, and there is a reason for this, 
so please let me know. :)

-Vadim


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]