Re:Fast Inverse Square Root
by qppo on Monday November 02, 2020 @08:05PM
(#24975285)
That's the Linux way to do it
Another way is a memcpy, which I believe is the most defined way to do type punning
But you also have to assume the size of those primitive types but that's pretty safe in modern C/C++.
[ Parent
| on HN ]
Re:Fast Inverse Square Root
by colejohnson66 on Monday November 02, 2020 @08:47PM
(#24975534)
In C++, type punning through pointers and unions is undefined behavior. Even `reinterpret_cast<T>` 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]
I really wish C and C++ had an explicit type punning operator. They are systems programming languages and this is very often needed.
C++20 introduces bit_cast to do this. The implementation of bit_cast is semantically equivalent to using memcpy.
Casting pointers in C used to work back in the day. It still works in most cases (except with aggressive optimization options), but it may may make some people uneasy.
There's a Torvalds rant about this specific UB and it's how they do it in the Linux kernel.
Linux is written in C. In C99 and later type punning trough unions is well defined operation. No issues there.
It’s C++ that’s lacking this feature. Not C.
I always thought the memcopy is optimized out, but can you explain how in
int i;
float f;
memcopy (&i, &f,4)
the memcopy can be optimized out? Probably I am misunderstanding the statement.
Re:Fast Inverse Square Root
by andrewaylett on Tuesday November 03, 2020 @12:57PM
(#24982095)
The C abstract machine presents i and f as having separate locations in memory, but a compiler that knows that memcopy does can avoid actually saving the target machine registers to the stack, then avoid even copying values between registers.
Thanks. Does that really happen?
Yes.
Re:Fast Inverse Square Root
by hurrrrrrrr on Tuesday November 03, 2020 @08:27AM
(#24979411)
The compiler is not beholden to the standard library but the standard. So all modern compilers come with a bit of knowledge of how standard library functions like memcpy are supposed to behave and as long as the visible effect is the same it's allowed to do anything it wants. So instead of calling the function memcpy for a size of 4, it can e.g. just use a mov instruction to move the value from a float to an integer register. [1] It does additional analysis on how the values are used and maybe it doesn't even need to move the value at all, but that's the gist of it.
Re:Fast Inverse Square Root
by colejohnson66 on Tuesday November 03, 2020 @09:03AM
(#24979712)
A good example of this is the string functions in the C stdlib. I’ve reverse engineered some programs where the compiler used x86’s built-in “string” instructions instead of calling out to, say, `strlen`.
Re:Fast Inverse Square Root
by ChrisFoster on Monday November 02, 2020 @10:03PM
(#24975919)
Punning through the union is explicitly allowed in the GCC documentation which explains why it's reliable for use in the Linux kernel. See
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Typ...
> you also have to assume the size of those primitive types
This usually works though there's some situations where you could still be surprised. For instance if you're programming embedded processors with avr-gcc you'll have sizeof(double)==4
Do modern compilers optimize away the memcpy?