/* Reverse the bytes in a 16-bit value */
#define BITSWAP16(val) \
((uint16)((((uint16)(val) & (uint16)0x00ffU) << 8) | \
(((uint16)(val) & (uint16)0xff00U) >> 8)))
/* Reverse the bytes in a 32-bit value */
#define BITSWAP32(val) \
((uint32)((((uint32)(val) & (uint32)0x000000ffU) << 24) | \
(((uint32)(val) & (uint32)0x0000ff00U) << 8) | \
(((uint32)(val) & (uint32)0x00ff0000U) >> 8) | \
(((uint32)(val) & (uint32)0xff000000U) >> 24)))
/* Reverse the two 16-bit halves of a 32-bit value */
#define BITSWAP32BY16(val) \
((uint32)((((uint32)(val) & (uint32)0x0000ffffU) << 16) | \
(((uint32)(val) & (uint32)0xffff0000U) >> 16)))
/* Reverse the bytes in a 64-bit value */
#define BITSWAP64(val) \
((uint64)((((uint64)(val) & 0x00000000000000ffULL) << 56) | \
(((uint64)(val) & 0x000000000000ff00ULL) << 40) | \
(((uint64)(val) & 0x0000000000ff0000ULL) << 24) | \
(((uint64)(val) & 0x00000000ff000000ULL) << 8) | \
(((uint64)(val) & 0x000000ff00000000ULL) >> 8) | \
(((uint64)(val) & 0x0000ff0000000000ULL) >> 24) | \
(((uint64)(val) & 0x00ff000000000000ULL) >> 40) | \
(((uint64)(val) & 0xff00000000000000ULL) >> 56)))
/* Reverse the two 32-bit halves of a 64-bit value */
#define BITSWAP64BY32(val) \
((uint64)((((uint64)(val) & 0x00000000ffffffffULL) << 32) | \
(((uint64)(val) & 0xffffffff00000000ULL) >> 32)))
#ifndef hton16
#ifndef IL_BIGENDIAN
#define HTON16(i) BITSWAP16(i)
#define hton16(i) BITswap16(i)
#define HTON32(i) BITSWAP32(i)
#define hton32(i) BITswap32(i)
#define NTOH16(i) BITSWAP16(i)
#define ntoh16(i) BITswap16(i)
#define NTOH32(i) BITSWAP32(i)
#define ntoh32(i) BITswap32(i)
#define LTOH16(i) (i)
#define ltoh16(i) (i)
#define LTOH32(i) (i)
#define ltoh32(i) (i)
#define HTOL16(i) (i)
#define htol16(i) (i)
#define HTOL32(i) (i)
#define htol32(i) (i)
#define HTOL64(i) (i)
#define htol64(i) (i)
#else /* IL_BIGENDIAN */
#define HTON16(i) (i)
#define hton16(i) (i)
#define HTON32(i) (i)
#define hton32(i) (i)
#define NTOH16(i) (i)
#define ntoh16(i) (i)
#define NTOH32(i) (i)
#define ntoh32(i) (i)
#define LTOH16(i) BITSWAP16(i)
#define ltoh16(i) BITswap16(i)
#define LTOH32(i) BITSWAP32(i)
#define ltoh32(i) BITswap32(i)
#define HTOL16(i) BITSWAP16(i)
#define htol16(i) BITswap16(i)
#define HTOL32(i) BITSWAP32(i)
#define htol32(i) BITswap32(i)
#define HTOL64(i) BITSWAP64(i)
#define htol64(i) BITswap64(i)
#endif /* IL_BIGENDIAN */
#endif /* hton16 */
宏定义
最新推荐文章于 2022-11-28 08:58:24 发布