[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/1] xen/include/xen/byteorder: Re-implement bswap/swab with compiler builtin functions


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Lin Liu <lin.liu@xxxxxxxxxx>
  • Date: Fri, 15 Oct 2021 07:26:17 +0000
  • Authentication-results: esa3.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: <sstabellini@xxxxxxxxxx>, <julien@xxxxxxx>, <Volodymyr_Babchuk@xxxxxxxx>, <andrew.cooper3@xxxxxxxxxx>, <George.Dunlap@xxxxxxxxxxxxx>, <ian.jackson@xxxxxxxxxxxxx>, <jbeulich@xxxxxxxx>, <konrad.wilk@xxxxxxxxxx>, <wl@xxxxxxx>, <roger.pau@xxxxxxxxxx>, Lin Liu <lin.liu@xxxxxxxxxx>
  • Delivery-date: Fri, 15 Oct 2021 07:27:49 +0000
  • Ironport-data: A9a23:vZkcJKksef0+p8jPt1pajGvo5gwlIURdPkR7XQ2eYbSJt1+Wr1Gzt xJNDG/Xbv2PYzGkeYt0YIi+oR5Q7Z+Gz9dkTVds/3s8ECMWpZLJC+rCIxarNUt+DCFioGGLT Sk6QoOdRCzhZiaE/n9BClVlxJVF/fngqoDUUYYoAQgsA185IMsdoUg7wbdg2tc12YHR7z6l4 rseneWOYDdJ5BYsWo4kw/rrRMRH5amaVJsw5zTSVNgT1LPsvyB94KE3fMldG0DQUIhMdtNWc s6YpF2PEsE1yD92Yj+tuu6TnkTn2dc+NyDW4pZdc/DKbhSvOkXee0v0XRYRQR4/ttmHozx+4 NYcpIebeVwWBL3j3+05UEVfCg11H6ITrdcrIVDn2SCS50jPcn+qyPRyFkAme4Yf/46bA0kXq 6ZecmpUKEne2aTmm9pXScE17ignBM3qOsUbu2xIxjDFF/c2B5vERs0m4PcFgW1h2pwWTZ4yY eI+OAJqa0zyXiFsPwk9L80vsNuBpUDWJmgwRFW9+vNsvjm7IBZK+LvqNsDcdpqVRMFW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GuuJzfOc9RNkQKI yOPgPuQjrtR8UPObn4vGCWfxuFan5kP5EhHwVJqy45lQTYZaivbBCFszAk=
  • Ironport-hdrordr: A9a23:nyS6E6qx96seHCkZA94/1IkaV5oTeYIsimQD101hICG8cqSj+f xG+85rsyMc6QxhIE3I9urhBEDtex/hHNtOkOws1NSZLW7bUQmTXeJfBOLZqlWKcUDDH6xmpM NdmsBFeaTN5DNB7PoSjjPWLz9Z+qjkzJyV
  • Ironport-sdr: 2rii4GPjgz2KYojilzVWsjmYgpchkTqBq3ssPa/EXXXVQ4lieqFDqlJpZcxgepz2H/Sf+GCmJw LU+TzuGNyXy0/ob4q7zRh4547ClC/YwlR1G8naGs7q60KVLDYWVCuwkU+fvWLu3OlHqNc68iOo N2ZtvrLl/ycAkFPin9IKYpZOQI12EoMnokGiHCgOkKJawSwr3Jagh/t80Q6T06usiO7zg02NWN 1R6JUrWzkTkPiXcuoYCR+IGaOLn1VpUp+uCnoDYk4+dKD6bkK9g5dORG7W4J0YdCI5fbza1qIB IkVVsVf1ZxdhzeLPQlhgV9bl
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

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.liu@xxxxxxxxxx>
---
 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




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.