Archived

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

Fastest way to increment a number in ASM?

This topic is 5800 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 dont know very much asm, and i was trying to optimize a loop. I wont bother to explain the point of this here, but the loop goes:
  
int i = 0;
float r = 0.0f;

for( ; i < 100000000; i++ )
__asm {
mov eax, r
add eax, .1
mov r, eax
}
  
and that ran pretty fast. I then tried to use r += 0.1f in a Release configuration and found that it ran way faster. I looked at the source listing output, but i dont understand the assembly behind it: fadd r dec eax I think that''s right, but i dont really have the source in front of me. Anyway, When i added mov eax, .1 just before that line, it got even faster. Still, i dont understand what is going on and i want to know what the fastest ASM version of this loop is and what exactly it does. ----------------------------- The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
Your code is invalid. The main registers are for integer math only. Floating point calculation must be done in the floating point unit. If your code actually works (does what you expect) I suspect the compiler is interpreting what you actually meant (as it knows that r and .1 are floating point values) and is generating the correct code.


As for what is the fastest way...no clue. I almost never bother with hand optimized assembly any more..the compiler is usually better than me (especially when it comes to pipeline pairing on Pentium and above processors).

Share this post


Link to post
Share on other sites
eax wont carry floats? awww....well should i just leave this to compiler''s choice of code?

-----------------------------
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.

Share this post


Link to post
Share on other sites
Yeah -

I don''t see what the point of this is, but you could do fixed point arithmetic in eax...you''d have to explain what its about though...

Nick

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Only floating point registers can hold floating point values (obviously): st(0) to st(7)

Fastest way to increment them: fadd st, [value], this takes 3 cycles or 1 cycle if overlapped.

Fastest way to increment an integer value: inc eax. This takes 0.5 cycles paired.

- AH

Share this post


Link to post
Share on other sites