History

byteorder: allow arch to opt to use GCC intrinsics for byteswapping

Since GCC 4.4, there have been __builtin_bswap32() and __builtin_bswap16() intrinsics. A __builtin_bswap16() came a little later (4.6 for PowerPC, 48 for other platforms). By using these instead of the inline assembler that most architectures have in their __arch_swabXX() macros, we let the compiler see what's actually happening. The resulting code should be at least as good, and much *better* in the cases where it can be combined with a nearby load or store, using a load-and-byteswap or store-and-byteswap instruction (e.g. lwbrx/stwbrx on PowerPC, movbe on Atom). When GCC is sufficiently recent *and* the architecture opts in to using the intrinsics by setting CONFIG_ARCH_USE_BUILTIN_BSWAP, they will be used in preference to the __arch_swabXX() macros. An architecture which does not set ARCH_USE_BUILTIN_BSWAP will continue to use its own hand-crafted macros.

David Woodhouse committed 13 years ago

Commit cf66bb91 parent 27d7c2a4 files · +45 −3Browse files

Showing the changes to include/uapi (1 of 4 files). Show all

Unified · Split

include/uapi/linux/swab.h+9−3
@@ -45,7 +45,9 @@ · 44 unmodified lines
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) · 2 unmodified lines
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) · 2 unmodified lines
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;