Skip to content

Commit 7457a13

Browse files
sebastianasgitster
authored andcommitted
bswap.h: always overwrite ntohl/ntohll macros
The ntohl and htonl macros are redefined because the provided macros were not always optimal. Sometimes it was a function call, sometimes it was a macro which did the shifting. Using the 'bswap' opcode on x86 provides probably better performance than performing the shifting. These macros are only overwritten on x86 if the "optimized" version is available. The ntohll and htonll macros are not available on every platform (at least glibc does not provide them) which means they need to be defined once the endianness of the system is determined. In order to get a more symmetrical setup, redfine the macros once the endianness of the system has been determined. Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d40aa59 commit 7457a13

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

compat/bswap.h

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,6 @@ static inline uint64_t git_bswap64(uint64_t x)
8787

8888
#endif
8989

90-
#if defined(bswap32)
91-
92-
#undef ntohl
93-
#undef htonl
94-
#define ntohl(x) bswap32(x)
95-
#define htonl(x) bswap32(x)
96-
97-
#endif
98-
99-
#if defined(bswap64)
100-
101-
#undef ntohll
102-
#undef htonll
103-
#define ntohll(x) bswap64(x)
104-
#define htonll(x) bswap64(x)
105-
106-
#else
107-
108-
#undef ntohll
109-
#undef htonll
110-
11190
#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
11291

11392
# define GIT_BYTE_ORDER __BYTE_ORDER
@@ -145,14 +124,33 @@ static inline uint64_t git_bswap64(uint64_t x)
145124

146125
#endif
147126

127+
#undef ntohl
128+
#undef htonl
129+
#undef ntohll
130+
#undef htonll
131+
148132
#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
149-
# define ntohll(n) (n)
150-
# define htonll(n) (n)
133+
# define ntohl(x) (x)
134+
# define htonl(x) (x)
135+
# define ntohll(x) (x)
136+
# define htonll(x) (x)
151137
#else
152-
# define ntohll(n) default_bswap64(n)
153-
# define htonll(n) default_bswap64(n)
154-
#endif
155138

139+
# if defined(bswap32)
140+
# define ntohl(x) bswap32(x)
141+
# define htonl(x) bswap32(x)
142+
# else
143+
# define ntohl(x) default_swab32(x)
144+
# define htonl(x) default_swab32(x)
145+
# endif
146+
147+
# if defined(bswap64)
148+
# define ntohll(x) bswap64(x)
149+
# define htonll(x) bswap64(x)
150+
# else
151+
# define ntohll(x) default_bswap64(x)
152+
# define htonll(x) default_bswap64(x)
153+
# endif
156154
#endif
157155

158156
static inline uint16_t get_be16(const void *ptr)

0 commit comments

Comments
 (0)