1/sqrt(x)

I was looking at www.gamedev.net and found this . I think(if it works) that you should replace and 1/sqrt() with what rodzilla suggested. (I had nothing to do with the gamedev.net post and do not know if it would work.) You should also replace sqrt() by calling 1/sqrt().

Interesting. I notice it uses a lot of pointers though, converting a float to an int representation of the float’s bits by referencing that position in memory as an int and then back again. That’s not something you can do easily in Java and thus I’m not sure it would result in being faster.

Would somthing like Float.floatToIntBits wourk?

Ah, good find. I’ve built the following function from the thread you point to:

    public static float invSqrtB(float fValue) {
      float fhalf = 0.5f*fValue;
      int i = Float.floatToIntBits(fValue);
      i = 0x5f3759df - (i >> 1);
      fValue = Float.intBitsToFloat(i);
      fValue = fValue*(1.5f - fhalf*fValue*fValue);
      return fValue;
    }



Unfortunately, it seems to run many times slower than just 1 / Math.sqrt(fValue). On my P4 2.0 GHz and 1.4.2_03 java, running the above through 100,000,000 iterations (adding up the total so the compiler doesn't ignore the loop), I get the new code running for 18,563 ms and the old Math.sqrt code running at 3,890 ms. It doesn't matter if I swap the two's order or what not, the results are pretty much the same (18 secs to 4)

The accuracy is not terrible, but the speed is definitely not better. Even in that thread there was an arguement about whether or not FPU was enabled when people were comparing one to the other. So I think we best stick with our current invsqrt function.

I’ve made some benchmark of this trick in java a while ago (it’s a very very old trick) and it’s way faster to call 1/sqrt(x) that using the trick… Even in C it doesn’t speed up the thing dramatically…

It’s also possible that Java is optimizing the call to Math.sqrt() and perhaps replacing it with a processor-specific square root assembly call or such. I would suggest that we simply leave the call as-is unless a significantly better solution appears.