Debian Codegraph @

debian/rust-vendor/chacha20/src/backends/avx512.rs

raw
1 #![allow(unsafe_op_in_unsafe_fn)]
2 use crate::{Rounds, Variant};
3 use core::marker::PhantomData;
4
5 #[cfg(feature = "cipher")]
6 use crate::{STATE_WORDS, chacha::Block};
7
8 #[cfg(feature = "cipher")]
9 use cipher::{
10 BlockSizeUser, ParBlocks, ParBlocksSizeUser, StreamCipherBackend, StreamCipherClosure,
11 consts::{U16, U64},
12 };
13
14 #[cfg(target_arch = "x86")]
15 use core::arch::x86::*;
16 #[cfg(target_arch = "x86_64")]
17 use core::arch::x86_64::*;
18
19 /// Maximum number of blocks processed in parallel.
20 /// We also support 8 and 4 in gen_tail_blocks.
21 const MAX_PAR_BLOCKS: usize = 16;
22
23 /// Divisor to compute `N`, the number of __m512i needed
24 /// to represent a number of parallel blocks.
25 const BLOCKS_PER_VECTOR: usize = 4;
26
27 const MAX_N: usize = MAX_PAR_BLOCKS / BLOCKS_PER_VECTOR;
28
29 #[inline]
30 #[target_feature(enable = "avx512f")]
31 #[cfg(feature = "cipher")]
32 pub(crate) unsafe fn inner<R, F, V>(state: &mut [u32; STATE_WORDS], f: F)
33 where
34 R: Rounds,
35 F: StreamCipherClosure<BlockSize = U64>,
36 V: Variant,
37 {
38 let simd_state = state.as_mut_ptr().cast::<i32>();
39
40 let mut backend = Backend::<R, V> {
41 state: [
42 _mm_loadu_epi32(simd_state),
43 _mm_loadu_epi32(simd_state.add(4)),
44 _mm_loadu_epi32(simd_state.add(8)),
45 ],
46 ctr: _mm_loadu_epi32(simd_state.add(12)),
47 _pd: PhantomData,
48 };
49
50 f.call(&mut backend);
51
52 // Update counter in the persistent state
53 state[12] = _mm_extract_epi32::<0>(backend.ctr) as u32;
54 if size_of::<V::Counter>() == 8 {
55 state[13] = _mm_extract_epi32::<1>(backend.ctr) as u32;
56 }
57 }
58
59 struct Backend<R: Rounds, V: Variant> {
60 state: [__m128i; 3],
61 ctr: __m128i,
62 _pd: PhantomData<(R, V)>,
63 }
64
65 #[cfg(feature = "cipher")]
66 impl<R: Rounds, V: Variant> Backend<R, V> {
67 #[inline]
68 #[target_feature(enable = "avx512f", enable = "avx512vl")]
69 unsafe fn increment_ctr(&mut self, amount: usize) {
70 match size_of::<V::Counter>() {
71 4 => {
72 self.ctr = _mm_add_epi32(self.ctr, _mm_set_epi32(0, 0, 0, amount as i32));
73 }
74 8 => {
75 self.ctr = _mm_add_epi64(self.ctr, _mm_set_epi64x(0, amount as i64));
76 }
77 _ => unreachable!(),
78 }
79 }
80
81 /// Generates blocks using the 512-bit-wide dispatch
82 /// with up to `N` vectors processed in parallel, producing
83 /// `N * BLOCKS_PER_VECTOR` blocks.
84 #[inline]
85 #[target_feature(enable = "avx512f", enable = "avx512vl")]
86 unsafe fn gen_blocks_fullwidth<const N: usize>(&mut self, blocks: &mut [Block]) {
87 let par_blocks = N * BLOCKS_PER_VECTOR;
88 assert!(blocks.len() <= par_blocks);
89
90 let mut ctrs = [_mm512_broadcast_i32x4(self.ctr); N];
91 for i in 0..ctrs.len() {
92 match size_of::<V::Counter>() {
93 4 => {
94 ctrs[i] = _mm512_add_epi32(
95 ctrs[i],
96 _mm512_set_epi32(
97 0,
98 0,
99 0,
100 (i * BLOCKS_PER_VECTOR + 3) as i32,
101 0,
102 0,
103 0,
104 (i * BLOCKS_PER_VECTOR + 2) as i32,
105 0,
106 0,
107 0,
108 (i * BLOCKS_PER_VECTOR + 1) as i32,
109 0,
110 0,
111 0,
112 (i * BLOCKS_PER_VECTOR) as i32,
113 ),
114 );
115 }
116 8 => {
117 ctrs[i] = _mm512_add_epi64(
118 ctrs[i],
119 _mm512_set_epi64(
120 0,
121 (i * BLOCKS_PER_VECTOR + 3) as i64,
122 0,
123 (i * BLOCKS_PER_VECTOR + 2) as i64,
124 0,
125 (i * BLOCKS_PER_VECTOR + 1) as i64,
126 0,
127 (i * BLOCKS_PER_VECTOR) as i64,
128 ),
129 );
130 }
131 _ => unreachable!(),
132 }
133 }
134
135 self.increment_ctr(blocks.len());
136
137 let result = rounds::<N, R>(&self.state.map(|v| _mm512_broadcast_i32x4(v)), &ctrs);
138
139 for i in 0..N {
140 let result_vectors = result[i];
141
142 // We have our data in SIMD vectors in the following layout
143 // (using a, b, c, and d to indicate the resp. 4 rows of each block,
144 // and Bn to denote the nth block):
145 // result_vectors[0]:
146 // B0a0 B0a1 B0a2 B0a3
147 // B1a0 B1a1 B1a2 B1a3
148 // ...
149 // B3a0 B3a1 B3a2 B3a2
150 //
151 // result_vectors[1]:
152 // B0b0 B0b1 B0b2 B0b3
153 // B1b0 B1b1 B1b2 B1b3
154 // ...
155 // B3b0 B3b1 B3b2 B3b2
156 //
157 // and so on for result_vectors[2] (storing c values) and result_vectors[3] (storing d values).
158 //
159 // To store to memory, we need to transpose to the following format:
160 // transposed[0]:
161 // B0a0 B0a1 B0a2 B0a3
162 // B0b0 B0b1 B0b2 B0b3
163 // B0c0 B0c1 B0c2 B0c3
164 // B0d0 B0d1 B0d2 B0d3
165 //
166 // and so on, such that each 512-bit SIMD vector
167 // contains a single contiguous block.
168 //
169 // We achieve this transposition using the following
170 // sequence of shuffles.
171
172 let temp_abab_block01 = _mm512_permutex2var_epi64(
173 result_vectors[0],
174 _mm512_setr_epi64(0, 1, 8, 9, 2, 3, 10, 11),
175 result_vectors[1],
176 );
177 let temp_abab_block23 = _mm512_permutex2var_epi64(
178 result_vectors[0],
179 _mm512_setr_epi64(4, 5, 12, 13, 6, 7, 14, 15),
180 result_vectors[1],
181 );
182
183 let temp_cdcd_block01 = _mm512_permutex2var_epi64(
184 result_vectors[2],
185 _mm512_setr_epi64(0, 1, 8, 9, 2, 3, 10, 11),
186 result_vectors[3],
187 );
188 let temp_cdcd_block23 = _mm512_permutex2var_epi64(
189 result_vectors[2],
190 _mm512_setr_epi64(4, 5, 12, 13, 6, 7, 14, 15),
191 result_vectors[3],
192 );
193
194 let block0 =
195 _mm512_shuffle_i32x4::<0b01_00_01_00>(temp_abab_block01, temp_cdcd_block01);
196 let block1 =
197 _mm512_shuffle_i32x4::<0b11_10_11_10>(temp_abab_block01, temp_cdcd_block01);
198 let block2 =
199 _mm512_shuffle_i32x4::<0b01_00_01_00>(temp_abab_block23, temp_cdcd_block23);
200 let block3 =
201 _mm512_shuffle_i32x4::<0b11_10_11_10>(temp_abab_block23, temp_cdcd_block23);
202
203 for (j, src_block) in [block0, block1, block2, block3].into_iter().enumerate() {
204 let dst_index = i * BLOCKS_PER_VECTOR + j;
205 if dst_index < blocks.len() {
206 _mm512_storeu_si512((&raw mut blocks[dst_index]).cast(), src_block);
207 }
208 }
209 }
210 }
211
212 /// Generates up to 2 blocks using 256-bit vectors.
213 #[inline]
214 #[target_feature(enable = "avx512f", enable = "avx512vl")]
215 unsafe fn gen_blocks_halfwidth(&mut self, blocks: &mut [Block]) {
216 assert!(blocks.len() <= 2);
217
218 let mut ctr = _mm256_broadcast_i32x4(self.ctr);
219
220 match size_of::<V::Counter>() {
221 4 => {
222 ctr = _mm256_add_epi32(ctr, _mm256_set_epi32(0, 0, 0, 1, 0, 0, 0, 0));
223 }
224 8 => {
225 ctr = _mm256_add_epi64(ctr, _mm256_set_epi64x(0, 1, 0, 0));
226 }
227 _ => unreachable!(),
228 }
229
230 self.increment_ctr(blocks.len());
231
232 let block_vectors = rounds_halfwide::<R>([
233 _mm256_broadcast_i32x4(self.state[0]),
234 _mm256_broadcast_i32x4(self.state[1]),
235 _mm256_broadcast_i32x4(self.state[2]),
236 ctr,
237 ]);
238
239 // Similar transpose operation as
240 // in gen_blocks_fullwidth.
241
242 let block0_ab = _mm256_permutex2var_epi64(
243 block_vectors[0],
244 _mm256_setr_epi64x(0, 1, 4, 5),
245 block_vectors[1],
246 );
247 let block0_cd = _mm256_permutex2var_epi64(
248 block_vectors[2],
249 _mm256_setr_epi64x(0, 1, 4, 5),
250 block_vectors[3],
251 );
252 let block1_ab = _mm256_permutex2var_epi64(
253 block_vectors[0],
254 _mm256_setr_epi64x(2, 3, 6, 7),
255 block_vectors[1],
256 );
257 let block1_cd = _mm256_permutex2var_epi64(
258 block_vectors[2],
259 _mm256_setr_epi64x(2, 3, 6, 7),
260 block_vectors[3],
261 );
262
263 for (i, (block_part_ab, block_part_cd)) in [(block0_ab, block0_cd), (block1_ab, block1_cd)]
264 .into_iter()
265 .enumerate()
266 {
267 if i < blocks.len() {
268 let dst = (&raw mut blocks[i]).cast::<i32>();
269 _mm256_storeu_epi32(dst, block_part_ab);
270 _mm256_storeu_epi32(
271 dst.add(size_of::<Block>() / 2 / size_of::<i32>()),
272 block_part_cd,
273 );
274 }
275 }
276 }
277 }
278
279 #[cfg(feature = "cipher")]
280 impl<R: Rounds, V: Variant> BlockSizeUser for Backend<R, V> {
281 type BlockSize = U64;
282 }
283
284 #[cfg(feature = "cipher")]
285 impl<R: Rounds, V: Variant> ParBlocksSizeUser for Backend<R, V> {
286 type ParBlocksSize = U16;
287 }
288
289 #[cfg(feature = "cipher")]
290 impl<R: Rounds, V: Variant> StreamCipherBackend for Backend<R, V> {
291 #[inline]
292 fn gen_par_ks_blocks(&mut self, blocks: &mut ParBlocks<Self>) {
293 unsafe { self.gen_blocks_fullwidth::<MAX_N>(blocks) }
294 }
295
296 #[inline(always)]
297 fn gen_ks_block(&mut self, block: &mut Block) {
298 // Fallback for generating a single block using quarter-width vectors
299 // (128).
300
301 unsafe {
302 let state = [self.state[0], self.state[1], self.state[2], self.ctr];
303
304 self.increment_ctr(1);
305
306 let result = rounds_quarterwide::<R>(state);
307
308 for row in 0..4 {
309 let dst = block.as_mut_ptr().cast::<i32>().add(row * 4);
310 _mm_storeu_epi32(dst, result[row]);
311 }
312 }
313 }
314
315 #[inline]
316 fn gen_tail_blocks(&mut self, blocks: &mut [cipher::Block<Self>]) {
317 assert!(blocks.len() < MAX_PAR_BLOCKS);
318
319 if blocks.is_empty() {
320 return;
321 }
322
323 // Fallback for generating a number of blocks less than
324 // MAX_PAR_BLOCKS.
325 unsafe {
326 if blocks.len() == 1 {
327 self.gen_ks_block(&mut blocks[0]);
328 } else if blocks.len() == 2 {
329 self.gen_blocks_halfwidth(blocks);
330 } else if blocks.len() <= 4 {
331 self.gen_blocks_fullwidth::<1>(blocks);
332 } else if blocks.len() <= 8 {
333 self.gen_blocks_fullwidth::<2>(blocks);
334 } else {
335 self.gen_blocks_fullwidth::<MAX_N>(blocks);
336 }
337 }
338 }
339 }
340
341 #[inline]
342 #[target_feature(enable = "avx512f")]
343 unsafe fn rounds<const N: usize, R: Rounds>(
344 v: &[__m512i; 3],
345 c: &[__m512i; N],
346 ) -> [[__m512i; 4]; N] {
347 let mut vs: [[__m512i; 4]; N] = [[_mm512_setzero_si512(); 4]; N];
348 for i in 0..N {
349 vs[i] = [v[0], v[1], v[2], c[i]];
350 }
351 for _ in 0..R::COUNT {
352 double_quarter_round(&mut vs);
353 }
354
355 for i in 0..N {
356 for j in 0..3 {
357 vs[i][j] = _mm512_add_epi32(vs[i][j], v[j]);
358 }
359 vs[i][3] = _mm512_add_epi32(vs[i][3], c[i]);
360 }
361
362 vs
363 }
364
365 #[inline]
366 #[target_feature(enable = "avx512f")]
367 unsafe fn double_quarter_round<const N: usize>(v: &mut [[__m512i; 4]; N]) {
368 add_xor_rot(v);
369 rows_to_cols(v);
370 add_xor_rot(v);
371 cols_to_rows(v);
372 }
373
374 /// The goal of this function is to transform the state words from:
375 /// ```text
376 /// [a0, a1, a2, a3] [ 0, 1, 2, 3]
377 /// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
378 /// [c0, c1, c2, c3] [ 8, 9, 10, 11]
379 /// [d0, d1, d2, d3] [12, 13, 14, 15]
380 /// ```
381 ///
382 /// to:
383 /// ```text
384 /// [a0, a1, a2, a3] [ 0, 1, 2, 3]
385 /// [b1, b2, b3, b0] == [ 5, 6, 7, 4]
386 /// [c2, c3, c0, c1] [10, 11, 8, 9]
387 /// [d3, d0, d1, d2] [15, 12, 13, 14]
388 /// ```
389 ///
390 /// so that we can apply [`add_xor_rot`] to the resulting columns, and have it compute the
391 /// "diagonal rounds" (as defined in RFC 7539) in parallel. In practice, this shuffle is
392 /// non-optimal: the last state word to be altered in `add_xor_rot` is `b`, so the shuffle
393 /// blocks on the result of `b` being calculated.
394 ///
395 /// We can optimize this by observing that the four quarter rounds in `add_xor_rot` are
396 /// data-independent: they only access a single column of the state, and thus the order of
397 /// the columns does not matter. We therefore instead shuffle the other three state words,
398 /// to obtain the following equivalent layout:
399 /// ```text
400 /// [a3, a0, a1, a2] [ 3, 0, 1, 2]
401 /// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
402 /// [c1, c2, c3, c0] [ 9, 10, 11, 8]
403 /// [d2, d3, d0, d1] [14, 15, 12, 13]
404 /// ```
405 ///
406 /// See https://github.com/sneves/blake2-avx2/pull/4 for additional details. The earliest
407 /// known occurrence of this optimization is in floodyberry's SSE4 ChaCha code from 2014:
408 /// - https://github.com/floodyberry/chacha-opt/blob/0ab65cb99f5016633b652edebaf3691ceb4ff753/chacha_blocks_ssse3-64.S#L639-L643
409 #[inline]
410 #[target_feature(enable = "avx512f")]
411 unsafe fn rows_to_cols<const N: usize>(vs: &mut [[__m512i; 4]; N]) {
412 // c >>>= 32; d >>>= 64; a >>>= 96;
413 for [a, _, c, d] in vs {
414 *c = _mm512_shuffle_epi32::<0b_00_11_10_01>(*c); // _MM_SHUFFLE(0, 3, 2, 1)
415 *d = _mm512_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
416 *a = _mm512_shuffle_epi32::<0b_10_01_00_11>(*a); // _MM_SHUFFLE(2, 1, 0, 3)
417 }
418 }
419
420 /// The goal of this function is to transform the state words from:
421 /// ```text
422 /// [a3, a0, a1, a2] [ 3, 0, 1, 2]
423 /// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
424 /// [c1, c2, c3, c0] [ 9, 10, 11, 8]
425 /// [d2, d3, d0, d1] [14, 15, 12, 13]
426 /// ```
427 ///
428 /// to:
429 /// ```text
430 /// [a0, a1, a2, a3] [ 0, 1, 2, 3]
431 /// [b0, b1, b2, b3] == [ 4, 5, 6, 7]
432 /// [c0, c1, c2, c3] [ 8, 9, 10, 11]
433 /// [d0, d1, d2, d3] [12, 13, 14, 15]
434 /// ```
435 ///
436 /// reversing the transformation of [`rows_to_cols`].
437 #[inline]
438 #[target_feature(enable = "avx512f")]
439 unsafe fn cols_to_rows<const N: usize>(vs: &mut [[__m512i; 4]; N]) {
440 // c <<<= 32; d <<<= 64; a <<<= 96;
441 for [a, _, c, d] in vs {
442 *c = _mm512_shuffle_epi32::<0b_10_01_00_11>(*c); // _MM_SHUFFLE(2, 1, 0, 3)
443 *d = _mm512_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
444 *a = _mm512_shuffle_epi32::<0b_00_11_10_01>(*a); // _MM_SHUFFLE(0, 3, 2, 1)
445 }
446 }
447
448 #[inline]
449 #[target_feature(enable = "avx512f")]
450 unsafe fn add_xor_rot<const N: usize>(vs: &mut [[__m512i; 4]; N]) {
451 // a += b; d ^= a; d <<<= (16, 16, 16, 16);
452 for [a, b, _, d] in vs.iter_mut() {
453 *a = _mm512_add_epi32(*a, *b);
454 *d = _mm512_xor_si512(*d, *a);
455 *d = _mm512_rol_epi32::<16>(*d);
456 }
457
458 // c += d; b ^= c; b <<<= (12, 12, 12, 12);
459 for [_, b, c, d] in vs.iter_mut() {
460 *c = _mm512_add_epi32(*c, *d);
461 *b = _mm512_xor_si512(*b, *c);
462 *b = _mm512_rol_epi32::<12>(*b);
463 }
464
465 // a += b; d ^= a; d <<<= (8, 8, 8, 8);
466 for [a, b, _, d] in vs.iter_mut() {
467 *a = _mm512_add_epi32(*a, *b);
468 *d = _mm512_xor_si512(*d, *a);
469 *d = _mm512_rol_epi32::<8>(*d);
470 }
471
472 // c += d; b ^= c; b <<<= (7, 7, 7, 7);
473 for [_, b, c, d] in vs.iter_mut() {
474 *c = _mm512_add_epi32(*c, *d);
475 *b = _mm512_xor_si512(*b, *c);
476 *b = _mm512_rol_epi32::<7>(*b);
477 }
478 }
479
480 // Below is another implementation of the round application
481 // that uses 256-bit vectors instead of 512-bit (but, unlike
482 // the avx2 module, can use new AVX-512 instructions like rotates).
483 // It is used for tail processing of shorter outputs,
484 // since 256-bit instructions can be faster and lower latency
485 // than 512-bit instructions on certain microarchitectures (e.g. Zen 4).
486
487 #[inline]
488 #[target_feature(enable = "avx512f", enable = "avx512vl")]
489 unsafe fn rounds_halfwide<R: Rounds>(v_in: [__m256i; 4]) -> [__m256i; 4] {
490 let mut v = v_in;
491
492 for _ in 0..R::COUNT {
493 double_quarter_round_halfwide(&mut v);
494 }
495
496 for (a, b) in v.iter_mut().zip(v_in) {
497 *a = _mm256_add_epi32(*a, b);
498 }
499
500 v
501 }
502
503 #[inline]
504 #[target_feature(enable = "avx512f", enable = "avx512vl")]
505 unsafe fn double_quarter_round_halfwide(v: &mut [__m256i; 4]) {
506 add_xor_rot_halfwide(v);
507 rows_to_cols_halfwide(v);
508 add_xor_rot_halfwide(v);
509 cols_to_rows_halfwide(v);
510 }
511
512 #[inline]
513 #[target_feature(enable = "avx512f", enable = "avx512vl")]
514 unsafe fn rows_to_cols_halfwide(v: &mut [__m256i; 4]) {
515 // c >>>= 32; d >>>= 64; a >>>= 96;
516 let [a, _, c, d] = v;
517 *c = _mm256_shuffle_epi32::<0b_00_11_10_01>(*c); // _MM_SHUFFLE(0, 3, 2, 1)
518 *d = _mm256_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
519 *a = _mm256_shuffle_epi32::<0b_10_01_00_11>(*a); // _MM_SHUFFLE(2, 1, 0, 3)
520 }
521
522 #[inline]
523 #[target_feature(enable = "avx512f", enable = "avx512vl")]
524 unsafe fn cols_to_rows_halfwide(v: &mut [__m256i; 4]) {
525 // c <<<= 32; d <<<= 64; a <<<= 96;
526 let [a, _, c, d] = v;
527 *c = _mm256_shuffle_epi32::<0b_10_01_00_11>(*c); // _MM_SHUFFLE(2, 1, 0, 3)
528 *d = _mm256_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
529 *a = _mm256_shuffle_epi32::<0b_00_11_10_01>(*a); // _MM_SHUFFLE(0, 3, 2, 1)
530 }
531
532 #[inline]
533 #[target_feature(enable = "avx512f", enable = "avx512vl")]
534 unsafe fn add_xor_rot_halfwide(v: &mut [__m256i; 4]) {
535 let [a, b, c, d] = v;
536
537 // a += b; d ^= a; d <<<= (16, 16, 16, 16);
538 *a = _mm256_add_epi32(*a, *b);
539 *d = _mm256_xor_si256(*d, *a);
540 *d = _mm256_rol_epi32::<16>(*d);
541
542 // c += d; b ^= c; b <<<= (12, 12, 12, 12);
543 *c = _mm256_add_epi32(*c, *d);
544 *b = _mm256_xor_si256(*b, *c);
545 *b = _mm256_rol_epi32::<12>(*b);
546
547 // a += b; d ^= a; d <<<= (8, 8, 8, 8);
548 *a = _mm256_add_epi32(*a, *b);
549 *d = _mm256_xor_si256(*d, *a);
550 *d = _mm256_rol_epi32::<8>(*d);
551
552 // c += d; b ^= c; b <<<= (7, 7, 7, 7);
553 *c = _mm256_add_epi32(*c, *d);
554 *b = _mm256_xor_si256(*b, *c);
555 *b = _mm256_rol_epi32::<7>(*b);
556 }
557
558 // Finally, below is an implementation using 128-bit vectors
559 // for the case of generating a single block.
560
561 #[inline(always)]
562 unsafe fn rounds_quarterwide<R: Rounds>(v_in: [__m128i; 4]) -> [__m128i; 4] {
563 let mut v = v_in;
564
565 for _ in 0..R::COUNT {
566 double_quarter_round_quarterwide(&mut v);
567 }
568
569 for (a, b) in v.iter_mut().zip(v_in) {
570 *a = _mm_add_epi32(*a, b);
571 }
572
573 v
574 }
575
576 #[inline(always)]
577 unsafe fn double_quarter_round_quarterwide(v: &mut [__m128i; 4]) {
578 add_xor_rot_quarterwide(v);
579 rows_to_cols_quarterwide(v);
580 add_xor_rot_quarterwide(v);
581 cols_to_rows_quarterwide(v);
582 }
583
584 #[inline(always)]
585 unsafe fn rows_to_cols_quarterwide(v: &mut [__m128i; 4]) {
586 // c >>>= 32; d >>>= 64; a >>>= 96;
587 let [a, _, c, d] = v;
588 *c = _mm_shuffle_epi32::<0b_00_11_10_01>(*c); // _MM_SHUFFLE(0, 3, 2, 1)
589 *d = _mm_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
590 *a = _mm_shuffle_epi32::<0b_10_01_00_11>(*a); // _MM_SHUFFLE(2, 1, 0, 3)
591 }
592
593 #[inline(always)]
594 unsafe fn cols_to_rows_quarterwide(v: &mut [__m128i; 4]) {
595 // c <<<= 32; d <<<= 64; a <<<= 96;
596 let [a, _, c, d] = v;
597 *c = _mm_shuffle_epi32::<0b_10_01_00_11>(*c); // _MM_SHUFFLE(2, 1, 0, 3)
598 *d = _mm_shuffle_epi32::<0b_01_00_11_10>(*d); // _MM_SHUFFLE(1, 0, 3, 2)
599 *a = _mm_shuffle_epi32::<0b_00_11_10_01>(*a); // _MM_SHUFFLE(0, 3, 2, 1)
600 }
601
602 #[inline(always)]
603 unsafe fn add_xor_rot_quarterwide(v: &mut [__m128i; 4]) {
604 let [a, b, c, d] = v;
605
606 // a += b; d ^= a; d <<<= (16, 16, 16, 16);
607 *a = _mm_add_epi32(*a, *b);
608 *d = _mm_xor_si128(*d, *a);
609 *d = _mm_rol_epi32::<16>(*d);
610
611 // c += d; b ^= c; b <<<= (12, 12, 12, 12);
612 *c = _mm_add_epi32(*c, *d);
613 *b = _mm_xor_si128(*b, *c);
614 *b = _mm_rol_epi32::<12>(*b);
615
616 // a += b; d ^= a; d <<<= (8, 8, 8, 8);
617 *a = _mm_add_epi32(*a, *b);
618 *d = _mm_xor_si128(*d, *a);
619 *d = _mm_rol_epi32::<8>(*d);
620
621 // c += d; b ^= c; b <<<= (7, 7, 7, 7);
622 *c = _mm_add_epi32(*c, *d);
623 *b = _mm_xor_si128(*b, *c);
624 *b = _mm_rol_epi32::<7>(*b);
625 }