1static inline __m256i
2enc_reshuffle (const __m256i input)
3{
4 // Translation of the SSSE3 reshuffling algorithm to AVX2. This one
5 // works with shifted (4 bytes) input in order to be able to work
6 // efficiently in the two 128-bit lanes.
7
8 // Input, bytes MSB to LSB:
9 // 0 0 0 0 x w v u t s r q p o n m
10 // l k j i h g f e d c b a 0 0 0 0
11
12 const __m256i in = _mm256_shuffle_epi8(input, _mm256_set_epi8(
13 10, 11, 9, 10,
14 7, 8, 6, 7,
15 4, 5, 3, 4,
16 1, 2, 0, 1,
17
18 14, 15, 13, 14,
19 11, 12, 10, 11,
20 8, 9, 7, 8,
21 5, 6, 4, 5));
22 // in, bytes MSB to LSB:
23 // w x v w
24 // t u s t
25 // q r p q
26 // n o m n
27 // k l j k
28 // h i g h
29 // e f d e
30 // b c a b
31
32 const __m256i t0 = _mm256_and_si256(in, _mm256_set1_epi32(0x0FC0FC00));
33 // bits, upper case are most significant bits, lower case are least
34 // significant bits.
35 // 0000wwww XX000000 VVVVVV00 00000000
36 // 0000tttt UU000000 SSSSSS00 00000000
37 // 0000qqqq RR000000 PPPPPP00 00000000
38 // 0000nnnn OO000000 MMMMMM00 00000000
39 // 0000kkkk LL000000 JJJJJJ00 00000000
40 // 0000hhhh II000000 GGGGGG00 00000000
41 // 0000eeee FF000000 DDDDDD00 00000000
42 // 0000bbbb CC000000 AAAAAA00 00000000
43
44 const __m256i t1 = _mm256_mulhi_epu16(t0, _mm256_set1_epi32(0x04000040));
45 // 00000000 00wwwwXX 00000000 00VVVVVV
46 // 00000000 00ttttUU 00000000 00SSSSSS
47 // 00000000 00qqqqRR 00000000 00PPPPPP
48 // 00000000 00nnnnOO 00000000 00MMMMMM
49 // 00000000 00kkkkLL 00000000 00JJJJJJ
50 // 00000000 00hhhhII 00000000 00GGGGGG
51 // 00000000 00eeeeFF 00000000 00DDDDDD
52 // 00000000 00bbbbCC 00000000 00AAAAAA
53
54 const __m256i t2 = _mm256_and_si256(in, _mm256_set1_epi32(0x003F03F0));
55 // 00000000 00xxxxxx 000000vv WWWW0000
56 // 00000000 00uuuuuu 000000ss TTTT0000
57 // 00000000 00rrrrrr 000000pp QQQQ0000
58 // 00000000 00oooooo 000000mm NNNN0000
59 // 00000000 00llllll 000000jj KKKK0000
60 // 00000000 00iiiiii 000000gg HHHH0000
61 // 00000000 00ffffff 000000dd EEEE0000
62 // 00000000 00cccccc 000000aa BBBB0000
63
64 const __m256i t3 = _mm256_mullo_epi16(t2, _mm256_set1_epi32(0x01000010));
65 // 00xxxxxx 00000000 00vvWWWW 00000000
66 // 00uuuuuu 00000000 00ssTTTT 00000000
67 // 00rrrrrr 00000000 00ppQQQQ 00000000
68 // 00oooooo 00000000 00mmNNNN 00000000
69 // 00llllll 00000000 00jjKKKK 00000000
70 // 00iiiiii 00000000 00ggHHHH 00000000
71 // 00ffffff 00000000 00ddEEEE 00000000
72 // 00cccccc 00000000 00aaBBBB 00000000
73
74 return _mm256_or_si256(t1, t3);
75 // 00xxxxxx 00wwwwXX 00vvWWWW 00VVVVVV
76 // 00uuuuuu 00ttttUU 00ssTTTT 00SSSSSS
77 // 00rrrrrr 00qqqqRR 00ppQQQQ 00PPPPPP
78 // 00oooooo 00nnnnOO 00mmNNNN 00MMMMMM
79 // 00llllll 00kkkkLL 00jjKKKK 00JJJJJJ
80 // 00iiiiii 00hhhhII 00ggHHHH 00GGGGGG
81 // 00ffffff 00eeeeFF 00ddEEEE 00DDDDDD
82 // 00cccccc 00bbbbCC 00aaBBBB 00AAAAAA
83}
84