This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Idioms for byteswapping and unaligned memory access
On Thu, Apr 20, 2006 at 08:38:00AM -0700, H. J. Lu wrote:
> On Thu, Apr 20, 2006 at 05:18:08PM +0200, Olivier Galibert wrote:
> > I need to be able to do unaligned memory accesses to memory in
> > big-endian or little-endian mode. For portability, I'd like to do it
> > in pure C, but I'd like the compiler to generate optimal sequences for
> > the operations. Most CPUs that I know of even have special
> > instructions designed to speed up part or all of these operations.
> >
> > So I'm looking for ways of writing these to-be-inlined elemental
> > functions in C that gcc will recognize as such, while still working
> > correctly, if more slowly, for other compilers.
> >
>
> bfd does that.
Not inline-able:
#include <bfd.h>
unsigned short read_16_le(const unsigned char *adr)
{
return bfd_getl16(adr);
}
gives:
subl $12, %esp
movl 16(%esp), %eax
movl %eax, (%esp)
call bfd_getl16
movzwl %ax, %eax
addl $12, %esp
ret
instead of the expected:
movl 4(%esp), %eax
movzwl (%eax), %eax
ret
OG.