Subject: [PATCH] bswap.h: Move the overwriting of the ntohl*/ htonl* macros.

From: Sebastian Andrzej Siewior <[email-masked]>
Date: Fri, 6 Jun 2025 18:57:18 +0200

The top of that file contains optimized bswap32/64 only for a few little
endian machines. Since the commit cited below there is one for every
architecture supporting the __builtin_bswap* directives.

Later in the file, the ntohl* macros are replaced with the bswap* macros
should they be provided. Since they are now provided even on big endian
machines they replace the nop with a swap.

Move the ntohl*/ htonl* replacement once it is determined that it is a
little architecture where the swap is performed.

Fixes: 6547d1c9cbafa ("bswap.h: add support for built-in bswap functions")
Signed-off-by: Sebastian Andrzej Siewior <[email-masked]>
---

This builds on top of v2.50.0-rc1 on s390x and -rc0 and x86-64. The
testsuite passes.

 compat/bswap.h | 37 ++++++++++++++++---------------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/compat/bswap.h b/compat/bswap.h
index 9e0f98e00b93a..5789d12c2c356 100644
--- a/compat/bswap.h
+++ b/compat/bswap.h
@@ -95,24 +95,6 @@ static inline uint64_t git_bswap64(uint64_t x)
 
 #endif
 
-#if defined(bswap32)
-
-#undef ntohl
-#undef htonl
-#define ntohl(x) bswap32(x)
-#define htonl(x) bswap32(x)
-
-#endif
-
-#if defined(bswap64)
-
-#undef ntohll
-#undef htonll
-#define ntohll(x) bswap64(x)
-#define htonll(x) bswap64(x)
-
-#else
-
 #undef ntohll
 #undef htonll
 
@@ -151,10 +133,23 @@ static inline uint64_t git_bswap64(uint64_t x)
 # define ntohll(n) (n)
 # define htonll(n) (n)
 #else
-# define ntohll(n) default_bswap64(n)
-# define htonll(n) default_bswap64(n)
-#endif
 
+# if defined(bswap32)
+#  undef ntohl
+#  undef htonl
+#  define ntohl(x) bswap32(x)
+#  define htonl(x) bswap32(x)
+# endif
+
+# if defined(bswap64)
+#  undef ntohll
+#  undef htonll
+#  define ntohll(x) bswap64(x)
+#  define htonll(x) bswap64(x)
+# else
+#  define ntohll(n) default_bswap64(n)
+#  define htonll(n) default_bswap64(n)
+# endif
 #endif
 
 static inline uint16_t get_be16(const void *ptr)
-- 
2.49.0