Add force attribute to the casts in endian.h

This commit is contained in:
2026-01-22 13:05:36 +01:00
parent 568f4e48cb
commit dae2c5bda4
2 changed files with 26 additions and 24 deletions

View File

@@ -9,31 +9,31 @@
#include <mcaselector-lite/types.h>
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
static inline u16 cvt_le16toh(le16 le) { return (u16)le; }
static inline u32 cvt_le32toh(le32 le) { return (u32)le; }
static inline u64 cvt_le64toh(le64 le) { return (u64)le; }
static inline le16 cvt_htole16(u16 u) { return (le16)u; }
static inline le32 cvt_htole32(u32 u) { return (le32)u; }
static inline le64 cvt_htole64(u64 u) { return (le64)u; }
static inline u16 cvt_be16toh(be16 be) { return __builtin_bswap16((u16)be); }
static inline u32 cvt_be32toh(be32 be) { return __builtin_bswap32((u32)be); }
static inline u64 cvt_be64toh(be64 be) { return __builtin_bswap64((u64)be); }
static inline be16 cvt_htobe16(u16 u) { return (be16)__builtin_bswap16(u); }
static inline be32 cvt_htobe32(u32 u) { return (be32)__builtin_bswap32(u); }
static inline be64 cvt_htobe64(u64 u) { return (be64)__builtin_bswap64(u); }
static inline u16 cvt_le16toh(le16 le) { return (FORCE u16)le; }
static inline u32 cvt_le32toh(le32 le) { return (FORCE u32)le; }
static inline u64 cvt_le64toh(le64 le) { return (FORCE u64)le; }
static inline le16 cvt_htole16(u16 u) { return (FORCE le16)u; }
static inline le32 cvt_htole32(u32 u) { return (FORCE le32)u; }
static inline le64 cvt_htole64(u64 u) { return (FORCE le64)u; }
static inline u16 cvt_be16toh(be16 be) { return __builtin_bswap16((FORCE u16)be); }
static inline u32 cvt_be32toh(be32 be) { return __builtin_bswap32((FORCE u32)be); }
static inline u64 cvt_be64toh(be64 be) { return __builtin_bswap64((FORCE u64)be); }
static inline be16 cvt_htobe16(u16 u) { return (FORCE be16)__builtin_bswap16(u); }
static inline be32 cvt_htobe32(u32 u) { return (FORCE be32)__builtin_bswap32(u); }
static inline be64 cvt_htobe64(u64 u) { return (FORCE be64)__builtin_bswap64(u); }
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
static inline u16 cvt_le16toh(le16 le) { return __builtin_bswap16((u16)le); }
static inline u32 cvt_le32toh(le32 le) { return __builtin_bswap32((u32)le); }
static inline u64 cvt_le64toh(le64 le) { return __builtin_bswap64((u64)le); }
static inline le16 cvt_htole16(u16 u) { return (le16)__builtin_bswap16(u); }
static inline le32 cvt_htole32(u32 u) { return (le32)__builtin_bswap32(u); }
static inline le64 cvt_htole64(u64 u) { return (le64)__builtin_bswap64(u); }
static inline u16 cvt_be16toh(be16 be) { return (u16)be; }
static inline u32 cvt_be32toh(be32 be) { return (u32)be; }
static inline u64 cvt_be64toh(be64 be) { return (u64)be; }
static inline be16 cvt_htobe16(u16 u) { return (be16)u; }
static inline be32 cvt_htobe32(u32 u) { return (be32)u; }
static inline be64 cvt_htobe64(u64 u) { return (be64)u; }
static inline u16 cvt_le16toh(le16 le) { return __builtin_bswap16((FORCE u16)le); }
static inline u32 cvt_le32toh(le32 le) { return __builtin_bswap32((FORCE u32)le); }
static inline u64 cvt_le64toh(le64 le) { return __builtin_bswap64((FORCE u64)le); }
static inline le16 cvt_htole16(u16 u) { return (FORCE le16)__builtin_bswap16(u); }
static inline le32 cvt_htole32(u32 u) { return (FORCE le32)__builtin_bswap32(u); }
static inline le64 cvt_htole64(u64 u) { return (FORCE le64)__builtin_bswap64(u); }
static inline u16 cvt_be16toh(be16 be) { return (FORCE u16)be; }
static inline u32 cvt_be32toh(be32 be) { return (FORCE u32)be; }
static inline u64 cvt_be64toh(be64 be) { return (FORCE u64)be; }
static inline be16 cvt_htobe16(u16 u) { return (FORCE be16)u; }
static inline be32 cvt_htobe32(u32 u) { return (FORCE be32)u; }
static inline be64 cvt_htobe64(u64 u) { return (FORCE be64)u; }
#else
#error "Machine architecture unsupported! Expected either big-endian or little-endian."
#endif

View File

@@ -25,8 +25,10 @@ typedef __UINTPTR_TYPE__ uintptr;
#ifdef __CHECKER__
#define BITWISE __attribute__((bitwise))
#define FORCE __attribute__((force))
#else
#define BITWISE
#define FORCE
#endif
typedef u16 BITWISE be16;