LCOV - code coverage report
Current view: top level - ballet/blake3 - blake3_sse2.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 477 0.0 %
Date: 2024-11-13 11:58:15 Functions: 0 25 0.0 %

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

Generated by: LCOV version 1.14