Connecting Tech Pros Worldwide Forums | Help | Site Map

C++ optimization for emulators

Timothy Stark
Guest
 
Posts: n/a
#1: Jul 19 '05
Hello folks,

Does anyone have any information about good books about C/C++ optiimization?
I am developing my emulator by using C++ language. I compared generated
assembly sources between two
operator functions. To add a pair of 18-bit values by two different
operators below.

inline Word10& operator += (register const Word10 &val)
{
if ((rh += val.rh) & ~H10_MASK)
( rh &= H10_MASK, lh++ );
lh = (lh + val.lh) & H10_MASK;
return *this;
}

inline Word10& operator + (register const Word10& x, register const Word10&
y)
{
return Word10 (x) += y;
}

vs.

#define op_add3(z, x, y) \
if ((z.rh = x.rh + y.rh) & ~H10_MASK) \
( z.rh &= H10_MASK, z.lh++ ); \
z.lh = (x.lh + y.lh) & H10_MASK;

I compared two generated assembly lines and noticed that operator + function
has a few more
instructions than op_add3 macro function because it uses an extra temp pair
for returning
results. Their results are 23 instructions (operator +) vs. 18 instructions
(op_add3) However, I noticed that one instruction is wasted in middle of
operator + function in assembly line.
Does anyone have any segguestions about C/C++ optimization for writing
emulators?

Thank you!
Tim Stark



Julián Albo
Guest
 
Posts: n/a
#2: Jul 19 '05

re: C++ optimization for emulators


Timothy Stark escribió:
[color=blue]
> inline Word10& operator += (register const Word10 &val)
> {
> if ((rh += val.rh) & ~H10_MASK)
> ( rh &= H10_MASK, lh++ );
> lh = (lh + val.lh) & H10_MASK;
> return *this;
> }
>
> inline Word10& operator + (register const Word10& x, register const Word10&
> y)
> {
> return Word10 (x) += y;
> }
>
> vs.
>
> #define op_add3(z, x, y) \
> if ((z.rh = x.rh + y.rh) & ~H10_MASK) \
> ( z.rh &= H10_MASK, z.lh++ ); \
> z.lh = (x.lh + y.lh) & H10_MASK;
>
> I compared two generated assembly lines and noticed that operator + function
> has a few more instructions than op_add3 macro function because it usesan
> extra temp pair for returning results. Their results are 23 instructions[/color]

You can write something like:

inline void add_assign (Word10 & z, const Word10 & x, const Word10 & y)
{
if ((z.rh = x.rh + y.rh) & ~H10_MASK)
( z.rh &= H10_MASK, z.lh++ );
z.lh = (x.lh + y.lh) & H10_MASK;
}

By the way, is this code correct? You are incrementing the destination
lh and then assigning it a value in both versions.

Regards.
Closed Thread


Similar C / C++ bytes