Skip to contentSkip to conversation

xen/include/xen/byteorder: Re-implement bswap/swab with compiler builtin functions

PATCH In review Lin Liu 2 participants 2 messages Oct 15, 2021 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Lin Liu

author
#1 lore raw

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

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

---
 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(-)
xen/arch/arm/arm64/lib/find_next_bit.c +4 -4
index 17cb176266..c03eb9d175 100644
@@ -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
xen/include/asm-x86/byteorder.h +0 -25
index 1f77e502a5..9c26b815b9 100644
@@ -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>
xen/include/xen/byteorder/big_endian.h +0 -12
index 40eb80a390..4d43d9b7b8 100644
@@ -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)))
xen/include/xen/byteorder/little_endian.h +0 -12
index 4955632793..29b0bd2d5c 100644
@@ -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))
xen/include/xen/byteorder/swab.h +15 -149

181 lines not on this page · raw message

Signature · 1 line
2.27.0
#2 lore raw
Quoted 7 lines · Lin Liu

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

__builtin_bswap{32,64}() appeared in gcc 4.3, __builtin_bswap16() in gcc 4.8. I don't think we can use these just yet without having any fallback. Apart from this aspect the change looks okay to me, with just one nit:

Quoted 17 lines
xen/include/xen/byteorder/swab.h +0 -11
@@ -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) ));      \
-})
-

xen/include/xen/byteorder/swab.h line 13

Quoted 10 lines
xen/include/xen/byteorder/swab.h +1 -9
-#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))

In cases like this one there's no need for the outer pair of parentheses.

Jan