Skip to content

Commit f982fd7

Browse files
committed
lbry: small sha512 improvement
1 parent f8aa16f commit f982fd7

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

lbry/cuda_sha512_lbry.cu

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,30 @@ uint64_t K_512[80] = {
4040
#undef xor3
4141
#define xor3(a,b,c) (a^b^c)
4242

43+
//#define ROR64_8(x) ROTR64(x,8)
44+
__device__ __inline__
45+
uint64_t ROR64_8(const uint64_t u64) {
46+
const uint2 a = vectorize(u64);
47+
uint2 result;
48+
result.x = __byte_perm(a.y, a.x, 0x0765);
49+
result.y = __byte_perm(a.y, a.x, 0x4321);
50+
return devectorize(result);
51+
}
52+
4353
#define bsg5_0(x) xor3(ROTR64(x,28),ROTR64(x,34),ROTR64(x,39))
4454
#define bsg5_1(x) xor3(ROTR64(x,14),ROTR64(x,18),ROTR64(x,41))
45-
#define ssg5_0(x) xor3(ROTR64(x,1),ROTR64(x,8),x>>7)
46-
#define ssg5_1(x) xor3(ROTR64(x,19),ROTR64(x,61),x>>6)
47-
55+
#define ssg5_0(x) xor3(ROTR64(x,1), ROR64_8(x), x>>7)
56+
#define ssg5_1(x) xor3(ROTR64(x,19),ROTR64(x,61), x>>6)
4857

4958
#define andor64(a,b,c) ((a & (b | c)) | (b & c))
5059
#define xandx64(e,f,g) (g ^ (e & (g ^ f)))
5160

5261
static __device__ __forceinline__
53-
void sha512_step2(uint64_t* r,const uint64_t W,const uint64_t K, const int ord)
62+
void sha512_step2(uint64_t* r, const uint64_t W, const uint64_t K, const int ord)
5463
{
5564
const uint64_t T1 = r[(15-ord) & 7] + K + W + bsg5_1(r[(12-ord) & 7]) + xandx64(r[(12-ord) & 7],r[(13-ord) & 7],r[(14-ord) & 7]);
56-
r[(15-ord)& 7] = andor64(r[( 8-ord) & 7],r[( 9-ord) & 7],r[(10-ord) & 7]) + bsg5_0(r[( 8-ord) & 7]) + T1;
57-
r[(11-ord)& 7]+= T1;
65+
r[(15-ord) & 7] = andor64(r[(8-ord) & 7],r[(9-ord) & 7],r[(10-ord) & 7]) + bsg5_0(r[(8-ord) & 7]) + T1;
66+
r[(11-ord) & 7] += T1;
5867
}
5968

6069
/**************************************************************************************************/
@@ -67,16 +76,17 @@ void lbry_sha512_gpu_hash_32(const uint32_t threads, uint64_t *g_hash)
6776
0x6A09E667F3BCC908, 0xBB67AE8584CAA73B, 0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1,
6877
0x510E527FADE682D1, 0x9B05688C2B3E6C1F, 0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179
6978
};
79+
7080
uint64_t r[8];
7181
uint64_t W[16];
7282
if (thread < threads)
7383
{
7484
uint64_t *pHash = &g_hash[thread<<3];
7585

76-
*(uint2x4*)&r[ 0] = *(uint2x4*)&IV512[ 0];
77-
*(uint2x4*)&r[ 4] = *(uint2x4*)&IV512[ 4];
86+
*(uint2x4*)&r[0] = *(uint2x4*)&IV512[0];
87+
*(uint2x4*)&r[4] = *(uint2x4*)&IV512[4];
7888

79-
*(uint2x4*)&W[ 0] = __ldg4((uint2x4*)&pHash[ 0]);
89+
*(uint2x4*)&W[0] = __ldg4((uint2x4*)pHash);
8090

8191
W[4] = 0x8000000000000000; // end tag
8292

@@ -91,7 +101,7 @@ void lbry_sha512_gpu_hash_32(const uint32_t threads, uint64_t *g_hash)
91101
}
92102

93103
#pragma unroll
94-
for (int i = 16; i < 80; i+=16){
104+
for (int i = 16; i < 80; i+=16) {
95105
#pragma unroll
96106
for (int j = 0; j<16; j++) {
97107
W[(i + j) & 15] += W[((i + j) - 7) & 15] + ssg5_0(W[((i + j) - 15) & 15]) + ssg5_1(W[((i + j) - 2) & 15]);

0 commit comments

Comments
 (0)