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

Re: [PATCH] block-sha1: Windows declares ntohl() in winsock2.h

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 18, 2009, 20:17 UTC
Message-ID
<7vprasc1vu.fsf@alter.siamese.dyndns.org>
In-Reply-To
<alpine.LFD.2.00.0908181411320.6044@xanadu.home>
Nicolas Pitre <nico@cam.org> writes:
Show 9 quoted lines
> On Tue, 18 Aug 2009, Junio C Hamano wrote:
>
>> To reduce confusion, you may want to rename compat/bswap.h to something
>> like compat/ntohl-htonl-fix.h ;-)
>
> Bah.  If you wish, you can edit the patch directly for this, unless you 
> really prefer me to repost.  Maybe we might want to add a 8-byte 
> versions of those as well eventually, which is why I chose a more 
> generic name.
Ok, here is what I came up with after many squashing...
-- >8 --
From: Nicolas Pitre <nico@cam.org>
Date: Tue, 18 Aug 2009 12:43:08 -0400
Subject: [PATCH] bswap: avoid potentially inefficient ntohl/htonl on i386/x86-64

Johannes Sixt reports that on Windows ntohl()/htonl() are not found in <arpa/inet.h>, and as a minimal fix we need to include <winsock2.h> instead. Sebastian Schuberth points out that they are implemented as out-of-line functions on Windows, which defeats these byteorder "macros" used for performance.

Use bswap instruction through gcc inline asm instead on i386/x86-64 as a generic solution to this.

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 block-sha1/sha1.c |    4 +---
 compat/bswap.h    |   19 +++++++++++++++++++
 git-compat-util.h |    2 ++
 3 files changed, 22 insertions(+), 3 deletions(-)
 create mode 100644 compat/bswap.h
diff --git a/block-sha1/sha1.c b/block-sha1/sha1.c
index 464cb25..51a27c1 100644
--- a/block-sha1/sha1.c
+++ b/block-sha1/sha1.c
@@ -4,9 +4,7 @@
  * and to avoid unnecessary copies into the context array.
  */
 
-#include <string.h>
-#include <arpa/inet.h>
-
+#include "../git-compat-util.h"
 #include "sha1.h"
 
 #if defined(__i386__) || defined(__x86_64__)
diff --git a/compat/bswap.h b/compat/bswap.h
new file mode 100644
index 0000000..78fd2df
--- /dev/null
+++ b/compat/bswap.h
@@ -0,0 +1,19 @@
+/*
+ * Let's make sure we always have a sane definition for ntohl()/htonl().
+ * Some libraries define those as a function call, just to perform byte
+ * swapping, bringing significant overhead to what should be a simple
+ * operation.
+ */
+
+#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
+
+#define bswap32(x) ({ \
+	unsigned int __res; \
+	__asm__("bswap %0" : "=r" (__res) : "0" (x)); \
+	__res; })
+#undef ntohl
+#undef htonl
+#define ntohl(x) bswap32(x)
+#define htonl(x) bswap32(x)
+
+#endif
diff --git a/git-compat-util.h b/git-compat-util.h
index 9f941e4..000859e 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -176,6 +176,8 @@ extern char *gitbasename(char *);
 #endif
 #endif
 
+#include "compat/bswap.h"
+
 /* General helper functions */
 extern void usage(const char *err) NORETURN;
 extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2)));
-- 
1.6.4.245.g50659
Previous: Nicolas PitreNext: Junio C Hamano
Message 31 of 34 in “block-sha1: Windows declares ntohl() in winsock2.h”
  1. block-sha1: Windows declares ntohl() in winsock2.hJohannes Sixt, Aug 18, 2009
  2. Sebastian SchuberthAug 18, 2009
  3. Junio C HamanoAug 18, 2009
  4. Sebastian SchuberthAug 18, 2009
  5. Artur SkawinaAug 18, 2009
  6. Sebastian SchuberthAug 18, 2009
  7. Junio C HamanoAug 18, 2009
  8. Linus TorvaldsAug 18, 2009
  9. Linus TorvaldsAug 18, 2009
  10. Sebastian SchuberthAug 18, 2009
  11. Junio C HamanoAug 18, 2009
  12. Nicolas PitreAug 18, 2009
  13. Nicolas PitreAug 18, 2009
  14. Junio C HamanoAug 18, 2009
  15. Nicolas PitreAug 18, 2009
  16. Junio C HamanoAug 18, 2009
  17. Nicolas PitreAug 18, 2009
  18. make sure byte swapping is optimal for gitNicolas Pitre, Aug 18, 2009
  19. block-sha1: guard gcc extensions with __GNUC__Nicolas Pitre, Aug 18, 2009
  20. Junio C HamanoAug 18, 2009
  21. Brandon CaseyAug 18, 2009
  22. Nicolas PitreAug 18, 2009
  23. Linus TorvaldsAug 18, 2009
  24. Brandon CaseyAug 18, 2009
  25. Nicolas PitreAug 20, 2009
  26. Linus TorvaldsAug 20, 2009
  27. Brandon CaseyAug 20, 2009
  28. Sebastian SchuberthAug 18, 2009
  29. Junio C HamanoAug 18, 2009
  30. Nicolas PitreAug 18, 2009
  31. Junio C HamanoAug 18, 2009
  32. Junio C HamanoAug 18, 2009
  33. Nicolas PitreAug 18, 2009
  34. Nicolas PitreAug 18, 2009

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.