Archived

This topic is now archived and is closed to further replies.

typecasting asm

This topic is 5775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I need a really fast macro or function that will convert a float to an intger. I don''t know asm well enough at the moment & I was wondering if any u knew how could do so ? All I really need is to round it down or something thanx in advance WizHarD

Share this post


Link to post
Share on other sites
Assembly language doesn''t deal with types such as float and int, it deals with raw memory. I can''t think of a single reason you would need to do this specifically in assembly. What is wrong with (in C++):

  
int main()
{
double d = 5.55;
int i = static_cast<int>(d);
}


I''m pretty convinced that a compiler could generate faster code for that than most people would write manually.

--
1st law of programming: Any given program, when running, is obsolete.

Share this post


Link to post
Share on other sites
From game programming gems 2, page 170, trick by Yossarian King

  
typedef union
{
int i;
float f;
} F2I;

F2I n; // float to convert

F2I magic; // magic number


magic.i = 130 << 23; // 1*2^23 in IEEE format

n.f = WHATEVER;

n.f += magic.f;
n.i -= magic.i;


n.i now holds the truncated float. This operation is faster than casting the float to an int (or so he claims).

Share this post


Link to post
Share on other sites
That technique makes platform dependent assumptions (like, for instance, that the platform employs IEEE fp format), and should by no means be considered portable code. Besides, typecasting is considered such bad form that I find it hard to imagine a properly written program suffering in performance terms from something you should try and avoid.

--
1st law of programming: Any given program, when running, is obsolete.

Edited by - SabreMan on February 19, 2002 2:49:39 PM

Share this post


Link to post
Share on other sites
quote:

SabreMan is quite right, you can''t optimize basic type castings, when compiled those casting are at most 2 instructions (not including loading to and saving from registers)



Unfortunately not. The rounding rules for float to int conversion in the ANSI C standard are *different* to those natively set on x86 CPUs. So an innocent cast (whether in C or C++ style), can turn into a not so innocent:

- save copy of FPU control word
- change rounding mode in control word
- do operation
- handle overflows etc
- restore the FPU control word

If you look at an assembly listing of a compiled x86 C/C++ program you''ll see something like "call _ftol" wherever you do a cast. The _ftol function is what does the above.

Having said that, the majority of people don''t need the float to int rounding rules to be ANSI compliant so:

1. There is a not-so-well-documented MSVC compiler switch "/QIfist" which tells the compiler to use simple 2 instruction casts rather than the ANSI compliant _ftol.

2. You could do it with 2 assembly instructions yourself (or better bung it into a macro or inline (pref naked) function):
fld dword ptr fValue
fistp iValue

3. There is also code which is faster than the _ftol function, but carries out the same overflow etc checking in the Intel Architecture Optimisations Guide (downloadable .pdf at developer.intel.com)

4. Or you could use IEEE binary tricks like Yossarian''s one above.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites