This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: -fstrict-aliasing and naughty code?




>> Provided I make (wild?) assumptions about the host and compiler, can I
>> instead write the above to use something like:
>> 
>> union {
>> unsigned64 u64;
>> unsigned32 u32[2];
>> } tmp_reg, tmp_reg1;
>> 
>> for (i = 0; i < 4; i++)
>> if (i < 2)
>> tmp_reg.u32[1 - i % 2] = ...
>> else
>> tmp_reg1.u32[1 - i %2] = ...;
>> cpu->registers[...] = tmp_reg.u64;
> 
> 
> Yes, this is documented to work:

Ok, thanks.

>      The practice of reading from a different union member than the one
>      most recently written to (called "type-punning") is common.  Even
>      with `-fstrict-aliasing', type-punning is allowed, provided the
>      memory is accessed through the union type.
> 
> However, it will be no more efficient than the more portable
> 
>   unsigned32 tmp_reg[2], tmp_reg1[2];
> 
>   for (i = 0; i < 4; i++)
>   if (i < 2)
>     tmp_reg[1 - i % 2] = ...
>   else
>     tmp_reg1[1 - i %2] = ...;
> cpu->registers[...] = (unsigned64)tmp_reg[0] << 32 | tmp_reg[1];
> 
> in fact it will usually be less efficient because GCC will allocate
> registers better for the second example.

Ok.   It's generated sim code - portability isn't important speed is :-)

thanks,
Andrew




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]