Header: endian.h with endianness conversion functions

This commit is contained in:
Imbus 2025-06-26 12:03:02 +02:00
parent 71ff137192
commit e82b75f8f0

92
lib/endian.h Normal file
View file

@ -0,0 +1,92 @@
#ifndef ENDIAN_KERNEL_H
#define ENDIAN_KERNEL_H
#include <types.h>
/** 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