From dae2c5bda49f179e4e64a5790f3fa5a25b693507 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 22 Jan 2026 13:05:36 +0100 Subject: [PATCH] Add `force` attribute to the casts in `endian.h` --- include/mcaselector-lite/endian.h | 48 +++++++++++++++---------------- include/mcaselector-lite/types.h | 2 ++ 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/include/mcaselector-lite/endian.h b/include/mcaselector-lite/endian.h index 10da1de..8a73149 100644 --- a/include/mcaselector-lite/endian.h +++ b/include/mcaselector-lite/endian.h @@ -9,31 +9,31 @@ #include #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 diff --git a/include/mcaselector-lite/types.h b/include/mcaselector-lite/types.h index 9c3dada..9f6d0e7 100644 --- a/include/mcaselector-lite/types.h +++ b/include/mcaselector-lite/types.h @@ -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;