Re:Float Toy
by Sharlin on Saturday August 20, 2022 @08:39PM
(#32536377)
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.)
[ Parent
| on HN ]
Re:Float Toy
by adgjlsfhk1 on Saturday August 20, 2022 @10:26PM
(#32536872)
One place where this is used in practice is in the code to compute floating point exponentials.
You can do lots of interesting things with bit shifts.
Like the famous Quake3 Fast Inverse Square
Re:Float Toy
by ChrisLomont on Saturday August 20, 2022 @10:13PM
(#32536822)
Plenty of languages have this in their standard libraries for exactly this purpose. I use it in C/C++ and C# frequently in numerical code.