From d859158581e9a3250f36cdeeb8ea67cda04053bd Mon Sep 17 00:00:00 2001 From: Nathan Hjelm Date: Mon, 16 Feb 2009 21:30:49 -0300 Subject: [PATCH] Make endianness macros endian-independent Implementation suggested by David Moore. Needed for proper universal code support on Darwin. --- libusb/libusb.h | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/libusb/libusb.h b/libusb/libusb.h index 7699138..27b3612 100644 --- a/libusb/libusb.h +++ b/libusb/libusb.h @@ -21,21 +21,13 @@ #ifndef __LIBUSB_H__ #define __LIBUSB_H__ -#include #include #include #include #include -#define bswap16(x) (((x & 0xff) << 8) | (x >> 8)) -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define libusb_cpu_to_le16(x) (x) -#define libusb_le16_to_cpu(x) (x) -#elif __BYTE_ORDER == __BIG_ENDIAN -#define libusb_le16_to_cpu(x) bswap16(x) -#define libusb_cpu_to_le16(x) bswap16(x) -#else -#error "Unrecognized endianness" +#ifdef __cplusplus +extern "C" { #endif /** \def libusb_cpu_to_le16 @@ -46,6 +38,16 @@ * \param x the host-endian value to convert * \returns the value in little-endian byte order */ +#define libusb_cpu_to_le16(x) ({ \ + union { \ + uint8_t b8[2]; \ + uint16_t b16; \ + } _tmp; \ + uint16_t _tmp2 = (uint16_t)(x); \ + _tmp.b8[1] = _tmp2 >> 8; \ + _tmp.b8[0] = _tmp2 & 0xff; \ + _tmp.b16; \ +}) /** \def libusb_le16_to_cpu * \ingroup misc @@ -55,10 +57,7 @@ * \param x the little-endian value to convert * \returns the value in host-endian byte order */ - -#ifdef __cplusplus -extern "C" { -#endif +#define libusb_le16_to_cpu libusb_cpu_to_le16 /* standard USB stuff */ -- 2.7.4