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

Re: x86 SHA1: Faster than OpenSSL

From
ASArtur Skawina <art.08.09@gmail.com>
Date
Aug 6, 2009, 03:19 UTC
Message-ID
<4A7A4BC5.7010106@gmail.com>
In-Reply-To
<alpine.LFD.2.01.0908051902580.3390@localhost.localdomain>
Linus Torvalds wrote:
Show 17 quoted lines
> 
> The bigger issue seems to be that it's shifter-limited, or that's what I 
> take away from my profiles. I suspect it's even _more_ shifter-limited on 
> some other micro-architectures, because gcc is being stupid, and generates
> 
> 	ror $31,%eax
> 
> from the "left shift + right shift" combination. It seems to -always- 
> generate a "ror", rather than trying to generate 'rot' if the shift count 
> would be smaller that way.
> 
> And I know _some_ old micro-architectures will literally internally loop 
> on the rol/ror counts, so "ror $31" can be _much_ more expensive than "rol 
> $1".
> 
> That isn't the case on my Nehalem, though. But I can't seem to get gcc to 
> generate better code without actually using inline asm..
The compiler does the right thing w/ something like this:
+#if __GNUC__>1 && defined(__i386)
+#define SHA_ROT(data,bits) ({ \
+  unsigned d = (data); \
+  if (bits<16) \
+    __asm__ ("roll %1,%0" : "=r" (d) : "I" (bits), "0" (d)); \
+  else \
+    __asm__ ("rorl %1,%0" : "=r" (d) : "I" (32-bits), "0" (d)); \
+  d; \
+  })
+#else
 #define SHA_ROT(X,n) (((X) << (n)) | ((X) >> (32-(n))))
+#endif
 
which doesn't obfuscate the code as much.
(I needed the asm on p4 anyway, as w/o it the mozilla version is even
 slower than an rfc3174 one. rol vs ror makes no measurable difference)
Show 8 quoted lines
>  static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data)
>  {
> @@ -93,7 +105,7 @@ static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data)
>  
>  	/* Unroll it? */
>  	for (t = 16; t <= 79; t++)
> -		W[t] = SHA_ROT(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
> +		W[t] = SHA_ROL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);
unrolling this once (but not more) is a win, at least on p4.
Show 8 quoted lines
>  #define T_0_19(t) \
> -	TEMP = SHA_ROT(A,5) + (((C^D)&B)^D)     + E + W[t] + 0x5a827999; \
> -	E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
> +	TEMP = SHA_ROL(A,5) + (((C^D)&B)^D)     + E + W[t] + 0x5a827999; \
> +	E = D; D = C; C = SHA_ROR(B, 2); B = A; A = TEMP;
>  
>  	T_0_19( 0); T_0_19( 1); T_0_19( 2); T_0_19( 3); T_0_19( 4);
>  	T_0_19( 5); T_0_19( 6); T_0_19( 7); T_0_19( 8); T_0_19( 9);
unrolling these otoh is a clear loss (iirc ~10%). 
artur
Previous: Linus TorvaldsNext: Linus Torvalds
Message 44 of 60 in “Re: Performance issue of 'git branch'”
  1. George SpelvinJul 26, 2009
  2. Request for benchmarking: x86 SHA1 codeGeorge Spelvin, Jul 31, 2009
  3. Erik Faye-LundJul 31, 2009
  4. George SpelvinJul 31, 2009
  5. Michael J GruberJul 31, 2009
  6. Erik Faye-LundJul 31, 2009
  7. Johannes SchindelinJul 31, 2009
  8. George SpelvinJul 31, 2009
  9. Erik Faye-LundJul 31, 2009
  10. George SpelvinJul 31, 2009
  11. Michael J GruberJul 31, 2009
  12. Michael J GruberJul 31, 2009
  13. Carlos R. MafraJul 31, 2009
  14. Brian RistucciaJul 31, 2009
  15. George SpelvinJul 31, 2009
  16. Jakub NarebskiJul 31, 2009
  17. Peter HarrisJul 31, 2009
  18. Peter HarrisJul 31, 2009
  19. x86 SHA1: Faster than OpenSSLGeorge Spelvin, Aug 3, 2009
  20. Jonathan del StrotherAug 3, 2009
  21. Mark LodatoAug 4, 2009
  22. Linus TorvaldsAug 4, 2009
  23. Linus TorvaldsAug 4, 2009
  24. Jon SmirlAug 4, 2009
  25. George SpelvinAug 4, 2009
  26. Jon SmirlAug 4, 2009
  27. Dmitry PotapovAug 4, 2009
  28. Andy PolyakovAug 18, 2009
  29. George SpelvinAug 4, 2009
  30. Linus TorvaldsAug 4, 2009
  31. George SpelvinAug 4, 2009
  32. Junio C HamanoAug 4, 2009
  33. George SpelvinAug 5, 2009
  34. Johannes SchindelinAug 5, 2009
  35. Junio C HamanoAug 5, 2009
  36. Linus TorvaldsAug 5, 2009
  37. Linus TorvaldsAug 5, 2009
  38. Linus TorvaldsAug 6, 2009
  39. Nicolas PitreAug 6, 2009
  40. Junio C HamanoAug 6, 2009
  41. Linus TorvaldsAug 6, 2009
  42. Nicolas PitreAug 6, 2009
  43. Linus TorvaldsAug 6, 2009
  44. Artur SkawinaAug 6, 2009
  45. Linus TorvaldsAug 6, 2009
  46. Linus TorvaldsAug 6, 2009
  47. Linus TorvaldsAug 6, 2009
  48. Artur SkawinaAug 6, 2009
  49. Linus TorvaldsAug 6, 2009
  50. Artur SkawinaAug 6, 2009
  51. George SpelvinAug 6, 2009
  52. George SpelvinAug 6, 2009
  53. Artur SkawinaAug 6, 2009
  54. Linus TorvaldsAug 6, 2009
  55. Artur SkawinaAug 6, 2009
  56. Artur SkawinaAug 6, 2009
  57. Artur SkawinaAug 6, 2009
  58. Erik Faye-LundAug 6, 2009
  59. Linus TorvaldsAug 4, 2009
  60. Andy PolyakovAug 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.