xen: implement byteswap
PATCH v5 1/6 xen: implement byteswap
In reply to a message not in the archiveswab() is massively over complicated and can be simplified by re-implementing using compiler builtins.
The compilers provide builtin function to swap bytes.
* gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
* clang: https://clang.llvm.org/docs/LanguageExtensions.html
This patch introduces a new byteswapping infrastructure in terms of compiler builtins and bswapXX(), so the swab() infrastructure can be retired.
---
Cc: Stefano Stabellini <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Bertrand Marquis <[email protected]>
Cc: Volodymyr Babchuk <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: "Roger Pau Monné" <[email protected]> Changes in v5:
- Move PASTE back to xen/lib.h
- Define PASTE for tools in xen-tools/libs.h
Changes in v4:
- Move PASTE definition to xen/compiler.h to support tools
- Revert emacs magics
Changes in v3:
- Check __has_builtin instead of GNUC version
Changes in v2:
- Add fallback for compilers without __builtin_bswap
- Implement with plain C instead of macros
--- tools/include/xen-tools/libs.h | 4 ++ xen/arch/arm/include/asm/byteorder.h | 6 ++- xen/arch/x86/include/asm/byteorder.h | 34 ++--------------- xen/include/xen/byteorder.h | 56 ++++++++++++++++++++++++++++ xen/include/xen/byteswap.h | 44 ++++++++++++++++++++++ xen/include/xen/compiler.h | 20 ++++++++++ 6 files changed, 132 insertions(+), 32 deletions(-) create mode 100644 xen/include/xen/byteorder.h create mode 100644 xen/include/xen/byteswap.h
tools/include/xen-tools/libs.h +4 -0
@@ -63,4 +63,8 @@#define ROUNDUP (_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))#endif +/* Indirect macros required for expanded argument pasting. */ +#define PASTE_ (a, b) a ## b +#define PASTE (a, b) PASTE_(a, b) +#endif /* __XEN_TOOLS_LIBS__ */
xen/arch/arm/include/asm/byteorder.h +4 -2
@@ -1,9 +1,11 @@#ifndef __ASM_ARM_BYTEORDER_H__#define __ASM_ARM_BYTEORDER_H__ -#define __BYTEORDER_HAS_U64__ +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif -#include <xen/byteorder/little_endian.h> +#include <xen/byteorder.h> #endif /* __ASM_ARM_BYTEORDER_H__ */ /*
xen/arch/x86/include/asm/byteorder.h +4 -30
@@ -1,36 +1,10 @@#ifndef __ASM_X86_BYTEORDER_H__#define __ASM_X86_BYTEORDER_H__ -#include <asm/types.h> -#include <xen/compiler.h> +#ifndef __BYTE_ORDER__ +#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ +#endif -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> +#include <xen/byteorder.h> #endif /* __ASM_X86_BYTEORDER_H__ */
xen/include/xen/byteorder.h new file +56 -0
56 lines not on this page · raw message
xen/include/xen/byteswap.h new file +44 -0
44 lines not on this page · raw message
xen/include/xen/compiler.h +20 -0
@@ -185,4 +185,24 @@# define CLANG_DISABLE_WARN_GCC_COMPAT_END #endif +#ifndef __has_builtin +/* +* Backwards compatibility for GCC < 10. +* All supported versions of Clang support __has_builtin +* */ +#define __has_builtin (x) GCC_has ## x + +#define GCC_has__builtin_bswap16 (CONFIG_GCC_VERSION >= 40800) +#define GCC_has__builtin_bswap32 (CONFIG_GCC_VERSION >= 40400) +#define GCC_has__builtin_bswap64 (CONFIG_GCC_VERSION >= 40400) +#endif + +#ifndef __ORDER_LITTLE_ENDIAN__ +# define __ORDER_LITTLE_ENDIAN__ 1234 +#endif + +#ifndef __ORDER_BIG_ENDIAN__ +# define __ORDER_BIG_ENDIAN__ 4321 +#endif +#endif /* __LINUX_COMPILER_H */
Signature · 1 line
2.27.0