From e82b75f8f06e3fdf40b32e24d385e77b22f96709 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 26 Jun 2025 12:03:02 +0200 Subject: [PATCH] Header: endian.h with endianness conversion functions --- lib/endian.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 lib/endian.h diff --git a/lib/endian.h b/lib/endian.h new file mode 100644 index 0000000..e6ea172 --- /dev/null +++ b/lib/endian.h @@ -0,0 +1,92 @@ +#ifndef ENDIAN_KERNEL_H +#define ENDIAN_KERNEL_H + +#include + +/** Swap byte order of 16-bit value */ +static inline u16 swap16(u16 x) { return (x >> 8) | (x << 8); } + +/** Swap byte order of 32-bit value */ +static inline u32 swap32(u32 x) { + return ((x >> 24) & 0x000000ff) | ((x >> 8) & 0x0000ff00) | + ((x << 8) & 0x00ff0000) | ((x << 24) & 0xff000000); +} + +/** Swap byte order of 64-bit value */ +static inline u64 swap64(u64 x) { + return ((x >> 56) & 0x00000000000000ffULL) | + ((x >> 40) & 0x000000000000ff00ULL) | + ((x >> 24) & 0x0000000000ff0000ULL) | + ((x >> 8) & 0x00000000ff000000ULL) | + ((x << 8) & 0x000000ff00000000ULL) | + ((x << 24) & 0x0000ff0000000000ULL) | + ((x << 40) & 0x00ff000000000000ULL) | + ((x << 56) & 0xff00000000000000ULL); +} + +#ifdef __LITTLE_ENDIAN__ +/** Convert 16-bit value to little-endian */ +static inline u16 to_le16(u16 x) { return x; } +/** Convert 16-bit little-endian value to host */ +static inline u16 from_le16(u16 x) { return x; } + +/** Convert 32-bit value to little-endian */ +static inline u32 to_le32(u32 x) { return x; } +/** Convert 32-bit little-endian value to host */ +static inline u32 from_le32(u32 x) { return x; } + +/** Convert 64-bit value to little-endian */ +static inline u64 to_le64(u64 x) { return x; } +/** Convert 64-bit little-endian value to host */ +static inline u64 from_le64(u64 x) { return x; } + +/** Convert 16-bit value to big-endian */ +static inline u16 to_be16(u16 x) { return swap16(x); } +/** Convert 16-bit big-endian value to host */ +static inline u16 from_be16(u16 x) { return swap16(x); } + +/** Convert 32-bit value to big-endian */ +static inline u32 to_be32(u32 x) { return swap32(x); } +/** Convert 32-bit big-endian value to host */ +static inline u32 from_be32(u32 x) { return swap32(x); } + +/** Convert 64-bit value to big-endian */ +static inline u64 to_be64(u64 x) { return swap64(x); } +/** Convert 64-bit big-endian value to host */ +static inline u64 from_be64(u64 x) { return swap64(x); } + +#else // Big-endian + +/** Convert 16-bit value to little-endian */ +static inline u16 to_le16(u16 x) { return swap16(x); } +/** Convert 16-bit little-endian value to host */ +static inline u16 from_le16(u16 x) { return swap16(x); } + +/** Convert 32-bit value to little-endian */ +static inline u32 to_le32(u32 x) { return swap32(x); } +/** Convert 32-bit little-endian value to host */ +static inline u32 from_le32(u32 x) { return swap32(x); } + +/** Convert 64-bit value to little-endian */ +static inline u64 to_le64(u64 x) { return swap64(x); } +/** Convert 64-bit little-endian value to host */ +static inline u64 from_le64(u64 x) { return swap64(x); } + +/** Convert 16-bit value to big-endian */ +static inline u16 to_be16(u16 x) { return x; } +/** Convert 16-bit big-endian value to host */ +static inline u16 from_be16(u16 x) { return x; } + +/** Convert 32-bit value to big-endian */ +static inline u32 to_be32(u32 x) { return x; } +/** Convert 32-bit big-endian value to host */ +static inline u32 from_be32(u32 x) { return x; } + +/** Convert 64-bit value to big-endian */ +static inline u64 to_be64(u64 x) { return x; } +/** Convert 64-bit big-endian value to host */ +static inline u64 from_be64(u64 x) { return x; } + +#endif // __LITTLE_ENDIAN__ + +#endif // ENDIAN_KERNEL_H