missed optimization compiling naive get_unaligned_le32 on x86

Zeev Tarantov zeev.tarantov@gmail.com
Fri Apr 15 16:25:00 GMT 2011


When compiling this code:

unsigned int get_le32(unsigned char *p)
{
  return p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24;
}

On gcc 4.6.0 rev. 172266 for x86-64, I get:

        movzbl  1(%rdi), %eax
        movzbl  2(%rdi), %edx
        sall    $8, %eax
        sall    $16, %edx
        orl     %edx, %eax
        movzbl  (%rdi), %edx
        orl     %edx, %eax
        movzbl  3(%rdi), %edx
        sall    $24, %edx
        orl     %edx, %eax
        ret

I hoped for much better code. I hoped to avoid ifdef's depending on
endianess, but this means I can't.
Am I missing something obvious that precludes the compiler from
optimizing the expression?
This is not a regression and other compilers didn't do any better, so
I hope I'm just missing something.

thanks,
-Z.T.



More information about the Gcc-help mailing list