Skip to content

Commit ce4e1f0

Browse files
committed
Merge #18553: Avoid non-trivial global constants in SHA-NI code
8508473 Avoid non-trivial global constants in SHA-NI code (Pieter Wuille) Pull request description: This is a potential solution for #18456. It seems that the compiler cannot turn `_mm_set_epi64x(<constant>,<constnant>)` into a constant itself, and thus emits a global initializer for the `MASK`, `INIT0`, and `INIT1` global constants in the sha-ni SHA256 implementation. Change this by turning them into dumb byte arrays, loading them into an SSE variable whenever needed. Tested on a SHA-NI capable machine. I do not observe any obvious performance impact (but this is hard to measure, it's already very fast...). ACKs for top commit: laanwj: Code review ACK 8508473 elichai: ACK 8508473 Tree-SHA512: 07049cf1a33624c22df2be48b814d5636c037b368861eb13ee073bdce2b7c902a56e96518218961f55a2a1631a40825ded6dbbc28d7fe0e7fec267d704e39112
2 parents 9e8e813 + 8508473 commit ce4e1f0

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/crypto/sha256_shani.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
#include <stdint.h>
1212
#include <immintrin.h>
1313

14-
15-
1614
namespace {
1715

18-
const __m128i MASK = _mm_set_epi64x(0x0c0d0e0f08090a0bULL, 0x0405060700010203ULL);
19-
const __m128i INIT0 = _mm_set_epi64x(0x6a09e667bb67ae85ull, 0x510e527f9b05688cull);
20-
const __m128i INIT1 = _mm_set_epi64x(0x3c6ef372a54ff53aull, 0x1f83d9ab5be0cd19ull);
16+
alignas(__m128i) const uint8_t MASK[16] = {0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0e, 0x0d, 0x0c};
17+
alignas(__m128i) const uint8_t INIT0[16] = {0x8c, 0x68, 0x05, 0x9b, 0x7f, 0x52, 0x0e, 0x51, 0x85, 0xae, 0x67, 0xbb, 0x67, 0xe6, 0x09, 0x6a};
18+
alignas(__m128i) const uint8_t INIT1[16] = {0x19, 0xcd, 0xe0, 0x5b, 0xab, 0xd9, 0x83, 0x1f, 0x3a, 0xf5, 0x4f, 0xa5, 0x72, 0xf3, 0x6e, 0x3c};
2119

2220
void inline __attribute__((always_inline)) QuadRound(__m128i& state0, __m128i& state1, uint64_t k1, uint64_t k0)
2321
{
@@ -67,12 +65,12 @@ void inline __attribute__((always_inline)) Unshuffle(__m128i& s0, __m128i& s1)
6765

6866
__m128i inline __attribute__((always_inline)) Load(const unsigned char* in)
6967
{
70-
return _mm_shuffle_epi8(_mm_loadu_si128((const __m128i*)in), MASK);
68+
return _mm_shuffle_epi8(_mm_loadu_si128((const __m128i*)in), _mm_load_si128((const __m128i*)MASK));
7169
}
7270

7371
void inline __attribute__((always_inline)) Save(unsigned char* out, __m128i s)
7472
{
75-
_mm_storeu_si128((__m128i*)out, _mm_shuffle_epi8(s, MASK));
73+
_mm_storeu_si128((__m128i*)out, _mm_shuffle_epi8(s, _mm_load_si128((const __m128i*)MASK)));
7674
}
7775
}
7876

@@ -149,8 +147,8 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
149147
__m128i bm0, bm1, bm2, bm3, bs0, bs1, bso0, bso1;
150148

151149
/* Transform 1 */
152-
bs0 = as0 = INIT0;
153-
bs1 = as1 = INIT1;
150+
bs0 = as0 = _mm_load_si128((const __m128i*)INIT0);
151+
bs1 = as1 = _mm_load_si128((const __m128i*)INIT1);
154152
am0 = Load(in);
155153
bm0 = Load(in + 64);
156154
QuadRound(as0, as1, am0, 0xe9b5dba5b5c0fbcfull, 0x71374491428a2f98ull);
@@ -219,10 +217,10 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
219217
ShiftMessageC(bm1, bm2, bm3);
220218
QuadRound(as0, as1, am3, 0xc67178f2bef9A3f7ull, 0xa4506ceb90befffaull);
221219
QuadRound(bs0, bs1, bm3, 0xc67178f2bef9A3f7ull, 0xa4506ceb90befffaull);
222-
as0 = _mm_add_epi32(as0, INIT0);
223-
bs0 = _mm_add_epi32(bs0, INIT0);
224-
as1 = _mm_add_epi32(as1, INIT1);
225-
bs1 = _mm_add_epi32(bs1, INIT1);
220+
as0 = _mm_add_epi32(as0, _mm_load_si128((const __m128i*)INIT0));
221+
bs0 = _mm_add_epi32(bs0, _mm_load_si128((const __m128i*)INIT0));
222+
as1 = _mm_add_epi32(as1, _mm_load_si128((const __m128i*)INIT1));
223+
bs1 = _mm_add_epi32(bs1, _mm_load_si128((const __m128i*)INIT1));
226224

227225
/* Transform 2 */
228226
aso0 = as0;
@@ -275,8 +273,8 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
275273
bm1 = bs1;
276274

277275
/* Transform 3 */
278-
bs0 = as0 = INIT0;
279-
bs1 = as1 = INIT1;
276+
bs0 = as0 = _mm_load_si128((const __m128i*)INIT0);
277+
bs1 = as1 = _mm_load_si128((const __m128i*)INIT1);
280278
QuadRound(as0, as1, am0, 0xe9b5dba5B5c0fbcfull, 0x71374491428a2f98ull);
281279
QuadRound(bs0, bs1, bm0, 0xe9b5dba5B5c0fbcfull, 0x71374491428a2f98ull);
282280
QuadRound(as0, as1, am1, 0xab1c5ed5923f82a4ull, 0x59f111f13956c25bull);
@@ -339,10 +337,10 @@ void Transform_2way(unsigned char* out, const unsigned char* in)
339337
ShiftMessageC(bm1, bm2, bm3);
340338
QuadRound(as0, as1, am3, 0xc67178f2bef9a3f7ull, 0xa4506ceb90befffaull);
341339
QuadRound(bs0, bs1, bm3, 0xc67178f2bef9a3f7ull, 0xa4506ceb90befffaull);
342-
as0 = _mm_add_epi32(as0, INIT0);
343-
bs0 = _mm_add_epi32(bs0, INIT0);
344-
as1 = _mm_add_epi32(as1, INIT1);
345-
bs1 = _mm_add_epi32(bs1, INIT1);
340+
as0 = _mm_add_epi32(as0, _mm_load_si128((const __m128i*)INIT0));
341+
bs0 = _mm_add_epi32(bs0, _mm_load_si128((const __m128i*)INIT0));
342+
as1 = _mm_add_epi32(as1, _mm_load_si128((const __m128i*)INIT1));
343+
bs1 = _mm_add_epi32(bs1, _mm_load_si128((const __m128i*)INIT1));
346344

347345
/* Extract hash into out */
348346
Unshuffle(as0, as1);

0 commit comments

Comments
 (0)