Accessing the vector elements

Ian Lance Taylor iant@google.com
Tue Oct 24 05:42:00 GMT 2006


Chris Lattner <clattner@apple.com> writes:

> >     union
> >     {
> >       vectortype v;
> >       float af[4];
> >     } u;
> >     u.v = v;
> >     return u.af[0];
> >
> > Ian
> 
> Or, alternatively:
> 
> vectortype v;
> 
> elementtype e = ((elementtype*)&v)[1]

Well, I don't recommend that in general.  gcc currently has an
exception to the aliasing rules, and treats vectortype* and
elementtype* as being in the same alias set.  But at my previous
company, we got significantly better generated code in some cases by
breaking that aliasing.  In particular this aliasing forces a pointer
to a vector of char to alias char* and therefore alias everything.
But if the compiler breaks that aliasing, then your suggestion of
using a cast will fail in some cases.

Ian



More information about the Gcc-help mailing list