git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Sebastian Andrzej Siewior <sebastian@breakpoint.cc>
Date
Jun 6, 2025, 16:57 UTC
Message-ID
<20250606165718.HOiC2U4X@breakpoint.cc>

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 <sebastian@breakpoint.cc>
---

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
Next: Junio C Hamano
Message 1 of 6 in “bswap.h: Move the overwriting of the ntohl*/ htonl* macros.”
  1. bswap.h: Move the overwriting of the ntohl*/ htonl* macros.Sebastian Andrzej Siewior, Jun 6, 2025
  2. Junio C HamanoJun 6, 2025
  3. Sebastian Andrzej SiewiorJun 6, 2025
  4. Junio C HamanoJun 6, 2025
  5. Junio C HamanoJun 7, 2025
  6. Sebastian Andrzej SiewiorJun 9, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.