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 <sebastian@breakpoint.cc> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Sebastian Andrzej Siewior committed Jul 15, 2025 at 21:12 UTC 4544cd19e429975882e20fa89dab7e73956f26e4
1 file changed +24 -26
compat/bswap.h
+24 -26
@@ -87,27 +87,6 @@ static inline uint64_t git_bswap64(uint64_t x)
87
88 #endif
89
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 -
90 #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
91
92 # define GIT_BYTE_ORDER __BYTE_ORDER
@@ -145,14 +124,33 @@ static inline uint64_t git_bswap64(uint64_t x)
124
125 #endif
126
127 +#undef ntohl
128 +#undef htonl
129 +#undef ntohll
130 +#undef htonll
131 +
132 #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)
137 #else
152 -# define ntohll(n) default_bswap64(n)
153 -# define htonll(n) default_bswap64(n)
154 -#endif
138
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
154 #endif
155
156 static inline uint16_t get_be16(const void *ptr)