include/share/endswap.h : Add endswapping of 16 bit values.

Also add macros H2LE_16 and H2LE_32, which do host to little-endian
swapping of 16 and 32 bit values respectively.
This commit is contained in:
Erik de Castro Lopo 2014-06-29 20:19:59 +10:00
parent a8567d4b4e
commit 699e7c457a

View File

@ -33,21 +33,38 @@
#if HAVE_BSWAP32 /* GCC and Clang */
#define ENDSWAP_16(x) (__builtin_bswap16 (x))
#define ENDSWAP_32(x) (__builtin_bswap32 (x))
#elif defined _MSC_VER /* Windows. Apparently in <stdlib.h>. */
#define ENDSWAP_16(x) (_byteswap_ushort (x))
#define ENDSWAP_32(x) (_byteswap_ulong (x))
#elif defined HAVE_BYTESWAP_H /* Linux */
#include <byteswap.h>
#define ENDSWAP_16(x) (bswap_16 (x))
#define ENDSWAP_32(x) (bswap_32 (x))
#else
#define ENDSWAP_16(x) (((((x) >> 8) & 0xFF00) + ((x) & 0xFF00) << 8))
#define ENDSWAP_32(x) ((((x) >> 24) & 0xFF) + (((x) >> 8) & 0xFF00) + (((x) & 0xFF00) << 8) + (((x) & 0xFF) << 24))
#endif
/* Host to little-endian byte swapping. */
#if CPU_IS_BIG_ENDIAN
#define H2LE_16(x) ENDSWAP_16 (x)
#define H2LE_32(x) ENDSWAP_32 (x)
#else
#define H2LE_16(x) (x)
#define H2LE_32(x) (x)
#endif