From 699e7c457a5d49fb4f021ebd4b0a28d6278e4eb5 Mon Sep 17 00:00:00 2001 From: Erik de Castro Lopo Date: Sun, 29 Jun 2014 20:19:59 +1000 Subject: [PATCH] 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. --- include/share/endswap.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/include/share/endswap.h b/include/share/endswap.h index e71f0f6c..74cdfc3b 100644 --- a/include/share/endswap.h +++ b/include/share/endswap.h @@ -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 . */ +#define ENDSWAP_16(x) (_byteswap_ushort (x)) #define ENDSWAP_32(x) (_byteswap_ulong (x)) #elif defined HAVE_BYTESWAP_H /* Linux */ #include +#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