[FFmpeg-devel] [PATCH 3/7] Add alias-safe union typedefs
Uoti Urpala
uoti.urpala
Sun Jan 31 16:08:37 CET 2010
On Sun, 2010-01-31 at 15:43 +0100, Michael Niedermayer wrote:
> Thats funny, if i comment
> a= *(int*)test;
> all work
> if i do the typepuning over intermediate global volatile void* it
> doesnnt neither did
> a= *(int*)(-1-((int)test ^-1));
> but adding +1 or
> a= *(int*)((long)test>>1<<1);
> does get the following code functioning and only the int case failing
>
> if i only put a single type of access in there + short accesses
> all work even the int cast
>
> conclusion, gccs optimizer is very oddly behaving but it seems the
> unions work without may_alias as long as no aliasing violating casts are
> detectable by gcc and when it detects such aliasing violation may_alias
> no longer works either.
I think the reason *(int*)test breaks the other accesses is simple:
first gcc sees the unsafe access, and then it optimizes the other ways
to use the same result, assuming it was valid since the programmer
already used it.
A hard to avoid problem with any union usage with explicit types is that
it makes the macro less general. You need to know what types were used
to write to the memory. For example if you want to use the read macros
on a buffer that you just wrote to using put_bits.h inlined code, then
you need to know that the inlined code may have written data using the
type uint32_t, and a type matching that must be present in the union. If
no such type is present in the union then it's certainly not safe.
More information about the ffmpeg-devel
mailing list