Archived

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

Asm Floating Point

This topic is 5398 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

I am converting this C++ code into assembly : fParticleAge += fTimeCounter; if(fParticleAge >= fDyingAge) { fParticleAge = DEATH_AGE; return FALSE; } All variables are floats. Heres my Asm so far : __asm { mov esi, this fld [esi].fParticleAge fld fTimeCounter fadd fstp [esi].fParticleAge } Note that the cpp code is within an object and thats what the mov esi, this is all about. So far i have the first line of cpp code working now i need to convert the if statement but i am stuggling. If anyone could help me out i would appreciate it. Also I would rather not use instructions that depend on a Pentium processor but if thats the only way then so be it. Cheers

Share this post


Link to post
Share on other sites
Why are you rewriting that code in assembly? If it is to speed it up, don''t. That code isn''t going to get much faster, if at all. In fact, you will probably make it slower. Compilers do a better job than you may think.

If profiling your code shows that you spend an abnormally large amount of time running that code, figure out a way to run it less.


Regards,
Drew Vogel

Share this post


Link to post
Share on other sites
Pleeeeease don''t hijack my thread with yet another compiler vs hand written asm argument. I am just rewriting this to get into assembly language more.

I asked a simple question and i just want a simple answer.

Share this post


Link to post
Share on other sites
It''s incredible. It has become impossible to post a topic remotely related to ASM, without getting those "don''t use ASM, ASM is eeeeevil" replies. I wonder where this sudden ASM-o-phobia came from. I guess it has to do with the fact, that it seems to be the first thing a CS student ''learns'' (probably because their instructors can''t code in ASM themselves...). (Un)fortunately, ASM is still very much needed in industrial high-performance applications, esp. in the computer graphics domain. Compilers/optimizers are still lightyears away from a human brain, when it comes to write really ultra-optimized ASM. Oh well, back on topic.

jonbell:
Note, that the following code is not optimized, for the sake of better understanding

First, you should rearrange your statements a little. Keep in mind, that fadd and friends can also address a memory area.

fld dword ptr [esi + fParticleAge_offset]
fadd dword ptr [fTimeCounter]
fst dword ptr [esi + fParticleAge_offset]

OK, now your fParticleAge member is updated, and we still have the result of the addition in st(0) (not yet popped). A floating point comparison is done using the fcom opcode family:

fcomp dword ptr [esi + fDyingAge_offset] // compare st(0) with dying age, pop the stack
fnstsw ax // store the status word in ax, don''t search for unmasked exception bits
sahf // copy AH in CPU status flags
jb @out // if fParticleAge < fDyingAge, then jump out
mov [esi + fParticleAge_offset], DEATH_AGE // update particle age (special, see below)


Note that I used a standard mov to update the death age. DEATH_AGE is a normal 32bit (constant) variable (although it contains a floating point value), so we don''t need any special (and slower) FPU instructions to simply copy it.

Also note that you have to use jb/ja type of conditional jumps, and not jl/jg ! That''s because the fcomp/sahf procedure only sets the zero and carry flags (and parity, but that''s not important here), and not the sign and overflow flag !

Share this post


Link to post
Share on other sites
Yann - Thanks a lot for your help and right on about the Asm-o-phobia. The sad fact is that with your (correct) comments you will probably tempt the argument into the thread from so called CS ''experts'' (a note to you guys; do a real degree in Software Engineering before passing comment on how to engineer software).

Where can I find an index of all floating point x86 instructions? My book doesn''t even cover the FPU and all of the FPU instructions are missing form the index.

Share this post


Link to post
Share on other sites
quote:

The sad fact is that with your (correct) comments you will probably tempt the argument into the thread from so called CS ''experts''


Hmm, yeah, you''re probably right. OK, so DISCLAIMER: from here on, any further offtopic material that is not related to the programmming of the FPU (esp. pro and contra ASM arguments) will be deleted without further notice !

quote:

Where can I find an index of all floating point x86 instructions? My book doesn''t even cover the FPU and all of the FPU instructions are missing form the index.


Have a look here, chapter 14.

Share this post


Link to post
Share on other sites
http://developer.intel.com/design/pentium4/manuals/245471.htm

There are PDF versions + the opportunity to order hard copies.

However - I''ve only ever received one book from intel - Book 3 ''System Programming Guide'', and NOT Book 2 ''Instruction Set Reference'' which is the one you want. It might be worth a try though.

Share this post


Link to post
Share on other sites
I got my documentation on the FPU and its instructions from an FTP site that was posted in another thread a while back...lets see here...

ftp://skyking.res.wpi.net/pub/CS/

Grab the IA-32 Intel ones, I have them all on my desktop.

"C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do, it blows away your whole leg."

- Bjarne Stroustrup

Realm Games Company

Share this post


Link to post
Share on other sites