This is a good thing to know about, but bear in mind that these days it's often slower than just using whatever built in square root you have access to. Especially beware in languages where you need to do something more than a cast to get the raw bits out of the floating point number, oftentimes that alone is enough to kill any performance gains you might otherwise see. Profile your code before and after, and make sure you've got an easy way to switch back if you realize that you've gone and made things slower (or worse, too inaccurate for your use case).
If this was a universally effective optimization with no potential downside, it would already be built in to your standard math library...
This isn't a universally effective optimization. It's an exquisite optimization for when a little imprecision is tolerable. Micro-optimizations like this are rarely worth the increase in cognitive complexity of the code, but Quake had to do a lot of math in very little time on 1996 hardware, so I'll trust Carmack (whose programming skills are legendary if you aren't familiar with him).
It's an approximation, and thus does not comply with the IEEE floating-point standard. That is, numerical results of the operations should be correct up to the precision of the floating-point number. There's lots of optimizations possible with floating-point math if speed/energy-efficiency is prioritized over accuracy or standard compliance.
All of the IEEE floating point standard is an approximation :).
It's a cool hack but very mid-90s centric and not that all applicable today. In modern pipelined processors with segregated register files the cost of moving a piece of data from floating point registers to integer registers and back again -- along with pretty much all modern instruction sets (SSE, AltiVec, NEON) coming with instructions that give you a means to calculate a reciprocal square root (either directly or with an estimate + refine) -- means that such a trick is no longer practical.
I think we should be thinking again the floating-point implementations on CPUs. I guess hacks like this directly on hardware would double the battery life on mobile devices. IEEE level accuracy is rarely needed either in number precision or computation accuracy. I guess fast IEEE square root isn't either space or energy efficient on silicon.
I disagree -- the amount of silicon dedicated to this type of operation is miniscule compared to the caches on modern processors. Removing hardware floating point reciprocal square root isn't going to have any noticeable effect on power consumption.
This sort of floating-point optimization, while admittedly quite imprecise, actually does still see plenty of application in high performance, framerate-critical gaming. Especially games with require a robust physics package (my previous employment was at a now-defunct maker of hardware-accelerated physics products). For these gaming applications, or specifically for the graphics drivers they depend on, you're coding multi-threaded routines in proprietary instruction sets that run on a GPU, which is becoming more and more like a parallel vector processor.
Comments
This is a good thing to know about, but bear in mind that these days it's often slower than just using whatever built in square root you have access to. Especially beware in languages where you need to do something more than a cast to get the raw bits out of the floating point number, oftentimes that alone is enough to kill any performance gains you might otherwise see. Profile your code before and after, and make sure you've got an easy way to switch back if you realize that you've gone and made things slower (or worse, too inaccurate for your use case).
If this was a universally effective optimization with no potential downside, it would already be built in to your standard math library...
This isn't a universally effective optimization. It's an exquisite optimization for when a little imprecision is tolerable. Micro-optimizations like this are rarely worth the increase in cognitive complexity of the code, but Quake had to do a lot of math in very little time on 1996 hardware, so I'll trust Carmack (whose programming skills are legendary if you aren't familiar with him).
Carmack didn't write that code. It was written by Greg Walsh.
It's an approximation, and thus does not comply with the IEEE floating-point standard. That is, numerical results of the operations should be correct up to the precision of the floating-point number. There's lots of optimizations possible with floating-point math if speed/energy-efficiency is prioritized over accuracy or standard compliance.
All of the IEEE floating point standard is an approximation :).
It's a cool hack but very mid-90s centric and not that all applicable today. In modern pipelined processors with segregated register files the cost of moving a piece of data from floating point registers to integer registers and back again -- along with pretty much all modern instruction sets (SSE, AltiVec, NEON) coming with instructions that give you a means to calculate a reciprocal square root (either directly or with an estimate + refine) -- means that such a trick is no longer practical.
I think we should be thinking again the floating-point implementations on CPUs. I guess hacks like this directly on hardware would double the battery life on mobile devices. IEEE level accuracy is rarely needed either in number precision or computation accuracy. I guess fast IEEE square root isn't either space or energy efficient on silicon.
I disagree -- the amount of silicon dedicated to this type of operation is miniscule compared to the caches on modern processors. Removing hardware floating point reciprocal square root isn't going to have any noticeable effect on power consumption.
This sort of floating-point optimization, while admittedly quite imprecise, actually does still see plenty of application in high performance, framerate-critical gaming. Especially games with require a robust physics package (my previous employment was at a now-defunct maker of hardware-accelerated physics products). For these gaming applications, or specifically for the graphics drivers they depend on, you're coding multi-threaded routines in proprietary instruction sets that run on a GPU, which is becoming more and more like a parallel vector processor.