#### Archived

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

# Optimize this ecuations

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

## Recommended Posts

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

##### Share on other sites
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?

x = 1;

[edited by - puzzler183 on June 30, 2002 12:46:58 PM]

##### Share on other sites
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

##### Share on other sites
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!

##### Share on other sites
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!

##### Share on other sites
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.

##### Share on other sites
maybe in optimized fpu asm is more fast, but the fpu asm that i write is slow (too many instructions for do something)

##### Share on other sites
Uh ok. With 4 repititions it gets close to one. You will ned about 1000...

##### Share on other sites
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?

##### Share on other sites
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

##### Share on other sites
dont divide, multiply by the reciprocal.

##### Share on other sites
doing the reciprocal is the same, 3 div ( for do the 3 reciprocals) and 3 mul after

##### Share on other sites
Hi,

Try:
Pauls Square Root Page
http://www.azillionmonkeys.com/qed/sqroot.html

Best Regards,
- James

Compiler macro?

##### Share on other sites
compiler macro? :? what?

##### Share on other sites
After a little more consideration a compiler macro probably wouldn''t be useful, since you won''t know the value of x at compile time. Of course, if you knew the fixed depth of the recursion, you could still program a compiler macro to convert the function to a simpler form...

...there was a good article on such techniques in the first Game Programming Gems tome... or you should be able to find something online.

Cheers,

Timkin

##### Share on other sites
Don''t perform the sqrt, test on the squared values
or use a LUT

If you need to calculate alot of square roots, then you can look into 3DNow!/SSE/SSE2 code.

##### Share on other sites
There is also a nice article on some float tricks in the GPG (one or two, not sure now).
Message was: get mantisa, get sqrt of mantisa from a table, modify proper with exponent.

you will get the sqrt with only some bit operations and a access to a table with like 4096 precalculated sqrts.

(Check the books, they are great)