Variable Optimization Question

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;
}
 
H

hari4063

I will use Test2() function becuase it is more readable. But using this
optimisations are not good practice. Most of todays compilers will do this
automaticly (or not). If you realy need size optimisation use some asm in
code :) But, all of this is my opinion.
 
C

Chris

Bryan said:
....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.
<snip>

On -O2 optimisation, gcc produces identical code for both these
functions on my machine. Don't try this kind of optimisation, as
compilers are better at it than you.

Chris
 
D

David Lindauer

Bryan said:
....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;
}

good compilers aren't going to generate vastly different code for these.
I would choose the second because it is easier to maintain. The first is
easier to create since you can cut and paste.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top