Bug 2803

Summary: uClibc should use __builtin_bswap{32,64} on at least some archs
Product: uClibc Reporter: Peter Collingbourne <peter>
Component: OtherAssignee: unassigned
Status: NEW ---    
Severity: normal CC: uclibc-cvs
Priority: P5    
Version: unspecified   
Target Milestone: ---   
Hardware: All   
OS: All   
Host: Target:
Build:
Attachments: [PATCH] byteswap: use compiler builtins if available

Description Peter Collingbourne 2010-11-13 20:18:33 UTC
Created attachment 2665 [details]
[PATCH] byteswap: use compiler builtins if available

GCC has provided the builtins __builtin_bswap32 and __builtin_bswap64 since 4.2.  If we have a sufficient GCC version, we should use the builtins instead of our own implementation.  This allows the compiler to apply optimisations (e.g. bswap(bswap(x)) = x).

Attached is a patch which should act as a starting point.

One issue to be decided: on which architectures should we use the builtin?  The patch uses the builtin on all architectures, but looking through the gcc source code it appears that the set of optimised bswap implementations overlaps but differs between gcc and uClibc.  gcc covers all the major architectures though, so this may not be such a crucial point.
Comment 1 Bernhard Reutner-Fischer 2012-04-17 21:29:34 UTC
IIRC you should get a bswap pass in -fexpensive-optimizations
Comment 2 Mike Frysinger 2012-04-19 03:46:29 UTC
Comment on attachment 2665 [details]
[PATCH] byteswap: use compiler builtins if available

i don't think it should be blindly overriding the arch code.  using the builtins should be done only if the arch hasn't selected it.

look at glibc's bits/byteswap.h.