Carmacks sqrt()

Started by
99 comments, last by jonbell 19 years, 10 months ago
I got really bored tonight and decided to practice linking C++ w/ C# and wrote this application that computes the sqrt of all numbers between 1 and 100,000 in several different ways and my timing results are as follows:

C# implementation of InvSqrt(): 1000 ms
C++ implementation of InvSqrt(): 10600 ms
fsqrt (via inline asm in C++): < 100 ns
.NET sqrt(): 501 ms

-- Exitus Acta Probat --
Advertisement
So no one has found where the 1597463007 constant came from? Is John Carmack from an advanced alien intelligence? I Googled the constant in hexadecimal and in decimal and no results were returned; what are you guys talking about?
Keep coming back, because it's worth it, if you work it, so work it, you're worth it!
*raises hand*

"What were the prerequisites for this class?"
Nothing more than a 6-year apprenticeship with Steven Hawkings.
the value is simple. it is build by two parts: the part that fits onto the mantissa, and the part that fits onto the exponent of a float.


1/sqrt(x) = x^-.5

1/sqrt(m*2^e) = m^-.5 * 2^(e*-.5)

you're bitshifting the whole float one to the right, to get the *.5 in the exponent part. you subtract it from another constant to negate that exponent 0 - e>>1 simply (dunno, i think 0 for the exponent is actually not binary 00000.. check the value, &0xFF and you see what "zero" is for exponents)


now, the m = m^-.5 part.. well.. as the mantissa is represented without the 1 in front, but the sign bit is in front (value is always positive, you can't sqrt from a negative one anyways), you get a clear defined other value if you shift to the right by one bit.

now the idea is simple. the actual mantissa part is (1+m) (as the one is not stored in the float), and you can expand (1+m)^-.5 in a taylor serie, and the first part is actually something in the form constant - m/2 + ......

and that constant - m/2 is exactly what is done with this bitshifting and subtracting from a constant. more or less. rest is just finding a good constant value..

and that quite accurate starting value then gets feed into an iterative solving equation. converging to the exact solution..


its all just math, no alien technology.. (while i was damn impressed the first time i've read it, myself:D)

"take a look around" - limp bizkit
www.google.com

[edited by - davepermen on February 26, 2003 3:20:39 AM]
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

quote:Original post by 63616C68h
Is John Carmack from an advanced alien intelligence?


Probably yes
PM Times change...Excuse my poor english!
Could someone explain this yet again? --Without the word "mantissa." So whay you''re saying is: you let the sub-language architecture of your processor deal with exponential expressions? If I''m correct, then could someone plz explain the pointer aritmetic going on in that illegible code? If we could understand how the code works, then we most likely could implement that ingenious concept more comfortably.
Keep coming back, because it's worth it, if you work it, so work it, you're worth it!
The word "Mantissa" shouldn''t be confusing.

From dictionary.com:

Mantissa:
The decimal part of a logarithm. In the logarithm 2.95424, the mantissa is 0.95424.

There are tools other than google
quote:Original post by jperalta
I got really bored tonight and decided to practice linking C++ w/ C# and wrote this application that computes the sqrt of all numbers between 1 and 100,000 in several different ways and my timing results are as follows:

C# implementation of InvSqrt(): 1000 ms
C++ implementation of InvSqrt(): 10600 ms
fsqrt (via inline asm in C++): < 100 ns
.NET sqrt(): 501 ms

-- Exitus Acta Probat --



Is Fsqrt the inverse? how about sqrt() ?? no? THATS why its faster. Try a .NET 1/sqrt(). We''re talking about inverse of a sqrt not regular sqrt. I think more than one person is confused on this matter.
no one cared. necro-ap''s should get disabled, just as every other ap.



If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

This topic is closed to new replies.

Advertisement