From cf3c9003487cbf8cc594dbb77e333e1609a1786e Mon Sep 17 00:00:00 2001 From: Przemyslaw Pawelczyk Date: Thu, 17 Nov 2016 15:57:19 +0100 Subject: [PATCH] librhash/byte_order.h: Consult also compiler's predefined macros. byte_order.h includes only if glibc is used (i.e. __GLIBC__ is defined), but there are other libc implementation that also provide endian.h, like musl libc for instance. Generally it's much better and cleaner to check feature test macros than check for particular implementation (and that is also why musl libc doesn't define __MUSL__). Unfortunately I am not aware of feature test macro guaranteeing endian.h availability. The simple solution is to add checking for compiler's pre-defined macros (__BYTE_ORDER__, __ORDER_LITTLE_ENDIAN__, __ORDER_LITTLE_ENDIAN__) after checking macros coming from system headers, i.e. in this case (__BYTE_ORDER, __BIG_ENDIAN, __LITTLE_ENDIAN). That way we can better support new architectures w/o hardcoding their endianness, which is especially useful if they support both. --- librhash/byte_order.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/librhash/byte_order.h b/librhash/byte_order.h index 77b8bb9..5cb081e 100644 --- a/librhash/byte_order.h +++ b/librhash/byte_order.h @@ -38,6 +38,8 @@ extern "C" { /* detect CPU endianness */ #if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && \ __BYTE_ORDER == __LITTLE_ENDIAN) || \ + (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \ + __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \ defined(CPU_IA32) || defined(CPU_X64) || \ defined(__ia64) || defined(__ia64__) || defined(__alpha__) || defined(_M_ALPHA) || \ defined(vax) || defined(MIPSEL) || defined(_ARM_) || defined(__arm__) @@ -46,6 +48,8 @@ extern "C" { # define IS_LITTLE_ENDIAN 1 #elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && \ __BYTE_ORDER == __BIG_ENDIAN) || \ + (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \ + __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \ defined(__sparc) || defined(__sparc__) || defined(sparc) || \ defined(_ARCH_PPC) || defined(_ARCH_PPC64) || defined(_POWER) || \ defined(__POWERPC__) || defined(POWERPC) || defined(__powerpc) || \ -- 2.8.3