Debian Codegraph @

debian/rust-vendor/blake2b_simd/src/avx2.rs

raw
1 #[cfg(target_arch = "x86")]
2 use core::arch::x86::*;
3 #[cfg(target_arch = "x86_64")]
4 use core::arch::x86_64::*;
5
6 use crate::guts::{
7 assemble_count, count_high, count_low, final_block, flag_word, input_debug_asserts, Finalize,
8 Job, LastNode, Stride,
9 };
10 use crate::{Count, Word, BLOCKBYTES, IV, SIGMA};
11 use core::cmp;
12 use core::mem;
13
14 pub const DEGREE: usize = 4;
15
16 #[inline(always)]
17 unsafe fn loadu(src: *const [Word; DEGREE]) -> __m256i {
18 // This is an unaligned load, so the pointer cast is allowed.
19 unsafe { _mm256_loadu_si256(src as *const __m256i) }
20 }
21
22 #[inline(always)]
23 unsafe fn storeu(src: __m256i, dest: *mut [Word; DEGREE]) {
24 // This is an unaligned store, so the pointer cast is allowed.
25 unsafe { _mm256_storeu_si256(dest as *mut __m256i, src) }
26 }
27
28 #[inline(always)]
29 unsafe fn loadu_128(mem_addr: &[u8; 16]) -> __m128i {
30 unsafe { _mm_loadu_si128(mem_addr.as_ptr() as *const __m128i) }
31 }
32
33 #[inline(always)]
34 unsafe fn add(a: __m256i, b: __m256i) -> __m256i {
35 unsafe { _mm256_add_epi64(a, b) }
36 }
37
38 #[inline(always)]
39 unsafe fn eq(a: __m256i, b: __m256i) -> __m256i {
40 unsafe { _mm256_cmpeq_epi64(a, b) }
41 }
42
43 #[inline(always)]
44 unsafe fn and(a: __m256i, b: __m256i) -> __m256i {
45 unsafe { _mm256_and_si256(a, b) }
46 }
47
48 #[inline(always)]
49 unsafe fn negate_and(a: __m256i, b: __m256i) -> __m256i {
50 // Note that "and not" implies the reverse of the actual arg order.
51 unsafe { _mm256_andnot_si256(a, b) }
52 }
53
54 #[inline(always)]
55 unsafe fn xor(a: __m256i, b: __m256i) -> __m256i {
56 unsafe { _mm256_xor_si256(a, b) }
57 }
58
59 #[inline(always)]
60 unsafe fn set1(x: u64) -> __m256i {
61 unsafe { _mm256_set1_epi64x(x as i64) }
62 }
63
64 #[inline(always)]
65 unsafe fn set4(a: u64, b: u64, c: u64, d: u64) -> __m256i {
66 unsafe { _mm256_setr_epi64x(a as i64, b as i64, c as i64, d as i64) }
67 }
68
69 // Adapted from https://github.com/rust-lang-nursery/stdsimd/pull/479.
70 macro_rules! _MM_SHUFFLE {
71 ($z:expr, $y:expr, $x:expr, $w:expr) => {
72 ($z << 6) | ($y << 4) | ($x << 2) | $w
73 };
74 }
75
76 // These rotations are the "simple version". For the "complicated version", see
77 // https://github.com/sneves/blake2-avx2/blob/b3723921f668df09ece52dcd225a36d4a4eea1d9/blake2b-common.h#L43-L46.
78 // For a discussion of the tradeoffs, see
79 // https://github.com/sneves/blake2-avx2/pull/5. In short:
80 // - Due to an LLVM bug (https://bugs.llvm.org/show_bug.cgi?id=44379), this
81 // version performs better on recent x86 chips.
82 // - LLVM is able to optimize this version to AVX-512 rotation instructions
83 // when those are enabled.
84
85 #[inline(always)]
86 unsafe fn rot32(x: __m256i) -> __m256i {
87 unsafe { _mm256_or_si256(_mm256_srli_epi64(x, 32), _mm256_slli_epi64(x, 64 - 32)) }
88 }
89
90 #[inline(always)]
91 unsafe fn rot24(x: __m256i) -> __m256i {
92 unsafe { _mm256_or_si256(_mm256_srli_epi64(x, 24), _mm256_slli_epi64(x, 64 - 24)) }
93 }
94
95 #[inline(always)]
96 unsafe fn rot16(x: __m256i) -> __m256i {
97 unsafe { _mm256_or_si256(_mm256_srli_epi64(x, 16), _mm256_slli_epi64(x, 64 - 16)) }
98 }
99
100 #[inline(always)]
101 unsafe fn rot63(x: __m256i) -> __m256i {
102 unsafe { _mm256_or_si256(_mm256_srli_epi64(x, 63), _mm256_slli_epi64(x, 64 - 63)) }
103 }
104
105 #[inline(always)]
106 unsafe fn g1(a: &mut __m256i, b: &mut __m256i, c: &mut __m256i, d: &mut __m256i, m: &mut __m256i) {
107 unsafe {
108 *a = add(*a, *m);
109 *a = add(*a, *b);
110 *d = xor(*d, *a);
111 *d = rot32(*d);
112 *c = add(*c, *d);
113 *b = xor(*b, *c);
114 *b = rot24(*b);
115 }
116 }
117
118 #[inline(always)]
119 unsafe fn g2(a: &mut __m256i, b: &mut __m256i, c: &mut __m256i, d: &mut __m256i, m: &mut __m256i) {
120 unsafe {
121 *a = add(*a, *m);
122 *a = add(*a, *b);
123 *d = xor(*d, *a);
124 *d = rot16(*d);
125 *c = add(*c, *d);
126 *b = xor(*b, *c);
127 *b = rot63(*b);
128 }
129 }
130
131 // Note the optimization here of leaving b as the unrotated row, rather than a.
132 // All the message loads below are adjusted to compensate for this. See
133 // discussion at https://github.com/sneves/blake2-avx2/pull/4
134 #[inline(always)]
135 unsafe fn diagonalize(a: &mut __m256i, _b: &mut __m256i, c: &mut __m256i, d: &mut __m256i) {
136 unsafe {
137 *a = _mm256_permute4x64_epi64(*a, _MM_SHUFFLE!(2, 1, 0, 3));
138 *d = _mm256_permute4x64_epi64(*d, _MM_SHUFFLE!(1, 0, 3, 2));
139 *c = _mm256_permute4x64_epi64(*c, _MM_SHUFFLE!(0, 3, 2, 1));
140 }
141 }
142
143 #[inline(always)]
144 unsafe fn undiagonalize(a: &mut __m256i, _b: &mut __m256i, c: &mut __m256i, d: &mut __m256i) {
145 unsafe {
146 *a = _mm256_permute4x64_epi64(*a, _MM_SHUFFLE!(0, 3, 2, 1));
147 *d = _mm256_permute4x64_epi64(*d, _MM_SHUFFLE!(1, 0, 3, 2));
148 *c = _mm256_permute4x64_epi64(*c, _MM_SHUFFLE!(2, 1, 0, 3));
149 }
150 }
151
152 #[inline(always)]
153 unsafe fn compress_block(
154 block: &[u8; BLOCKBYTES],
155 words: &mut [Word; 8],
156 count: Count,
157 last_block: Word,
158 last_node: Word,
159 ) {
160 unsafe {
161 let (words_low, words_high) = words.split_at_mut(DEGREE);
162 let words_low: &mut [Word; DEGREE] = words_low.try_into().unwrap();
163 let words_high: &mut [Word; DEGREE] = words_high.try_into().unwrap();
164 let iv_low: &[Word; DEGREE] = (&IV[..DEGREE]).try_into().unwrap();
165 let iv_high: &[Word; DEGREE] = (&IV[DEGREE..]).try_into().unwrap();
166 let mut a = loadu(words_low);
167 let mut b = loadu(words_high);
168 let mut c = loadu(iv_low);
169 let flags = set4(count_low(count), count_high(count), last_block, last_node);
170 let mut d = xor(loadu(iv_high), flags);
171
172 let msg_chunks: (
173 &[u8; 16],
174 &[u8; 16],
175 &[u8; 16],
176 &[u8; 16],
177 &[u8; 16],
178 &[u8; 16],
179 &[u8; 16],
180 &[u8; 16],
181 ) = (
182 (&block[0 * 16..][..16]).try_into().unwrap(),
183 (&block[1 * 16..][..16]).try_into().unwrap(),
184 (&block[2 * 16..][..16]).try_into().unwrap(),
185 (&block[3 * 16..][..16]).try_into().unwrap(),
186 (&block[4 * 16..][..16]).try_into().unwrap(),
187 (&block[5 * 16..][..16]).try_into().unwrap(),
188 (&block[6 * 16..][..16]).try_into().unwrap(),
189 (&block[7 * 16..][..16]).try_into().unwrap(),
190 );
191 let m0 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.0));
192 let m1 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.1));
193 let m2 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.2));
194 let m3 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.3));
195 let m4 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.4));
196 let m5 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.5));
197 let m6 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.6));
198 let m7 = _mm256_broadcastsi128_si256(loadu_128(msg_chunks.7));
199
200 let iv0 = a;
201 let iv1 = b;
202 let mut t0;
203 let mut t1;
204 let mut b0;
205
206 // round 1
207 t0 = _mm256_unpacklo_epi64(m0, m1);
208 t1 = _mm256_unpacklo_epi64(m2, m3);
209 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
210 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
211 t0 = _mm256_unpackhi_epi64(m0, m1);
212 t1 = _mm256_unpackhi_epi64(m2, m3);
213 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
214 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
215 diagonalize(&mut a, &mut b, &mut c, &mut d);
216 t0 = _mm256_unpacklo_epi64(m7, m4);
217 t1 = _mm256_unpacklo_epi64(m5, m6);
218 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
219 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
220 t0 = _mm256_unpackhi_epi64(m7, m4);
221 t1 = _mm256_unpackhi_epi64(m5, m6);
222 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
223 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
224 undiagonalize(&mut a, &mut b, &mut c, &mut d);
225
226 // round 2
227 t0 = _mm256_unpacklo_epi64(m7, m2);
228 t1 = _mm256_unpackhi_epi64(m4, m6);
229 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
230 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
231 t0 = _mm256_unpacklo_epi64(m5, m4);
232 t1 = _mm256_alignr_epi8(m3, m7, 8);
233 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
234 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
235 diagonalize(&mut a, &mut b, &mut c, &mut d);
236 t0 = _mm256_unpackhi_epi64(m2, m0);
237 t1 = _mm256_blend_epi32(m5, m0, 0x33);
238 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
239 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
240 t0 = _mm256_alignr_epi8(m6, m1, 8);
241 t1 = _mm256_blend_epi32(m3, m1, 0x33);
242 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
243 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
244 undiagonalize(&mut a, &mut b, &mut c, &mut d);
245
246 // round 3
247 t0 = _mm256_alignr_epi8(m6, m5, 8);
248 t1 = _mm256_unpackhi_epi64(m2, m7);
249 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
250 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
251 t0 = _mm256_unpacklo_epi64(m4, m0);
252 t1 = _mm256_blend_epi32(m6, m1, 0x33);
253 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
254 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
255 diagonalize(&mut a, &mut b, &mut c, &mut d);
256 t0 = _mm256_alignr_epi8(m5, m4, 8);
257 t1 = _mm256_unpackhi_epi64(m1, m3);
258 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
259 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
260 t0 = _mm256_unpacklo_epi64(m2, m7);
261 t1 = _mm256_blend_epi32(m0, m3, 0x33);
262 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
263 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
264 undiagonalize(&mut a, &mut b, &mut c, &mut d);
265
266 // round 4
267 t0 = _mm256_unpackhi_epi64(m3, m1);
268 t1 = _mm256_unpackhi_epi64(m6, m5);
269 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
270 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
271 t0 = _mm256_unpackhi_epi64(m4, m0);
272 t1 = _mm256_unpacklo_epi64(m6, m7);
273 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
274 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
275 diagonalize(&mut a, &mut b, &mut c, &mut d);
276 t0 = _mm256_alignr_epi8(m1, m7, 8);
277 t1 = _mm256_shuffle_epi32(m2, _MM_SHUFFLE!(1, 0, 3, 2));
278 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
279 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
280 t0 = _mm256_unpacklo_epi64(m4, m3);
281 t1 = _mm256_unpacklo_epi64(m5, m0);
282 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
283 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
284 undiagonalize(&mut a, &mut b, &mut c, &mut d);
285
286 // round 5
287 t0 = _mm256_unpackhi_epi64(m4, m2);
288 t1 = _mm256_unpacklo_epi64(m1, m5);
289 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
290 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
291 t0 = _mm256_blend_epi32(m3, m0, 0x33);
292 t1 = _mm256_blend_epi32(m7, m2, 0x33);
293 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
294 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
295 diagonalize(&mut a, &mut b, &mut c, &mut d);
296 t0 = _mm256_alignr_epi8(m7, m1, 8);
297 t1 = _mm256_alignr_epi8(m3, m5, 8);
298 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
299 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
300 t0 = _mm256_unpackhi_epi64(m6, m0);
301 t1 = _mm256_unpacklo_epi64(m6, m4);
302 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
303 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
304 undiagonalize(&mut a, &mut b, &mut c, &mut d);
305
306 // round 6
307 t0 = _mm256_unpacklo_epi64(m1, m3);
308 t1 = _mm256_unpacklo_epi64(m0, m4);
309 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
310 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
311 t0 = _mm256_unpacklo_epi64(m6, m5);
312 t1 = _mm256_unpackhi_epi64(m5, m1);
313 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
314 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
315 diagonalize(&mut a, &mut b, &mut c, &mut d);
316 t0 = _mm256_alignr_epi8(m2, m0, 8);
317 t1 = _mm256_unpackhi_epi64(m3, m7);
318 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
319 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
320 t0 = _mm256_unpackhi_epi64(m4, m6);
321 t1 = _mm256_alignr_epi8(m7, m2, 8);
322 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
323 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
324 undiagonalize(&mut a, &mut b, &mut c, &mut d);
325
326 // round 7
327 t0 = _mm256_blend_epi32(m0, m6, 0x33);
328 t1 = _mm256_unpacklo_epi64(m7, m2);
329 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
330 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
331 t0 = _mm256_unpackhi_epi64(m2, m7);
332 t1 = _mm256_alignr_epi8(m5, m6, 8);
333 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
334 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
335 diagonalize(&mut a, &mut b, &mut c, &mut d);
336 t0 = _mm256_unpacklo_epi64(m4, m0);
337 t1 = _mm256_blend_epi32(m4, m3, 0x33);
338 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
339 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
340 t0 = _mm256_unpackhi_epi64(m5, m3);
341 t1 = _mm256_shuffle_epi32(m1, _MM_SHUFFLE!(1, 0, 3, 2));
342 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
343 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
344 undiagonalize(&mut a, &mut b, &mut c, &mut d);
345
346 // round 8
347 t0 = _mm256_unpackhi_epi64(m6, m3);
348 t1 = _mm256_blend_epi32(m1, m6, 0x33);
349 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
350 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
351 t0 = _mm256_alignr_epi8(m7, m5, 8);
352 t1 = _mm256_unpackhi_epi64(m0, m4);
353 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
354 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
355 diagonalize(&mut a, &mut b, &mut c, &mut d);
356 t0 = _mm256_blend_epi32(m2, m1, 0x33);
357 t1 = _mm256_alignr_epi8(m4, m7, 8);
358 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
359 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
360 t0 = _mm256_unpacklo_epi64(m5, m0);
361 t1 = _mm256_unpacklo_epi64(m2, m3);
362 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
363 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
364 undiagonalize(&mut a, &mut b, &mut c, &mut d);
365
366 // round 9
367 t0 = _mm256_unpacklo_epi64(m3, m7);
368 t1 = _mm256_alignr_epi8(m0, m5, 8);
369 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
370 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
371 t0 = _mm256_unpackhi_epi64(m7, m4);
372 t1 = _mm256_alignr_epi8(m4, m1, 8);
373 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
374 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
375 diagonalize(&mut a, &mut b, &mut c, &mut d);
376 t0 = _mm256_unpacklo_epi64(m5, m6);
377 t1 = _mm256_unpackhi_epi64(m6, m0);
378 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
379 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
380 t0 = _mm256_alignr_epi8(m1, m2, 8);
381 t1 = _mm256_alignr_epi8(m2, m3, 8);
382 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
383 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
384 undiagonalize(&mut a, &mut b, &mut c, &mut d);
385
386 // round 10
387 t0 = _mm256_unpacklo_epi64(m5, m4);
388 t1 = _mm256_unpackhi_epi64(m3, m0);
389 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
390 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
391 t0 = _mm256_unpacklo_epi64(m1, m2);
392 t1 = _mm256_blend_epi32(m2, m3, 0x33);
393 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
394 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
395 diagonalize(&mut a, &mut b, &mut c, &mut d);
396 t0 = _mm256_unpackhi_epi64(m6, m7);
397 t1 = _mm256_unpackhi_epi64(m4, m1);
398 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
399 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
400 t0 = _mm256_blend_epi32(m5, m0, 0x33);
401 t1 = _mm256_unpacklo_epi64(m7, m6);
402 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
403 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
404 undiagonalize(&mut a, &mut b, &mut c, &mut d);
405
406 // round 11
407 t0 = _mm256_unpacklo_epi64(m0, m1);
408 t1 = _mm256_unpacklo_epi64(m2, m3);
409 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
410 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
411 t0 = _mm256_unpackhi_epi64(m0, m1);
412 t1 = _mm256_unpackhi_epi64(m2, m3);
413 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
414 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
415 diagonalize(&mut a, &mut b, &mut c, &mut d);
416 t0 = _mm256_unpacklo_epi64(m7, m4);
417 t1 = _mm256_unpacklo_epi64(m5, m6);
418 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
419 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
420 t0 = _mm256_unpackhi_epi64(m7, m4);
421 t1 = _mm256_unpackhi_epi64(m5, m6);
422 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
423 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
424 undiagonalize(&mut a, &mut b, &mut c, &mut d);
425
426 // round 12
427 t0 = _mm256_unpacklo_epi64(m7, m2);
428 t1 = _mm256_unpackhi_epi64(m4, m6);
429 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
430 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
431 t0 = _mm256_unpacklo_epi64(m5, m4);
432 t1 = _mm256_alignr_epi8(m3, m7, 8);
433 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
434 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
435 diagonalize(&mut a, &mut b, &mut c, &mut d);
436 t0 = _mm256_unpackhi_epi64(m2, m0);
437 t1 = _mm256_blend_epi32(m5, m0, 0x33);
438 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
439 g1(&mut a, &mut b, &mut c, &mut d, &mut b0);
440 t0 = _mm256_alignr_epi8(m6, m1, 8);
441 t1 = _mm256_blend_epi32(m3, m1, 0x33);
442 b0 = _mm256_blend_epi32(t0, t1, 0xF0);
443 g2(&mut a, &mut b, &mut c, &mut d, &mut b0);
444 undiagonalize(&mut a, &mut b, &mut c, &mut d);
445
446 a = xor(a, c);
447 b = xor(b, d);
448 a = xor(a, iv0);
449 b = xor(b, iv1);
450
451 storeu(a, words_low);
452 storeu(b, words_high);
453 }
454 }
455
456 #[target_feature(enable = "avx2")]
457 pub unsafe fn compress1_loop(
458 input: &[u8],
459 words: &mut [Word; 8],
460 mut count: Count,
461 last_node: LastNode,
462 finalize: Finalize,
463 stride: Stride,
464 ) {
465 unsafe {
466 input_debug_asserts(input, finalize);
467
468 let mut local_words = *words;
469
470 let mut fin_offset = input.len().saturating_sub(1);
471 fin_offset -= fin_offset % stride.padded_blockbytes();
472 let mut buf = [0; BLOCKBYTES];
473 let (fin_block, fin_len, _) = final_block(input, fin_offset, &mut buf, stride);
474 let fin_last_block = flag_word(finalize.yes());
475 let fin_last_node = flag_word(finalize.yes() && last_node.yes());
476
477 let mut offset = 0;
478 loop {
479 let block;
480 let count_delta;
481 let last_block;
482 let last_node;
483 if offset == fin_offset {
484 block = fin_block;
485 count_delta = fin_len;
486 last_block = fin_last_block;
487 last_node = fin_last_node;
488 } else {
489 // This unsafe cast avoids bounds checks. There's guaranteed to be
490 // enough input because `offset < fin_offset`.
491 block = &*(input.as_ptr().add(offset) as *const [u8; BLOCKBYTES]);
492 count_delta = BLOCKBYTES;
493 last_block = flag_word(false);
494 last_node = flag_word(false);
495 };
496
497 count = count.wrapping_add(count_delta as Count);
498 compress_block(block, &mut local_words, count, last_block, last_node);
499
500 // Check for termination before bumping the offset, to avoid overflow.
501 if offset == fin_offset {
502 break;
503 }
504
505 offset += stride.padded_blockbytes();
506 }
507
508 *words = local_words;
509 }
510 }
511
512 // Performance note: Factoring out a G function here doesn't hurt performance,
513 // unlike in the case of BLAKE2s where it hurts substantially. In fact, on my
514 // machine, it helps a tiny bit. But the difference it tiny, so I'm going to
515 // stick to the approach used by https://github.com/sneves/blake2-avx2
516 // until/unless I can be sure the (tiny) improvement is consistent across
517 // different Intel microarchitectures. Smaller code size is nice, but a
518 // divergence between the BLAKE2b and BLAKE2s implementations is less nice.
519 #[inline(always)]
520 unsafe fn round(v: &mut [__m256i; 16], m: &[__m256i; 16], r: usize) {
521 unsafe {
522 v[0] = add(v[0], m[SIGMA[r][0] as usize]);
523 v[1] = add(v[1], m[SIGMA[r][2] as usize]);
524 v[2] = add(v[2], m[SIGMA[r][4] as usize]);
525 v[3] = add(v[3], m[SIGMA[r][6] as usize]);
526 v[0] = add(v[0], v[4]);
527 v[1] = add(v[1], v[5]);
528 v[2] = add(v[2], v[6]);
529 v[3] = add(v[3], v[7]);
530 v[12] = xor(v[12], v[0]);
531 v[13] = xor(v[13], v[1]);
532 v[14] = xor(v[14], v[2]);
533 v[15] = xor(v[15], v[3]);
534 v[12] = rot32(v[12]);
535 v[13] = rot32(v[13]);
536 v[14] = rot32(v[14]);
537 v[15] = rot32(v[15]);
538 v[8] = add(v[8], v[12]);
539 v[9] = add(v[9], v[13]);
540 v[10] = add(v[10], v[14]);
541 v[11] = add(v[11], v[15]);
542 v[4] = xor(v[4], v[8]);
543 v[5] = xor(v[5], v[9]);
544 v[6] = xor(v[6], v[10]);
545 v[7] = xor(v[7], v[11]);
546 v[4] = rot24(v[4]);
547 v[5] = rot24(v[5]);
548 v[6] = rot24(v[6]);
549 v[7] = rot24(v[7]);
550 v[0] = add(v[0], m[SIGMA[r][1] as usize]);
551 v[1] = add(v[1], m[SIGMA[r][3] as usize]);
552 v[2] = add(v[2], m[SIGMA[r][5] as usize]);
553 v[3] = add(v[3], m[SIGMA[r][7] as usize]);
554 v[0] = add(v[0], v[4]);
555 v[1] = add(v[1], v[5]);
556 v[2] = add(v[2], v[6]);
557 v[3] = add(v[3], v[7]);
558 v[12] = xor(v[12], v[0]);
559 v[13] = xor(v[13], v[1]);
560 v[14] = xor(v[14], v[2]);
561 v[15] = xor(v[15], v[3]);
562 v[12] = rot16(v[12]);
563 v[13] = rot16(v[13]);
564 v[14] = rot16(v[14]);
565 v[15] = rot16(v[15]);
566 v[8] = add(v[8], v[12]);
567 v[9] = add(v[9], v[13]);
568 v[10] = add(v[10], v[14]);
569 v[11] = add(v[11], v[15]);
570 v[4] = xor(v[4], v[8]);
571 v[5] = xor(v[5], v[9]);
572 v[6] = xor(v[6], v[10]);
573 v[7] = xor(v[7], v[11]);
574 v[4] = rot63(v[4]);
575 v[5] = rot63(v[5]);
576 v[6] = rot63(v[6]);
577 v[7] = rot63(v[7]);
578
579 v[0] = add(v[0], m[SIGMA[r][8] as usize]);
580 v[1] = add(v[1], m[SIGMA[r][10] as usize]);
581 v[2] = add(v[2], m[SIGMA[r][12] as usize]);
582 v[3] = add(v[3], m[SIGMA[r][14] as usize]);
583 v[0] = add(v[0], v[5]);
584 v[1] = add(v[1], v[6]);
585 v[2] = add(v[2], v[7]);
586 v[3] = add(v[3], v[4]);
587 v[15] = xor(v[15], v[0]);
588 v[12] = xor(v[12], v[1]);
589 v[13] = xor(v[13], v[2]);
590 v[14] = xor(v[14], v[3]);
591 v[15] = rot32(v[15]);
592 v[12] = rot32(v[12]);
593 v[13] = rot32(v[13]);
594 v[14] = rot32(v[14]);
595 v[10] = add(v[10], v[15]);
596 v[11] = add(v[11], v[12]);
597 v[8] = add(v[8], v[13]);
598 v[9] = add(v[9], v[14]);
599 v[5] = xor(v[5], v[10]);
600 v[6] = xor(v[6], v[11]);
601 v[7] = xor(v[7], v[8]);
602 v[4] = xor(v[4], v[9]);
603 v[5] = rot24(v[5]);
604 v[6] = rot24(v[6]);
605 v[7] = rot24(v[7]);
606 v[4] = rot24(v[4]);
607 v[0] = add(v[0], m[SIGMA[r][9] as usize]);
608 v[1] = add(v[1], m[SIGMA[r][11] as usize]);
609 v[2] = add(v[2], m[SIGMA[r][13] as usize]);
610 v[3] = add(v[3], m[SIGMA[r][15] as usize]);
611 v[0] = add(v[0], v[5]);
612 v[1] = add(v[1], v[6]);
613 v[2] = add(v[2], v[7]);
614 v[3] = add(v[3], v[4]);
615 v[15] = xor(v[15], v[0]);
616 v[12] = xor(v[12], v[1]);
617 v[13] = xor(v[13], v[2]);
618 v[14] = xor(v[14], v[3]);
619 v[15] = rot16(v[15]);
620 v[12] = rot16(v[12]);
621 v[13] = rot16(v[13]);
622 v[14] = rot16(v[14]);
623 v[10] = add(v[10], v[15]);
624 v[11] = add(v[11], v[12]);
625 v[8] = add(v[8], v[13]);
626 v[9] = add(v[9], v[14]);
627 v[5] = xor(v[5], v[10]);
628 v[6] = xor(v[6], v[11]);
629 v[7] = xor(v[7], v[8]);
630 v[4] = xor(v[4], v[9]);
631 v[5] = rot63(v[5]);
632 v[6] = rot63(v[6]);
633 v[7] = rot63(v[7]);
634 v[4] = rot63(v[4]);
635 }
636 }
637
638 // We'd rather make this a regular function with #[inline(always)], but for
639 // some reason that blows up compile times by about 10 seconds, at least in
640 // some cases (BLAKE2b avx2.rs). This macro seems to get the same performance
641 // result, without the compile time issue.
642 macro_rules! compress4_transposed {
643 (
644 $h_vecs:expr,
645 $msg_vecs:expr,
646 $count_low:expr,
647 $count_high:expr,
648 $lastblock:expr,
649 $lastnode:expr,
650 ) => {
651 let h_vecs: &mut [__m256i; 8] = $h_vecs;
652 let msg_vecs: &[__m256i; 16] = $msg_vecs;
653 let count_low: __m256i = $count_low;
654 let count_high: __m256i = $count_high;
655 let lastblock: __m256i = $lastblock;
656 let lastnode: __m256i = $lastnode;
657
658 let mut v = [
659 h_vecs[0],
660 h_vecs[1],
661 h_vecs[2],
662 h_vecs[3],
663 h_vecs[4],
664 h_vecs[5],
665 h_vecs[6],
666 h_vecs[7],
667 set1(IV[0]),
668 set1(IV[1]),
669 set1(IV[2]),
670 set1(IV[3]),
671 xor(set1(IV[4]), count_low),
672 xor(set1(IV[5]), count_high),
673 xor(set1(IV[6]), lastblock),
674 xor(set1(IV[7]), lastnode),
675 ];
676
677 round(&mut v, &msg_vecs, 0);
678 round(&mut v, &msg_vecs, 1);
679 round(&mut v, &msg_vecs, 2);
680 round(&mut v, &msg_vecs, 3);
681 round(&mut v, &msg_vecs, 4);
682 round(&mut v, &msg_vecs, 5);
683 round(&mut v, &msg_vecs, 6);
684 round(&mut v, &msg_vecs, 7);
685 round(&mut v, &msg_vecs, 8);
686 round(&mut v, &msg_vecs, 9);
687 round(&mut v, &msg_vecs, 10);
688 round(&mut v, &msg_vecs, 11);
689
690 h_vecs[0] = xor(xor(h_vecs[0], v[0]), v[8]);
691 h_vecs[1] = xor(xor(h_vecs[1], v[1]), v[9]);
692 h_vecs[2] = xor(xor(h_vecs[2], v[2]), v[10]);
693 h_vecs[3] = xor(xor(h_vecs[3], v[3]), v[11]);
694 h_vecs[4] = xor(xor(h_vecs[4], v[4]), v[12]);
695 h_vecs[5] = xor(xor(h_vecs[5], v[5]), v[13]);
696 h_vecs[6] = xor(xor(h_vecs[6], v[6]), v[14]);
697 h_vecs[7] = xor(xor(h_vecs[7], v[7]), v[15]);
698 };
699 }
700
701 #[inline(always)]
702 unsafe fn interleave128(a: __m256i, b: __m256i) -> (__m256i, __m256i) {
703 unsafe {
704 (
705 _mm256_permute2x128_si256(a, b, 0x20),
706 _mm256_permute2x128_si256(a, b, 0x31),
707 )
708 }
709 }
710
711 // There are several ways to do a transposition. We could do it naively, with 8 separate
712 // _mm256_set_epi64x instructions, referencing each of the 64 words explicitly. Or we could copy
713 // the vecs into contiguous storage and then use gather instructions. This third approach is to use
714 // a series of unpack instructions to interleave the vectors. In my benchmarks, interleaving is the
715 // fastest approach. To test this, run `cargo +nightly bench --bench libtest load_4` in the
716 // https://github.com/oconnor663/bao_experiments repo.
717 #[inline(always)]
718 unsafe fn transpose_vecs(
719 vec_a: __m256i,
720 vec_b: __m256i,
721 vec_c: __m256i,
722 vec_d: __m256i,
723 ) -> [__m256i; DEGREE] {
724 unsafe {
725 // Interleave 64-bit lates. The low unpack is lanes 00/22 and the high is 11/33.
726 let ab_02 = _mm256_unpacklo_epi64(vec_a, vec_b);
727 let ab_13 = _mm256_unpackhi_epi64(vec_a, vec_b);
728 let cd_02 = _mm256_unpacklo_epi64(vec_c, vec_d);
729 let cd_13 = _mm256_unpackhi_epi64(vec_c, vec_d);
730
731 // Interleave 128-bit lanes.
732 let (abcd_0, abcd_2) = interleave128(ab_02, cd_02);
733 let (abcd_1, abcd_3) = interleave128(ab_13, cd_13);
734
735 [abcd_0, abcd_1, abcd_2, abcd_3]
736 }
737 }
738
739 #[inline(always)]
740 unsafe fn transpose_state_vecs(jobs: &[Job; DEGREE]) -> [__m256i; 8] {
741 unsafe {
742 // Load all the state words into transposed vectors, where the first vector
743 // has the first word of each state, etc. Transposing once at the beginning
744 // and once at the end is more efficient that repeating it for each block.
745 let words0: (&[Word; DEGREE], &[Word; DEGREE]) = (
746 (&jobs[0].words[..DEGREE]).try_into().unwrap(),
747 (&jobs[0].words[DEGREE..]).try_into().unwrap(),
748 );
749 let words1: (&[Word; DEGREE], &[Word; DEGREE]) = (
750 (&jobs[1].words[..DEGREE]).try_into().unwrap(),
751 (&jobs[1].words[DEGREE..]).try_into().unwrap(),
752 );
753 let words2: (&[Word; DEGREE], &[Word; DEGREE]) = (
754 (&jobs[2].words[..DEGREE]).try_into().unwrap(),
755 (&jobs[2].words[DEGREE..]).try_into().unwrap(),
756 );
757 let words3: (&[Word; DEGREE], &[Word; DEGREE]) = (
758 (&jobs[3].words[..DEGREE]).try_into().unwrap(),
759 (&jobs[3].words[DEGREE..]).try_into().unwrap(),
760 );
761 let [h0, h1, h2, h3] = transpose_vecs(
762 loadu(words0.0),
763 loadu(words1.0),
764 loadu(words2.0),
765 loadu(words3.0),
766 );
767 let [h4, h5, h6, h7] = transpose_vecs(
768 loadu(words0.1),
769 loadu(words1.1),
770 loadu(words2.1),
771 loadu(words3.1),
772 );
773 [h0, h1, h2, h3, h4, h5, h6, h7]
774 }
775 }
776
777 #[inline(always)]
778 unsafe fn untranspose_state_vecs(h_vecs: &[__m256i; 8], jobs: &mut [Job; DEGREE]) {
779 unsafe {
780 // Un-transpose the updated state vectors back into the caller's arrays.
781 let [job0, job1, job2, job3] = jobs;
782 let words0 = job0.words.split_at_mut(DEGREE);
783 let words0: (&mut [Word; DEGREE], &mut [Word; DEGREE]) =
784 (words0.0.try_into().unwrap(), words0.1.try_into().unwrap());
785 let words1 = job1.words.split_at_mut(DEGREE);
786 let words1: (&mut [Word; DEGREE], &mut [Word; DEGREE]) =
787 (words1.0.try_into().unwrap(), words1.1.try_into().unwrap());
788 let words2 = job2.words.split_at_mut(DEGREE);
789 let words2: (&mut [Word; DEGREE], &mut [Word; DEGREE]) =
790 (words2.0.try_into().unwrap(), words2.1.try_into().unwrap());
791 let words3 = job3.words.split_at_mut(DEGREE);
792 let words3: (&mut [Word; DEGREE], &mut [Word; DEGREE]) =
793 (words3.0.try_into().unwrap(), words3.1.try_into().unwrap());
794 let out = transpose_vecs(h_vecs[0], h_vecs[1], h_vecs[2], h_vecs[3]);
795 storeu(out[0], words0.0);
796 storeu(out[1], words1.0);
797 storeu(out[2], words2.0);
798 storeu(out[3], words3.0);
799 let out = transpose_vecs(h_vecs[4], h_vecs[5], h_vecs[6], h_vecs[7]);
800 storeu(out[0], words0.1);
801 storeu(out[1], words1.1);
802 storeu(out[2], words2.1);
803 storeu(out[3], words3.1);
804 }
805 }
806
807 #[inline(always)]
808 unsafe fn transpose_msg_vecs(blocks: [*const [u8; BLOCKBYTES]; DEGREE]) -> [__m256i; 16] {
809 unsafe {
810 // These input arrays have no particular alignment, so we use unaligned
811 // loads to read from them.
812 let block0 = blocks[0] as *const [Word; DEGREE];
813 let block1 = blocks[1] as *const [Word; DEGREE];
814 let block2 = blocks[2] as *const [Word; DEGREE];
815 let block3 = blocks[3] as *const [Word; DEGREE];
816 let [m0, m1, m2, m3] = transpose_vecs(
817 loadu(block0.add(0)),
818 loadu(block1.add(0)),
819 loadu(block2.add(0)),
820 loadu(block3.add(0)),
821 );
822 let [m4, m5, m6, m7] = transpose_vecs(
823 loadu(block0.add(1)),
824 loadu(block1.add(1)),
825 loadu(block2.add(1)),
826 loadu(block3.add(1)),
827 );
828 let [m8, m9, m10, m11] = transpose_vecs(
829 loadu(block0.add(2)),
830 loadu(block1.add(2)),
831 loadu(block2.add(2)),
832 loadu(block3.add(2)),
833 );
834 let [m12, m13, m14, m15] = transpose_vecs(
835 loadu(block0.add(3)),
836 loadu(block1.add(3)),
837 loadu(block2.add(3)),
838 loadu(block3.add(3)),
839 );
840 [
841 m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15,
842 ]
843 }
844 }
845
846 #[inline(always)]
847 unsafe fn load_counts(jobs: &[Job; DEGREE]) -> (__m256i, __m256i) {
848 unsafe {
849 (
850 set4(
851 count_low(jobs[0].count),
852 count_low(jobs[1].count),
853 count_low(jobs[2].count),
854 count_low(jobs[3].count),
855 ),
856 set4(
857 count_high(jobs[0].count),
858 count_high(jobs[1].count),
859 count_high(jobs[2].count),
860 count_high(jobs[3].count),
861 ),
862 )
863 }
864 }
865
866 #[inline(always)]
867 unsafe fn store_counts(jobs: &mut [Job; DEGREE], low: __m256i, high: __m256i) {
868 unsafe {
869 let low_ints: [Word; DEGREE] = mem::transmute(low);
870 let high_ints: [Word; DEGREE] = mem::transmute(high);
871 for i in 0..DEGREE {
872 jobs[i].count = assemble_count(low_ints[i], high_ints[i]);
873 }
874 }
875 }
876
877 #[inline(always)]
878 unsafe fn add_to_counts(lo: &mut __m256i, hi: &mut __m256i, delta: __m256i) {
879 unsafe {
880 // If the low counts reach zero, that means they wrapped, unless the delta
881 // was also zero.
882 *lo = add(*lo, delta);
883 let lo_reached_zero = eq(*lo, set1(0));
884 let delta_was_zero = eq(delta, set1(0));
885 let hi_inc = and(set1(1), negate_and(delta_was_zero, lo_reached_zero));
886 *hi = add(*hi, hi_inc);
887 }
888 }
889
890 #[inline(always)]
891 unsafe fn flags_vec(flags: [bool; DEGREE]) -> __m256i {
892 unsafe {
893 set4(
894 flag_word(flags[0]),
895 flag_word(flags[1]),
896 flag_word(flags[2]),
897 flag_word(flags[3]),
898 )
899 }
900 }
901
902 #[target_feature(enable = "avx2")]
903 pub unsafe fn compress4_loop(jobs: &mut [Job; DEGREE], finalize: Finalize, stride: Stride) {
904 unsafe {
905 // If we're not finalizing, there can't be a partial block at the end.
906 for job in jobs.iter() {
907 input_debug_asserts(job.input, finalize);
908 }
909
910 let msg_ptrs = [
911 jobs[0].input.as_ptr(),
912 jobs[1].input.as_ptr(),
913 jobs[2].input.as_ptr(),
914 jobs[3].input.as_ptr(),
915 ];
916 let mut h_vecs = transpose_state_vecs(&jobs);
917 let (mut counts_lo, mut counts_hi) = load_counts(&jobs);
918
919 // Prepare the final blocks (note, which could be empty if the input is
920 // empty). Do all this before entering the main loop.
921 let min_len = jobs.iter().map(|job| job.input.len()).min().unwrap();
922 let mut fin_offset = min_len.saturating_sub(1);
923 fin_offset -= fin_offset % stride.padded_blockbytes();
924 // Performance note, making these buffers mem::uninitialized() seems to
925 // cause problems in the optimizer.
926 let mut buf0: [u8; BLOCKBYTES] = [0; BLOCKBYTES];
927 let mut buf1: [u8; BLOCKBYTES] = [0; BLOCKBYTES];
928 let mut buf2: [u8; BLOCKBYTES] = [0; BLOCKBYTES];
929 let mut buf3: [u8; BLOCKBYTES] = [0; BLOCKBYTES];
930 let (block0, len0, finalize0) = final_block(jobs[0].input, fin_offset, &mut buf0, stride);
931 let (block1, len1, finalize1) = final_block(jobs[1].input, fin_offset, &mut buf1, stride);
932 let (block2, len2, finalize2) = final_block(jobs[2].input, fin_offset, &mut buf2, stride);
933 let (block3, len3, finalize3) = final_block(jobs[3].input, fin_offset, &mut buf3, stride);
934 let fin_blocks: [*const [u8; BLOCKBYTES]; DEGREE] = [block0, block1, block2, block3];
935 let fin_counts_delta = set4(len0 as Word, len1 as Word, len2 as Word, len3 as Word);
936 let fin_last_block;
937 let fin_last_node;
938 if finalize.yes() {
939 fin_last_block = flags_vec([finalize0, finalize1, finalize2, finalize3]);
940 fin_last_node = flags_vec([
941 finalize0 && jobs[0].last_node.yes(),
942 finalize1 && jobs[1].last_node.yes(),
943 finalize2 && jobs[2].last_node.yes(),
944 finalize3 && jobs[3].last_node.yes(),
945 ]);
946 } else {
947 fin_last_block = set1(0);
948 fin_last_node = set1(0);
949 }
950
951 // The main loop.
952 let mut offset = 0;
953 loop {
954 let blocks;
955 let counts_delta;
956 let last_block;
957 let last_node;
958 if offset == fin_offset {
959 blocks = fin_blocks;
960 counts_delta = fin_counts_delta;
961 last_block = fin_last_block;
962 last_node = fin_last_node;
963 } else {
964 blocks = [
965 msg_ptrs[0].add(offset) as *const [u8; BLOCKBYTES],
966 msg_ptrs[1].add(offset) as *const [u8; BLOCKBYTES],
967 msg_ptrs[2].add(offset) as *const [u8; BLOCKBYTES],
968 msg_ptrs[3].add(offset) as *const [u8; BLOCKBYTES],
969 ];
970 counts_delta = set1(BLOCKBYTES as Word);
971 last_block = set1(0);
972 last_node = set1(0);
973 };
974
975 let m_vecs = transpose_msg_vecs(blocks);
976 add_to_counts(&mut counts_lo, &mut counts_hi, counts_delta);
977 compress4_transposed!(
978 &mut h_vecs,
979 &m_vecs,
980 counts_lo,
981 counts_hi,
982 last_block,
983 last_node,
984 );
985
986 // Check for termination before bumping the offset, to avoid overflow.
987 if offset == fin_offset {
988 break;
989 }
990
991 offset += stride.padded_blockbytes();
992 }
993
994 // Write out the results.
995 untranspose_state_vecs(&h_vecs, &mut *jobs);
996 store_counts(&mut *jobs, counts_lo, counts_hi);
997 let max_consumed = offset.saturating_add(stride.padded_blockbytes());
998 for job in jobs.iter_mut() {
999 let consumed = cmp::min(max_consumed, job.input.len());
1000 job.input = &job.input[consumed..];
1001 }
1002 }
1003 }