ARM memcpy on unaligned pointers and -O2

Richard Earnshaw rearnsha@arm.com
Thu Dec 13 09:29:00 GMT 2001


> > No, your pointer is aligned.  You declared it to be so in the declarations
> > of your functions.  Having done this, the compiler can and does use this
> > knowledge to optimise the code.
> >
> > R.
> 
> I recognized that afterwords and cast the arguments to memcpy()
> 
> uint32
> ld32( const uint32* p)
> {
> 	uint32 ret;
> 	memcpy( &ret, (uint8*)p, sizeof( ret));
> 	return ret;
> }
> 
> void
> str32( uint32* p, uint32 val)
> {
> 	memcpy( (uint8*)p, &val, sizeof( *p));
> }
> 
> It seems the behaviour persists though. In fact, it has the exact same 
> behaviour if I change the parameters from uint32* to void*.

No.  Your pointer is still aligned, and the cast has no effect (there 
already was an implicit cast in the prototype).

The only way you can make this work is to change the signatures of str32 
and ld32 to take void* or char* pointers.  Anything else is just lying to 
the compiler about the type of pointer you are passing.

R.



More information about the Gcc-bugs mailing list