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
  • Found on the Internet

    • 21 Nov 2008
    • 2 Responses
    •  views
    • Life Reflection Share
    • Edit
    • Delete
    • Tags
    • Autopost
    Got this form Paulo Coelho's blog:
    Number 7 Life is sexually transmitted. Number 6 Good health is merely the slowest possible rate at which one can die. Number 5 Men have two emotions: Hungry and Horny. If you see him without an erection, make him a sandwich. Number 4 Give a person a fish and you feed them for a day; teach a person to use the Internet and they won’t bother you for weeks. Number 3 Health nuts are going to feel stupid someday, lying in hospitals dying of nothing. Number 2 All of us could take a lesson from the weather. It pays no attention to criticism. Number 1 In the ’60’s, people took acid to make the world weird. Now the world is weird and people take Prozac to make it normal.
    These are interesting takes on life; quite true too. Do you agree? Hmm...Right now, I'm hungry. Hahahaha!
    • Tweet
  • About

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

    28430 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