how to call a c++ constructor in vc++ inline assembly 
Author Message
 how to call a c++ constructor in vc++ inline assembly

Hi,

I'm doing some handcrafted assembly optimizing of my c++ code, and
thusfar I have been unable to call symbols that are not declared
within the same asm block. I was wondering if someone has come up with
a technique or procedure to fill in the propper address in the call,
or stuff like that.

so I have:

vertex::vertex(float x, float y, float z)
{
// some vertex constructor code

Quote:
}

vertex operator - (const vertex& src1)
{

        float x;
        float y;
        float z;

        __asm {

                mov             eax,    DWORD PTR [src1]                
                fld             DWORD   PTR [eax]
                fchs

                fld             DWORD   PTR [eax+4]
                fchs

                fld             DWORD   PTR [eax+8]
                fchs

                fstp    DWORD   PTR [z]
                fstp    DWORD   PTR [y]
                fstp    DWORD   PTR [x]
        }

        return vertex(x,y,z);  // <-- I want to translate this too because of
                               //     redundant floating point
load/save

Quote:
}

So far I have been able to cope with this disability, but I've come
near the point where I want to translate and optimize the calls to
constructors of structs / classes, as shown above.  The problem is the
vc++ compiler which decorates the names, and the preprocessor which
doesn't allow the decorated names being used in the assembly block.

If you can help me, I'll post the complete class here, which is a
vertex class and contains some pretty standard functions which are
hand optimized. The class as it is now incresed performance of my
software by as much as 40+%.

cheers,
ignace.



Fri, 25 Feb 2005 06:34:33 GMT  
 how to call a c++ constructor in vc++ inline assembly



Quote:
> Hi,

> I'm doing some handcrafted assembly optimizing of my c++ code, and
> thusfar I have been unable to call symbols that are not declared
> within the same asm block. I was wondering if someone has come up with
> a technique or procedure to fill in the propper address in the call,
> or stuff like that.

> so I have:

> vertex::vertex(float x, float y, float z)
> {
> // some vertex constructor code
> }

> vertex operator - (const vertex& src1)
> {

>         float x;
> float y;
> float z;

> __asm {

> mov eax, DWORD PTR [src1]
> fld DWORD PTR [eax]
> fchs

> fld DWORD PTR [eax+4]
> fchs

> fld DWORD PTR [eax+8]
> fchs

> fstp DWORD PTR [z]
> fstp DWORD PTR [y]
> fstp DWORD PTR [x]
> }

> return vertex(x,y,z);  // <-- I want to translate this too because of
>                                //     redundant floating point
> load/save
> }

> So far I have been able to cope with this disability, but I've come
> near the point where I want to translate and optimize the calls to
> constructors of structs / classes, as shown above.  The problem is the
> vc++ compiler which decorates the names, and the preprocessor which
> doesn't allow the decorated names being used in the assembly block.

Surprise!

The decorated names are chosen so that they do not collide with valid
identifiers. So you cannot use these names, by design...

Quote:
> If you can help me, I'll post the complete class here, which is a
> vertex class and contains some pretty standard functions which are
> hand optimized. The class as it is now incresed performance of my
> software by as much as 40+%.

> cheers,
> ignace.

Are you sure that you assembly is actually improving the code??

I did a quick test with a made up vertex class:

class vertex
{
public:
   vertex(float x, float y, float z) : MyX(x), MyY(y), MyZ(z)
   { }

   vertex operator-() const
   { return vertex(-MyX, -MyY, -MyZ); }

private:
   float MyX, MyY, MyZ;

Quote:
};

vertex neg(const vertex& value)
{ return -value; }

Looking at the disassembly of operator-(), I get this code in VC++.NET:

; 15   :    { return vertex(-MyX, -MyY, -MyZ); }

  00000 d9 41 08         fld     DWORD PTR [ecx+8]
  00003 8b 44 24 04      mov     eax, DWORD PTR ___$ReturnUdt$[esp-4]
  00007 d9 e0            fchs
  00009 d9 41 04         fld     DWORD PTR [ecx+4]
  0000c d9 e0            fchs
  0000e d9 01            fld     DWORD PTR [ecx]
  00010 d9 e0            fchs
  00012 d9 18            fstp    DWORD PTR [eax]
  00014 d9 58 04         fstp    DWORD PTR [eax+4]
  00017 d9 58 08         fstp    DWORD PTR [eax+8]
  0001a c2 04 00         ret     4

Seems pretty hard to improve on.  :-)

Bo Persson



Fri, 25 Feb 2005 23:55:58 GMT  
 how to call a c++ constructor in vc++ inline assembly

Quote:
> > So far I have been able to cope with this disability, but I've come
> > near the point where I want to translate and optimize the calls to
> > constructors of structs / classes, as shown above.  The problem is the
> > vc++ compiler which decorates the names, and the preprocessor which
> > doesn't allow the decorated names being used in the assembly block.

> Surprise!

> The decorated names are chosen so that they do not collide with valid
> identifiers. So you cannot use these names, by design...

I figured it was, but that wasn't my question.  Is there a way to get
an address reference without having to rely on precompiled code in the
inline block.

Quote:
> Are you sure that you assembly is actually improving the code??
> Looking at the disassembly of operator-(), I get this code in VC++.NET:

> ; 15   :    { return vertex(-MyX, -MyY, -MyZ); }

>   00000 d9 41 08         fld     DWORD PTR [ecx+8]
>   00003 8b 44 24 04      mov     eax, DWORD PTR ___$ReturnUdt$[esp-4]
>   00007 d9 e0            fchs
>   00009 d9 41 04         fld     DWORD PTR [ecx+4]
>   0000c d9 e0            fchs
>   0000e d9 01            fld     DWORD PTR [ecx]
>   00010 d9 e0            fchs
>   00012 d9 18            fstp    DWORD PTR [eax]
>   00014 d9 58 04         fstp    DWORD PTR [eax+4]
>   00017 d9 58 08         fstp    DWORD PTR [eax+8]
>   0001a c2 04 00         ret     4

> Seems pretty hard to improve on.  :-)

> Bo Persson


Yes! That code looks as tight as I would have liked it, Bo. However
you'll notice it's not exactly the same since it has 'inlined' the
call to the constructor, which has totally disappeared into the ret 4
and the mov. Ofcourse in this case the constructor doesn't need to be
much more than that, but it's more the technique I'm after, since I
want to apply it to other classes as well (if it works). You could
argue that I could just 'inline' the complete constructor code as well
in that case, but then calls become hard to trace and profile, so I
want to keep them separate for now.

You'll also agree that this function doesn't allow for much optimizing
because it's too simle. Other more complex functions might be, at
least, they are compared to vc6 code as far as I can see. I'm a bit
surprised VC.Net comes out this good, actually. In vc6 I've seen some
of the most horrible redundancy of code you wouldn't believe.

So, any bright idea's on that address fetching ? :)

cheers,
ignace.



Sat, 26 Feb 2005 08:16:15 GMT  
 how to call a c++ constructor in vc++ inline assembly

Quote:
> I'm a bit
> surprised vc.net comes out this good, actually. In vc6 I've seen some
> of the most horrible redundancy of code you wouldn't believe.

Well since I was so surprised, I thought I had to test it and
effectively, c++.NET produces much better code than the vc6 compiler.
The iline-ing of functions gives much better overall results. So
making the swap to .NET realy pays off.

Quote:
> So, any bright idea's on that address fetching ? :)

Still an open question :)

cheers,
ignace.



Wed, 02 Mar 2005 17:56:30 GMT  
 how to call a c++ constructor in vc++ inline assembly
12345678901234567890123456789012345678901234567890123456789012345678901234567890


 >

Quote:
> > I'm a bit
> > surprised vc.net comes out this good, actually. In vc6 I've seen some
> > of the most horrible redundancy of code you wouldn't believe.

> Well since I was so surprised, I thought I had to test it and
> effectively, c++.NET produces much better code than the vc6 compiler.
> The iline-ing of functions gives much better overall results. So
> making the swap to .NET realy pays off.

        Really? I bought VC++.net recently, and I can't get it to optimize
for anything! The options are grayed out, and even if I put the optimization
switches in manually, it still won't optimize! What are you doing
differently?

                                                - Lyle Goldman, USA

Quote:

> > So, any bright idea's on that address fetching ? :)

> Still an open question :)

> cheers,
> ignace.



Sun, 13 Mar 2005 02:27:09 GMT  
 how to call a c++ constructor in vc++ inline assembly

Quote:

>    Really? I bought VC++.net recently, and I can't get it to optimize
>for anything! The options are grayed out, and even if I put the optimization
>switches in manually, it still won't optimize! What are you doing
>differently?

Which one did you buy?  Visual C++ .NET Standard Edition has the optimizer
disabled.  You have to get Visual Studio .NET Professional Edition to turn
on the optimizer.

Incredible but true.
--

  Providenza & Boekelheide, Inc.



Sun, 13 Mar 2005 13:56:17 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. vc++ inline assembler

2. Help with optimizing newbie code (inline assembly under VC++ 6.0)

3. Basics of inline assembly (VC++/win32): The Stack

4. Basics of inline assembly (VC++/win32): Segment Registers

5. Help with optimizing newbie code (inline assembly under VC++ 6.0)

6. Help with VC++ 4.0 inline assembly.

7. VC++ calling fortran function and fortran function calling a c++ function

8. vc++ 6 swig tcl c++

9. Graphics with VC++'s inline assembler

10. how to call one vc++ dll inLabview?

11. how to call prolog to vc++?

12. Inline assembly in Borland C++ 5.0

 

 
Powered by phpBB® Forum Software