Fix compilation error on big-endian machines

add-journal
Yury Kurlykov 2019-12-12 02:05:26 +10:00
parent 9aae8f1eba
commit 1b1c1299cf
Signed by: t1meshift
GPG Key ID: B133F3167ABF94D8
1 changed files with 25 additions and 14 deletions

View File

@ -31,25 +31,36 @@
*/
#if BYTE_ORDER == BIG_ENDIAN
/*
* These inline functions are used for TO_LE and FROM_LE macros.
* The reason is a preprocessor can't include leXXtoh and htoleXX macros into them.
*/
static inline uint16_t __htole16(uint16_t x) { return htole16(x); }
static inline uint16_t __le16toh(uint16_t x) { return le16toh(x); }
static inline uint32_t __htole32(uint32_t x) { return htole32(x); }
static inline uint32_t __le32toh(uint32_t x) { return le32toh(x); }
static inline uint64_t __htole64(uint64_t x) { return htole64(x); }
static inline uint64_t __le64toh(uint64_t x) { return le64toh(x); }
#define TO_LE(x) _Generic((x), \
uint16_t: htole16, \
int16_t: htole16, \
uint32_t: htole32, \
int32_t: htole32, \
uint64_t: htole64, \
int64_t: htole64, \
uint16_t: __htole16, \
int16_t: __htole16, \
uint32_t: __htole32, \
int32_t: __htole32, \
uint64_t: __htole64, \
int64_t: __htole64 \
)(x)
#define FROM_LE(x) _Generic((x), \
uint16_t: le16toh, \
int16_t: le16toh, \
uint32_t: le32toh, \
int32_t: le32toh, \
uint64_t: le64toh, \
int64_t: le64toh, \
uint16_t: __le16toh, \
int16_t: __le16toh, \
uint32_t: __le32toh, \
int32_t: __le32toh, \
uint64_t: __le64toh, \
int64_t: __le64toh \
)(x)
#define REASSIGN_FROM_LE(x) (x) = FROM_LE((x))
#define REASSIGN_FROM_LE(x) (void)((x) = FROM_LE((x)))
#else
#define TO_LE(x) (x)
#define FROM_LE(x) (x)