5 ms·
A while ago I realized that a very fast way to multiply or divide a 32-bit float by 2^n is to simply reinterpret it as int and add/subtract n<<23. Plus some che
by Sharlin 4y ago
A while ago I realized that a very fast way to multiply or divide a 32-bit float by 2^n is to simply reinterpret it as int and add/subtract n<<23. Plus some checks if you care about overflow. Obvious in retrospect, and may not be worth it on modern desktop hardware, but still a nice trick. (This was in the context of optimizing a texture sampler for power-of-two textures.)
- alberth 4y agoYou can do lots of interesting things with bit shifts. Like the famous Quake3 Fast Inverse Square https://www.beyond3d.com/content/articles/8/ https://www.beyond3d.com/content/articles/8/
- ChrisLomont 4y agoPlenty of languages have this in their standard libraries for exactly this purpose. I use it in C/C++ and C# frequently in numerical code.
- Sharlin 4y agoOh wow, I had totally missed that C has such functions as `ldexp` and `scalb*`.
- adgjlsfhk1 4y agoOne place where this is used in practice is in the code to compute floating point exponentials.