Neatest peice of code I have ever seen

Need some advice? Trying out a new idea? Fancy a beer?
Join us for some general banter and good times.
Post Reply
User avatar
erazz
9x Developer
Posts: 682
Joined: Tue Dec 27, 2011 6:25 pm
Country: -
Location: NJ-USA
Contact:

Neatest peice of code I have ever seen

Post by erazz »

Code: Select all

float fastInvSqrt(float x)
{
   float halfx(0.5f*x);
    union {
        float f;
        uint32_t l;
    }i;
    i.f = x;
    i.l = 0x5f3759df - (i.l>>1);
    i.f = i.f*(1.5f - halfx*i.f*i.f);
   return i.f;
}
http://en.wikipedia.org/wiki/Fast_inverse_square_root



It calculates x^(-1/2) which is the inverse square root.
It's extremely useful when you want to normalize a set of vectors.

Normaly you would do this:

Code: Select all

n = sqrt(a+b+c)
a = a/n
b = b/n
c = c/n

With this little puppy you do this:

Code: Select all

n = fastInvSqrt(a+b+c)
a = a * n
b = b * n
c = c * n

It's fantastically cool because:
1) It calculates the approximate value. That is, a value that is very close to the real one. It does this much much faster too.
2) It allows us to use multiplications instead of divisions - neat again
3) It includes some of the deepest most intriguing bit hacks I have ever seen.

Truly the work of serious masters. I am in awe!
Z

BEWARE - WE ARE IN THE AIR!!!
What goes up... Should be controlled by a 9X!

User avatar
Rob Thomson
Site Admin
Posts: 4543
Joined: Tue Dec 27, 2011 11:34 am
Country: United Kingdom
Location: Albury, Guildford
Contact:

Re: Neatest peice of code I have ever seen

Post by Rob Thomson »

I am so happy for you (lol)
Slope Soaring, FPV, and pretty much anything 'high tech'
...........if you think it should be in the wiki.. ask me for wiki access, then go add it!
User avatar
MikeB
9x Developer
Posts: 17993
Joined: Tue Dec 27, 2011 1:24 pm
Country: -
Location: Poole, Dorset, UK

Re: Neatest peice of code I have ever seen

Post by MikeB »

Interesting, the GNU compiler for the ARM processor does multiplication instead of division by a constant. To divide by x, it multiplies by 2^n/x, (64 bit result) then shifts right n bits.

Mike.
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
User avatar
Westy
Posts: 248
Joined: Tue Jan 31, 2012 8:44 pm
Country: -
Location: Te Awamutu, Waikato, New Zealand

Re: Neatest peice of code I have ever seen

Post by Westy »

Whhhoooosssshhhhhh ....... straight over my head ..... I can feel a head ache coming on haha
User avatar
cre8tiveleo
Posts: 1434
Joined: Tue Dec 27, 2011 6:13 pm
Country: -
Location: Ontario,(GTA North)
Contact:

Neatest peice of code I have ever seen

Post by cre8tiveleo »

Does this help getting sound on the ersky board? (hint hint)

I love routines that make things interesting, very cool.

:D


---
I am here: http://tapatalk.com/map.php?o5wbed

User avatar
jhsa
Posts: 19480
Joined: Tue Dec 27, 2011 5:13 pm
Country: Germany

Re: Neatest peice of code I have ever seen

Post by jhsa »

Does this help getting sound on the mormal 9x board? (hint hint) :mrgreen: :mrgreen: ;)
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9

Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
G550Ted
Posts: 389
Joined: Tue Dec 27, 2011 6:15 pm
Country: -
Location: Savannah, GA, USA

Re: Neatest peice of code I have ever seen

Post by G550Ted »

Maybe the key to having dancing Leprechauns on the LCD? :mrgreen: :lol: :mrgreen:

Erin go Bragh!

Ted
ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Neatest peice of code I have ever seen

Post by ReSt »

or calculate GPS distances

Reinhard
User avatar
erazz
9x Developer
Posts: 682
Joined: Tue Dec 27, 2011 6:25 pm
Country: -
Location: NJ-USA
Contact:

Re: Neatest peice of code I have ever seen

Post by erazz »

The ERSky board doesn't need it. It has a built in FPU. It can do inverse square roots very very quickely.


It has no bearing whatsoever on XX9x. It's just neat.
Z

BEWARE - WE ARE IN THE AIR!!!
What goes up... Should be controlled by a 9X!
User avatar
cre8tiveleo
Posts: 1434
Joined: Tue Dec 27, 2011 6:13 pm
Country: -
Location: Ontario,(GTA North)
Contact:

Neatest peice of code I have ever seen

Post by cre8tiveleo »

I knew that... Ersky needs a voice.

So many cool toys to play with... Not enough time.


---
I am here: http://tapatalk.com/map.php?nlrmh2
ReSt
Posts: 1581
Joined: Tue Dec 27, 2011 11:34 pm
Country: -

Re: Neatest peice of code I have ever seen

Post by ReSt »

Erazz,
do you also have a nice routine for calculating the cosine of an angel (approximately)?

Reinhard
User avatar
Kilrah
Posts: 11109
Joined: Sat Feb 18, 2012 6:56 pm
Country: Switzerland

Re: Neatest peice of code I have ever seen

Post by Kilrah »

The cosine of an angel sounds like a beautiful curve!
User avatar
jhsa
Posts: 19480
Joined: Tue Dec 27, 2011 5:13 pm
Country: Germany

Re: Neatest peice of code I have ever seen

Post by jhsa »

definitely :P
My er9x/Ersky9x/eepskye Video Tutorials
https://www.youtube.com/playlist?list=PL5uJhoD7sAKidZmkhMpYpp_qcuIqJXhb9

Donate to Er9x/Ersky9x:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YHX43JR3J7XGW
User avatar
MikeB
9x Developer
Posts: 17993
Joined: Tue Dec 27, 2011 1:24 pm
Country: -
Location: Poole, Dorset, UK

Re: Neatest peice of code I have ever seen

Post by MikeB »

erazz wrote:The ERSky board doesn't need it. It has a built in FPU.
I can't see a FPU on the M3 CPU, single cycle 32 bit multiply, and hardware divide yes. Have I missed something?

Mike.
erskyTx/er9x developer
The difficult we do immediately,
The impossible takes a little longer!
User avatar
cre8tiveleo
Posts: 1434
Joined: Tue Dec 27, 2011 6:13 pm
Country: -
Location: Ontario,(GTA North)
Contact:

Re: Neatest peice of code I have ever seen

Post by cre8tiveleo »

Isn't Vector Floating point and fpu thingy? No wait, m4 is dsp and optional fpu... I thought it had. *shrugs* too many things to remember.
User avatar
erazz
9x Developer
Posts: 682
Joined: Tue Dec 27, 2011 6:25 pm
Country: -
Location: NJ-USA
Contact:

Re: Neatest peice of code I have ever seen

Post by erazz »

Hmmm.... Guess there isn't an FPU there after all. I was mixed up with another microcontroller :D
Z

BEWARE - WE ARE IN THE AIR!!!
What goes up... Should be controlled by a 9X!
User avatar
cre8tiveleo
Posts: 1434
Joined: Tue Dec 27, 2011 6:13 pm
Country: -
Location: Ontario,(GTA North)
Contact:

Re: Neatest peice of code I have ever seen

Post by cre8tiveleo »

See, we're not perfect.. woah is me... where's the gin and vodka... ahhhh.. all is better.
User avatar
kaos
Posts: 3247
Joined: Wed Dec 28, 2011 1:15 am
Country: United States

Re: Neatest peice of code I have ever seen

Post by kaos »

Ha ha, the twisted brains finally got overtwisted. :mrgreen:
wait, my be it is unwinding. :)
SkyNorth
Posts: 958
Joined: Tue Dec 27, 2011 11:40 am
Country: -
Location: Mansfield , Ontario

Re: Neatest peice of code I have ever seen

Post by SkyNorth »


Post Reply

Return to “The Pickled Gnu (The Pub)”