xen: Implement common byte{order,swap}.h

The current swab??() infrastructure is unnecessarily complicated, and can be
replaced entirely with compiler builtins.

All supported compilers provide __BYTE_ORDER__ and __builtin_bswap??().

Nothing in Xen cares about the values of __{BIG,LITTLE}_ENDIAN; just that one
of them is defined.  Therefore, centralise their definitions in xen/config.h

Signed-off-by: Lin Liu <lin.liu@citrix.com>
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
This commit is contained in:
Lin Liu authored and Andrew Cooper committed 2025-04-18 15:16:12 +01:00
1 parent a20e5908e2
commit f6784ea2f9
5 files changed
+67 -14

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef XEN_BYTEORDER_H
#define XEN_BYTEORDER_H
#include <xen/byteswap.h>
#include <xen/stdint.h>
#if defined(__LITTLE_ENDIAN)
# define cpu_to_le64(x) ((uint64_t)(x))
# define le64_to_cpu(x) ((uint64_t)(x))
# define cpu_to_le32(x) ((uint32_t)(x))
# define le32_to_cpu(x) ((uint32_t)(x))
# define cpu_to_le16(x) ((uint16_t)(x))
# define le16_to_cpu(x) ((uint16_t)(x))
# define cpu_to_be64(x) bswap64(x)
# define be64_to_cpu(x) bswap64(x)
# define cpu_to_be32(x) bswap32(x)
# define be32_to_cpu(x) bswap32(x)
# define cpu_to_be16(x) bswap16(x)
# define be16_to_cpu(x) bswap16(x)
#elif defined(__BIG_ENDIAN)
# define cpu_to_le64(x) bswap64(x)
# define le64_to_cpu(x) bswap64(x)
# define cpu_to_le32(x) bswap32(x)
# define le32_to_cpu(x) bswap32(x)
# define cpu_to_le16(x) bswap16(x)
# define le16_to_cpu(x) bswap16(x)
# define cpu_to_be64(x) ((uint64_t)(x))
# define be64_to_cpu(x) ((uint64_t)(x))
# define cpu_to_be32(x) ((uint32_t)(x))
# define be32_to_cpu(x) ((uint32_t)(x))
# define cpu_to_be16(x) ((uint16_t)(x))
# define be16_to_cpu(x) ((uint16_t)(x))
#else
# error Unknown Endianness
#endif /* __*_ENDIAN */
#endif /* XEN_BYTEORDER_H */
-7
View File
@@ -1,13 +1,6 @@
#ifndef __XEN_BYTEORDER_BIG_ENDIAN_H__
#define __XEN_BYTEORDER_BIG_ENDIAN_H__
#ifndef __BIG_ENDIAN
#define __BIG_ENDIAN 4321
#endif
#ifndef __BIG_ENDIAN_BITFIELD
#define __BIG_ENDIAN_BITFIELD
#endif
#include <xen/types.h>
#include <xen/byteorder/swab.h>
@@ -1,13 +1,6 @@
#ifndef __XEN_BYTEORDER_LITTLE_ENDIAN_H__
#define __XEN_BYTEORDER_LITTLE_ENDIAN_H__
#ifndef __LITTLE_ENDIAN
#define __LITTLE_ENDIAN 1234
#endif
#ifndef __LITTLE_ENDIAN_BITFIELD
#define __LITTLE_ENDIAN_BITFIELD
#endif
#include <xen/types.h>
#include <xen/byteorder/swab.h>
+15
View File
@@ -0,0 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef XEN_BYTESWAP_H
#define XEN_BYTESWAP_H
#define bswap16(x) __builtin_bswap16(x)
#define bswap32(x) __builtin_bswap32(x)
#define bswap64(x) __builtin_bswap64(x)
#if BITS_PER_LONG == 64
# define bswapl(x) bswap64(x)
#elif BITS_PER_LONG == 32
# define bswapl(x) bswap32(x)
#endif
#endif /* XEN_BYTESWAP_H */
+8
View File
@@ -108,4 +108,12 @@
/* It is assumed that sizeof(void *) == __alignof(void *) */
#define POINTER_ALIGN __SIZEOF_POINTER__
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define __LITTLE_ENDIAN
# define __LITTLE_ENDIAN_BITFIELD
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define __BIG_ENDIAN
# define __BIG_ENDIAN_BITFIELD
#endif
#endif /* __XEN_CONFIG_H__ */
Reference in new issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.