[FFmpeg-devel] [PATCH 3/7] Add alias-safe union typedefs
Michael Niedermayer
michaelni
Mon Feb 1 00:20:04 CET 2010
On Sun, Jan 31, 2010 at 03:56:23PM -0500, Alexander Strange wrote:
>
> On Jan 31, 2010, at 8:08 AM, 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.
>
> It's not legal to cast a pointer like that, so any type cast without extra attributes can't work with strict aliasing:
>
> C99 6.3.2.3 p7:
> A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type.
fine
> If the resulting pointer is not correctly aligned for the pointed-to type, the behavior is undefined.
yes, of course, all accesses thrugh *intXY_t are aligned, or we use special
types marked with attributes that have no alignment requirements or plain char
> Otherwise, when converted back again, the result shall compare equal to the original pointer.
not relevant to us here
>When a pointer to an object is
> converted to a pointer to a character type, the result points to the lowest addressed byte of the object.
not relevant to us here
> Successive increments of the result, up to the size of the object, yield pointers to the remaining bytes of the object.
not relevant to us here
>
> There's no text about dereferencing a converted pointer, so it's undefined.
6.5.7 Expressions ISO/IEC 9899:TC2 Draft May 6 2005 (latest i have)
7 An object shall have its stored value accessed only by an lvalue expression that has one of
the following types:74)
a type compatible with the effective type of the object,
a qualified version of a type compatible with the effective type of the object,
a type that is the signed or unsigned type corresponding to the effective type of the
object,
a type that is the signed or unsigned type corresponding to a qualied version of the
effective type of the object,
an aggregate or union type that includes one of the aforementioned types among its
members (including, recursively, a member of a subaggregate or contained union), or
a character type.
74) The intent of this list is to specify those circumstances in which an object may or may not be aliased.
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
The real ebay dictionary, page 2
"100% positive feedback" - "All either got their money back or didnt complain"
"Best seller ever, very honest" - "Seller refunded buyer after failed scam"
-------------- 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/20100201/ff014683/attachment.pgp>
More information about the ffmpeg-devel
mailing list