This is the mail archive of the
mailing list for the GCC project.
Re: [PATCH] Optimize manual byte swap implementations using the GCC bswap builtin
On Fri, 2008-11-28 at 17:04 +0000, Andrew Haley wrote:
> Andreas Krebbel wrote:
> >> Sure. I just want to make sure that whatever optimization you've done
> >> actually hits the places where it's important.
> > Yepp. htons is a good point. Coming from a big endian target it's not
> > the first thing I care about ;) Actually I'm wondering why s390 provides
> > an accelerated bswap version in glibc at all?! But it is probably also
> > useful in other contexts.
> > Of course there are common byte swap implementations which the bswap
> > pass does not cover. E.g. something like that:
> > a = (a << 16) | (a >> 16);
> > a = ((a & 0x00ff00ff) << 8) | ((a & 0xff00ff00) >> 8);
> > But I'm not sure how important it is. It don't think it occurs very often.
> Sure. I would have guessed that something like
> a = ((a << 24)
> | (((u8)(a>>8) << 16))
> | (((u8)(a>>16) << 8))
> | (a >> 24));
> was also common, but you've looked at the kernel sources.
Browsing Google Code Search, I've found the following:
#define rotr(x,n) ((x >> n) | (x << (32 - n)))
#define rotl(x,n) ((x << n) | (x >> (32 - n)))
#define bswap(x) (rotl(x, 8) & 0x00ff00ff | rotr(x, 8) & 0xff00ff00)
which is probably important enough to recognize.
Another somewhat common idiom is:
# define swab32(x)\
((((uint8_t*)&x) << 24) | (((uint8_t*)&x) << 16) | \
(((uint8_t*)&x) << 8) | (((uint8_t*)&x)))
primarily found in media libraries (a52dec, libdv, libdts, libogmdvd, etc.).
AFAICT, the idiom used by the Linux kernel and glibc is by far the most
Nicholas Miell <firstname.lastname@example.org>