Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > round double to integer (inline-assembler)
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 26th March 00:09
sean gamz
External User
 
Posts: 1
Default round double to integer (inline-assembler)



Hey everybody,

I am currently developing an win32 application in VC++ 6.0, in which I
permanently have to round double variables to integer variables. Since this
conversion has to be very fast, I am now starting to explore the world of
inline-assembly and would appreciate any kind of help and support.

This is the skeleton of the function so far:

int __fastcall MathLib::Round(const double &dbl)
{
int x;

__asm {
???
}

return x;
}

What is the fastest method to solve this problem?

Thanks in advance

Sean
  Reply With Quote


  sponsored links


2 26th March 00:10
nico vrouwe
External User
 
Posts: 1
Default round double to integer (inline-assembler)



Hey,

The shortest I guess would be:

int __fastcall MathLib::Round(const double &dbl)
{
int x;
__asm
{
fld qword ptr [dbl]
fistp [x]
}
return x;
}

But you should __inline it if that's not done automatically for best speeds.

But you might want to read Agner Fog's text on this subject:
http://www.agner.org/assem/pentopth.zip
Check out chapter 27.5.

Also, if you're working on large arrays of doubles/ints at a time, you might
want to do the whole array at once using SSE code. But you should get more
familiar with ASM first I think.

Hope this helps,
/Nico
  Reply With Quote
3 26th March 00:10
arargh307nospam
External User
 
Posts: 1
Default round double to integer (inline-assembler)


<snip>

Wouldn't
x = (int)*dbl;
be about the same thing (assumming I got the syntax correct) ?

<snip> --
Arargh307 at [drop the 'http://www.' from ->] http://www.arargh.com
BCET Basic Compiler Page: http://www.arargh.com/basic/index.html

To reply by email, remove the garbage from the reply address.
  Reply With Quote
4 26th March 00:10
nico vrouwe
External User
 
Posts: 1
Default round double to integer (inline-assembler)


Well, one would think so. But actually it isn't.

That code you gave (when *dbl changed to dbl) calls __ftol, the piece of
code that brianchon posted. It does this in both debug and release builds.
No inlining happens there.

When using i=(int)dbl it compiles to this:
fld QWORD PTR _dbl$[esp+12]
call __ftol

When inlining the function I gave it compiles to this:
; 28 : int blah = dbl2int(dbl);
fld QWORD PTR _dbl$[esp+12]
fistp DWORD PTR _x$42279[esp+12]
mov eax, DWORD PTR _x$42279[esp+12]

However the VC++ compiler is quite skilled at optimizing floating point
stuff (sometimes, anyway) so converting code to asm might not make as much
of a difference as you think.

/Nico
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666