Accessing the vector elements

Chris Lattner clattner@apple.com
Tue Oct 24 06:01:00 GMT 2006


On Oct 23, 2006, at 10:42 PM, Ian Lance Taylor wrote:

> 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.

Right.

> 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.

The compiler shouldn't break that aliasing, and using a union will  
give you poor code anyway.  LLVM will optimize either idiom into  
element extract/insert operations, but I don't think GCC will in an  
any case.  If you care about performance with GCC, you have to use  
ISA-specific builtins.

-Chris



More information about the Gcc-help mailing list