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 not very useful (as most CPU:s have more accurate reciprocal approximations these days).
Comments URL: https://news.ycombinator.com/item?id=42547867
Points: 17
# Comments: 9
Autentifică-te pentru a adăuga comentarii
Alte posturi din acest grup
Article URL: https://en.wikipedia.org/wiki/Larry_(cat)
Comments URL: https://

Article URL: https://arxiv.org/abs/2304.02868
Comments URL: https://news.ycombinator.c

Article URL: https://github.com/beeeeep54/rust-typescript
Comments URL: ht
I built gaspype, a Python library for fast thermodynamic calculations, like equilibrium reactions. It's lightweight, written in typed Python/Numpy, and comes with a large species database.
Gaspy

Article URL: https://docs.bunkerweb.io/latest/
Comments URL: https://news.ycombinator