LCOV - code coverage report
Current view: top level - ballet/blake3 - blake3_sse41.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 211 472 44.7 %
Date: 2024-11-13 11:58:15 Functions: 15 24 62.5 %

          Line data    Source code
       1             : 
       2             : // Source originally from https://github.com/BLAKE3-team/BLAKE3
       3             : // From commit: c0ea395cf91d242f078c23d5f8d87eb9dd5f7b78
       4             : 
       5             : #include "blake3_impl.h"
       6             : 
       7             : #include <immintrin.h>
       8             : 
       9           0 : #define DEGREE 4
      10             : 
      11             : #define _mm_shuffle_ps2(a, b, c)                                               \
      12         160 :   (_mm_castps_si128(                                                           \
      13         160 :       _mm_shuffle_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b), (c))))
      14             : 
      15          80 : INLINE __m128i loadu(const uint8_t src[16]) {
      16          80 :   return _mm_loadu_si128((const __m128i *)src);
      17          80 : }
      18             : 
      19          40 : INLINE void storeu(__m128i src, uint8_t dest[16]) {
      20          40 :   _mm_storeu_si128((__m128i *)dest, src);
      21          40 : }
      22             : 
      23         840 : INLINE __m128i addv(__m128i a, __m128i b) { return _mm_add_epi32(a, b); }
      24             : 
      25             : // Note that clang-format doesn't like the name "xor" for some reason.
      26         880 : INLINE __m128i xorv(__m128i a, __m128i b) { return _mm_xor_si128(a, b); }
      27             : 
      28           0 : INLINE __m128i set1(uint32_t x) { return _mm_set1_epi32((int32_t)x); }
      29             : 
      30          20 : INLINE __m128i set4(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {
      31          20 :   return _mm_setr_epi32((int32_t)a, (int32_t)b, (int32_t)c, (int32_t)d);
      32          20 : }
      33             : 
      34         140 : INLINE __m128i rot16(__m128i x) {
      35         140 :   return _mm_shuffle_epi8(
      36         140 :       x, _mm_set_epi8(13, 12, 15, 14, 9, 8, 11, 10, 5, 4, 7, 6, 1, 0, 3, 2));
      37         140 : }
      38             : 
      39         140 : INLINE __m128i rot12(__m128i x) {
      40         140 :   return xorv(_mm_srli_epi32(x, 12), _mm_slli_epi32(x, 32 - 12));
      41         140 : }
      42             : 
      43         140 : INLINE __m128i rot8(__m128i x) {
      44         140 :   return _mm_shuffle_epi8(
      45         140 :       x, _mm_set_epi8(12, 15, 14, 13, 8, 11, 10, 9, 4, 7, 6, 5, 0, 3, 2, 1));
      46         140 : }
      47             : 
      48         140 : INLINE __m128i rot7(__m128i x) {
      49         140 :   return xorv(_mm_srli_epi32(x, 7), _mm_slli_epi32(x, 32 - 7));
      50         140 : }
      51             : 
      52             : INLINE void g1(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,
      53         140 :                __m128i m) {
      54         140 :   *row0 = addv(addv(*row0, m), *row1);
      55         140 :   *row3 = xorv(*row3, *row0);
      56         140 :   *row3 = rot16(*row3);
      57         140 :   *row2 = addv(*row2, *row3);
      58         140 :   *row1 = xorv(*row1, *row2);
      59         140 :   *row1 = rot12(*row1);
      60         140 : }
      61             : 
      62             : INLINE void g2(__m128i *row0, __m128i *row1, __m128i *row2, __m128i *row3,
      63         140 :                __m128i m) {
      64         140 :   *row0 = addv(addv(*row0, m), *row1);
      65         140 :   *row3 = xorv(*row3, *row0);
      66         140 :   *row3 = rot8(*row3);
      67         140 :   *row2 = addv(*row2, *row3);
      68         140 :   *row1 = xorv(*row1, *row2);
      69         140 :   *row1 = rot7(*row1);
      70         140 : }
      71             : 
      72             : // Note the optimization here of leaving row1 as the unrotated row, rather than
      73             : // row0. All the message loads below are adjusted to compensate for this. See
      74             : // discussion at https://github.com/sneves/blake2-avx2/pull/4
      75          70 : INLINE void diagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {
      76          70 :   *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(2, 1, 0, 3));
      77          70 :   *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));
      78          70 :   *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(0, 3, 2, 1));
      79          70 : }
      80             : 
      81          70 : INLINE void undiagonalize(__m128i *row0, __m128i *row2, __m128i *row3) {
      82          70 :   *row0 = _mm_shuffle_epi32(*row0, _MM_SHUFFLE(0, 3, 2, 1));
      83          70 :   *row3 = _mm_shuffle_epi32(*row3, _MM_SHUFFLE(1, 0, 3, 2));
      84          70 :   *row2 = _mm_shuffle_epi32(*row2, _MM_SHUFFLE(2, 1, 0, 3));
      85          70 : }
      86             : 
      87             : INLINE void compress_pre(__m128i rows[4], const uint32_t cv[8],
      88             :                          const uint8_t block[BLAKE3_BLOCK_LEN],
      89          10 :                          uint8_t block_len, uint64_t counter, uint8_t flags) {
      90          10 :   rows[0] = loadu((uint8_t *)&cv[0]);
      91          10 :   rows[1] = loadu((uint8_t *)&cv[4]);
      92          10 :   rows[2] = set4(IV[0], IV[1], IV[2], IV[3]);
      93          10 :   rows[3] = set4(counter_low(counter), counter_high(counter),
      94          10 :                  (uint32_t)block_len, (uint32_t)flags);
      95             : 
      96          10 :   __m128i m0 = loadu(&block[sizeof(__m128i) * 0]);
      97          10 :   __m128i m1 = loadu(&block[sizeof(__m128i) * 1]);
      98          10 :   __m128i m2 = loadu(&block[sizeof(__m128i) * 2]);
      99          10 :   __m128i m3 = loadu(&block[sizeof(__m128i) * 3]);
     100             : 
     101          10 :   __m128i t0, t1, t2, t3, tt;
     102             : 
     103             :   // Round 1. The first round permutes the message words from the original
     104             :   // input order, into the groups that get mixed in parallel.
     105          10 :   t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(2, 0, 2, 0)); //  6  4  2  0
     106          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
     107          10 :   t1 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 3, 1)); //  7  5  3  1
     108          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
     109          10 :   diagonalize(&rows[0], &rows[2], &rows[3]);
     110          10 :   t2 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(2, 0, 2, 0)); // 14 12 10  8
     111          10 :   t2 = _mm_shuffle_epi32(t2, _MM_SHUFFLE(2, 1, 0, 3));   // 12 10  8 14
     112          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
     113          10 :   t3 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 1, 3, 1)); // 15 13 11  9
     114          10 :   t3 = _mm_shuffle_epi32(t3, _MM_SHUFFLE(2, 1, 0, 3));   // 13 11  9 15
     115          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
     116          10 :   undiagonalize(&rows[0], &rows[2], &rows[3]);
     117          10 :   m0 = t0;
     118          10 :   m1 = t1;
     119          10 :   m2 = t2;
     120          10 :   m3 = t3;
     121             : 
     122             :   // Round 2. This round and all following rounds apply a fixed permutation
     123             :   // to the message words from the round before.
     124          10 :   t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
     125          10 :   t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
     126          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
     127          10 :   t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
     128          10 :   tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
     129          10 :   t1 = _mm_blend_epi16(tt, t1, 0xCC);
     130          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
     131          10 :   diagonalize(&rows[0], &rows[2], &rows[3]);
     132          10 :   t2 = _mm_unpacklo_epi64(m3, m1);
     133          10 :   tt = _mm_blend_epi16(t2, m2, 0xC0);
     134          10 :   t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
     135          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
     136          10 :   t3 = _mm_unpackhi_epi32(m1, m3);
     137          10 :   tt = _mm_unpacklo_epi32(m2, t3);
     138          10 :   t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
     139          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
     140          10 :   undiagonalize(&rows[0], &rows[2], &rows[3]);
     141          10 :   m0 = t0;
     142          10 :   m1 = t1;
     143          10 :   m2 = t2;
     144          10 :   m3 = t3;
     145             : 
     146             :   // Round 3
     147          10 :   t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
     148          10 :   t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
     149          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
     150          10 :   t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
     151          10 :   tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
     152          10 :   t1 = _mm_blend_epi16(tt, t1, 0xCC);
     153          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
     154          10 :   diagonalize(&rows[0], &rows[2], &rows[3]);
     155          10 :   t2 = _mm_unpacklo_epi64(m3, m1);
     156          10 :   tt = _mm_blend_epi16(t2, m2, 0xC0);
     157          10 :   t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
     158          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
     159          10 :   t3 = _mm_unpackhi_epi32(m1, m3);
     160          10 :   tt = _mm_unpacklo_epi32(m2, t3);
     161          10 :   t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
     162          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
     163          10 :   undiagonalize(&rows[0], &rows[2], &rows[3]);
     164          10 :   m0 = t0;
     165          10 :   m1 = t1;
     166          10 :   m2 = t2;
     167          10 :   m3 = t3;
     168             : 
     169             :   // Round 4
     170          10 :   t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
     171          10 :   t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
     172          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
     173          10 :   t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
     174          10 :   tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
     175          10 :   t1 = _mm_blend_epi16(tt, t1, 0xCC);
     176          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
     177          10 :   diagonalize(&rows[0], &rows[2], &rows[3]);
     178          10 :   t2 = _mm_unpacklo_epi64(m3, m1);
     179          10 :   tt = _mm_blend_epi16(t2, m2, 0xC0);
     180          10 :   t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
     181          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
     182          10 :   t3 = _mm_unpackhi_epi32(m1, m3);
     183          10 :   tt = _mm_unpacklo_epi32(m2, t3);
     184          10 :   t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
     185          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
     186          10 :   undiagonalize(&rows[0], &rows[2], &rows[3]);
     187          10 :   m0 = t0;
     188          10 :   m1 = t1;
     189          10 :   m2 = t2;
     190          10 :   m3 = t3;
     191             : 
     192             :   // Round 5
     193          10 :   t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
     194          10 :   t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
     195          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
     196          10 :   t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
     197          10 :   tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
     198          10 :   t1 = _mm_blend_epi16(tt, t1, 0xCC);
     199          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
     200          10 :   diagonalize(&rows[0], &rows[2], &rows[3]);
     201          10 :   t2 = _mm_unpacklo_epi64(m3, m1);
     202          10 :   tt = _mm_blend_epi16(t2, m2, 0xC0);
     203          10 :   t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
     204          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
     205          10 :   t3 = _mm_unpackhi_epi32(m1, m3);
     206          10 :   tt = _mm_unpacklo_epi32(m2, t3);
     207          10 :   t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
     208          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
     209          10 :   undiagonalize(&rows[0], &rows[2], &rows[3]);
     210          10 :   m0 = t0;
     211          10 :   m1 = t1;
     212          10 :   m2 = t2;
     213          10 :   m3 = t3;
     214             : 
     215             :   // Round 6
     216          10 :   t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
     217          10 :   t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
     218          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
     219          10 :   t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
     220          10 :   tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
     221          10 :   t1 = _mm_blend_epi16(tt, t1, 0xCC);
     222          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
     223          10 :   diagonalize(&rows[0], &rows[2], &rows[3]);
     224          10 :   t2 = _mm_unpacklo_epi64(m3, m1);
     225          10 :   tt = _mm_blend_epi16(t2, m2, 0xC0);
     226          10 :   t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
     227          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
     228          10 :   t3 = _mm_unpackhi_epi32(m1, m3);
     229          10 :   tt = _mm_unpacklo_epi32(m2, t3);
     230          10 :   t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
     231          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
     232          10 :   undiagonalize(&rows[0], &rows[2], &rows[3]);
     233          10 :   m0 = t0;
     234          10 :   m1 = t1;
     235          10 :   m2 = t2;
     236          10 :   m3 = t3;
     237             : 
     238             :   // Round 7
     239          10 :   t0 = _mm_shuffle_ps2(m0, m1, _MM_SHUFFLE(3, 1, 1, 2));
     240          10 :   t0 = _mm_shuffle_epi32(t0, _MM_SHUFFLE(0, 3, 2, 1));
     241          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t0);
     242          10 :   t1 = _mm_shuffle_ps2(m2, m3, _MM_SHUFFLE(3, 3, 2, 2));
     243          10 :   tt = _mm_shuffle_epi32(m0, _MM_SHUFFLE(0, 0, 3, 3));
     244          10 :   t1 = _mm_blend_epi16(tt, t1, 0xCC);
     245          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t1);
     246          10 :   diagonalize(&rows[0], &rows[2], &rows[3]);
     247          10 :   t2 = _mm_unpacklo_epi64(m3, m1);
     248          10 :   tt = _mm_blend_epi16(t2, m2, 0xC0);
     249          10 :   t2 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(1, 3, 2, 0));
     250          10 :   g1(&rows[0], &rows[1], &rows[2], &rows[3], t2);
     251          10 :   t3 = _mm_unpackhi_epi32(m1, m3);
     252          10 :   tt = _mm_unpacklo_epi32(m2, t3);
     253          10 :   t3 = _mm_shuffle_epi32(tt, _MM_SHUFFLE(0, 1, 3, 2));
     254          10 :   g2(&rows[0], &rows[1], &rows[2], &rows[3], t3);
     255          10 :   undiagonalize(&rows[0], &rows[2], &rows[3]);
     256          10 : }
     257             : 
     258             : void blake3_compress_in_place_sse41(uint32_t cv[8],
     259             :                                     const uint8_t block[BLAKE3_BLOCK_LEN],
     260             :                                     uint8_t block_len, uint64_t counter,
     261           0 :                                     uint8_t flags) {
     262           0 :   __m128i rows[4];
     263           0 :   compress_pre(rows, cv, block, block_len, counter, flags);
     264           0 :   storeu(xorv(rows[0], rows[2]), (uint8_t *)&cv[0]);
     265           0 :   storeu(xorv(rows[1], rows[3]), (uint8_t *)&cv[4]);
     266           0 : }
     267             : 
     268             : void blake3_compress_xof_sse41(const uint32_t cv[8],
     269             :                                const uint8_t block[BLAKE3_BLOCK_LEN],
     270             :                                uint8_t block_len, uint64_t counter,
     271          10 :                                uint8_t flags, uint8_t out[64]) {
     272          10 :   __m128i rows[4];
     273          10 :   compress_pre(rows, cv, block, block_len, counter, flags);
     274          10 :   storeu(xorv(rows[0], rows[2]), &out[0]);
     275          10 :   storeu(xorv(rows[1], rows[3]), &out[16]);
     276          10 :   storeu(xorv(rows[2], loadu((uint8_t *)&cv[0])), &out[32]);
     277          10 :   storeu(xorv(rows[3], loadu((uint8_t *)&cv[4])), &out[48]);
     278          10 : }
     279             : 
     280           0 : INLINE void round_fn(__m128i v[16], __m128i m[16], size_t r) {
     281           0 :   v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][0]]);
     282           0 :   v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][2]]);
     283           0 :   v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][4]]);
     284           0 :   v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][6]]);
     285           0 :   v[0] = addv(v[0], v[4]);
     286           0 :   v[1] = addv(v[1], v[5]);
     287           0 :   v[2] = addv(v[2], v[6]);
     288           0 :   v[3] = addv(v[3], v[7]);
     289           0 :   v[12] = xorv(v[12], v[0]);
     290           0 :   v[13] = xorv(v[13], v[1]);
     291           0 :   v[14] = xorv(v[14], v[2]);
     292           0 :   v[15] = xorv(v[15], v[3]);
     293           0 :   v[12] = rot16(v[12]);
     294           0 :   v[13] = rot16(v[13]);
     295           0 :   v[14] = rot16(v[14]);
     296           0 :   v[15] = rot16(v[15]);
     297           0 :   v[8] = addv(v[8], v[12]);
     298           0 :   v[9] = addv(v[9], v[13]);
     299           0 :   v[10] = addv(v[10], v[14]);
     300           0 :   v[11] = addv(v[11], v[15]);
     301           0 :   v[4] = xorv(v[4], v[8]);
     302           0 :   v[5] = xorv(v[5], v[9]);
     303           0 :   v[6] = xorv(v[6], v[10]);
     304           0 :   v[7] = xorv(v[7], v[11]);
     305           0 :   v[4] = rot12(v[4]);
     306           0 :   v[5] = rot12(v[5]);
     307           0 :   v[6] = rot12(v[6]);
     308           0 :   v[7] = rot12(v[7]);
     309           0 :   v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][1]]);
     310           0 :   v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][3]]);
     311           0 :   v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][5]]);
     312           0 :   v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][7]]);
     313           0 :   v[0] = addv(v[0], v[4]);
     314           0 :   v[1] = addv(v[1], v[5]);
     315           0 :   v[2] = addv(v[2], v[6]);
     316           0 :   v[3] = addv(v[3], v[7]);
     317           0 :   v[12] = xorv(v[12], v[0]);
     318           0 :   v[13] = xorv(v[13], v[1]);
     319           0 :   v[14] = xorv(v[14], v[2]);
     320           0 :   v[15] = xorv(v[15], v[3]);
     321           0 :   v[12] = rot8(v[12]);
     322           0 :   v[13] = rot8(v[13]);
     323           0 :   v[14] = rot8(v[14]);
     324           0 :   v[15] = rot8(v[15]);
     325           0 :   v[8] = addv(v[8], v[12]);
     326           0 :   v[9] = addv(v[9], v[13]);
     327           0 :   v[10] = addv(v[10], v[14]);
     328           0 :   v[11] = addv(v[11], v[15]);
     329           0 :   v[4] = xorv(v[4], v[8]);
     330           0 :   v[5] = xorv(v[5], v[9]);
     331           0 :   v[6] = xorv(v[6], v[10]);
     332           0 :   v[7] = xorv(v[7], v[11]);
     333           0 :   v[4] = rot7(v[4]);
     334           0 :   v[5] = rot7(v[5]);
     335           0 :   v[6] = rot7(v[6]);
     336           0 :   v[7] = rot7(v[7]);
     337             : 
     338           0 :   v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][8]]);
     339           0 :   v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][10]]);
     340           0 :   v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][12]]);
     341           0 :   v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][14]]);
     342           0 :   v[0] = addv(v[0], v[5]);
     343           0 :   v[1] = addv(v[1], v[6]);
     344           0 :   v[2] = addv(v[2], v[7]);
     345           0 :   v[3] = addv(v[3], v[4]);
     346           0 :   v[15] = xorv(v[15], v[0]);
     347           0 :   v[12] = xorv(v[12], v[1]);
     348           0 :   v[13] = xorv(v[13], v[2]);
     349           0 :   v[14] = xorv(v[14], v[3]);
     350           0 :   v[15] = rot16(v[15]);
     351           0 :   v[12] = rot16(v[12]);
     352           0 :   v[13] = rot16(v[13]);
     353           0 :   v[14] = rot16(v[14]);
     354           0 :   v[10] = addv(v[10], v[15]);
     355           0 :   v[11] = addv(v[11], v[12]);
     356           0 :   v[8] = addv(v[8], v[13]);
     357           0 :   v[9] = addv(v[9], v[14]);
     358           0 :   v[5] = xorv(v[5], v[10]);
     359           0 :   v[6] = xorv(v[6], v[11]);
     360           0 :   v[7] = xorv(v[7], v[8]);
     361           0 :   v[4] = xorv(v[4], v[9]);
     362           0 :   v[5] = rot12(v[5]);
     363           0 :   v[6] = rot12(v[6]);
     364           0 :   v[7] = rot12(v[7]);
     365           0 :   v[4] = rot12(v[4]);
     366           0 :   v[0] = addv(v[0], m[(size_t)MSG_SCHEDULE[r][9]]);
     367           0 :   v[1] = addv(v[1], m[(size_t)MSG_SCHEDULE[r][11]]);
     368           0 :   v[2] = addv(v[2], m[(size_t)MSG_SCHEDULE[r][13]]);
     369           0 :   v[3] = addv(v[3], m[(size_t)MSG_SCHEDULE[r][15]]);
     370           0 :   v[0] = addv(v[0], v[5]);
     371           0 :   v[1] = addv(v[1], v[6]);
     372           0 :   v[2] = addv(v[2], v[7]);
     373           0 :   v[3] = addv(v[3], v[4]);
     374           0 :   v[15] = xorv(v[15], v[0]);
     375           0 :   v[12] = xorv(v[12], v[1]);
     376           0 :   v[13] = xorv(v[13], v[2]);
     377           0 :   v[14] = xorv(v[14], v[3]);
     378           0 :   v[15] = rot8(v[15]);
     379           0 :   v[12] = rot8(v[12]);
     380           0 :   v[13] = rot8(v[13]);
     381           0 :   v[14] = rot8(v[14]);
     382           0 :   v[10] = addv(v[10], v[15]);
     383           0 :   v[11] = addv(v[11], v[12]);
     384           0 :   v[8] = addv(v[8], v[13]);
     385           0 :   v[9] = addv(v[9], v[14]);
     386           0 :   v[5] = xorv(v[5], v[10]);
     387           0 :   v[6] = xorv(v[6], v[11]);
     388           0 :   v[7] = xorv(v[7], v[8]);
     389           0 :   v[4] = xorv(v[4], v[9]);
     390           0 :   v[5] = rot7(v[5]);
     391           0 :   v[6] = rot7(v[6]);
     392           0 :   v[7] = rot7(v[7]);
     393           0 :   v[4] = rot7(v[4]);
     394           0 : }
     395             : 
     396           0 : INLINE void transpose_vecs(__m128i vecs[DEGREE]) {
     397             :   // Interleave 32-bit lanes. The low unpack is lanes 00/11 and the high is
     398             :   // 22/33. Note that this doesn't split the vector into two lanes, as the
     399             :   // AVX2 counterparts do.
     400           0 :   __m128i ab_01 = _mm_unpacklo_epi32(vecs[0], vecs[1]);
     401           0 :   __m128i ab_23 = _mm_unpackhi_epi32(vecs[0], vecs[1]);
     402           0 :   __m128i cd_01 = _mm_unpacklo_epi32(vecs[2], vecs[3]);
     403           0 :   __m128i cd_23 = _mm_unpackhi_epi32(vecs[2], vecs[3]);
     404             : 
     405             :   // Interleave 64-bit lanes.
     406           0 :   __m128i abcd_0 = _mm_unpacklo_epi64(ab_01, cd_01);
     407           0 :   __m128i abcd_1 = _mm_unpackhi_epi64(ab_01, cd_01);
     408           0 :   __m128i abcd_2 = _mm_unpacklo_epi64(ab_23, cd_23);
     409           0 :   __m128i abcd_3 = _mm_unpackhi_epi64(ab_23, cd_23);
     410             : 
     411           0 :   vecs[0] = abcd_0;
     412           0 :   vecs[1] = abcd_1;
     413           0 :   vecs[2] = abcd_2;
     414           0 :   vecs[3] = abcd_3;
     415           0 : }
     416             : 
     417             : INLINE void transpose_msg_vecs(const uint8_t *const *inputs,
     418           0 :                                size_t block_offset, __m128i out[16]) {
     419           0 :   out[0] = loadu(&inputs[0][block_offset + 0 * sizeof(__m128i)]);
     420           0 :   out[1] = loadu(&inputs[1][block_offset + 0 * sizeof(__m128i)]);
     421           0 :   out[2] = loadu(&inputs[2][block_offset + 0 * sizeof(__m128i)]);
     422           0 :   out[3] = loadu(&inputs[3][block_offset + 0 * sizeof(__m128i)]);
     423           0 :   out[4] = loadu(&inputs[0][block_offset + 1 * sizeof(__m128i)]);
     424           0 :   out[5] = loadu(&inputs[1][block_offset + 1 * sizeof(__m128i)]);
     425           0 :   out[6] = loadu(&inputs[2][block_offset + 1 * sizeof(__m128i)]);
     426           0 :   out[7] = loadu(&inputs[3][block_offset + 1 * sizeof(__m128i)]);
     427           0 :   out[8] = loadu(&inputs[0][block_offset + 2 * sizeof(__m128i)]);
     428           0 :   out[9] = loadu(&inputs[1][block_offset + 2 * sizeof(__m128i)]);
     429           0 :   out[10] = loadu(&inputs[2][block_offset + 2 * sizeof(__m128i)]);
     430           0 :   out[11] = loadu(&inputs[3][block_offset + 2 * sizeof(__m128i)]);
     431           0 :   out[12] = loadu(&inputs[0][block_offset + 3 * sizeof(__m128i)]);
     432           0 :   out[13] = loadu(&inputs[1][block_offset + 3 * sizeof(__m128i)]);
     433           0 :   out[14] = loadu(&inputs[2][block_offset + 3 * sizeof(__m128i)]);
     434           0 :   out[15] = loadu(&inputs[3][block_offset + 3 * sizeof(__m128i)]);
     435           0 :   for (size_t i = 0; i < 4; ++i) {
     436           0 :     _mm_prefetch((const void *)&inputs[i][block_offset + 256], _MM_HINT_T0);
     437           0 :   }
     438           0 :   transpose_vecs(&out[0]);
     439           0 :   transpose_vecs(&out[4]);
     440           0 :   transpose_vecs(&out[8]);
     441           0 :   transpose_vecs(&out[12]);
     442           0 : }
     443             : 
     444             : INLINE void load_counters(uint64_t counter, bool increment_counter,
     445           0 :                           __m128i *out_lo, __m128i *out_hi) {
     446           0 :   const __m128i mask = _mm_set1_epi32(-(int32_t)increment_counter);
     447           0 :   const __m128i add0 = _mm_set_epi32(3, 2, 1, 0);
     448           0 :   const __m128i add1 = _mm_and_si128(mask, add0);
     449           0 :   __m128i l = _mm_add_epi32(_mm_set1_epi32((int32_t)counter), add1);
     450           0 :   __m128i carry = _mm_cmpgt_epi32(_mm_xor_si128(add1, _mm_set1_epi32((int)0x80000000)),
     451           0 :                                   _mm_xor_si128(   l, _mm_set1_epi32((int)0x80000000)));
     452           0 :   __m128i h = _mm_sub_epi32(_mm_set1_epi32((int32_t)(counter >> 32)), carry);
     453           0 :   *out_lo = l;
     454           0 :   *out_hi = h;
     455           0 : }
     456             : 
     457             : static
     458             : void blake3_hash4_sse41(const uint8_t *const *inputs, size_t blocks,
     459             :                         const uint32_t key[8], uint64_t counter,
     460             :                         bool increment_counter, uint8_t flags,
     461           0 :                         uint8_t flags_start, uint8_t flags_end, uint8_t *out) {
     462           0 :   __m128i h_vecs[8] = {
     463           0 :       set1(key[0]), set1(key[1]), set1(key[2]), set1(key[3]),
     464           0 :       set1(key[4]), set1(key[5]), set1(key[6]), set1(key[7]),
     465           0 :   };
     466           0 :   __m128i counter_low_vec, counter_high_vec;
     467           0 :   load_counters(counter, increment_counter, &counter_low_vec,
     468           0 :                 &counter_high_vec);
     469           0 :   uint8_t block_flags = flags | flags_start;
     470             : 
     471           0 :   for (size_t block = 0; block < blocks; block++) {
     472           0 :     if (block + 1 == blocks) {
     473           0 :       block_flags |= flags_end;
     474           0 :     }
     475           0 :     __m128i block_len_vec = set1(BLAKE3_BLOCK_LEN);
     476           0 :     __m128i block_flags_vec = set1(block_flags);
     477           0 :     __m128i msg_vecs[16];
     478           0 :     transpose_msg_vecs(inputs, block * BLAKE3_BLOCK_LEN, msg_vecs);
     479             : 
     480           0 :     __m128i v[16] = {
     481           0 :         h_vecs[0],       h_vecs[1],        h_vecs[2],     h_vecs[3],
     482           0 :         h_vecs[4],       h_vecs[5],        h_vecs[6],     h_vecs[7],
     483           0 :         set1(IV[0]),     set1(IV[1]),      set1(IV[2]),   set1(IV[3]),
     484           0 :         counter_low_vec, counter_high_vec, block_len_vec, block_flags_vec,
     485           0 :     };
     486           0 :     round_fn(v, msg_vecs, 0);
     487           0 :     round_fn(v, msg_vecs, 1);
     488           0 :     round_fn(v, msg_vecs, 2);
     489           0 :     round_fn(v, msg_vecs, 3);
     490           0 :     round_fn(v, msg_vecs, 4);
     491           0 :     round_fn(v, msg_vecs, 5);
     492           0 :     round_fn(v, msg_vecs, 6);
     493           0 :     h_vecs[0] = xorv(v[0], v[8]);
     494           0 :     h_vecs[1] = xorv(v[1], v[9]);
     495           0 :     h_vecs[2] = xorv(v[2], v[10]);
     496           0 :     h_vecs[3] = xorv(v[3], v[11]);
     497           0 :     h_vecs[4] = xorv(v[4], v[12]);
     498           0 :     h_vecs[5] = xorv(v[5], v[13]);
     499           0 :     h_vecs[6] = xorv(v[6], v[14]);
     500           0 :     h_vecs[7] = xorv(v[7], v[15]);
     501             : 
     502           0 :     block_flags = flags;
     503           0 :   }
     504             : 
     505           0 :   transpose_vecs(&h_vecs[0]);
     506           0 :   transpose_vecs(&h_vecs[4]);
     507             :   // The first four vecs now contain the first half of each output, and the
     508             :   // second four vecs contain the second half of each output.
     509           0 :   storeu(h_vecs[0], &out[0 * sizeof(__m128i)]);
     510           0 :   storeu(h_vecs[4], &out[1 * sizeof(__m128i)]);
     511           0 :   storeu(h_vecs[1], &out[2 * sizeof(__m128i)]);
     512           0 :   storeu(h_vecs[5], &out[3 * sizeof(__m128i)]);
     513           0 :   storeu(h_vecs[2], &out[4 * sizeof(__m128i)]);
     514           0 :   storeu(h_vecs[6], &out[5 * sizeof(__m128i)]);
     515           0 :   storeu(h_vecs[3], &out[6 * sizeof(__m128i)]);
     516           0 :   storeu(h_vecs[7], &out[7 * sizeof(__m128i)]);
     517           0 : }
     518             : 
     519             : INLINE void hash_one_sse41(const uint8_t *input, size_t blocks,
     520             :                            const uint32_t key[8], uint64_t counter,
     521             :                            uint8_t flags, uint8_t flags_start,
     522           0 :                            uint8_t flags_end, uint8_t out[BLAKE3_OUT_LEN]) {
     523           0 :   uint32_t cv[8];
     524           0 :   memcpy(cv, key, BLAKE3_KEY_LEN);
     525           0 :   uint8_t block_flags = flags | flags_start;
     526           0 :   while (blocks > 0) {
     527           0 :     if (blocks == 1) {
     528           0 :       block_flags |= flags_end;
     529           0 :     }
     530           0 :     blake3_compress_in_place_sse41(cv, input, BLAKE3_BLOCK_LEN, counter,
     531           0 :                                    block_flags);
     532           0 :     input = &input[BLAKE3_BLOCK_LEN];
     533           0 :     blocks -= 1;
     534           0 :     block_flags = flags;
     535           0 :   }
     536           0 :   memcpy(out, cv, BLAKE3_OUT_LEN);
     537           0 : }
     538             : 
     539             : void blake3_hash_many_sse41(const uint8_t *const *inputs, size_t num_inputs,
     540             :                             size_t blocks, const uint32_t key[8],
     541             :                             uint64_t counter, bool increment_counter,
     542             :                             uint8_t flags, uint8_t flags_start,
     543           0 :                             uint8_t flags_end, uint8_t *out) {
     544           0 :   while (num_inputs >= DEGREE) {
     545           0 :     blake3_hash4_sse41(inputs, blocks, key, counter, increment_counter, flags,
     546           0 :                        flags_start, flags_end, out);
     547           0 :     if (increment_counter) {
     548           0 :       counter += DEGREE;
     549           0 :     }
     550           0 :     inputs += DEGREE;
     551           0 :     num_inputs -= DEGREE;
     552           0 :     out = &out[DEGREE * BLAKE3_OUT_LEN];
     553           0 :   }
     554           0 :   while (num_inputs > 0) {
     555           0 :     hash_one_sse41(inputs[0], blocks, key, counter, flags, flags_start,
     556           0 :                    flags_end, out);
     557           0 :     if (increment_counter) {
     558           0 :       counter += 1;
     559           0 :     }
     560           0 :     inputs += 1;
     561           0 :     num_inputs -= 1;
     562           0 :     out = &out[BLAKE3_OUT_LEN];
     563           0 :   }
     564           0 : }

Generated by: LCOV version 1.14