C++ optimization for emulators

T

Timothy Stark

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
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Timothy Stark escribió:
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

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.
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top