Advertisement

Optimize this ecuations

Started by June 30, 2002 10:18 AM
16 comments, last by q2guy 22 years, 7 months ago
how can i optimize this?: // 139 cycles x = 0.5*(1 + c); x = 0.5*(x + c/x); x = 0.5*(x + c/x); x = 0.5*(x + c/x); c is a constant thnx
Are they floats or integers? What does this accomplish? Form there I can probably help...

I've figured out what it does, and I ahve a better question: why are trying to optimize somethign that approximates 1? That's completely stupid... Why not just use 1 and not a fancy set of equations that eat cycles like there's no tommorow?

So in answer to your original question:
x = 1;  


[edited by - puzzler183 on June 30, 2002 12:41:23 PM]

[edited by - puzzler183 on June 30, 2002 12:46:58 PM]
Advertisement
precalculate c/x and then use it - you get 1 instead 3 divs
if using int. values change (blah)*0.5 to (blah)>>1 (shift instead mul)
write in asm
With best regards, Mirek Czerwiñski
It doesn''t aproximate one! It calculates a square root!
Maybe you should check the algo with different constants than 1

I don''t know anyway to optimise this except defining c and x as ''register'' (assuming you use c/c++), so acess to these two vars is as fast as possible. You could also calculate the difference between the two latest iterations and then quit the loop if you got enough precision.


Yesterday we still stood at the verge of the abyss,
today we''re a step onward!
Yesterday we still stood at the verge of the abyss,today we're a step onward! Don't klick me!!!
quote:
Original post by MirekCz
precalculate c/x and then use it - you get 1 instead 3 divs
if using int. values change (blah)*0.5 to (blah)>>1 (shift instead mul)
write in asm


you cannot precalculate c/x as it changex with every iteration...


Yesterday we still stood at the verge of the abyss,
today we''re a step onward!
Yesterday we still stood at the verge of the abyss,today we're a step onward! Don't klick me!!!
yes, is the square root the numbers are floats, and i cant precalculate c/x because it changes between equations.
Maybe i can aproximate 1/x for c/x and convert it in c*aprox, but i dont know how aproximate 1/x.
thnx for your replys
Advertisement
maybe in optimized fpu asm is more fast, but the fpu asm that i write is slow (too many instructions for do something)
Uh ok. With 4 repititions it gets close to one. You will ned about 1000...
I''ve tried something like:
_asm
{
fld c
fsqrt
fstp x
}
and it''s slower than standard sqrtf(), so I honestly doubt if it''s possible to beat the standard library.
Maybe someone has another suggestion?

Puzzler183, with 4 repetitions not get closer to 1, get closer to sqrt of c,
p.e. c=5.2

x = 0.5*(1 + 5.2) = 3.1
x = 0.5*(3.1 + 5.2/3.1) = 2.3887
x = 0.5*(2.3887 + 5.2/2.3887) = 2.2828
x = 0.5*(2.2828 + 5.2/2.2828) = 2.2803

sqrt(5.2) = 2.2803

get a calculator and test it

This topic is closed to new replies.

Advertisement