[FFmpeg-devel] [PATCH 3/7] Add alias-safe union typedefs

Michael Niedermayer michaelni
Sun Jan 31 17:25:54 CET 2010

On Sun, Jan 31, 2010 at 05:08:37PM +0200, Uoti Urpala wrote:
> 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.

I think you miss here that data would be written as type union uint32_t+X
where X is the effective type of the array and it would be read as
union of whatever+X. X being common so no aliassing arises in ISO C

Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

It is dangerous to be right in matters on which the established authorities
are wrong. -- Voltaire
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20100131/db1edaa4/attachment.pgp>

More information about the ffmpeg-devel mailing list