/**
* Swap upper and lower bytes in a 16-bit value.
*/
-#define swab16(x) ((uint16_t)(ROTR(x, 8)))
+#define SWAB16(x) ((uint16_t)(ROTR((x), 8) + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t))))
#if GNUC_PREREQ(4, 3)
-#define SWAB32(x) __builtin_bswap32(x)
-#define SWAB64(x) __builtin_bswap64(x)
+#define SWAB32(x) ((uint32_t)(__builtin_bswap32((x) + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))))
+#define SWAB64(x) ((uint64_t)(__builtin_bswap64((x) + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))))
#else
/**
* Reverse bytes in a 32-bit value (e.g.: 0x12345678 -> 0x78563412).
(((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
(((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
- (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
+ (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t))))
/**
* Reverse bytes in a 64-bit value.
(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
- (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
+ (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t))))
#endif
#if CPU_BYTE_ORDER == CPU_LITTLE_ENDIAN
-#define cpu_to_le16(x) \
- (x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))
-#define cpu_to_le32(x) \
- (x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))
-#define cpu_to_le64(x) \
- (x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))
-#define cpu_to_be16(x) \
- SWAB16(x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))
-#define cpu_to_be32(x) \
- SWAB32(x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))
-#define cpu_to_be64(x) \
- SWAB64(x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))
+#define cpu_to_le16(x) ((uint16_t)(x + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t))))
+#define cpu_to_le32(x) ((uint32_t)(x + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t))))
+#define cpu_to_le64(x) ((uint64_t)(x + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t))))
+#define cpu_to_be16(x) SWAB16(x)
+#define cpu_to_be32(x) SWAB32(x)
+#define cpu_to_be64(x) SWAB64(x)
#elif CPU_BYTE_ORDER == CPU_BIG_ENDIAN
-#define cpu_to_le16(x) \
- SWAB16(x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))
-#define cpu_to_le32(x) \
- SWAB32(x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))
-#define cpu_to_le64(x) \
- SWAB64(x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))
-#define cpu_to_be16(x) \
- (x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t)))
-#define cpu_to_be32(x) \
- (x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t)))
-#define cpu_to_be64(x) \
- (x + STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t)))
+#define cpu_to_le16(x) SWAB16(x)
+#define cpu_to_le32(x) SWAB32(x)
+#define cpu_to_le64(x) SWAB64(x)
+#define cpu_to_be16(x) ((uint16_t)(x + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint16_t))))
+#define cpu_to_be32(x) ((uint32_t)(x + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint32_t))))
+#define cpu_to_be64(x) ((uint64_t)(x + \
+ STATIC_ASSERT_EXPR(sizeof(x) == sizeof(uint64_t))))
#else
#error "unrecognized CPU endianness"
#endif
template<typename T>
INLINE T swab(T x);
-template<> INLINE uint16_t swab(uint16_t x) { return swab16(x); }
-template<> INLINE uint32_t swab(uint32_t x) { return swab32(x); }
-template<> INLINE uint64_t swab(uint64_t x) { return swab64(x); }
-template<> INLINE int16_t swab(int16_t x) { return static_cast<int16_t>(swab16(static_cast<uint16_t>(x))); }
-template<> INLINE int32_t swab(int32_t x) { return static_cast<int32_t>(swab32(static_cast<uint32_t>(x))); }
-template<> INLINE int64_t swab(int64_t x) { return static_cast<int64_t>(swab64(static_cast<uint64_t>(x))); }
+template<> INLINE uint16_t swab(uint16_t x) { return SWAB16(x); }
+template<> INLINE uint32_t swab(uint32_t x) { return SWAB32(x); }
+template<> INLINE uint64_t swab(uint64_t x) { return SWAB64(x); }
+template<> INLINE int16_t swab(int16_t x) { return static_cast<int16_t>(SWAB16(static_cast<uint16_t>(x))); }
+template<> INLINE int32_t swab(int32_t x) { return static_cast<int32_t>(SWAB32(static_cast<uint32_t>(x))); }
+template<> INLINE int64_t swab(int64_t x) { return static_cast<int64_t>(SWAB64(static_cast<uint64_t>(x))); }
template<> INLINE float swab(float x) { return swab_float(x); }
/// Type generic conversion from CPU byte order to big-endian byte order.