This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: How to type-pun and why
- From: Andrew Haley <aph at redhat dot com>
- To: Mattias Engdegård <mattias at virtutech dot se>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Mon, 12 Jun 2006 16:23:12 +0100
- Subject: Re: How to type-pun and why
- References: <e6jvfg$kmq$1@sea.gmane.org>
Mattias Engdegård writes:
> Some code I had needed to do integer operations on (the bits of)
> floating-point values. The naive
>
> double d = something;
> uint64_t x = *(uint64_t *)&d;
>
> gives a type aliasing diagnostic in 4.1, and rightly so. However,
> attempting to "fix" this by adding an alias-anything intermediate
> pointer to void or char,
>
> uint64_t x = *(uint64_t *)(void *)&d;
>
> suppresses the diagnostic but still won't work properly - gcc does
> not admit that x depends on d, so the assignment of d sometimes
> ends up after that of x. Using a union intermediate instead,
>
> union { double d; uint64_t x; } u;
> u.d = something;
> uint64_t x = u.x;
>
> works as expected, but I would like to know why
It's a gcc extension to ISO C.
> - if it is a matter of luck with the current implementation it
> might cease working when the compiler changes or becomes more
> aggressive in the future.
I doubt it. We have to keep it working becasue so much library code
requires it.
> As far as I can tell from the Standard, it could be argued that
> none of the above versions are allowed at all, so it's not obvious
> to me why one should be permitted but not another. I would be
> grateful for any help with references.
Andrew.