4 ms·
> Fast inverse square root is genius but it would just be way-too-clever technical debt if Carmack hadn’t written it and others didn’t learn from him. FISR is
by gioele 7y ago
> Fast inverse square root is genius but it would just be way-too-clever technical debt if Carmack hadn’t written it and others didn’t learn from him.
FISR is a beautiful hack, but it hardly technical debt. This is the original code. Note how the trick has been hidden behind a normal API such as `Q_rsqrt`:
float Q_rsqrt( float number ) {
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
return y;
}
This is how you walk the fine line between technical debt and useful hack.
- dragontamer 7y agoNot really. If the comment "what the fuck" were instead "estimate floating-point exponent with bit-wise arithmetic" and "SingleStep forward with Newton's Method", then the code would be better documented The comment "what the fuck" and "evil floating point bit level hacking" don't help anybody. And "1st iteration" is only helpful if you knew it was an iteration of Newton's method.
- remram 7y agoAgreed, this is really far from technical debt. It's self-contained, the purpose of the function is clear. Add a comment or two, a unit test, and this is perfect. What I would call technical debt is if he had inlined those tricks all over his code, making it very hard to debug a wrong result or move away from this technique.