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

[PATCH v3 3/5] bswap.h: Always overwrite ntohl/ ntohll macros

From
Sebastian Andrzej Siewior <sebastian@breakpoint.cc>
Date
Jul 15, 2025, 19:12 UTC
Message-ID
<20250715191230.12081-4-sebastian@breakpoint.cc>
In-Reply-To
<20250715191230.12081-1-sebastian@breakpoint.cc>

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>
---
 compat/bswap.h | 54 ++++++++++++++++++++++++--------------------------
 1 file changed, 26 insertions(+), 28 deletions(-)
diff --git a/compat/bswap.h b/compat/bswap.h
index fd604d9f7b74b..aeef304f671f5 100644
--- a/compat/bswap.h
+++ b/compat/bswap.h
@@ -87,27 +87,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
-
 #if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
 
 # define GIT_BYTE_ORDER __BYTE_ORDER
@@ -145,14 +124,33 @@ static inline uint64_t git_bswap64(uint64_t x)
 
 #endif
 
-#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
-# define ntohll(n) (n)
-# define htonll(n) (n)
-#else
-# define ntohll(n) default_bswap64(n)
-# define htonll(n) default_bswap64(n)
-#endif
+#undef ntohl
+#undef htonl
+#undef ntohll
+#undef htonll
 
+#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
+# define ntohl(x) (x)
+# define htonl(x) (x)
+# define ntohll(x) (x)
+# define htonll(x) (x)
+#else
+
+# if defined(bswap32)
+#  define ntohl(x) bswap32(x)
+#  define htonl(x) bswap32(x)
+# else
+#  define ntohl(x) default_swab32(x)
+#  define htonl(x) default_swab32(x)
+# endif
+
+# if defined(bswap64)
+#  define ntohll(x) bswap64(x)
+#  define htonll(x) bswap64(x)
+# else
+#  define ntohll(x) default_bswap64(x)
+#  define htonll(x) default_bswap64(x)
+# endif
 #endif
 
 static inline uint16_t get_be16(const void *ptr)
-- 
2.50.0
Previous: Sebastian Andrzej SiewiorNext: Junio C Hamano
Message 6 of 9 in “bswap.h: Rework ntohl handling”
  1. 0/5 bswap.h: Rework ntohl handlingSebastian Andrzej Siewior, Jul 15, 2025
  2. 1/5 bswap.h: Add support for __BYTE_ORDER__Sebastian Andrzej Siewior, Jul 15, 2025
  3. 4/5 bswap.h: Remove optimized x86 version of bswap32/64Sebastian Andrzej Siewior, Jul 15, 2025
  4. 5/5 bswap.h: Provide a built-in based version of bswap32/64 if possibleSebastian Andrzej Siewior, Jul 15, 2025
  5. 2/5 bswap.h: Define GIT_LITTLE_ENDIAN on msvc as little endianSebastian Andrzej Siewior, Jul 15, 2025
  6. 3/5 bswap.h: Always overwrite ntohl/ ntohll macrosSebastian Andrzej Siewior, Jul 15, 2025
  7. Junio C HamanoJul 15, 2025
  8. brian m. carlsonJul 15, 2025
  9. Sebastian Andrzej SiewiorJul 16, 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.