Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,091 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,584 people online right now. Registration is fast and FREE... Join Now!




Little Gem of Programming

 
Reply to this topicStart new topic

Little Gem of Programming, Ran across this at random today

Rating  5
NickDMax
19 Nov, 2007 - 08:58 AM
Post #1

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
I ran across this article (and follow up article) today and had to read it though twice. It was simply great for a former graphics geek like me.


The article is basically about the history behind this little C hack:
CODE
float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i>>1);
    x = *(float*)&i;
    x = x*(1.5f - xhalf*x*x);
    return x;
}
This code and its like are not seen in most modern programs. It may hide somewhere in the basement of a graphics library or physics engine but not in the application code.

The code is a simplified version of the Newton-Raphson's method applied to approximating 1/sqrt(x). It is an amazing little bit-twiddling hack that calculates a good "first guess" which optimizes the NR-Method so that it converges faster.


The article was great to read because it ran past the names of iconic names in graphics programming: John Carmack, Michael Abrash, Terje Mathisen (who I had never head of), and Gary Tarolli.


..you just don't see code like this anymore...

The *magic* behind the code is eventually ascribed to Clive Moler -- the inventor of MatLAB. Amazing little piece of code!
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Little Gem Of Programming
3 Dec, 2007 - 08:24 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
That is a pretty slick piece of code Nick. bit shifting is not utilized nearly enough in modern day coding. Also the reuse of the same variable allows for quick reading of variable values.
Knowing there's a better solution is one thing, finding it is another. I'll be adding this to my collection of math algorithms smile.gif
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Little Gem Of Programming
3 Dec, 2007 - 11:57 PM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 11 times
My Contributions
Cool!
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Little Gem Of Programming
6 Dec, 2007 - 04:11 AM
Post #4

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 11 times
My Contributions
I wanted to post Duff's device, but couldn't remember it's name. Anyway, here it is, enjoy!
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Little Gem Of Programming
6 Dec, 2007 - 08:18 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
I hadn't see that before (even though I have both books mentioned... must be in the exercises smile.gif ). I have actually used fall though in a similar manner (though I put my loop outside the switch -- this is much better) in a line drawing function. My program only broke it up to 4 conditions... there was some reason why but I don't remember what it was.

Anyway, I later learned faster ways to draw a line.

very cool little trick to keep in mind when you have a loop that the compiler can't really unroll for you.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:20PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month