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]

-fstrict-aliasing and naughty code?


Hello,

I'm trying to understand how to write ``bad'' (host dependant) code that 
doesn't get screwed by strict aliasing.

For instance, the code snipit:

>  unsigned i;
>       unsigned64 tmp_reg, tmp_reg1;
> 
>       for (i = 0; i < 4; i++)
> 	*( (i < 2 ? (unsigned32 *) &tmp_reg
> 	          : (unsigned32 *) &tmp_reg1)
> 	   + (1 - i % 2) ) = ...;
>       cpu->registers[...] = tmp_reg;
> 

I'm told, is bad because:

> apparently, when -fstrict-aliasing is in effect, gcc is
> allowed to assume that the expression inside the for loop
> has no effect on the value of tmp_reg and tmp_reg1, since
> the assignment is to an object of dissimilar type.

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;

or even

	union {
	   unsigned64 u64;
	  struct {
	    unsigned32 a0;
	    unsigned32 a1;
	  } u32;
	} tmp_reg, tmp_reg1;

        for (i = 0; i < 4; i++)
	  switch (i)
	  case 0: tmp_reg.u32.a1 = ...; break;
	  ....
	cpu->registers[...] = tmp_reg.u64;

Andrew


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