Skip to content

Commit 7e3dae4

Browse files
vmggitster
authored andcommitted
compat: add endianness helpers
The POSIX standard doesn't currently define a `ntohll`/`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. Many thanks to Ramsay Jones <[email protected]> and Torsten Bögershausen <[email protected]> for cygwin/mingw/msvc portability fixes. Signed-off-by: Vicent Marti <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ec73f58 commit 7e3dae4

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

compat/bswap.h

+75-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,20 @@ static inline uint32_t default_swab32(uint32_t val)
1717
((val & 0x000000ff) << 24));
1818
}
1919

20+
static inline uint64_t default_bswap64(uint64_t val)
21+
{
22+
return (((val & (uint64_t)0x00000000000000ffULL) << 56) |
23+
((val & (uint64_t)0x000000000000ff00ULL) << 40) |
24+
((val & (uint64_t)0x0000000000ff0000ULL) << 24) |
25+
((val & (uint64_t)0x00000000ff000000ULL) << 8) |
26+
((val & (uint64_t)0x000000ff00000000ULL) >> 8) |
27+
((val & (uint64_t)0x0000ff0000000000ULL) >> 24) |
28+
((val & (uint64_t)0x00ff000000000000ULL) >> 40) |
29+
((val & (uint64_t)0xff00000000000000ULL) >> 56));
30+
}
31+
2032
#undef bswap32
33+
#undef bswap64
2134

2235
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2336

@@ -32,19 +45,80 @@ static inline uint32_t git_bswap32(uint32_t x)
3245
return result;
3346
}
3447

48+
#define bswap64 git_bswap64
49+
#if defined(__x86_64__)
50+
static inline uint64_t git_bswap64(uint64_t x)
51+
{
52+
uint64_t result;
53+
if (__builtin_constant_p(x))
54+
result = default_bswap64(x);
55+
else
56+
__asm__("bswap %q0" : "=r" (result) : "0" (x));
57+
return result;
58+
}
59+
#else
60+
static inline uint64_t git_bswap64(uint64_t x)
61+
{
62+
union { uint64_t i64; uint32_t i32[2]; } tmp, result;
63+
if (__builtin_constant_p(x))
64+
result.i64 = default_bswap64(x);
65+
else {
66+
tmp.i64 = x;
67+
result.i32[0] = git_bswap32(tmp.i32[1]);
68+
result.i32[1] = git_bswap32(tmp.i32[0]);
69+
}
70+
return result.i64;
71+
}
72+
#endif
73+
3574
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
3675

3776
#include <stdlib.h>
3877

3978
#define bswap32(x) _byteswap_ulong(x)
79+
#define bswap64(x) _byteswap_uint64(x)
4080

4181
#endif
4282

43-
#ifdef bswap32
83+
#if defined(bswap32)
4484

4585
#undef ntohl
4686
#undef htonl
4787
#define ntohl(x) bswap32(x)
4888
#define htonl(x) bswap32(x)
4989

5090
#endif
91+
92+
#if defined(bswap64)
93+
94+
#undef ntohll
95+
#undef htonll
96+
#define ntohll(x) bswap64(x)
97+
#define htonll(x) bswap64(x)
98+
99+
#else
100+
101+
#undef ntohll
102+
#undef htonll
103+
104+
#if !defined(__BYTE_ORDER)
105+
# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
106+
# define __BYTE_ORDER BYTE_ORDER
107+
# define __LITTLE_ENDIAN LITTLE_ENDIAN
108+
# define __BIG_ENDIAN BIG_ENDIAN
109+
# endif
110+
#endif
111+
112+
#if !defined(__BYTE_ORDER)
113+
# error "Cannot determine endianness"
114+
#endif
115+
116+
#if __BYTE_ORDER == __BIG_ENDIAN
117+
# define ntohll(n) (n)
118+
# define htonll(n) (n)
119+
#else
120+
# define ntohll(n) default_bswap64(n)
121+
# define htonll(n) default_bswap64(n)
122+
#endif
123+
124+
#endif

0 commit comments

Comments
 (0)