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

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

From
ASArtur Skawina <art.08.09@gmail.com>
Date
Aug 18, 2009, 12:56 UTC
Message-ID
<4A8AA511.1060205@gmail.com>
In-Reply-To
<4A8A8661.5060908@gmail.com>
Sebastian Schuberth wrote:
> As ntohl()/htonl() are function calls (that internally do shifts), I
> doubt they're faster than the shift macros, though I haven't measured
> it. However, I do not suggest to go for the macros on Windows/Intel, but
> to apply the following patch on top of your patch:
Show 5 quoted lines
> On Windows/Intel, ntohl()/htonl() are function calls that do shifts to
> swap the
> byte order. Using the native bswap instruction boths gets rid of the
> shifts and
> the function call overhead to gain some performance.

Umm, nothing like this should be needed on linux; the compiler/glibc will choose bswap itself. (see endian.h and bits/byteswap.h). I did try using __builtin_bswap32 directly and the result was a few (3 or 4, iirc) differently scheduled instructions, that's all, no performance difference.

Show 20 quoted lines
>   * Performance might be improved if the CPU architecture is OK with
> - * unaligned 32-bit loads and a fast ntohl() is available.
> + * unaligned 32-bit loads and a fast ntohl() is available. On Intel,
> + * use the bswap built-in to get rid of the function call overhead.
>   * Otherwise fall back to byte loads and shifts which is portable,
>   * and is faster on architectures with memory alignment issues.
>   */
> 
> -#if defined(__i386__) || defined(__x86_64__) || \
> -    defined(__ppc__) || defined(__ppc64__) || \
> -    defined(__powerpc__) || defined(__powerpc64__) || \
> -    defined(__s390__) || defined(__s390x__)
> +#if defined(__i386__) || defined(__x86_64__)
>
> +#define get_be32(p)    __builtin_bswap32(*(unsigned int *)(p))
> +#define put_be32(p, v)    do { *(unsigned int *)(p) = __builtin_bswap32(v); } while (0)
> +
> +#elif defined(__ppc__) || defined(__ppc64__) || \
> +      defined(__powerpc__) || defined(__powerpc64__) || \
> +      defined(__s390__) || defined(__s390x__)

I'd limit it to windows and any other ia32 platform that doesn't pick the bswaps itself; as is, it just adds an unnecessary hidden gcc dependency.

Hmm, it's actually a gcc-4.3+ dependency, so it won't even build w/ gcc 4.2; something like this would be required: "(__GNUC__>=4 && __GNUC_MINOR__>=3)" .

artur
Previous: Sebastian SchuberthNext: Sebastian Schuberth
Message 5 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.