Skip to content

Commit 7f2de81

Browse files
committed
Merge tag 'byteswap-for-linus-20121219' of git://git.infradead.org/users/dwmw2/byteswap
Pull preparatory gcc intrisics bswap patch from David Woodhouse: "This single patch is effectively a no-op for now. It enables architectures to opt in to using GCC's __builtin_bswapXX() intrinsics for byteswapping, and if we merge this now then the architecture maintainers can enable it for their arch during the next cycle without dependency issues. It's worth making it a par-arch opt-in, because although in *theory* the compiler should never do worse than hand-coded assembler (and of course it also ought to do a lot better on platforms like Atom and PowerPC which have load-and-swap or store-and-swap instructions), that isn't always the case. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46453 for example." * tag 'byteswap-for-linus-20121219' of git://git.infradead.org/users/dwmw2/byteswap: byteorder: allow arch to opt to use GCC intrinsics for byteswapping
2 parents 5977107 + cf66bb9 commit 7f2de81

4 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎arch/Kconfig‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,25 @@ config HAVE_EFFICIENT_UNALIGNED_ACCESS
113113
See Documentation/unaligned-memory-access.txt for more
114114
information on the topic of unaligned memory accesses.
115115

116+
config ARCH_USE_BUILTIN_BSWAP
117+
bool
118+
help
119+
Modern versions of GCC (since 4.4) have builtin functions
120+
for handling byte-swapping. Using these, instead of the old
121+
inline assembler that the architecture code provides in the
122+
__arch_bswapXX() macros, allows the compiler to see what's
123+
happening and offers more opportunity for optimisation. In
124+
particular, the compiler will be able to combine the byteswap
125+
with a nearby load or store and use load-and-swap or
126+
store-and-swap instructions if the architecture has them. It
127+
should almost *never* result in code which is worse than the
128+
hand-coded assembler in <asm/swab.h>. But just in case it
129+
does, the use of the builtins is optional.
130+
131+
Any architecture with load-and-swap or store-and-swap
132+
instructions should set this. And it shouldn't hurt to set it
133+
on architectures that don't have such instructions.
134+
116135
config HAVE_SYSCALL_WRAPPERS
117136
bool
118137

‎include/linux/compiler-gcc4.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,13 @@
6363
#define __compiletime_warning(message) __attribute__((warning(message)))
6464
#define __compiletime_error(message) __attribute__((error(message)))
6565
#endif
66+
67+
#ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP
68+
#if __GNUC_MINOR__ >= 4
69+
#define __HAVE_BUILTIN_BSWAP32__
70+
#define __HAVE_BUILTIN_BSWAP64__
71+
#endif
72+
#if __GNUC_MINOR__ >= 8 || (defined(__powerpc__) && __GNUC_MINOR__ >= 6)
73+
#define __HAVE_BUILTIN_BSWAP16__
74+
#endif
75+
#endif

‎include/linux/compiler-intel.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@
2929
#endif
3030

3131
#define uninitialized_var(x) x
32+
33+
#ifndef __HAVE_BUILTIN_BSWAP16__
34+
/* icc has this, but it's called _bswap16 */
35+
#define __HAVE_BUILTIN_BSWAP16__
36+
#define __builtin_bswap16 _bswap16
37+
#endif
38+

‎include/uapi/linux/swab.h‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545

4646
static inline __attribute_const__ __u16 __fswab16(__u16 val)
4747
{
48-
#ifdef __arch_swab16
48+
#ifdef __HAVE_BUILTIN_BSWAP16__
49+
return __builtin_bswap16(val);
50+
#elif defined (__arch_swab16)
4951
return __arch_swab16(val);
5052
#else
5153
return ___constant_swab16(val);
@@ -54,7 +56,9 @@ static inline __attribute_const__ __u16 __fswab16(__u16 val)
5456

5557
static inline __attribute_const__ __u32 __fswab32(__u32 val)
5658
{
57-
#ifdef __arch_swab32
59+
#ifdef __HAVE_BUILTIN_BSWAP32__
60+
return __builtin_bswap32(val);
61+
#elif defined(__arch_swab32)
5862
return __arch_swab32(val);
5963
#else
6064
return ___constant_swab32(val);
@@ -63,7 +67,9 @@ static inline __attribute_const__ __u32 __fswab32(__u32 val)
6367

6468
static inline __attribute_const__ __u64 __fswab64(__u64 val)
6569
{
66-
#ifdef __arch_swab64
70+
#ifdef __HAVE_BUILTIN_BSWAP64__
71+
return __builtin_bswap64(val);
72+
#elif defined (__arch_swab64)
6773
return __arch_swab64(val);
6874
#elif defined(__SWAB_64_THRU_32__)
6975
__u32 h = val >> 32;

0 commit comments

Comments
 (0)