This is the mail archive of the gcc-bugs@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]

[Bug tree-optimization/15556] Idiom for rotations of 16bit values not recognized


------- Additional Comments From bernie at develer dot com  2004-07-23 02:41 -------
The commonly used byte-swapping idiom also produces
sub-optimal code (GCC 3.4.1 on x86):

 unsigned short swab16(unsigned short x)
 {
     return (x >> 8) | (x << 8);
 }

swab16:
       movzwl  4(%esp), %edx
       movl    %edx, %eax
       shrw    $8, %ax
       sall    $8, %edx
       orl     %edx, %eax
       movzwl  %ax, %eax
       ret

Assing a few useless masks helps GCC produce
somewhat better code:


unsigned short swab16(unsigned short x)
{
  return ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00);
}

swab16:
        movl    4(%esp), %eax
        rolw    $8, %ax
        movzwl  %ax, %eax
        ret


A single xchg instruction would suffice on x86.
Both the Linux kernel and glibc are using inline
asm to workaround this missing optimization.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15556


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