<div dir="ltr">Thanks. Fixed in r287475.</div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Nov 19, 2016 at 5:15 PM, Davide Italiano <span dir="ltr"><<a href="mailto:davide@freebsd.org" target="_blank">davide@freebsd.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Sat, Nov 19, 2016 at 5:03 PM, Rui Ueyama via llvm-commits<br>
<<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br>
> Author: ruiu<br>
> Date: Sat Nov 19 19:03:22 2016<br>
> New Revision: 287473<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=287473&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=287473&view=rev</a><br>
> Log:<br>
> SHA1: unroll loop in hashBlock.<br>
><br>
> This code is taken from public domain.<br>
> <a href="https://github.com/jsonn/src/blob/trunk/common/lib/libc/hash/sha1/sha1.c" rel="noreferrer" target="_blank">https://github.com/jsonn/src/<wbr>blob/trunk/common/lib/libc/<wbr>hash/sha1/sha1.c</a><br>
><br>
> I wrote a sha1 command and ran it on my Xeon E5-2680 v2 2.80GHz machine.<br>
> Here is a result. The new hash function is 37% faster than before.<br>
><br>
> Performance counter stats for './llvm-sha1-old /ssd/build/bin/lld' (10 runs):<br>
><br>
> 6640.503687 task-clock (msec) # 1.001 CPUs utilized ( +- 0.03% )<br>
> 54 context-switches # 0.008 K/sec ( +- 5.03% )<br>
> 5 cpu-migrations # 0.001 K/sec ( +- 31.73% )<br>
> 183,803 page-faults # 0.028 M/sec ( +- 0.00% )<br>
> 18,527,954,113 cycles # 2.790 GHz ( +- 0.03% )<br>
> 4,993,237,485 stalled-cycles-frontend # 26.95% frontend cycles idle ( +- 0.11% )<br>
> <not supported> stalled-cycles-backend<br>
> 50,217,149,423 instructions # 2.71 insns per cycle<br>
> # 0.10 stalled cycles per insn ( +- 0.00% )<br>
> 6,094,322,337 branches # 917.750 M/sec ( +- 0.00% )<br>
> 11,778,239 branch-misses # 0.19% of all branches ( +- 0.01% )<br>
><br>
> 6.634017401 seconds time elapsed ( +- 0.03% )<br>
><br>
> Performance counter stats for './llvm-sha1-new /ssd/build/bin/lld' (10 runs):<br>
><br>
> 4167.062720 task-clock (msec) # 1.001 CPUs utilized ( +- 0.02% )<br>
> 52 context-switches # 0.012 K/sec ( +- 16.45% )<br>
> 7 cpu-migrations # 0.002 K/sec ( +- 32.20% )<br>
> 183,804 page-faults # 0.044 M/sec ( +- 0.00% )<br>
> 11,626,611,958 cycles # 2.790 GHz ( +- 0.02% )<br>
> 4,491,897,976 stalled-cycles-frontend # 38.63% frontend cycles idle ( +- 0.05% )<br>
> <not supported> stalled-cycles-backend<br>
> 24,320,180,617 instructions # 2.09 insns per cycle<br>
> # 0.18 stalled cycles per insn ( +- 0.00% )<br>
> 1,574,674,576 branches # 377.886 M/sec ( +- 0.00% )<br>
> 11,769,693 branch-misses # 0.75% of all branches ( +- 0.00% )<br>
><br>
> 4.163251552 seconds time elapsed ( +- 0.02% )<br>
><br>
> Differential Revision: <a href="https://reviews.llvm.org/D26890" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D26890</a><br>
><br>
> Modified:<br>
> llvm/trunk/include/llvm/<wbr>Support/SHA1.h<br>
> llvm/trunk/lib/Support/SHA1.<wbr>cpp<br>
><br>
> Modified: llvm/trunk/include/llvm/<wbr>Support/SHA1.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/SHA1.h?rev=287473&r1=287472&r2=287473&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/include/<wbr>llvm/Support/SHA1.h?rev=<wbr>287473&r1=287472&r2=287473&<wbr>view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- llvm/trunk/include/llvm/<wbr>Support/SHA1.h (original)<br>
> +++ llvm/trunk/include/llvm/<wbr>Support/SHA1.h Sat Nov 19 19:03:22 2016<br>
> @@ -61,7 +61,10 @@ private:<br>
><br>
> // Internal State<br>
> struct {<br>
> - uint32_t Buffer[BLOCK_LENGTH / 4];<br>
> + union {<br>
> + uint8_t C[BLOCK_LENGTH];<br>
> + uint32_t L[BLOCK_LENGTH / 4];<br>
> + } Buffer;<br>
> uint32_t State[HASH_LENGTH / 4];<br>
> uint32_t ByteCount;<br>
> uint8_t BufferOffset;<br>
><br>
> Modified: llvm/trunk/lib/Support/SHA1.<wbr>cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SHA1.cpp?rev=287473&r1=287472&r2=287473&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/<wbr>Support/SHA1.cpp?rev=287473&<wbr>r1=287472&r2=287473&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- llvm/trunk/lib/Support/SHA1.<wbr>cpp (original)<br>
> +++ llvm/trunk/lib/Support/SHA1.<wbr>cpp Sat Nov 19 19:03:22 2016<br>
> @@ -6,8 +6,10 @@<br>
> // License. See LICENSE.TXT for details.<br>
> //<br>
> //===-------------------------<wbr>------------------------------<wbr>---------------===//<br>
> +//<br>
> // This code is taken from public domain<br>
> -// (<a href="http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c" rel="noreferrer" target="_blank">http://oauth.googlecode.com/<wbr>svn/code/c/liboauth/src/sha1.c</a><wbr>)<br>
> +// (<a href="http://oauth.googlecode.com/svn/code/c/liboauth/src/sha1.c" rel="noreferrer" target="_blank">http://oauth.googlecode.com/<wbr>svn/code/c/liboauth/src/sha1.c</a> and<br>
> +// <a href="http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/hash/sha1/sha1.c?rev=1.6" rel="noreferrer" target="_blank">http://cvsweb.netbsd.org/<wbr>bsdweb.cgi/src/common/lib/<wbr>libc/hash/sha1/sha1.c?rev=1.6</a>)<br>
> // and modified by wrapping it in a C++ interface for LLVM,<br>
> // and removing unnecessary code.<br>
> //<br>
> @@ -25,6 +27,56 @@ using namespace llvm;<br>
> #define SHA_BIG_ENDIAN<br>
> #endif<br>
><br>
> +static uint32_t rol(uint32_t number, int bits) {<br>
> + return (number << bits) | (number >> (32 - bits));<br>
> +};<br>
<br>
</div></div>Nit: number/bits should be uppercase (LLVM convention && consistency<br>
with the rest of the file).<br>
<div class="HOEnZb"><div class="h5"><br>
> +<br>
> +#if SHA_BIG_ENDIAN<br>
> +static uint32_t blk0(uint32_t *Buf, int I) {<br>
> + Buf[I] = (rol(Buf[I], 24) & 0xFF00FF00) | (rol(Buf[I], 8) & 0x00FF00FF);<br>
> + return Buf[I];<br>
> +}<br>
> +#else<br>
> +static uint32_t blk0(uint32_t *Buf, int I) { return Buf[I]; }<br>
> +#endif<br>
> +<br>
> +static uint32_t blk(uint32_t *Buf, int I) {<br>
> + Buf[I & 15] = rol(Buf[(I + 13) & 15] ^ Buf[(I + 8) & 15] ^ Buf[(I + 2) & 15] ^<br>
> + Buf[I & 15],<br>
> + 1);<br>
> + return Buf[I & 15];<br>
> +}<br>
> +<br>
> +static void r0(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,<br>
> + int I, uint32_t *Buf) {<br>
> + E += ((B & (C ^ D)) ^ D) + blk0(Buf, I) + 0x5A827999 + rol(A, 5);<br>
> + B = rol(B, 30);<br>
> +}<br>
> +<br>
> +static void r1(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,<br>
> + int I, uint32_t *Buf) {<br>
> + E += ((B & (C ^ D)) ^ D) + blk(Buf, I) + 0x5A827999 + rol(A, 5);<br>
> + B = rol(B, 30);<br>
> +}<br>
> +<br>
> +static void r2(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,<br>
> + int I, uint32_t *Buf) {<br>
> + E += (B ^ C ^ D) + blk(Buf, I) + 0x6ED9EBA1 + rol(A, 5);<br>
> + B = rol(B, 30);<br>
> +}<br>
> +<br>
> +static void r3(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,<br>
> + int I, uint32_t *Buf) {<br>
> + E += (((B | C) & D) | (B & C)) + blk(Buf, I) + 0x8F1BBCDC + rol(A, 5);<br>
> + B = rol(B, 30);<br>
> +}<br>
> +<br>
> +static void r4(uint32_t &A, uint32_t &B, uint32_t &C, uint32_t &D, uint32_t &E,<br>
> + int I, uint32_t *Buf) {<br>
> + E += (B ^ C ^ D) + blk(Buf, I) + 0xCA62C1D6 + rol(A, 5);<br>
> + B = rol(B, 30);<br>
> +}<br>
> +<br>
> /* code */<br>
> #define SHA1_K0 0x5a827999<br>
> #define SHA1_K20 0x6ed9eba1<br>
> @@ -47,56 +99,112 @@ void SHA1::init() {<br>
> InternalState.BufferOffset = 0;<br>
> }<br>
><br>
> -static uint32_t rol32(uint32_t number, uint8_t bits) {<br>
> - return ((number << bits) | (number >> (32 - bits)));<br>
> -}<br>
> -<br>
> void SHA1::hashBlock() {<br>
> - uint8_t i;<br>
> - uint32_t a, b, c, d, e, t;<br>
> -<br>
> - a = InternalState.State[0];<br>
> - b = InternalState.State[1];<br>
> - c = InternalState.State[2];<br>
> - d = InternalState.State[3];<br>
> - e = InternalState.State[4];<br>
> - for (i = 0; i < 80; i++) {<br>
> - if (i >= 16) {<br>
> - t = InternalState.Buffer[(i + 13) & 15] ^<br>
> - InternalState.Buffer[(i + 8) & 15] ^<br>
> - InternalState.Buffer[(i + 2) & 15] ^ InternalState.Buffer[i & 15];<br>
> - InternalState.Buffer[i & 15] = rol32(t, 1);<br>
> - }<br>
> - if (i < 20) {<br>
> - t = (d ^ (b & (c ^ d))) + SHA1_K0;<br>
> - } else if (i < 40) {<br>
> - t = (b ^ c ^ d) + SHA1_K20;<br>
> - } else if (i < 60) {<br>
> - t = ((b & c) | (d & (b | c))) + SHA1_K40;<br>
> - } else {<br>
> - t = (b ^ c ^ d) + SHA1_K60;<br>
> - }<br>
> - t += rol32(a, 5) + e + InternalState.Buffer[i & 15];<br>
> - e = d;<br>
> - d = c;<br>
> - c = rol32(b, 30);<br>
> - b = a;<br>
> - a = t;<br>
> - }<br>
> - InternalState.State[0] += a;<br>
> - InternalState.State[1] += b;<br>
> - InternalState.State[2] += c;<br>
> - InternalState.State[3] += d;<br>
> - InternalState.State[4] += e;<br>
> + uint32_t A = InternalState.State[0];<br>
> + uint32_t B = InternalState.State[1];<br>
> + uint32_t C = InternalState.State[2];<br>
> + uint32_t D = InternalState.State[3];<br>
> + uint32_t E = InternalState.State[4];<br>
> +<br>
> + // 4 rounds of 20 operations each. Loop unrolled.<br>
> + r0(A, B, C, D, E, 0, InternalState.Buffer.L);<br>
> + r0(E, A, B, C, D, 1, InternalState.Buffer.L);<br>
> + r0(D, E, A, B, C, 2, InternalState.Buffer.L);<br>
> + r0(C, D, E, A, B, 3, InternalState.Buffer.L);<br>
> + r0(B, C, D, E, A, 4, InternalState.Buffer.L);<br>
> + r0(A, B, C, D, E, 5, InternalState.Buffer.L);<br>
> + r0(E, A, B, C, D, 6, InternalState.Buffer.L);<br>
> + r0(D, E, A, B, C, 7, InternalState.Buffer.L);<br>
> + r0(C, D, E, A, B, 8, InternalState.Buffer.L);<br>
> + r0(B, C, D, E, A, 9, InternalState.Buffer.L);<br>
> + r0(A, B, C, D, E, 10, InternalState.Buffer.L);<br>
> + r0(E, A, B, C, D, 11, InternalState.Buffer.L);<br>
> + r0(D, E, A, B, C, 12, InternalState.Buffer.L);<br>
> + r0(C, D, E, A, B, 13, InternalState.Buffer.L);<br>
> + r0(B, C, D, E, A, 14, InternalState.Buffer.L);<br>
> + r0(A, B, C, D, E, 15, InternalState.Buffer.L);<br>
> + r1(E, A, B, C, D, 16, InternalState.Buffer.L);<br>
> + r1(D, E, A, B, C, 17, InternalState.Buffer.L);<br>
> + r1(C, D, E, A, B, 18, InternalState.Buffer.L);<br>
> + r1(B, C, D, E, A, 19, InternalState.Buffer.L);<br>
> +<br>
> + r2(A, B, C, D, E, 20, InternalState.Buffer.L);<br>
> + r2(E, A, B, C, D, 21, InternalState.Buffer.L);<br>
> + r2(D, E, A, B, C, 22, InternalState.Buffer.L);<br>
> + r2(C, D, E, A, B, 23, InternalState.Buffer.L);<br>
> + r2(B, C, D, E, A, 24, InternalState.Buffer.L);<br>
> + r2(A, B, C, D, E, 25, InternalState.Buffer.L);<br>
> + r2(E, A, B, C, D, 26, InternalState.Buffer.L);<br>
> + r2(D, E, A, B, C, 27, InternalState.Buffer.L);<br>
> + r2(C, D, E, A, B, 28, InternalState.Buffer.L);<br>
> + r2(B, C, D, E, A, 29, InternalState.Buffer.L);<br>
> + r2(A, B, C, D, E, 30, InternalState.Buffer.L);<br>
> + r2(E, A, B, C, D, 31, InternalState.Buffer.L);<br>
> + r2(D, E, A, B, C, 32, InternalState.Buffer.L);<br>
> + r2(C, D, E, A, B, 33, InternalState.Buffer.L);<br>
> + r2(B, C, D, E, A, 34, InternalState.Buffer.L);<br>
> + r2(A, B, C, D, E, 35, InternalState.Buffer.L);<br>
> + r2(E, A, B, C, D, 36, InternalState.Buffer.L);<br>
> + r2(D, E, A, B, C, 37, InternalState.Buffer.L);<br>
> + r2(C, D, E, A, B, 38, InternalState.Buffer.L);<br>
> + r2(B, C, D, E, A, 39, InternalState.Buffer.L);<br>
> +<br>
> + r3(A, B, C, D, E, 40, InternalState.Buffer.L);<br>
> + r3(E, A, B, C, D, 41, InternalState.Buffer.L);<br>
> + r3(D, E, A, B, C, 42, InternalState.Buffer.L);<br>
> + r3(C, D, E, A, B, 43, InternalState.Buffer.L);<br>
> + r3(B, C, D, E, A, 44, InternalState.Buffer.L);<br>
> + r3(A, B, C, D, E, 45, InternalState.Buffer.L);<br>
> + r3(E, A, B, C, D, 46, InternalState.Buffer.L);<br>
> + r3(D, E, A, B, C, 47, InternalState.Buffer.L);<br>
> + r3(C, D, E, A, B, 48, InternalState.Buffer.L);<br>
> + r3(B, C, D, E, A, 49, InternalState.Buffer.L);<br>
> + r3(A, B, C, D, E, 50, InternalState.Buffer.L);<br>
> + r3(E, A, B, C, D, 51, InternalState.Buffer.L);<br>
> + r3(D, E, A, B, C, 52, InternalState.Buffer.L);<br>
> + r3(C, D, E, A, B, 53, InternalState.Buffer.L);<br>
> + r3(B, C, D, E, A, 54, InternalState.Buffer.L);<br>
> + r3(A, B, C, D, E, 55, InternalState.Buffer.L);<br>
> + r3(E, A, B, C, D, 56, InternalState.Buffer.L);<br>
> + r3(D, E, A, B, C, 57, InternalState.Buffer.L);<br>
> + r3(C, D, E, A, B, 58, InternalState.Buffer.L);<br>
> + r3(B, C, D, E, A, 59, InternalState.Buffer.L);<br>
> +<br>
> + r4(A, B, C, D, E, 60, InternalState.Buffer.L);<br>
> + r4(E, A, B, C, D, 61, InternalState.Buffer.L);<br>
> + r4(D, E, A, B, C, 62, InternalState.Buffer.L);<br>
> + r4(C, D, E, A, B, 63, InternalState.Buffer.L);<br>
> + r4(B, C, D, E, A, 64, InternalState.Buffer.L);<br>
> + r4(A, B, C, D, E, 65, InternalState.Buffer.L);<br>
> + r4(E, A, B, C, D, 66, InternalState.Buffer.L);<br>
> + r4(D, E, A, B, C, 67, InternalState.Buffer.L);<br>
> + r4(C, D, E, A, B, 68, InternalState.Buffer.L);<br>
> + r4(B, C, D, E, A, 69, InternalState.Buffer.L);<br>
> + r4(A, B, C, D, E, 70, InternalState.Buffer.L);<br>
> + r4(E, A, B, C, D, 71, InternalState.Buffer.L);<br>
> + r4(D, E, A, B, C, 72, InternalState.Buffer.L);<br>
> + r4(C, D, E, A, B, 73, InternalState.Buffer.L);<br>
> + r4(B, C, D, E, A, 74, InternalState.Buffer.L);<br>
> + r4(A, B, C, D, E, 75, InternalState.Buffer.L);<br>
> + r4(E, A, B, C, D, 76, InternalState.Buffer.L);<br>
> + r4(D, E, A, B, C, 77, InternalState.Buffer.L);<br>
> + r4(C, D, E, A, B, 78, InternalState.Buffer.L);<br>
> + r4(B, C, D, E, A, 79, InternalState.Buffer.L);<br>
> +<br>
> + InternalState.State[0] += A;<br>
> + InternalState.State[1] += B;<br>
> + InternalState.State[2] += C;<br>
> + InternalState.State[3] += D;<br>
> + InternalState.State[4] += E;<br>
> }<br>
><br>
> void SHA1::addUncounted(uint8_t data) {<br>
> - uint8_t *const b = (uint8_t *)InternalState.Buffer;<br>
> #ifdef SHA_BIG_ENDIAN<br>
> - b[InternalState.BufferOffset] = data;<br>
> + InternalState.Buffer.C[<wbr>InternalState.BufferOffset] = data;<br>
> #else<br>
> - b[InternalState.BufferOffset ^ 3] = data;<br>
> + InternalState.Buffer.C[<wbr>InternalState.BufferOffset ^ 3] = data;<br>
> #endif<br>
> +<br>
> InternalState.BufferOffset++;<br>
> if (InternalState.BufferOffset == BLOCK_LENGTH) {<br>
> hashBlock();<br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Davide<br>
<br>
"There are no solved problems; there are only problems that are more<br>
or less solved" -- Henri Poincare<br>
</font></span></blockquote></div><br></div>