From 3cf6bb34063406e107f51aa52db67d65839f5e58 Mon Sep 17 00:00:00 2001 From: Charles Bailey Date: Fri, 2 May 2014 08:55:29 +0100 Subject: compat/bswap.h: detect endianness on more platforms that don't use BYTE_ORDER Signed-off-by: Charles Bailey Signed-off-by: Junio C Hamano --- compat/bswap.h | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/compat/bswap.h b/compat/bswap.h index 120c6c1d37..f08a9fe87c 100644 --- a/compat/bswap.h +++ b/compat/bswap.h @@ -101,19 +101,34 @@ static inline uint64_t git_bswap64(uint64_t x) #undef ntohll #undef htonll -#if !defined(__BYTE_ORDER) -# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) -# define __BYTE_ORDER BYTE_ORDER -# define __LITTLE_ENDIAN LITTLE_ENDIAN -# define __BIG_ENDIAN BIG_ENDIAN +#if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) + +# define GIT_BYTE_ORDER BYTE_ORDER +# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN +# define GIT_BIG_ENDIAN BIG_ENDIAN + +#elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN) + +# define GIT_BYTE_ORDER __BYTE_ORDER +# define GIT_LITTLE_ENDIAN __LITTLE_ENDIAN +# define GIT_BIG_ENDIAN __BIG_ENDIAN + +#else + +# define GIT_BIG_ENDIAN 4321 +# define GIT_LITTLE_ENDIAN 1234 + +# if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# define GIT_BYTE_ORDER GIT_BIG_ENDIAN +# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN +# else +# error "Cannot determine endianness" # endif -#endif -#if !defined(__BYTE_ORDER) -# error "Cannot determine endianness" #endif -#if __BYTE_ORDER == __BIG_ENDIAN +#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN # define ntohll(n) (n) # define htonll(n) (n) #else -- cgit v1.2.3 From 839fa9c500c7771d15831e2bcf77697cd70620f9 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Fri, 2 May 2014 12:36:10 -0700 Subject: compat/bswap.h: restore preference __BIG_ENDIAN over BIG_ENDIAN The previous commit swaps the order we check the macros defined by the compiler and the system headers from the original. Since the order of check should not matter (i.e. it is insane to define both __BIG_ENDIAN and friends and BIG_ENDIAN and friends and in a conflicting way), it is the most conservative thing to do not to change it. Signed-off-by: Junio C Hamano --- compat/bswap.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compat/bswap.h b/compat/bswap.h index f08a9fe87c..c4293db004 100644 --- a/compat/bswap.h +++ b/compat/bswap.h @@ -101,18 +101,18 @@ static inline uint64_t git_bswap64(uint64_t x) #undef ntohll #undef htonll -#if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) - -# define GIT_BYTE_ORDER BYTE_ORDER -# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN -# define GIT_BIG_ENDIAN BIG_ENDIAN - -#elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN) +#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN) # define GIT_BYTE_ORDER __BYTE_ORDER # define GIT_LITTLE_ENDIAN __LITTLE_ENDIAN # define GIT_BIG_ENDIAN __BIG_ENDIAN +#elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) + +# define GIT_BYTE_ORDER BYTE_ORDER +# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN +# define GIT_BIG_ENDIAN BIG_ENDIAN + #else # define GIT_BIG_ENDIAN 4321 -- cgit v1.2.3 From 9c65ee15ee11c9b9d2fc9d7306fb1e238b35d0c1 Mon Sep 17 00:00:00 2001 From: Ben Walton Date: Fri, 30 May 2014 16:22:40 +0100 Subject: compat/bswap.h: fix endianness detection The changes to make detection of endianness more portable had a bug that breaks on (at least) Solaris x86. The bug appears to be a simple copy/paste typo. It checks for _BIG_ENDIAN and not _LITTLE_ENDIAN for both the case where we would decide the system is big endian and little endian. Instead, the second test should be for _LITTLE_ENDIAN and not _BIG_ENDIAN. Two fixes were possible: 1. Change the negation order of the conditions in the second test. 2. Reverse the order of the conditions in the second test. Use the second option so that the condition we expect is always a positive check. Signed-off-by: Ben Walton Signed-off-by: Junio C Hamano --- compat/bswap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/bswap.h b/compat/bswap.h index c4293db004..f6fd9a6a6c 100644 --- a/compat/bswap.h +++ b/compat/bswap.h @@ -120,7 +120,7 @@ static inline uint64_t git_bswap64(uint64_t x) # if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) # define GIT_BYTE_ORDER GIT_BIG_ENDIAN -# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) # define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN # else # error "Cannot determine endianness" -- cgit v1.2.3