Reciprocal Approximation with 1 Subtraction
Today's find: You can get a floating-point approximation of 1/x that's accurate to 3 bits with a single integer subtraction instruction. float fast_reciprocal(float x)
{
unsigned i = *(unsigned *) &x;
i = 0x7effffffU - i;
return *(float *) &i;
}
The magic number 0x7effffff accomplishes two things:1) The exponent is calculated as 253-e, which effectively negates the exponent and subtracts 1.2) The mantissa is approximated as a 1st order polynomial in the interval [1, 2).Interesting, but perhaps ...
Read more at news.ycombinator.com