This is the mail archive of the egcs@egcs.cygnus.com mailing list for the EGCS project. See the EGCS home page for more information.


[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index] [Subject Index] [Author Index] [Thread Index]

Re: unalignment override



Let me add a bit to this --

On Tue, Feb 23, 1999 at 10:03:53AM -0800, Joe Buck wrote:
> The compiler is allowed to assume that a pointer to int
> is always properly aligned, and therefore reduces
> 
> 	if ((long)address & 3)
> to
> 	if (false)

We're actually not doing quite this, but instead using the
known alignment of the int* to optimize

	*((unsigned char *)address+1)

If we were not allowed to make this assumption, the performance
of references to/from most structs would drop considerably.

> You're close.  Change the argument type to
> 
> 	unsigned char* address

Also, better is to write that function like so

	static inline int read_dword (void *address)
	{
	  struct {
	    int data __attribute__((packed));
	  } *p = address;

	  return p->data;
	}

Gcc knows how to optimize unaligned loads described in this manner.


r~