From: Vicent Marti <tan...@gmail.com>

The POSIX standard doesn't currently define a `nothll`/`htonll`
function pair to perform network-to-host and host-to-network
swaps of 64-bit data. These 64-bit swaps are necessary for the on-disk
storage of EWAH bitmaps if they are not in native byte order.

Signed-off-by: Vicent Marti <tan...@gmail.com>
Signed-off-by: Jeff King <p...@peff.net>
---
 compat/bswap.h | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/compat/bswap.h b/compat/bswap.h
index 5061214..ea1a9ed 100644
--- a/compat/bswap.h
+++ b/compat/bswap.h
@@ -47,4 +47,39 @@ static inline uint32_t git_bswap32(uint32_t x)
 #define ntohl(x) bswap32(x)
 #define htonl(x) bswap32(x)
 
+#ifndef __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
+#      else
+#              error "Cannot determine endianness"
+#      endif
+#endif
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+# define ntohll(n) (n)
+# define htonll(n) (n)
+#elif __BYTE_ORDER == __LITTLE_ENDIAN
+#      if defined(__GNUC__) && defined(__GLIBC__)
+#              include <byteswap.h>
+#      else /* GNUC & GLIBC */
+static inline uint64_t bswap_64(uint64_t val)
+{
+       return ((val & (uint64_t)0x00000000000000ffULL) << 56)
+               | ((val & (uint64_t)0x000000000000ff00ULL) << 40)
+               | ((val & (uint64_t)0x0000000000ff0000ULL) << 24)
+               | ((val & (uint64_t)0x00000000ff000000ULL) <<  8)
+               | ((val & (uint64_t)0x000000ff00000000ULL) >>  8)
+               | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24)
+               | ((val & (uint64_t)0x00ff000000000000ULL) >> 40)
+               | ((val & (uint64_t)0xff00000000000000ULL) >> 56);
+}
+#      endif /* GNUC & GLIBC */
+#      define ntohll(n) bswap_64(n)
+#      define htonll(n) bswap_64(n)
+#else /* __BYTE_ORDER */
+#      error "Can't define htonll or ntohll!"
+#endif
+
 #endif
-- 
1.8.4.1.898.g8bf8a41.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to