Loki's blog

  • Home
  • maloki.net
  • Inverse Square Root

    • 5 Jan 2009
    • 0 Responses
    •  views
    • Programming Share Technical Technology gaming geek
    • Edit
    • Delete
    • Tags
    • Autopost

    I just bumped into a clever little code snippet used to find the inverse square root:

    float InvSqrt(float x) {
        float xhalf = 0.5f * x;
        int i = *(int*)&x;   // store floating-point bits in integer
        i = 0x5f3759d5 - (i >> 1);  // initial guess for Newton's method
        x = *(float*)&i;  // convert new bits into float
        x = x*(1.5f - xhalf*x*x);  // One round of Newton's method
        return x;
    }

    [ taken with comments from Kalid Azad @ BetterExpalined.com: Understanding Quake’s Fast Inverse Square Root. ]

    Ok, it does not get the exact value but you get a good enough estimate, FAST!

    There are two important things to note here:

    • Newton’s method (also known as the Newton-Raphson method)
    • The ‘magic number’: 0x5f3759d5, an optimized first guess

    For a better explanation, I suggest reading Kalid Azad’s article and the paper by Charles McEniry.

    Wait a minute, so why is this useful?

    In graphics programming, this can be used to normalize vectors quickly. If you noticed the title on the first link, it’s used in the Quake 3 engine. So does that mean the legendary John Carmack wrote this code? If you’re interested, you can read the Origin of Quake3’s Fast InvSqrt() at Beyond3D.

    What did I learn?

    I usually forget one thing: there are times when speed trumps accuracy. A close quick estimate would be better and there are a number of optimations that can be made. Much thanks goes to Doc Mana who first introduced me to the Newton-Raphson method & how to apply it on code. 

    • Tweet
  • Version X of Y

    • 15 May 2007
    • 0 Responses
    •  views
    • Complaint People Technical work
    • Edit
    • Delete
    • Tags
    • Autopost
    What use is database versioning system in Ruby on Rails if your team mates don't use them? o.o;; Argh! Now that I want to revert to an older version, it's hell! Using rake db:migrate VERSION=_ won't work properly unless I either use all the unedited files or all the new files ;_; Currently trying to it x.x
    • Tweet
  • About

    26-year old Filipino game developer and gamer who loves food, especially cookies. Mmmm, Cookies!

    28432 Views
  • Archive

    • 2012 (3)
      • May (3)
    • 2011 (1)
      • May (1)
    • 2010 (1)
      • September (1)
    • 2009 (8)
      • July (1)
      • May (1)
      • April (2)
      • January (4)
    • 2008 (17)
      • December (1)
      • November (3)
      • September (3)
      • July (6)
      • June (1)
      • April (1)
      • January (2)
    • 2007 (24)
      • November (2)
      • October (2)
      • September (3)
      • August (4)
      • July (6)
      • June (2)
      • May (2)
      • March (2)
      • February (1)
    • 2006 (10)
      • November (3)
      • March (1)
      • February (2)
      • January (4)
    • 2005 (5)
      • December (1)
      • November (2)
      • March (1)
      • January (1)
    • 2004 (3)
      • November (2)
      • October (1)

    Get Updates

    Subscribe via RSS
    Twitter
  • Other blogs

    • Photography
    • Singularity
    • Hungry
    • Tumblog

    maloki.net

    • Home
    • Projects
    • Contact