> [Fixed point arithmetic] has some niche uses but isn't commonly used It is very common in the embedded world and in hardware. I routinely program a CPU which has no FPU, so any arithmetic will have to be done in fixed point. It's perfectly possible to get work done with fixed point arith, it just requires a bit more thinking through so you don't run out of bits. Usually, I write unit tests where I compile my code f…
Fast Inverse Square Root
61–70 of 100 posts
Re: Fast Inverse Square Root
#62Earlier quoted context omitted.
That's the Linux way to do it union { int i; float f } u { .f = 1.23f }; int i = u.i; Another way is a memcpy, which I believe is the most defined way to do type punning int i; float f; memcpy(&i, &f, 4); But you also have to assume the size of those primitive types but that's pretty safe in modern C/C++.
In C++, type punning through pointers and unions is undefined behavior. Even `reinterpret_cast ` isn’t allowed because of aliasing (IIRC). The only “defined” way to do type punning is a memcpy. A compiler targeting something like x86 would optimize out the memcpy. For more information, see the C++20 final draft[0§7.6.1.9] [0]: https://isocpp.org/files/papers/N4860.pdf
Re: Fast Inverse Square Root
#63Re: Fast Inverse Square Root
#64A trick here is that one often doesn't have to do the square root. For instance, if you want something to happen when an object is 5 units away from another object, it's normal to do
if sqrt( (x2-x1)^2 - (y2-y1)^2 )
but instead you can do if (x2-x1)^2 - (y2-y1)^2
trading a sqrt for a squaring. And often the square of the distance can be cached or even a constant. So a lot of libraries (for instance vector library from libgdx[0]) contain a dst function, but also a dst2 function that skips the squaring to save some cycles when not needed.[0]: https://libgdx.badlogicgames.com/ci/nightlies/docs/api/com/b...
Re: Fast Inverse Square Root
#65Re: Fast Inverse Square Root
#66A little self-promotion: I've written a blog post to answer the question that this one ends with (how to optimize divisions by constant integers): https://rubenvannieuwpoort.nl/posts/division-by-constant-uns... https://ridiculousfish.com/blog/posts/labor-of-division-epis... and https://ridiculousfish.com/blog/posts/labor-of-division-epis... also explain this (and are probably better written than my blogpost).
Off-topic comment, I really like the clutter-free look of your site, with the latex to html generator. For anyone curious like I was: https://github.com/rubenvannieuwpoort/static-site-generator
Re: Fast Inverse Square Root
#67This is an explanation of the fast inverse square root that I hope is easy to understand. I also managed to improve on it a little bit.
Thanks for this. I have one nit. You show `q = 1598029824 - u/2;` as being identical to `q = 0x5F400000 - u >> 1;`, but every language I know of uses a different order of operations, giving different results. It might be clearest to provide parentheses in the second case.
Re: Fast Inverse Square Root
#68Re: Fast Inverse Square Root
#69> Games calculate square roots and inverse square roots all the time to find the lengths of vectors A trick here is that one often doesn't have to do the square root. For instance, if you want something to happen when an object is 5 units away from another object, it's normal to do if sqrt( (x2-x1)^2 - (y2-y1)^2 ) but instead you can do if (x2-x1)^2 - (y2-y1)^2 trading a sqrt for a squaring. And often the square of t…
if (x2-x1) - (y2-y1)
I once used this in a path tracer to speed things up a little. The results where less accurate but sometimes this can be used as trade-off.Re: Fast Inverse Square Root
#70This is an explanation of the fast inverse square root that I hope is easy to understand. I also managed to improve on it a little bit.
You mention the wiki page is badly written, and I have the same feeling whenever I read about anything mathematics related on it. But... it's written by volunteers, so I'll take what I can get. Since you obviously have the talent for explaining things, do you think the wiki page could be edited and improved for clarity?