Debian Codegraph @

crypto_encode/953x3/avx/encode.c

raw
1 // 20250302 djb: port to -Wc++-compat
2 #include <immintrin.h>
3 #include "crypto_encode.h"
4 #include "crypto_uint8.h"
5 #define uint8 crypto_uint8
6
7 #define p 953
8 #define loops 8
9 #define overshoot 18
10
11 static const __attribute((aligned(32))) uint8 lobytes_buf[32] = {
12 255,0,255,0,255,0,255,0,
13 255,0,255,0,255,0,255,0,
14 255,0,255,0,255,0,255,0,
15 255,0,255,0,255,0,255,0,
16 };
17 #define lobytes (*(__m256i *) lobytes_buf)
18
19 void crypto_encode(unsigned char *s,const void *v)
20 {
21 const uint8 *f = (const uint8 *) v;
22 int loop;
23 const uint8 *nextf = f+128-4*overshoot;
24 unsigned char *nexts = s+32-overshoot;
25
26 for (loop = loops;loop > 0;--loop) {
27 __m256i f0 = _mm256_loadu_si256((const __m256i *) (f+0));
28 __m256i f1 = _mm256_loadu_si256((const __m256i *) (f+32));
29 __m256i f2 = _mm256_loadu_si256((const __m256i *) (f+64));
30 __m256i f3 = _mm256_loadu_si256((const __m256i *) (f+96));
31 f = nextf;
32 nextf += 128;
33
34 __m256i a0 = _mm256_packus_epi16(f0&lobytes,f1&lobytes);
35 /* 0 2 4 6 8 10 12 14 32 34 36 38 40 42 44 46 */
36 /* 16 18 20 22 24 26 28 30 48 50 52 54 56 58 60 62 */
37 __m256i a1 = _mm256_packus_epi16(_mm256_srli_epi16(f0,8),_mm256_srli_epi16(f1,8));
38 /* 1 3 ... */
39 __m256i a2 = _mm256_packus_epi16(f2&lobytes,f3&lobytes);
40 __m256i a3 = _mm256_packus_epi16(_mm256_srli_epi16(f2,8),_mm256_srli_epi16(f3,8));
41
42 a0 = _mm256_add_epi8(a0,_mm256_slli_epi16(a1&_mm256_set1_epi8(63),2));
43 a2 = _mm256_add_epi8(a2,_mm256_slli_epi16(a3&_mm256_set1_epi8(63),2));
44
45 __m256i b0 = _mm256_packus_epi16(a0&lobytes,a2&lobytes);
46 /* 0 4 8 12 32 36 40 44 64 68 72 76 96 100 104 108 */
47 /* 16 20 24 28 48 52 56 60 80 84 88 92 112 116 120 124 */
48 __m256i b2 = _mm256_packus_epi16(_mm256_srli_epi16(a0,8),_mm256_srli_epi16(a2,8));
49 /* 2 6 ... */
50
51 b0 = _mm256_add_epi8(b0,_mm256_slli_epi16(b2&_mm256_set1_epi8(15),4));
52
53 b0 = _mm256_permutevar8x32_epi32(b0,_mm256_set_epi32(7,3,6,2,5,1,4,0));
54
55 b0 = _mm256_add_epi8(b0,_mm256_set1_epi8(85));
56
57 _mm256_storeu_si256((__m256i *) s,b0);
58 s = nexts;
59 nexts += 32;
60 }
61
62 *s++ = *f++ + 1;
63 }