[FFmpeg-devel] [PATCH 3/7] Add alias-safe union typedefs
Michael Niedermayer
michaelni
Sun Jan 31 15:43:04 CET 2010
On Sun, Jan 31, 2010 at 02:37:17PM +0100, Michael Niedermayer wrote:
> On Sun, Jan 31, 2010 at 02:08:15PM +0100, Michael Niedermayer wrote:
> > On Fri, Jan 29, 2010 at 11:06:15AM +0000, M?ns Rullg?rd wrote:
> > > Michael Niedermayer <michaelni at gmx.at> writes:
> > >
> > > > On Fri, Jan 29, 2010 at 02:12:10AM +0000, Mans Rullgard wrote:
> > > >> ---
> > > >> libavutil/intreadwrite.h | 18 ++++++++++++++++++
> > > >> 1 files changed, 18 insertions(+), 0 deletions(-)
> > > >
> > > > I belive these types would be usefull to projects using libavutil
> > >
> > > Here we go again...
> > >
> > > Those rely on the may_alias attribute being supported, which is tested
> > > by configure. I guess it could be changed to check for gcc version
> > > instead, although that is less reliable with non-gcc compilers.
> >
> > After looking in the c spec ..
> > why do we need may_alias at all? a union should be enough or am i missing
> > something?
> >
> > That said, these unions only need a char array as a char access can alias
> > anything. Otherwise if iam wrong they need float & double too
> >
> > And with gcc a much better solution is along the lines of
> >
> > #define UD(v,access)\
> > ((union {\
> > access a;\
> > typeof(v) b[sizeof(access)/sizeof(typeof(v))];\
> > }*)(v))->a
> >
> > this specifies only the effective type + the accesed type and thus reduces
> > the restrictions placed on the optimizer.
> > It also shows how ridiculous this is as the compiler clearly knows the types
> > already.
>
> Here are the 2 macros one for gcc like compilers and one for ISO C
>
> #define AV_UA(v,access)\
> ((union {\
> access a;\
> typeof(v) b[sizeof(access)/sizeof(typeof(v))];\
> }*)(v))->a
>
> #define AV_UA2(v,access)\
> ((union {\
> access a;\
> char b[sizeof(access)];\
> }*)(v))->a
>
> but now the bad news, none works, not even the attribue may_alias code
> unless iam missing something ...
> (tested with gcc 4.3 and 4.4)
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.
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Democracy is the form of government in which you can choose your dictator
-------------- 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/ff7ebe42/attachment.pgp>
More information about the ffmpeg-devel
mailing list