This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug tree-optimization/15556] Idiom for rotations of 16bit values not recognized
- From: "bernie at develer dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 23 Jul 2004 02:41:39 -0000
- Subject: [Bug tree-optimization/15556] Idiom for rotations of 16bit values not recognized
- References: <20040520191543.15556.cesarb@nitnet.com.br>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- 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