Hi.
I am trying to optimize some code that I use in a loop. I have an unsigned int and I test the left most bit to decide whether to take some action, then shift the remaining bits to the left. Right now I use:
Code:
unsigned int tempbc; // previously filled with bits to process
unsigned int val1, y; // assigned elsewhere in the loop
float val2; // assigned elsewhere in the loop
// start loop
if ((int)tempbc < 0)
val1 -= y;
tempbc += tempbc; // shift 1 bit to left
// do some other stuff
if ((int)tempbc < 0)
val2 += y;
tempbc += tempbc; // shift 1 bit to left
I'm using MSVC and when I compile my code, it produces a test of tempbc with a conditional jump based on the sign followed by the add for tempbc. I'd really like to somehow change the code so that I can do the tempbc += tempbc first and then test or manipulate the carry flag but I can't come up with code to make this happen. (I don't want to use inline assembly because it is a complicated function and I'd like the code to be as portable as possible.)
This is some pseudo-assembly code for what I'd like the end result to be.
For the first case:
Code:
add tempbc, tempbc
sbb dummyreg, dummyreg // -1 if carry flag set in add, 0 otherwise
and dummyreg, y
sub val1, y
For the second vase:
Code:
add tempbc, tempbc
jnc LABEL_NEXT
fisub y
LABEL_NEXT:
Hopefully that psedocode helps you understand what I'm trying to accomplish. Any suggestions?
Kmart