Current implementation of bswap/swab is overhaul which involves byte operations, compilers has builtin functions can help to simply the implementation * GCC: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html * Clang: https://clang.llvm.org/docs/LanguageExtensions.html
This commit replace the implementation with compiler builtins Signed-off-by: Lin Liu <lin....@citrix.com> --- xen/arch/arm/arm64/lib/find_next_bit.c | 8 +- xen/include/asm-x86/byteorder.h | 25 ---- xen/include/xen/byteorder/big_endian.h | 12 -- xen/include/xen/byteorder/little_endian.h | 12 -- xen/include/xen/byteorder/swab.h | 164 ++-------------------- 5 files changed, 19 insertions(+), 202 deletions(-) diff --git a/xen/arch/arm/arm64/lib/find_next_bit.c b/xen/arch/arm/arm64/lib/find_next_bit.c index 17cb176266..c03eb9d175 100644 --- a/xen/arch/arm/arm64/lib/find_next_bit.c +++ b/xen/arch/arm/arm64/lib/find_next_bit.c @@ -167,9 +167,9 @@ EXPORT_SYMBOL(find_first_zero_bit); static inline unsigned long ext2_swabp(const unsigned long * x) { #if BITS_PER_LONG == 64 - return (unsigned long) __swab64p((u64 *) x); + return (unsigned long) swab64p((u64 *) x); #elif BITS_PER_LONG == 32 - return (unsigned long) __swab32p((u32 *) x); + return (unsigned long) swab32p((u32 *) x); #else #error BITS_PER_LONG not defined #endif @@ -179,9 +179,9 @@ static inline unsigned long ext2_swabp(const unsigned long * x) static inline unsigned long ext2_swab(const unsigned long y) { #if BITS_PER_LONG == 64 - return (unsigned long) __swab64((u64) y); + return (unsigned long) swab64((u64) y); #elif BITS_PER_LONG == 32 - return (unsigned long) __swab32((u32) y); + return (unsigned long) swab32((u32) y); #else #error BITS_PER_LONG not defined #endif diff --git a/xen/include/asm-x86/byteorder.h b/xen/include/asm-x86/byteorder.h index 1f77e502a5..9c26b815b9 100644 --- a/xen/include/asm-x86/byteorder.h +++ b/xen/include/asm-x86/byteorder.h @@ -4,31 +4,6 @@ #include <asm/types.h> #include <xen/compiler.h> -static inline __attribute_const__ __u32 ___arch__swab32(__u32 x) -{ - asm("bswap %0" : "=r" (x) : "0" (x)); - return x; -} - -static inline __attribute_const__ __u64 ___arch__swab64(__u64 val) -{ - union { - struct { __u32 a,b; } s; - __u64 u; - } v; - v.u = val; - asm("bswapl %0 ; bswapl %1 ; xchgl %0,%1" - : "=r" (v.s.a), "=r" (v.s.b) - : "0" (v.s.a), "1" (v.s.b)); - return v.u; -} - -/* Do not define swab16. Gcc is smart enough to recognize "C" version and - convert it into rotation or exhange. */ - -#define __arch__swab64(x) ___arch__swab64(x) -#define __arch__swab32(x) ___arch__swab32(x) - #define __BYTEORDER_HAS_U64__ #include <xen/byteorder/little_endian.h> diff --git a/xen/include/xen/byteorder/big_endian.h b/xen/include/xen/byteorder/big_endian.h index 40eb80a390..4d43d9b7b8 100644 --- a/xen/include/xen/byteorder/big_endian.h +++ b/xen/include/xen/byteorder/big_endian.h @@ -11,18 +11,6 @@ #include <xen/types.h> #include <xen/byteorder/swab.h> -#define __constant_cpu_to_le64(x) ((__force __le64)___constant_swab64((x))) -#define __constant_le64_to_cpu(x) ___constant_swab64((__force __u64)(__le64)(x)) -#define __constant_cpu_to_le32(x) ((__force __le32)___constant_swab32((x))) -#define __constant_le32_to_cpu(x) ___constant_swab32((__force __u32)(__le32)(x)) -#define __constant_cpu_to_le16(x) ((__force __le16)___constant_swab16((x))) -#define __constant_le16_to_cpu(x) ___constant_swab16((__force __u16)(__le16)(x)) -#define __constant_cpu_to_be64(x) ((__force __be64)(__u64)(x)) -#define __constant_be64_to_cpu(x) ((__force __u64)(__be64)(x)) -#define __constant_cpu_to_be32(x) ((__force __be32)(__u32)(x)) -#define __constant_be32_to_cpu(x) ((__force __u32)(__be32)(x)) -#define __constant_cpu_to_be16(x) ((__force __be16)(__u16)(x)) -#define __constant_be16_to_cpu(x) ((__force __u16)(__be16)(x)) #define __cpu_to_le64(x) ((__force __le64)__swab64((x))) #define __le64_to_cpu(x) __swab64((__force __u64)(__le64)(x)) #define __cpu_to_le32(x) ((__force __le32)__swab32((x))) diff --git a/xen/include/xen/byteorder/little_endian.h b/xen/include/xen/byteorder/little_endian.h index 4955632793..29b0bd2d5c 100644 --- a/xen/include/xen/byteorder/little_endian.h +++ b/xen/include/xen/byteorder/little_endian.h @@ -11,18 +11,6 @@ #include <xen/types.h> #include <xen/byteorder/swab.h> -#define __constant_cpu_to_le64(x) ((__force __le64)(__u64)(x)) -#define __constant_le64_to_cpu(x) ((__force __u64)(__le64)(x)) -#define __constant_cpu_to_le32(x) ((__force __le32)(__u32)(x)) -#define __constant_le32_to_cpu(x) ((__force __u32)(__le32)(x)) -#define __constant_cpu_to_le16(x) ((__force __le16)(__u16)(x)) -#define __constant_le16_to_cpu(x) ((__force __u16)(__le16)(x)) -#define __constant_cpu_to_be64(x) ((__force __be64)___constant_swab64((x))) -#define __constant_be64_to_cpu(x) ___constant_swab64((__force __u64)(__be64)(x)) -#define __constant_cpu_to_be32(x) ((__force __be32)___constant_swab32((x))) -#define __constant_be32_to_cpu(x) ___constant_swab32((__force __u32)(__be32)(x)) -#define __constant_cpu_to_be16(x) ((__force __be16)___constant_swab16((x))) -#define __constant_be16_to_cpu(x) ___constant_swab16((__force __u16)(__be16)(x)) #define __cpu_to_le64(x) ((__force __le64)(__u64)(x)) #define __le64_to_cpu(x) ((__force __u64)(__le64)(x)) #define __cpu_to_le32(x) ((__force __le32)(__u32)(x)) diff --git a/xen/include/xen/byteorder/swab.h b/xen/include/xen/byteorder/swab.h index b7e30f0503..babdbe2387 100644 --- a/xen/include/xen/byteorder/swab.h +++ b/xen/include/xen/byteorder/swab.h @@ -10,166 +10,32 @@ * to clean up support for bizarre-endian architectures. */ -/* casts are necessary for constants, because we never know how for sure - * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way. - */ -#define ___swab16(x) \ -({ \ - __u16 __x = (x); \ - ((__u16)( \ - (((__u16)(__x) & (__u16)0x00ffU) << 8) | \ - (((__u16)(__x) & (__u16)0xff00U) >> 8) )); \ -}) - -#define ___swab32(x) \ -({ \ - __u32 __x = (x); \ - ((__u32)( \ - (((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \ - (((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \ - (((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \ - (((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \ -}) +#define __swab16(x) (__builtin_bswap16(x)) -#define ___swab64(x) \ -({ \ - __u64 __x = (x); \ - ((__u64)( \ - (__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \ - (__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \ - (__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \ - (__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) << 8) | \ - (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >> 8) | \ - (__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \ - (__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \ - (__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \ -}) +#define __swab32(x) (__builtin_bswap32(x)) \ -#define ___constant_swab16(x) \ - ((__u16)( \ - (((__u16)(x) & (__u16)0x00ffU) << 8) | \ - (((__u16)(x) & (__u16)0xff00U) >> 8) )) -#define ___constant_swab32(x) \ - ((__u32)( \ - (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \ - (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \ - (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \ - (((__u32)(x) & (__u32)0xff000000UL) >> 24) )) -#define ___constant_swab64(x) \ - ((__u64)( \ - (__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \ - (__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \ - (__u64)(((__u64)(x) & (__u64)0x0000000000ff0000ULL) << 24) | \ - (__u64)(((__u64)(x) & (__u64)0x00000000ff000000ULL) << 8) | \ - (__u64)(((__u64)(x) & (__u64)0x000000ff00000000ULL) >> 8) | \ - (__u64)(((__u64)(x) & (__u64)0x0000ff0000000000ULL) >> 24) | \ - (__u64)(((__u64)(x) & (__u64)0x00ff000000000000ULL) >> 40) | \ - (__u64)(((__u64)(x) & (__u64)0xff00000000000000ULL) >> 56) )) - -/* - * provide defaults when no architecture-specific optimization is detected - */ -#ifndef __arch__swab16 -# define __arch__swab16(x) ({ __u16 __tmp = (x) ; ___swab16(__tmp); }) -#endif -#ifndef __arch__swab32 -# define __arch__swab32(x) ({ __u32 __tmp = (x) ; ___swab32(__tmp); }) -#endif -#ifndef __arch__swab64 -# define __arch__swab64(x) ({ __u64 __tmp = (x) ; ___swab64(__tmp); }) -#endif +#define __swab64(x) (__builtin_bswap64(x)) \ -#ifndef __arch__swab16p -# define __arch__swab16p(x) __arch__swab16(*(x)) +#ifndef __swab16p +# define __swab16p(x) __swab16(*(x)) #endif -#ifndef __arch__swab32p -# define __arch__swab32p(x) __arch__swab32(*(x)) +#ifndef __swab32p +# define __swab32p(x) __swab32(*(x)) #endif -#ifndef __arch__swab64p -# define __arch__swab64p(x) __arch__swab64(*(x)) +#ifndef __swab64p +# define __swab64p(x) __swab64(*(x)) #endif -#ifndef __arch__swab16s -# define __arch__swab16s(x) do { *(x) = __arch__swab16p((x)); } while (0) +#ifndef __swab16s +# define __swab16s(x) do { *(x) = __swab16p((x)); } while (0) #endif -#ifndef __arch__swab32s -# define __arch__swab32s(x) do { *(x) = __arch__swab32p((x)); } while (0) +#ifndef __swab32s +# define __swab32s(x) do { *(x) = __swab32p((x)); } while (0) #endif -#ifndef __arch__swab64s -# define __arch__swab64s(x) do { *(x) = __arch__swab64p((x)); } while (0) +#ifndef __swab64s +# define __swab64s(x) do { *(x) = __swab64p((x)); } while (0) #endif - -/* - * Allow constant folding - */ -#if defined(__GNUC__) && defined(__OPTIMIZE__) -# define __swab16(x) \ -(__builtin_constant_p((__u16)(x)) ? \ - ___swab16((x)) : \ - __fswab16((x))) -# define __swab32(x) \ -(__builtin_constant_p((__u32)(x)) ? \ - ___swab32((x)) : \ - __fswab32((x))) -# define __swab64(x) \ -(__builtin_constant_p((__u64)(x)) ? \ - ___swab64((x)) : \ - __fswab64((x))) -#else -# define __swab16(x) __fswab16(x) -# define __swab32(x) __fswab32(x) -# define __swab64(x) __fswab64(x) -#endif /* OPTIMIZE */ - - -static inline __attribute_const__ __u16 __fswab16(__u16 x) -{ - return __arch__swab16(x); -} -static inline __u16 __swab16p(const __u16 *x) -{ - return __arch__swab16p(x); -} -static inline void __swab16s(__u16 *addr) -{ - __arch__swab16s(addr); -} - -static inline __attribute_const__ __u32 __fswab32(__u32 x) -{ - return __arch__swab32(x); -} -static inline __u32 __swab32p(const __u32 *x) -{ - return __arch__swab32p(x); -} -static inline void __swab32s(__u32 *addr) -{ - __arch__swab32s(addr); -} - -#ifdef __BYTEORDER_HAS_U64__ -static inline __attribute_const__ __u64 __fswab64(__u64 x) -{ -# ifdef __SWAB_64_THRU_32__ - __u32 h = x >> 32; - __u32 l = x & ((1ULL<<32)-1); - return (((__u64)__swab32(l)) << 32) | ((__u64)(__swab32(h))); -# else - return __arch__swab64(x); -# endif -} -static inline __u64 __swab64p(const __u64 *x) -{ - return __arch__swab64p(x); -} -static inline void __swab64s(__u64 *addr) -{ - __arch__swab64s(addr); -} -#endif /* __BYTEORDER_HAS_U64__ */ - #define swab16 __swab16 #define swab32 __swab32 #define swab64 __swab64 -- 2.27.0