Line data Source code
1 : #define FD_SHA256_BATCH_IMPL 2
2 :
3 : #include "fd_sha256.h"
4 : #include "fd_sha256_constants.h"
5 : #include "../../util/simd/fd_avx512.h"
6 : #include "../../util/simd/fd_avx.h"
7 :
8 : FD_STATIC_ASSERT( FD_SHA256_BATCH_MAX==16UL, compat );
9 :
10 : void
11 : fd_sha256_private_batch_avx( ulong batch_cnt,
12 : void const * batch_data,
13 : ulong const * batch_sz,
14 : void * const * batch_hash );
15 :
16 : void
17 : fd_sha256_private_batch_avx512( ulong batch_cnt,
18 : void const * _batch_data,
19 : ulong const * batch_sz,
20 638315 : void * const * _batch_hash ) {
21 :
22 : /* If the batch is small enough, it is more efficient to use the
23 : narrow batched implementations. The threshold for fallback depends
24 : on whether that itself narrower batched implementation is using
25 : SHA-NI acceleration for really small batches. */
26 :
27 638315 : # if FD_HAS_SHANI
28 638315 : # define MIN_BATCH_CNT (5UL)
29 : # else
30 : # define MIN_BATCH_CNT (2UL)
31 : # endif
32 :
33 638315 : if( FD_UNLIKELY( batch_cnt<MIN_BATCH_CNT ) ) {
34 315130 : fd_sha256_private_batch_avx( batch_cnt, _batch_data, batch_sz, _batch_hash );
35 315130 : return;
36 315130 : }
37 :
38 323185 : # undef MIN_BATCH_CNT
39 :
40 : /* SHA appends to the end of each message 9 bytes of additional data
41 : (a messaging terminator byte and the big endian ulong with the
42 : message size in bits) and enough zero padding to make the message
43 : an integer number of blocks long. We compute the 1 or 2 tail
44 : blocks of each message here. We then process complete blocks of
45 : the original messages in place, switching to processing these tail
46 : blocks in the same pass toward the end. TODO: This code could
47 : probably be SIMD optimized slightly more (this is where all the
48 : really performance suboptimally designed parts of SHA live so it is
49 : just inherently gross). The main optimization would probably be to
50 : allow tail reading to use a faster memcpy and then maybe some
51 : vectorization of the bswap. */
52 :
53 323185 : ulong const * batch_data = (ulong const *)_batch_data;
54 :
55 323185 : ulong batch_tail_data[ FD_SHA256_BATCH_MAX ] __attribute__((aligned(64)));
56 323185 : ulong batch_tail_rem [ FD_SHA256_BATCH_MAX ] __attribute__((aligned(64)));
57 :
58 323185 : uchar scratch[ FD_SHA256_BATCH_MAX*2UL*FD_SHA256_PRIVATE_BUF_MAX ] __attribute__((aligned(128)));
59 323185 : do {
60 323185 : ulong scratch_free = (ulong)scratch;
61 :
62 323185 : wwv_t zero = wwv_zero();
63 :
64 5087802 : for( ulong batch_idx=0UL; batch_idx<batch_cnt; batch_idx++ ) {
65 :
66 : /* Allocate the tail blocks for this message */
67 :
68 4764617 : ulong data = batch_data[ batch_idx ];
69 4764617 : ulong sz = batch_sz [ batch_idx ];
70 :
71 4764617 : ulong tail_data = scratch_free;
72 4764617 : ulong tail_data_sz = sz & (FD_SHA256_PRIVATE_BUF_MAX-1UL);
73 4764617 : ulong tail_data_off = fd_ulong_align_dn( sz, FD_SHA256_PRIVATE_BUF_MAX );
74 4764617 : ulong tail_sz = fd_ulong_align_up( tail_data_sz+9UL, FD_SHA256_PRIVATE_BUF_MAX );
75 :
76 4764617 : batch_tail_data[ batch_idx ] = tail_data;
77 4764617 : batch_tail_rem [ batch_idx ] = tail_sz >> FD_SHA256_PRIVATE_LG_BUF_MAX;
78 :
79 4764617 : scratch_free += tail_sz;
80 :
81 : /* Populate the tail blocks. We first clear the blocks (note that
82 : it is okay to clobber bytes 64:127 if tail_sz only 64, saving a
83 : nasty branch). Then we copy any straggler data bytes into the
84 : tail, terminate the message, and finally record the size of the
85 : message in bits at the end as a big endian ulong. */
86 :
87 4764617 : wwv_st( (ulong *) tail_data, zero );
88 4764617 : wwv_st( (ulong *)(tail_data+64), zero );
89 :
90 4764617 : # if 1
91 : /* Quick experiments found that, once again, straight memcpy is
92 : much slower than a fd_memcpy is slightly slower than a
93 : site-optimized handrolled memcpy (fd_memcpy would be less L1I
94 : cache footprint though). They also found that doing the below
95 : in a branchless way is slightly worse and an ILP optimized
96 : version of the conditional calculation is about the same. They
97 : also found that vectorizing the overall loop and/or Duffing the
98 : vectorized loop did not provide noticeable performance
99 : improvements under various styles of memcpy. */
100 4764617 : ulong src = data + tail_data_off;
101 4764617 : ulong dst = tail_data;
102 4764617 : ulong rem = tail_data_sz;
103 7260435 : while( rem>=32UL ) { wv_st( (ulong *)dst, wv_ldu( (ulong const *)src ) ); dst += 32UL; src += 32UL; rem -= 32UL; }
104 11734260 : while( rem>= 8UL ) { *(ulong *)dst = FD_LOAD( ulong, src ); dst += 8UL; src += 8UL; rem -= 8UL; }
105 4764617 : if ( rem>= 4UL ) { *(uint *)dst = FD_LOAD( uint, src ); dst += 4UL; src += 4UL; rem -= 4UL; }
106 4764617 : if ( rem>= 2UL ) { *(ushort *)dst = FD_LOAD( ushort, src ); dst += 2UL; src += 2UL; rem -= 2UL; }
107 4764617 : if ( rem ) { *(uchar *)dst = FD_LOAD( uchar, src ); dst++; }
108 4764617 : *(uchar *)dst = (uchar)0x80;
109 : # else
110 : fd_memcpy( (void *)tail_data, (void const *)(data + tail_data_off), tail_data_sz );
111 : *((uchar *)(tail_data+tail_data_sz)) = (uchar)0x80;
112 : # endif
113 :
114 4764617 : *((ulong *)(tail_data+tail_sz-8UL )) = fd_ulong_bswap( sz<<3 );
115 4764617 : }
116 323185 : } while(0);
117 :
118 323185 : wwu_t s0 = wwu_bcast( FD_SHA256_INITIAL_A );
119 323185 : wwu_t s1 = wwu_bcast( FD_SHA256_INITIAL_B );
120 323185 : wwu_t s2 = wwu_bcast( FD_SHA256_INITIAL_C );
121 323185 : wwu_t s3 = wwu_bcast( FD_SHA256_INITIAL_D );
122 323185 : wwu_t s4 = wwu_bcast( FD_SHA256_INITIAL_E );
123 323185 : wwu_t s5 = wwu_bcast( FD_SHA256_INITIAL_F );
124 323185 : wwu_t s6 = wwu_bcast( FD_SHA256_INITIAL_G );
125 323185 : wwu_t s7 = wwu_bcast( FD_SHA256_INITIAL_H );
126 :
127 323185 : wwv_t zero = wwv_zero();
128 323185 : wwv_t one = wwv_one();
129 323185 : wwv_t wwv_64 = wwv_bcast( FD_SHA256_PRIVATE_BUF_MAX );
130 323185 : wwv_t W_sentinel = wwv_bcast( (ulong)scratch );
131 :
132 323185 : wwv_t tail_lo = wwv_ld( batch_tail_data ); wwv_t tail_hi = wwv_ld( batch_tail_data+8 );
133 323185 : wwv_t tail_rem_lo = wwv_ld( batch_tail_rem ); wwv_t tail_rem_hi = wwv_ld( batch_tail_rem +8 );
134 323185 : wwv_t W_lo = wwv_ld( batch_data ); wwv_t W_hi = wwv_ld( batch_data +8 );
135 :
136 323185 : wwv_t block_rem_lo = wwv_if( ((1<<batch_cnt)-1) & 0xff,
137 323185 : wwv_add( wwv_shr( wwv_ld( batch_sz ), FD_SHA256_PRIVATE_LG_BUF_MAX ), tail_rem_lo ), zero );
138 323185 : wwv_t block_rem_hi = wwv_if( ((1<<batch_cnt)-1) >> 8,
139 323185 : wwv_add( wwv_shr( wwv_ld( batch_sz+8 ), FD_SHA256_PRIVATE_LG_BUF_MAX ), tail_rem_hi ), zero );
140 :
141 4687745 : for(;;) {
142 4687745 : int active_lane_lo = wwv_ne( block_rem_lo, zero );
143 4687745 : int active_lane_hi = wwv_ne( block_rem_hi, zero );
144 4687745 : if( FD_UNLIKELY( !(active_lane_lo | active_lane_hi) ) ) break;
145 :
146 : /* Switch lanes that have hit the end of their in-place bulk
147 : processing to their out-of-place scratch tail regions as
148 : necessary. */
149 :
150 4364560 : W_lo = wwv_if( wwv_eq( block_rem_lo, tail_rem_lo ), tail_lo, W_lo );
151 4364560 : W_hi = wwv_if( wwv_eq( block_rem_hi, tail_rem_hi ), tail_hi, W_hi );
152 :
153 : /* At this point, we have at least 1 block in this message segment
154 : pass that has not been processed. Load the next 64 bytes of
155 : each unprocessed block. Inactive lanes (e.g. message segments
156 : in this pass for which we've already processed all the blocks)
157 : will load garbage from a sentinel location (and the result of
158 : the state computations for the inactive lane will be ignored). */
159 :
160 4364560 : ulong _W0; ulong _W1; ulong _W2; ulong _W3; ulong _W4; ulong _W5; ulong _W6; ulong _W7;
161 4364560 : ulong _W8; ulong _W9; ulong _Wa; ulong _Wb; ulong _Wc; ulong _Wd; ulong _We; ulong _Wf;
162 4364560 : wwv_unpack( wwv_if( active_lane_lo, W_lo, W_sentinel ), _W0, _W1, _W2, _W3, _W4, _W5, _W6, _W7 );
163 4364560 : wwv_unpack( wwv_if( active_lane_hi, W_hi, W_sentinel ), _W8, _W9, _Wa, _Wb, _Wc, _Wd, _We, _Wf );
164 4364560 : uchar const * W0 = (uchar const *)_W0; uchar const * W1 = (uchar const *)_W1;
165 4364560 : uchar const * W2 = (uchar const *)_W2; uchar const * W3 = (uchar const *)_W3;
166 4364560 : uchar const * W4 = (uchar const *)_W4; uchar const * W5 = (uchar const *)_W5;
167 4364560 : uchar const * W6 = (uchar const *)_W6; uchar const * W7 = (uchar const *)_W7;
168 4364560 : uchar const * W8 = (uchar const *)_W8; uchar const * W9 = (uchar const *)_W9;
169 4364560 : uchar const * Wa = (uchar const *)_Wa; uchar const * Wb = (uchar const *)_Wb;
170 4364560 : uchar const * Wc = (uchar const *)_Wc; uchar const * Wd = (uchar const *)_Wd;
171 4364560 : uchar const * We = (uchar const *)_We; uchar const * Wf = (uchar const *)_Wf;
172 :
173 4364560 : wwu_t x0; wwu_t x1; wwu_t x2; wwu_t x3; wwu_t x4; wwu_t x5; wwu_t x6; wwu_t x7;
174 4364560 : wwu_t x8; wwu_t x9; wwu_t xa; wwu_t xb; wwu_t xc; wwu_t xd; wwu_t xe; wwu_t xf;
175 4364560 : wwu_transpose_16x16( wwu_bswap( wwu_ldu( W0 ) ), wwu_bswap( wwu_ldu( W1 ) ),
176 4364560 : wwu_bswap( wwu_ldu( W2 ) ), wwu_bswap( wwu_ldu( W3 ) ),
177 4364560 : wwu_bswap( wwu_ldu( W4 ) ), wwu_bswap( wwu_ldu( W5 ) ),
178 4364560 : wwu_bswap( wwu_ldu( W6 ) ), wwu_bswap( wwu_ldu( W7 ) ),
179 4364560 : wwu_bswap( wwu_ldu( W8 ) ), wwu_bswap( wwu_ldu( W9 ) ),
180 4364560 : wwu_bswap( wwu_ldu( Wa ) ), wwu_bswap( wwu_ldu( Wb ) ),
181 4364560 : wwu_bswap( wwu_ldu( Wc ) ), wwu_bswap( wwu_ldu( Wd ) ),
182 4364560 : wwu_bswap( wwu_ldu( We ) ), wwu_bswap( wwu_ldu( Wf ) ),
183 4364560 : x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, xa, xb, xc, xd, xe, xf );
184 :
185 : /* Compute the SHA-256 state updates */
186 :
187 4364560 : wwu_t a = s0; wwu_t b = s1; wwu_t c = s2; wwu_t d = s3; wwu_t e = s4; wwu_t f = s5; wwu_t g = s6; wwu_t h = s7;
188 :
189 4364560 : # define Sigma0(x) wwu_xor( wwu_rol(x,30), wwu_xor( wwu_rol(x,19), wwu_rol(x,10) ) )
190 4364560 : # define Sigma1(x) wwu_xor( wwu_rol(x,26), wwu_xor( wwu_rol(x,21), wwu_rol(x, 7) ) )
191 4364560 : # define sigma0(x) wwu_xor( wwu_rol(x,25), wwu_xor( wwu_rol(x,14), wwu_shr(x, 3) ) )
192 4364560 : # define sigma1(x) wwu_xor( wwu_rol(x,15), wwu_xor( wwu_rol(x,13), wwu_shr(x,10) ) )
193 4364560 : # define Ch(x,y,z) wwu_xor( wwu_and(x,y), wwu_andnot(x,z) )
194 4364560 : # define Maj(x,y,z) wwu_xor( wwu_and(x,y), wwu_xor( wwu_and(x,z), wwu_and(y,z) ) )
195 4364560 : # define SHA_CORE(xi,ki) \
196 279331840 : T1 = wwu_add( wwu_add(xi,ki), wwu_add( wwu_add( h, Sigma1(e) ), Ch(e, f, g) ) ); \
197 279331840 : T2 = wwu_add( Sigma0(a), Maj(a, b, c) ); \
198 279331840 : h = g; \
199 279331840 : g = f; \
200 279331840 : f = e; \
201 279331840 : e = wwu_add( d, T1 ); \
202 279331840 : d = c; \
203 279331840 : c = b; \
204 279331840 : b = a; \
205 279331840 : a = wwu_add( T1, T2 )
206 :
207 4364560 : wwu_t T1;
208 4364560 : wwu_t T2;
209 :
210 4364560 : SHA_CORE( x0, wwu_bcast( fd_sha256_K[ 0] ) );
211 4364560 : SHA_CORE( x1, wwu_bcast( fd_sha256_K[ 1] ) );
212 4364560 : SHA_CORE( x2, wwu_bcast( fd_sha256_K[ 2] ) );
213 4364560 : SHA_CORE( x3, wwu_bcast( fd_sha256_K[ 3] ) );
214 4364560 : SHA_CORE( x4, wwu_bcast( fd_sha256_K[ 4] ) );
215 4364560 : SHA_CORE( x5, wwu_bcast( fd_sha256_K[ 5] ) );
216 4364560 : SHA_CORE( x6, wwu_bcast( fd_sha256_K[ 6] ) );
217 4364560 : SHA_CORE( x7, wwu_bcast( fd_sha256_K[ 7] ) );
218 4364560 : SHA_CORE( x8, wwu_bcast( fd_sha256_K[ 8] ) );
219 4364560 : SHA_CORE( x9, wwu_bcast( fd_sha256_K[ 9] ) );
220 4364560 : SHA_CORE( xa, wwu_bcast( fd_sha256_K[10] ) );
221 4364560 : SHA_CORE( xb, wwu_bcast( fd_sha256_K[11] ) );
222 4364560 : SHA_CORE( xc, wwu_bcast( fd_sha256_K[12] ) );
223 4364560 : SHA_CORE( xd, wwu_bcast( fd_sha256_K[13] ) );
224 4364560 : SHA_CORE( xe, wwu_bcast( fd_sha256_K[14] ) );
225 4364560 : SHA_CORE( xf, wwu_bcast( fd_sha256_K[15] ) );
226 17458240 : for( ulong i=16UL; i<64UL; i+=16UL ) {
227 13093680 : x0 = wwu_add( wwu_add( x0, sigma0(x1) ), wwu_add( sigma1(xe), x9 ) ); SHA_CORE( x0, wwu_bcast( fd_sha256_K[i ] ) );
228 13093680 : x1 = wwu_add( wwu_add( x1, sigma0(x2) ), wwu_add( sigma1(xf), xa ) ); SHA_CORE( x1, wwu_bcast( fd_sha256_K[i+ 1UL] ) );
229 13093680 : x2 = wwu_add( wwu_add( x2, sigma0(x3) ), wwu_add( sigma1(x0), xb ) ); SHA_CORE( x2, wwu_bcast( fd_sha256_K[i+ 2UL] ) );
230 13093680 : x3 = wwu_add( wwu_add( x3, sigma0(x4) ), wwu_add( sigma1(x1), xc ) ); SHA_CORE( x3, wwu_bcast( fd_sha256_K[i+ 3UL] ) );
231 13093680 : x4 = wwu_add( wwu_add( x4, sigma0(x5) ), wwu_add( sigma1(x2), xd ) ); SHA_CORE( x4, wwu_bcast( fd_sha256_K[i+ 4UL] ) );
232 13093680 : x5 = wwu_add( wwu_add( x5, sigma0(x6) ), wwu_add( sigma1(x3), xe ) ); SHA_CORE( x5, wwu_bcast( fd_sha256_K[i+ 5UL] ) );
233 13093680 : x6 = wwu_add( wwu_add( x6, sigma0(x7) ), wwu_add( sigma1(x4), xf ) ); SHA_CORE( x6, wwu_bcast( fd_sha256_K[i+ 6UL] ) );
234 13093680 : x7 = wwu_add( wwu_add( x7, sigma0(x8) ), wwu_add( sigma1(x5), x0 ) ); SHA_CORE( x7, wwu_bcast( fd_sha256_K[i+ 7UL] ) );
235 13093680 : x8 = wwu_add( wwu_add( x8, sigma0(x9) ), wwu_add( sigma1(x6), x1 ) ); SHA_CORE( x8, wwu_bcast( fd_sha256_K[i+ 8UL] ) );
236 13093680 : x9 = wwu_add( wwu_add( x9, sigma0(xa) ), wwu_add( sigma1(x7), x2 ) ); SHA_CORE( x9, wwu_bcast( fd_sha256_K[i+ 9UL] ) );
237 13093680 : xa = wwu_add( wwu_add( xa, sigma0(xb) ), wwu_add( sigma1(x8), x3 ) ); SHA_CORE( xa, wwu_bcast( fd_sha256_K[i+10UL] ) );
238 13093680 : xb = wwu_add( wwu_add( xb, sigma0(xc) ), wwu_add( sigma1(x9), x4 ) ); SHA_CORE( xb, wwu_bcast( fd_sha256_K[i+11UL] ) );
239 13093680 : xc = wwu_add( wwu_add( xc, sigma0(xd) ), wwu_add( sigma1(xa), x5 ) ); SHA_CORE( xc, wwu_bcast( fd_sha256_K[i+12UL] ) );
240 13093680 : xd = wwu_add( wwu_add( xd, sigma0(xe) ), wwu_add( sigma1(xb), x6 ) ); SHA_CORE( xd, wwu_bcast( fd_sha256_K[i+13UL] ) );
241 13093680 : xe = wwu_add( wwu_add( xe, sigma0(xf) ), wwu_add( sigma1(xc), x7 ) ); SHA_CORE( xe, wwu_bcast( fd_sha256_K[i+14UL] ) );
242 13093680 : xf = wwu_add( wwu_add( xf, sigma0(x0) ), wwu_add( sigma1(xd), x8 ) ); SHA_CORE( xf, wwu_bcast( fd_sha256_K[i+15UL] ) );
243 13093680 : }
244 :
245 4364560 : # undef SHA_CORE
246 4364560 : # undef Sigma0
247 4364560 : # undef Sigma1
248 4364560 : # undef sigma0
249 4364560 : # undef sigma1
250 4364560 : # undef Ch
251 4364560 : # undef Maj
252 :
253 : /* Apply the state updates to the active lanes */
254 :
255 4364560 : int active_lane = active_lane_lo | (active_lane_hi<<8);
256 :
257 4364560 : s0 = wwu_add_if( active_lane, s0, a, s0 );
258 4364560 : s1 = wwu_add_if( active_lane, s1, b, s1 );
259 4364560 : s2 = wwu_add_if( active_lane, s2, c, s2 );
260 4364560 : s3 = wwu_add_if( active_lane, s3, d, s3 );
261 4364560 : s4 = wwu_add_if( active_lane, s4, e, s4 );
262 4364560 : s5 = wwu_add_if( active_lane, s5, f, s5 );
263 4364560 : s6 = wwu_add_if( active_lane, s6, g, s6 );
264 4364560 : s7 = wwu_add_if( active_lane, s7, h, s7 );
265 :
266 : /* Advance to the next message segment blocks. In pseudo code,
267 : the below is:
268 :
269 : W += 64; if( block_rem ) block_rem--;
270 :
271 : Since we do not load anything at W(lane) above unless
272 : block_rem(lane) is non-zero, we can omit vector conditional
273 : operations for W(lane) below. */
274 :
275 4364560 : W_lo = wwv_add( W_lo, wwv_64 );
276 4364560 : W_hi = wwv_add( W_hi, wwv_64 );
277 :
278 4364560 : block_rem_lo = wwv_sub_if( active_lane_lo, block_rem_lo, one, block_rem_lo );
279 4364560 : block_rem_hi = wwv_sub_if( active_lane_hi, block_rem_hi, one, block_rem_hi );
280 4364560 : }
281 :
282 : /* Store the results. FIXME: Probably could optimize the transpose
283 : further by taking into account needed stores (and then maybe go
284 : direct into memory ... would need a family of such transposed
285 : stores). */
286 :
287 323185 : wwu_transpose_2x8x8( wwu_bswap(s0), wwu_bswap(s1), wwu_bswap(s2), wwu_bswap(s3),
288 323185 : wwu_bswap(s4), wwu_bswap(s5), wwu_bswap(s6), wwu_bswap(s7), s0,s1,s2,s3,s4,s5,s6,s7 );
289 :
290 323185 : uint * const * batch_hash = (uint * const *)_batch_hash;
291 323185 : switch( batch_cnt ) { /* application dependent prob */
292 276337 : case 16UL: wu_stu( batch_hash[15], _mm512_extracti32x8_epi32( s7, 1 ) ); __attribute__((fallthrough));
293 278293 : case 15UL: wu_stu( batch_hash[14], _mm512_extracti32x8_epi32( s6, 1 ) ); __attribute__((fallthrough));
294 280298 : case 14UL: wu_stu( batch_hash[13], _mm512_extracti32x8_epi32( s5, 1 ) ); __attribute__((fallthrough));
295 282292 : case 13UL: wu_stu( batch_hash[12], _mm512_extracti32x8_epi32( s4, 1 ) ); __attribute__((fallthrough));
296 284309 : case 12UL: wu_stu( batch_hash[11], _mm512_extracti32x8_epi32( s3, 1 ) ); __attribute__((fallthrough));
297 286243 : case 11UL: wu_stu( batch_hash[10], _mm512_extracti32x8_epi32( s2, 1 ) ); __attribute__((fallthrough));
298 288183 : case 10UL: wu_stu( batch_hash[ 9], _mm512_extracti32x8_epi32( s1, 1 ) ); __attribute__((fallthrough));
299 290178 : case 9UL: wu_stu( batch_hash[ 8], _mm512_extracti32x8_epi32( s0, 1 ) ); __attribute__((fallthrough));
300 292192 : case 8UL: wu_stu( batch_hash[ 7], _mm512_extracti32x8_epi32( s7, 0 ) ); __attribute__((fallthrough));
301 294189 : case 7UL: wu_stu( batch_hash[ 6], _mm512_extracti32x8_epi32( s6, 0 ) ); __attribute__((fallthrough));
302 296178 : case 6UL: wu_stu( batch_hash[ 5], _mm512_extracti32x8_epi32( s5, 0 ) ); __attribute__((fallthrough));
303 323185 : case 5UL: wu_stu( batch_hash[ 4], _mm512_extracti32x8_epi32( s4, 0 ) ); __attribute__((fallthrough));
304 323185 : case 4UL: wu_stu( batch_hash[ 3], _mm512_extracti32x8_epi32( s3, 0 ) ); __attribute__((fallthrough));
305 323185 : case 3UL: wu_stu( batch_hash[ 2], _mm512_extracti32x8_epi32( s2, 0 ) ); __attribute__((fallthrough));
306 323185 : case 2UL: wu_stu( batch_hash[ 1], _mm512_extracti32x8_epi32( s1, 0 ) ); __attribute__((fallthrough));
307 323185 : case 1UL: wu_stu( batch_hash[ 0], _mm512_extracti32x8_epi32( s0, 0 ) ); __attribute__((fallthrough));
308 323185 : default: break;
309 323185 : }
310 323185 : }
311 :
312 : #if defined(__znver5__)
313 : #define MIN_ACTIVE (6) /* Zen 5 has high AVX-512 throughput */
314 : #else
315 22 : #define MIN_ACTIVE (8) /* Baseline 1 IPC AVX-512 needs more batching to win against SHA-NI */
316 : #endif
317 :
318 22 : ulong fd_sha256_simd_lane_min( void ) { return MIN_ACTIVE; }
319 1023 : ulong fd_sha256_simd_lane_max( void ) { return 16UL; }
320 17 : ulong fd_sha256_simd_iter_cost_q8( void ) { return 1357UL; } /* 5.3x on Zen 5: 16 lanes at 96.5 M hashes/s vs 31.7 M hashes/s single lane with SHA-NI */
321 :
322 : void
323 : fd_sha256_hash_32_repeated_batch_avx512( uchar const * hash_in,
324 : uchar * hash_out,
325 : ulong cnt,
326 1000 : ulong batch_cnt ) {
327 :
328 : /* Below the SIMD floor, SHA-NI (or the scalar core) wins. */
329 :
330 1000 : if( FD_UNLIKELY( batch_cnt<MIN_ACTIVE ) ) {
331 2072 : for( ulong i=0UL; i<batch_cnt; i++ ) fd_sha256_hash_32_repeated( hash_in+32UL*i, hash_out+32UL*i, cnt );
332 460 : return;
333 460 : }
334 :
335 : /* Gather the batch into a 16 lane scratch buffer. Lanes at and
336 : beyond batch_cnt hash zeros; their results are never stored. */
337 :
338 540 : uchar scratch[ 16UL*32UL ] __attribute__((aligned(64)));
339 540 : memset( scratch, 0, sizeof(scratch) );
340 540 : memcpy( scratch, hash_in, 32UL*batch_cnt );
341 :
342 540 : wwu_t const iv0 = wwu_bcast( FD_SHA256_INITIAL_A );
343 540 : wwu_t const iv1 = wwu_bcast( FD_SHA256_INITIAL_B );
344 540 : wwu_t const iv2 = wwu_bcast( FD_SHA256_INITIAL_C );
345 540 : wwu_t const iv3 = wwu_bcast( FD_SHA256_INITIAL_D );
346 540 : wwu_t const iv4 = wwu_bcast( FD_SHA256_INITIAL_E );
347 540 : wwu_t const iv5 = wwu_bcast( FD_SHA256_INITIAL_F );
348 540 : wwu_t const iv6 = wwu_bcast( FD_SHA256_INITIAL_G );
349 540 : wwu_t const iv7 = wwu_bcast( FD_SHA256_INITIAL_H );
350 :
351 540 : # define SCALAR_ROTR(x,n) ( ((x)>>(n)) | ((x)<<(32-(n))) )
352 540 : # define SCALAR_sigma0(x) ( SCALAR_ROTR((x), 7) ^ SCALAR_ROTR((x),18) ^ ((x)>> 3) )
353 540 : # define SCALAR_sigma1(x) ( SCALAR_ROTR((x),17) ^ SCALAR_ROTR((x),19) ^ ((x)>>10) )
354 540 : uint const PAD8 = 0x80000000U;
355 540 : uint const PADF = 256U;
356 :
357 540 : # define LOAD_PAIR( i ) _mm512_inserti64x4( _mm512_castsi256_si512( wu_ld( (uint const *)(scratch+32UL*(i)) ) ), \
358 540 : wu_ld( (uint const *)(scratch+32UL*((i)+8UL)) ), 1 )
359 4320 : # define STORE_PAIR( i, s ) do { \
360 4320 : wu_st( (uint *)(scratch+32UL*(i)), _mm512_extracti32x8_epi32( (s), 0 ) ); \
361 4320 : wu_st( (uint *)(scratch+32UL*((i)+8UL)), _mm512_extracti32x8_epi32( (s), 1 ) ); \
362 4320 : } while(0)
363 :
364 540 : # define Sigma0(x) _mm512_ternarylogic_epi32( wwu_rol(x,30), wwu_rol(x,19), wwu_rol(x,10), 0x96 )
365 540 : # define Sigma1(x) _mm512_ternarylogic_epi32( wwu_rol(x,26), wwu_rol(x,21), wwu_rol(x, 7), 0x96 )
366 540 : # define sigma0(x) _mm512_ternarylogic_epi32( wwu_rol(x,25), wwu_rol(x,14), wwu_shr(x, 3), 0x96 )
367 540 : # define sigma1(x) _mm512_ternarylogic_epi32( wwu_rol(x,15), wwu_rol(x,13), wwu_shr(x,10), 0x96 )
368 540 : # define Ch(x,y,z) _mm512_ternarylogic_epi32( (x), (y), (z), 0xCA )
369 540 : # define Maj(x,y,z) _mm512_ternarylogic_epi32( (x), (y), (z), 0xE8 )
370 :
371 540 : # define SHA_CORE(wk) \
372 3412352 : T1 = wwu_add( (wk), wwu_add( wwu_add( h, Sigma1(e) ), Ch(e, f, g) ) ); \
373 3412352 : T2 = wwu_add( Sigma0(a), Maj(a, b, c) ); \
374 3412352 : h = g; \
375 3412352 : g = f; \
376 3412352 : f = e; \
377 3412352 : e = wwu_add( d, T1 ); \
378 3412352 : d = c; \
379 3412352 : c = b; \
380 3412352 : b = a; \
381 3412352 : a = wwu_add( T1, T2 )
382 :
383 2985808 : # define ROUND(xi,ki) SHA_CORE( wwu_add( xi, wwu_bcast( fd_sha256_K[ki] ) ) )
384 :
385 540 : # define EXPAND_ROUNDS(i) \
386 106636 : x0 = wwu_add( wwu_add( x0, sigma0(x1) ), wwu_add( sigma1(xe), x9 ) ); ROUND( x0, (i) ); \
387 106636 : x1 = wwu_add( wwu_add( x1, sigma0(x2) ), wwu_add( sigma1(xf), xa ) ); ROUND( x1, (i)+ 1UL ); \
388 106636 : x2 = wwu_add( wwu_add( x2, sigma0(x3) ), wwu_add( sigma1(x0), xb ) ); ROUND( x2, (i)+ 2UL ); \
389 106636 : x3 = wwu_add( wwu_add( x3, sigma0(x4) ), wwu_add( sigma1(x1), xc ) ); ROUND( x3, (i)+ 3UL ); \
390 106636 : x4 = wwu_add( wwu_add( x4, sigma0(x5) ), wwu_add( sigma1(x2), xd ) ); ROUND( x4, (i)+ 4UL ); \
391 106636 : x5 = wwu_add( wwu_add( x5, sigma0(x6) ), wwu_add( sigma1(x3), xe ) ); ROUND( x5, (i)+ 5UL ); \
392 106636 : x6 = wwu_add( wwu_add( x6, sigma0(x7) ), wwu_add( sigma1(x4), xf ) ); ROUND( x6, (i)+ 6UL ); \
393 106636 : x7 = wwu_add( wwu_add( x7, sigma0(x8) ), wwu_add( sigma1(x5), x0 ) ); ROUND( x7, (i)+ 7UL ); \
394 106636 : x8 = wwu_add( wwu_add( x8, sigma0(x9) ), wwu_add( sigma1(x6), x1 ) ); ROUND( x8, (i)+ 8UL ); \
395 106636 : x9 = wwu_add( wwu_add( x9, sigma0(xa) ), wwu_add( sigma1(x7), x2 ) ); ROUND( x9, (i)+ 9UL ); \
396 106636 : xa = wwu_add( wwu_add( xa, sigma0(xb) ), wwu_add( sigma1(x8), x3 ) ); ROUND( xa, (i)+10UL ); \
397 106636 : xb = wwu_add( wwu_add( xb, sigma0(xc) ), wwu_add( sigma1(x9), x4 ) ); ROUND( xb, (i)+11UL ); \
398 106636 : xc = wwu_add( wwu_add( xc, sigma0(xd) ), wwu_add( sigma1(xa), x5 ) ); ROUND( xc, (i)+12UL ); \
399 106636 : xd = wwu_add( wwu_add( xd, sigma0(xe) ), wwu_add( sigma1(xb), x6 ) ); ROUND( xd, (i)+13UL ); \
400 106636 : xe = wwu_add( wwu_add( xe, sigma0(xf) ), wwu_add( sigma1(xc), x7 ) ); ROUND( xe, (i)+14UL ); \
401 106636 : xf = wwu_add( wwu_add( xf, sigma0(x0) ), wwu_add( sigma1(xd), x8 ) ); ROUND( xf, (i)+15UL )
402 :
403 : /* Transpose 16 lanes x 8 words into 8 vectors of 16 lanes each. */
404 :
405 540 : wwu_t s0; wwu_t s1; wwu_t s2; wwu_t s3; wwu_t s4; wwu_t s5; wwu_t s6; wwu_t s7;
406 540 : wwu_transpose_2x8x8( wwu_bswap( LOAD_PAIR( 0 ) ), wwu_bswap( LOAD_PAIR( 1 ) ),
407 540 : wwu_bswap( LOAD_PAIR( 2 ) ), wwu_bswap( LOAD_PAIR( 3 ) ),
408 540 : wwu_bswap( LOAD_PAIR( 4 ) ), wwu_bswap( LOAD_PAIR( 5 ) ),
409 540 : wwu_bswap( LOAD_PAIR( 6 ) ), wwu_bswap( LOAD_PAIR( 7 ) ),
410 540 : s0, s1, s2, s3, s4, s5, s6, s7 );
411 :
412 53858 : for( ulong iter=0UL; iter<cnt; iter++ ) {
413 53318 : wwu_t a = iv0; wwu_t b = iv1; wwu_t c = iv2; wwu_t d = iv3; wwu_t e = iv4; wwu_t f = iv5; wwu_t g = iv6; wwu_t h = iv7;
414 53318 : wwu_t T1;
415 53318 : wwu_t T2;
416 :
417 53318 : wwu_t x0 = s0; wwu_t x1 = s1; wwu_t x2 = s2; wwu_t x3 = s3; wwu_t x4 = s4; wwu_t x5 = s5; wwu_t x6 = s6; wwu_t x7 = s7;
418 53318 : wwu_t x8; wwu_t x9; wwu_t xa; wwu_t xb; wwu_t xc; wwu_t xd; wwu_t xe; wwu_t xf;
419 :
420 53318 : ROUND( x0, 0 ); ROUND( x1, 1 ); ROUND( x2, 2 ); ROUND( x3, 3 );
421 53318 : ROUND( x4, 4 ); ROUND( x5, 5 ); ROUND( x6, 6 ); ROUND( x7, 7 );
422 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[ 8] + PAD8 ) );
423 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[ 9] ) );
424 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[10] ) );
425 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[11] ) );
426 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[12] ) );
427 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[13] ) );
428 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[14] ) );
429 53318 : SHA_CORE( wwu_bcast( fd_sha256_K[15] + PADF ) );
430 :
431 : /* x8=PAD8, x9..xe=0, xf=PADF folded in as constants */
432 :
433 53318 : x0 = wwu_add( x0, sigma0(x1) ); ROUND( x0, 16 );
434 53318 : x1 = wwu_add( wwu_add( x1, sigma0(x2) ), wwu_bcast( SCALAR_sigma1(PADF) ) ); ROUND( x1, 17 );
435 53318 : x2 = wwu_add( wwu_add( x2, sigma0(x3) ), sigma1(x0) ); ROUND( x2, 18 );
436 53318 : x3 = wwu_add( wwu_add( x3, sigma0(x4) ), sigma1(x1) ); ROUND( x3, 19 );
437 53318 : x4 = wwu_add( wwu_add( x4, sigma0(x5) ), sigma1(x2) ); ROUND( x4, 20 );
438 53318 : x5 = wwu_add( wwu_add( x5, sigma0(x6) ), sigma1(x3) ); ROUND( x5, 21 );
439 53318 : x6 = wwu_add( wwu_add( x6, sigma0(x7) ), wwu_add( sigma1(x4), wwu_bcast( PADF ) ) ); ROUND( x6, 22 );
440 53318 : x7 = wwu_add( wwu_add( x7, wwu_bcast( SCALAR_sigma0(PAD8) ) ), wwu_add( sigma1(x5), x0 ) ); ROUND( x7, 23 );
441 53318 : x8 = wwu_add( wwu_bcast( PAD8 ), wwu_add( sigma1(x6), x1 ) ); ROUND( x8, 24 );
442 53318 : x9 = wwu_add( sigma1(x7), x2 ); ROUND( x9, 25 );
443 53318 : xa = wwu_add( sigma1(x8), x3 ); ROUND( xa, 26 );
444 53318 : xb = wwu_add( sigma1(x9), x4 ); ROUND( xb, 27 );
445 53318 : xc = wwu_add( sigma1(xa), x5 ); ROUND( xc, 28 );
446 53318 : xd = wwu_add( sigma1(xb), x6 ); ROUND( xd, 29 );
447 53318 : xe = wwu_add( wwu_bcast( SCALAR_sigma0(PADF) ), wwu_add( sigma1(xc), x7 ) ); ROUND( xe, 30 );
448 53318 : xf = wwu_add( wwu_add( wwu_bcast( PADF ), sigma0(x0) ), wwu_add( sigma1(xd), x8 ) ); ROUND( xf, 31 );
449 :
450 53318 : EXPAND_ROUNDS( 32 );
451 53318 : EXPAND_ROUNDS( 48 );
452 :
453 53318 : s0 = wwu_add( a, iv0 ); s1 = wwu_add( b, iv1 ); s2 = wwu_add( c, iv2 ); s3 = wwu_add( d, iv3 );
454 53318 : s4 = wwu_add( e, iv4 ); s5 = wwu_add( f, iv5 ); s6 = wwu_add( g, iv6 ); s7 = wwu_add( h, iv7 );
455 53318 : }
456 :
457 540 : wwu_t t0; wwu_t t1; wwu_t t2; wwu_t t3; wwu_t t4; wwu_t t5; wwu_t t6; wwu_t t7;
458 540 : wwu_transpose_2x8x8( wwu_bswap(s0), wwu_bswap(s1), wwu_bswap(s2), wwu_bswap(s3),
459 540 : wwu_bswap(s4), wwu_bswap(s5), wwu_bswap(s6), wwu_bswap(s7), t0,t1,t2,t3,t4,t5,t6,t7 );
460 540 : STORE_PAIR( 0, t0 ); STORE_PAIR( 1, t1 ); STORE_PAIR( 2, t2 ); STORE_PAIR( 3, t3 );
461 540 : STORE_PAIR( 4, t4 ); STORE_PAIR( 5, t5 ); STORE_PAIR( 6, t6 ); STORE_PAIR( 7, t7 );
462 :
463 540 : memcpy( hash_out, scratch, 32UL*batch_cnt );
464 :
465 540 : # undef EXPAND_ROUNDS
466 540 : # undef ROUND
467 540 : # undef SHA_CORE
468 540 : # undef Maj
469 540 : # undef Ch
470 540 : # undef sigma1
471 540 : # undef sigma0
472 540 : # undef Sigma1
473 540 : # undef Sigma0
474 540 : # undef SCALAR_sigma1
475 540 : # undef SCALAR_sigma0
476 540 : # undef SCALAR_ROTR
477 540 : # undef STORE_PAIR
478 540 : # undef LOAD_PAIR
479 540 : }
480 :
481 : #undef MIN_ACTIVE
|