B
Bryan Parkoff
.....I try to reduce un-necessary temporal variables so it can be
optimized for best performance. I try to assign only one register
storage so two variables can access to only one register storage
before they are stored into memory location (variable storage).
.....Please take a look at two functions below. Byte variable and
Carry variable are global variables outside of functions.
.....Do you notice that there are two duplicated lines in Test()
function. First line is the exact same as second line, but it adds
right shift. Before and after optimization, there are no temporal
variable.
.....Test2() function has three lines. First line has one temporal
variable that it is always set to 16 Bits or Word. Second line and
third line are only 8 Bits or Byte. It copies Word (16 Bits) into
register storage before it is right shifted. Carry variable copies
the data (8 Bits) from register storage. It looks like register
storage has 16 Bits, but only low byte will be copied into Byte
variable while high byte will be ignored. It does not require to use
"var & 0xFF", but it only uses "(unsigned char)". After optimization,
temporal variable will be removed, but temporal variable will be used
during the debugging before optimization. Please note that (unsigned
char) might add "var & 0xFF" by adding ADD instruction from C/C++
compiler on other CPU machines except x86 machine.
.....My source code only uses level 4 warning instead of level 3
warning because I want to control (unsigned char) and (unsigned word)
so it can be always bug free.
.....Please suggest which Test() function or Test2() function is best
for readable and stable. I would think to choose Test(), but Test2()
would save my time by reducing minor bug or free bug.
.....What do you think?
Bryan Parkoff
unsigned char Byte = 0xFE;
unsigned char Carry = 0;
void Test(void)
{
Carry = (unsigned char)((Byte + 0x03) >> 8);
Byte = (unsigned char)(Byte + 0x03);
}
void Test2(void)
{
unsigned short Word = Byte + 0x03;
Carry = (unsigned char)(Word >> 8);
Byte = (unsigned char)Word;
}
optimized for best performance. I try to assign only one register
storage so two variables can access to only one register storage
before they are stored into memory location (variable storage).
.....Please take a look at two functions below. Byte variable and
Carry variable are global variables outside of functions.
.....Do you notice that there are two duplicated lines in Test()
function. First line is the exact same as second line, but it adds
right shift. Before and after optimization, there are no temporal
variable.
.....Test2() function has three lines. First line has one temporal
variable that it is always set to 16 Bits or Word. Second line and
third line are only 8 Bits or Byte. It copies Word (16 Bits) into
register storage before it is right shifted. Carry variable copies
the data (8 Bits) from register storage. It looks like register
storage has 16 Bits, but only low byte will be copied into Byte
variable while high byte will be ignored. It does not require to use
"var & 0xFF", but it only uses "(unsigned char)". After optimization,
temporal variable will be removed, but temporal variable will be used
during the debugging before optimization. Please note that (unsigned
char) might add "var & 0xFF" by adding ADD instruction from C/C++
compiler on other CPU machines except x86 machine.
.....My source code only uses level 4 warning instead of level 3
warning because I want to control (unsigned char) and (unsigned word)
so it can be always bug free.
.....Please suggest which Test() function or Test2() function is best
for readable and stable. I would think to choose Test(), but Test2()
would save my time by reducing minor bug or free bug.
.....What do you think?
Bryan Parkoff
unsigned char Byte = 0xFE;
unsigned char Carry = 0;
void Test(void)
{
Carry = (unsigned char)((Byte + 0x03) >> 8);
Byte = (unsigned char)(Byte + 0x03);
}
void Test2(void)
{
unsigned short Word = Byte + 0x03;
Carry = (unsigned char)(Word >> 8);
Byte = (unsigned char)Word;
}