Debian Codegraph @

debian/rust-vendor/blake3/src/wasm32_simd.rs

raw
1 /*
2 * This code is based on rust_sse2.rs of the same distribution, and is subject to further improvements.
3 * Some comments are left intact even if their applicability is questioned.
4 *
5 * Performance measurements with a primitive benchmark with ~16Kb of data:
6 *
7 * | M1 native | 11,610 ns |
8 * | M1 Wasm SIMD | 13,355 ns |
9 * | M1 Wasm | 22,037 ns |
10 * | x64 native | 6,713 ns |
11 * | x64 Wasm SIMD | 11,985 ns |
12 * | x64 Wasm | 25,978 ns |
13 *
14 * wasmtime v12.0.1 was used on both platforms.
15 */
16
17 use core::arch::wasm32::*;
18
19 use crate::{
20 BLOCK_LEN, CVBytes, CVWords, IV, IncrementCounter, MSG_SCHEDULE, OUT_LEN, counter_high,
21 counter_low,
22 };
23
24 pub const DEGREE: usize = 4;
25
26 #[inline(always)]
27 unsafe fn loadu(src: *const u8) -> v128 {
28 // This is an unaligned load, so the pointer cast is allowed.
29 unsafe { v128_load(src as *const v128) }
30 }
31
32 #[inline(always)]
33 unsafe fn storeu(src: v128, dest: *mut u8) {
34 // This is an unaligned store, so the pointer cast is allowed.
35 unsafe { v128_store(dest as *mut v128, src) }
36 }
37
38 #[inline(always)]
39 fn add(a: v128, b: v128) -> v128 {
40 i32x4_add(a, b)
41 }
42
43 #[inline(always)]
44 fn xor(a: v128, b: v128) -> v128 {
45 v128_xor(a, b)
46 }
47
48 #[inline(always)]
49 fn set1(x: u32) -> v128 {
50 i32x4_splat(x as i32)
51 }
52
53 #[inline(always)]
54 fn set4(a: u32, b: u32, c: u32, d: u32) -> v128 {
55 i32x4(a as i32, b as i32, c as i32, d as i32)
56 }
57
58 // rot16 and rot8 use i8x16_shuffle (1 WASM instruction) instead of
59 // shift+OR (3 instructions) since they are byte-aligned rotations.
60 // rot12 and rot7 are not byte-aligned, so they still use shift+OR.
61 // For the x86 "shuffles vs shifts" discussion, see
62 // https://github.com/sneves/blake2-avx2/pull/5. On x86, shifts can be
63 // faster due to an LLVM bug (https://bugs.llvm.org/show_bug.cgi?id=44379),
64 // but on WASM SIMD targets, i8x16_shuffle is ~20% faster for rot8/rot16.
65 #[inline(always)]
66 fn rot16(a: v128) -> v128 {
67 i8x16_shuffle::<2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9, 14, 15, 12, 13>(a, a)
68 }
69
70 #[inline(always)]
71 fn rot12(a: v128) -> v128 {
72 v128_or(u32x4_shr(a, 12), u32x4_shl(a, 32 - 12))
73 }
74
75 #[inline(always)]
76 fn rot8(a: v128) -> v128 {
77 i8x16_shuffle::<1, 2, 3, 0, 5, 6, 7, 4, 9, 10, 11, 8, 13, 14, 15, 12>(a, a)
78 }
79
80 #[inline(always)]
81 fn rot7(a: v128) -> v128 {
82 v128_or(u32x4_shr(a, 7), u32x4_shl(a, 32 - 7))
83 }
84
85 #[inline(always)]
86 fn g1(row0: &mut v128, row1: &mut v128, row2: &mut v128, row3: &mut v128, m: v128) {
87 *row0 = add(add(*row0, m), *row1);
88 *row3 = xor(*row3, *row0);
89 *row3 = rot16(*row3);
90 *row2 = add(*row2, *row3);
91 *row1 = xor(*row1, *row2);
92 *row1 = rot12(*row1);
93 }
94
95 #[inline(always)]
96 fn g2(row0: &mut v128, row1: &mut v128, row2: &mut v128, row3: &mut v128, m: v128) {
97 *row0 = add(add(*row0, m), *row1);
98 *row3 = xor(*row3, *row0);
99 *row3 = rot8(*row3);
100 *row2 = add(*row2, *row3);
101 *row1 = xor(*row1, *row2);
102 *row1 = rot7(*row1);
103 }
104
105 // It could be a function, but artimetics in const generics is too limited yet.
106 macro_rules! shuffle {
107 ($a: expr, $b: expr, $z:expr, $y:expr, $x:expr, $w:expr) => {
108 i32x4_shuffle::<{ $w }, { $x }, { $y + 4 }, { $z + 4 }>($a, $b)
109 };
110 }
111
112 #[inline(always)]
113 fn unpacklo_epi64(a: v128, b: v128) -> v128 {
114 i64x2_shuffle::<0, 2>(a, b)
115 }
116
117 #[inline(always)]
118 fn unpackhi_epi64(a: v128, b: v128) -> v128 {
119 i64x2_shuffle::<1, 3>(a, b)
120 }
121
122 #[inline(always)]
123 fn unpacklo_epi32(a: v128, b: v128) -> v128 {
124 i32x4_shuffle::<0, 4, 1, 5>(a, b)
125 }
126
127 #[inline(always)]
128 fn unpackhi_epi32(a: v128, b: v128) -> v128 {
129 i32x4_shuffle::<2, 6, 3, 7>(a, b)
130 }
131
132 #[inline(always)]
133 fn shuffle_epi32<const I3: usize, const I2: usize, const I1: usize, const I0: usize>(
134 a: v128,
135 ) -> v128 {
136 // Please note that generic arguments in declaration and implementation are in
137 // different order.
138 // second arg is actually ignored.
139 i32x4_shuffle::<I0, I1, I2, I3>(a, a)
140 }
141
142 #[inline(always)]
143 fn blend_epi16(a: v128, b: v128, imm8: i32) -> v128 {
144 // imm8 is always constant; it allows to implement this function with
145 // i16x8_shuffle. However, it is marginally slower on x64.
146 let bits = i16x8(0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80);
147 let mut mask = i16x8_splat(imm8 as i16);
148 mask = v128_and(mask, bits);
149 mask = i16x8_eq(mask, bits);
150 // The swapped argument order is equivalent to mask negation.
151 v128_bitselect(b, a, mask)
152 }
153
154 // Note the optimization here of leaving row1 as the unrotated row, rather than
155 // row0. All the message loads below are adjusted to compensate for this. See
156 // discussion at https://github.com/sneves/blake2-avx2/pull/4
157 #[inline(always)]
158 fn diagonalize(row0: &mut v128, row2: &mut v128, row3: &mut v128) {
159 *row0 = shuffle_epi32::<2, 1, 0, 3>(*row0);
160 *row3 = shuffle_epi32::<1, 0, 3, 2>(*row3);
161 *row2 = shuffle_epi32::<0, 3, 2, 1>(*row2);
162 }
163
164 #[inline(always)]
165 fn undiagonalize(row0: &mut v128, row2: &mut v128, row3: &mut v128) {
166 *row0 = shuffle_epi32::<0, 3, 2, 1>(*row0);
167 *row3 = shuffle_epi32::<1, 0, 3, 2>(*row3);
168 *row2 = shuffle_epi32::<2, 1, 0, 3>(*row2);
169 }
170
171 #[inline(always)]
172 fn compress_pre(
173 cv: &CVWords,
174 block: &[u8; BLOCK_LEN],
175 block_len: u8,
176 counter: u64,
177 flags: u8,
178 ) -> [v128; 4] {
179 // safe because CVWords is [u32; 8]
180 let row0 = &mut unsafe { loadu(cv.as_ptr().add(0) as *const u8) };
181 let row1 = &mut unsafe { loadu(cv.as_ptr().add(4) as *const u8) };
182 let row2 = &mut set4(IV[0], IV[1], IV[2], IV[3]);
183 let row3 = &mut set4(
184 counter_low(counter),
185 counter_high(counter),
186 block_len as u32,
187 flags as u32,
188 );
189
190 // safe because block is &[u8; 64]
191 let mut m0 = unsafe { loadu(block.as_ptr().add(0 * 4 * DEGREE)) };
192 let mut m1 = unsafe { loadu(block.as_ptr().add(1 * 4 * DEGREE)) };
193 let mut m2 = unsafe { loadu(block.as_ptr().add(2 * 4 * DEGREE)) };
194 let mut m3 = unsafe { loadu(block.as_ptr().add(3 * 4 * DEGREE)) };
195
196 let mut t0;
197 let mut t1;
198 let mut t2;
199 let mut t3;
200 let mut tt;
201
202 // Round 1. The first round permutes the message words from the original
203 // input order, into the groups that get mixed in parallel.
204 t0 = shuffle!(m0, m1, 2, 0, 2, 0); // 6 4 2 0
205 g1(row0, row1, row2, row3, t0);
206 t1 = shuffle!(m0, m1, 3, 1, 3, 1); // 7 5 3 1
207 g2(row0, row1, row2, row3, t1);
208 diagonalize(row0, row2, row3);
209 t2 = shuffle!(m2, m3, 2, 0, 2, 0); // 14 12 10 8
210 t2 = shuffle_epi32::<2, 1, 0, 3>(t2); // 12 10 8 14
211 g1(row0, row1, row2, row3, t2);
212 t3 = shuffle!(m2, m3, 3, 1, 3, 1); // 15 13 11 9
213 t3 = shuffle_epi32::<2, 1, 0, 3>(t3); // 13 11 9 15
214 g2(row0, row1, row2, row3, t3);
215 undiagonalize(row0, row2, row3);
216 m0 = t0;
217 m1 = t1;
218 m2 = t2;
219 m3 = t3;
220
221 // Round 2. This round and all following rounds apply a fixed permutation
222 // to the message words from the round before.
223 t0 = shuffle!(m0, m1, 3, 1, 1, 2);
224 t0 = shuffle_epi32::<0, 3, 2, 1>(t0);
225 g1(row0, row1, row2, row3, t0);
226 t1 = shuffle!(m2, m3, 3, 3, 2, 2);
227 tt = shuffle_epi32::<0, 0, 3, 3>(m0);
228 t1 = blend_epi16(tt, t1, 0xCC);
229 g2(row0, row1, row2, row3, t1);
230 diagonalize(row0, row2, row3);
231 t2 = unpacklo_epi64(m3, m1);
232 tt = blend_epi16(t2, m2, 0xC0);
233 t2 = shuffle_epi32::<1, 3, 2, 0>(tt);
234 g1(row0, row1, row2, row3, t2);
235 t3 = unpackhi_epi32(m1, m3);
236 tt = unpacklo_epi32(m2, t3);
237 t3 = shuffle_epi32::<0, 1, 3, 2>(tt);
238 g2(row0, row1, row2, row3, t3);
239 undiagonalize(row0, row2, row3);
240 m0 = t0;
241 m1 = t1;
242 m2 = t2;
243 m3 = t3;
244
245 // Round 3
246 t0 = shuffle!(m0, m1, 3, 1, 1, 2);
247 t0 = shuffle_epi32::<0, 3, 2, 1>(t0);
248 g1(row0, row1, row2, row3, t0);
249 t1 = shuffle!(m2, m3, 3, 3, 2, 2);
250 tt = shuffle_epi32::<0, 0, 3, 3>(m0);
251 t1 = blend_epi16(tt, t1, 0xCC);
252 g2(row0, row1, row2, row3, t1);
253 diagonalize(row0, row2, row3);
254 t2 = unpacklo_epi64(m3, m1);
255 tt = blend_epi16(t2, m2, 0xC0);
256 t2 = shuffle_epi32::<1, 3, 2, 0>(tt);
257 g1(row0, row1, row2, row3, t2);
258 t3 = unpackhi_epi32(m1, m3);
259 tt = unpacklo_epi32(m2, t3);
260 t3 = shuffle_epi32::<0, 1, 3, 2>(tt);
261 g2(row0, row1, row2, row3, t3);
262 undiagonalize(row0, row2, row3);
263 m0 = t0;
264 m1 = t1;
265 m2 = t2;
266 m3 = t3;
267
268 // Round 4
269 t0 = shuffle!(m0, m1, 3, 1, 1, 2);
270 t0 = shuffle_epi32::<0, 3, 2, 1>(t0);
271 g1(row0, row1, row2, row3, t0);
272 t1 = shuffle!(m2, m3, 3, 3, 2, 2);
273 tt = shuffle_epi32::<0, 0, 3, 3>(m0);
274 t1 = blend_epi16(tt, t1, 0xCC);
275 g2(row0, row1, row2, row3, t1);
276 diagonalize(row0, row2, row3);
277 t2 = unpacklo_epi64(m3, m1);
278 tt = blend_epi16(t2, m2, 0xC0);
279 t2 = shuffle_epi32::<1, 3, 2, 0>(tt);
280 g1(row0, row1, row2, row3, t2);
281 t3 = unpackhi_epi32(m1, m3);
282 tt = unpacklo_epi32(m2, t3);
283 t3 = shuffle_epi32::<0, 1, 3, 2>(tt);
284 g2(row0, row1, row2, row3, t3);
285 undiagonalize(row0, row2, row3);
286 m0 = t0;
287 m1 = t1;
288 m2 = t2;
289 m3 = t3;
290
291 // Round 5
292 t0 = shuffle!(m0, m1, 3, 1, 1, 2);
293 t0 = shuffle_epi32::<0, 3, 2, 1>(t0);
294 g1(row0, row1, row2, row3, t0);
295 t1 = shuffle!(m2, m3, 3, 3, 2, 2);
296 tt = shuffle_epi32::<0, 0, 3, 3>(m0);
297 t1 = blend_epi16(tt, t1, 0xCC);
298 g2(row0, row1, row2, row3, t1);
299 diagonalize(row0, row2, row3);
300 t2 = unpacklo_epi64(m3, m1);
301 tt = blend_epi16(t2, m2, 0xC0);
302 t2 = shuffle_epi32::<1, 3, 2, 0>(tt);
303 g1(row0, row1, row2, row3, t2);
304 t3 = unpackhi_epi32(m1, m3);
305 tt = unpacklo_epi32(m2, t3);
306 t3 = shuffle_epi32::<0, 1, 3, 2>(tt);
307 g2(row0, row1, row2, row3, t3);
308 undiagonalize(row0, row2, row3);
309 m0 = t0;
310 m1 = t1;
311 m2 = t2;
312 m3 = t3;
313
314 // Round 6
315 t0 = shuffle!(m0, m1, 3, 1, 1, 2);
316 t0 = shuffle_epi32::<0, 3, 2, 1>(t0);
317 g1(row0, row1, row2, row3, t0);
318 t1 = shuffle!(m2, m3, 3, 3, 2, 2);
319 tt = shuffle_epi32::<0, 0, 3, 3>(m0);
320 t1 = blend_epi16(tt, t1, 0xCC);
321 g2(row0, row1, row2, row3, t1);
322 diagonalize(row0, row2, row3);
323 t2 = unpacklo_epi64(m3, m1);
324 tt = blend_epi16(t2, m2, 0xC0);
325 t2 = shuffle_epi32::<1, 3, 2, 0>(tt);
326 g1(row0, row1, row2, row3, t2);
327 t3 = unpackhi_epi32(m1, m3);
328 tt = unpacklo_epi32(m2, t3);
329 t3 = shuffle_epi32::<0, 1, 3, 2>(tt);
330 g2(row0, row1, row2, row3, t3);
331 undiagonalize(row0, row2, row3);
332 m0 = t0;
333 m1 = t1;
334 m2 = t2;
335 m3 = t3;
336
337 // Round 7
338 t0 = shuffle!(m0, m1, 3, 1, 1, 2);
339 t0 = shuffle_epi32::<0, 3, 2, 1>(t0);
340 g1(row0, row1, row2, row3, t0);
341 t1 = shuffle!(m2, m3, 3, 3, 2, 2);
342 tt = shuffle_epi32::<0, 0, 3, 3>(m0);
343 t1 = blend_epi16(tt, t1, 0xCC);
344 g2(row0, row1, row2, row3, t1);
345 diagonalize(row0, row2, row3);
346 t2 = unpacklo_epi64(m3, m1);
347 tt = blend_epi16(t2, m2, 0xC0);
348 t2 = shuffle_epi32::<1, 3, 2, 0>(tt);
349 g1(row0, row1, row2, row3, t2);
350 t3 = unpackhi_epi32(m1, m3);
351 tt = unpacklo_epi32(m2, t3);
352 t3 = shuffle_epi32::<0, 1, 3, 2>(tt);
353 g2(row0, row1, row2, row3, t3);
354 undiagonalize(row0, row2, row3);
355
356 [*row0, *row1, *row2, *row3]
357 }
358
359 #[target_feature(enable = "simd128")]
360 pub fn compress_in_place(
361 cv: &mut CVWords,
362 block: &[u8; BLOCK_LEN],
363 block_len: u8,
364 counter: u64,
365 flags: u8,
366 ) {
367 let [row0, row1, row2, row3] = compress_pre(cv, block, block_len, counter, flags);
368 // it stores in reversed order...
369 // safe because CVWords is [u32; 8]
370 unsafe {
371 storeu(xor(row0, row2), cv.as_mut_ptr().add(0) as *mut u8);
372 storeu(xor(row1, row3), cv.as_mut_ptr().add(4) as *mut u8);
373 }
374 }
375
376 #[target_feature(enable = "simd128")]
377 pub fn compress_xof(
378 cv: &CVWords,
379 block: &[u8; BLOCK_LEN],
380 block_len: u8,
381 counter: u64,
382 flags: u8,
383 ) -> [u8; 64] {
384 let [mut row0, mut row1, mut row2, mut row3] =
385 compress_pre(cv, block, block_len, counter, flags);
386 row0 = xor(row0, row2);
387 row1 = xor(row1, row3);
388 // safe because CVWords is [u32; 8]
389 row2 = xor(row2, unsafe { loadu(cv.as_ptr().add(0) as *const u8) });
390 row3 = xor(row3, unsafe { loadu(cv.as_ptr().add(4) as *const u8) });
391 // It seems to be architecture dependent, but works.
392 // safe because sizes match, and every state of u8 is valid.
393 unsafe { core::mem::transmute([row0, row1, row2, row3]) }
394 }
395
396 #[inline(always)]
397 fn round(v: &mut [v128; 16], m: &[v128; 16], r: usize) {
398 v[0] = add(v[0], m[MSG_SCHEDULE[r][0] as usize]);
399 v[1] = add(v[1], m[MSG_SCHEDULE[r][2] as usize]);
400 v[2] = add(v[2], m[MSG_SCHEDULE[r][4] as usize]);
401 v[3] = add(v[3], m[MSG_SCHEDULE[r][6] as usize]);
402 v[0] = add(v[0], v[4]);
403 v[1] = add(v[1], v[5]);
404 v[2] = add(v[2], v[6]);
405 v[3] = add(v[3], v[7]);
406 v[12] = xor(v[12], v[0]);
407 v[13] = xor(v[13], v[1]);
408 v[14] = xor(v[14], v[2]);
409 v[15] = xor(v[15], v[3]);
410 v[12] = rot16(v[12]);
411 v[13] = rot16(v[13]);
412 v[14] = rot16(v[14]);
413 v[15] = rot16(v[15]);
414 v[8] = add(v[8], v[12]);
415 v[9] = add(v[9], v[13]);
416 v[10] = add(v[10], v[14]);
417 v[11] = add(v[11], v[15]);
418 v[4] = xor(v[4], v[8]);
419 v[5] = xor(v[5], v[9]);
420 v[6] = xor(v[6], v[10]);
421 v[7] = xor(v[7], v[11]);
422 v[4] = rot12(v[4]);
423 v[5] = rot12(v[5]);
424 v[6] = rot12(v[6]);
425 v[7] = rot12(v[7]);
426 v[0] = add(v[0], m[MSG_SCHEDULE[r][1] as usize]);
427 v[1] = add(v[1], m[MSG_SCHEDULE[r][3] as usize]);
428 v[2] = add(v[2], m[MSG_SCHEDULE[r][5] as usize]);
429 v[3] = add(v[3], m[MSG_SCHEDULE[r][7] as usize]);
430 v[0] = add(v[0], v[4]);
431 v[1] = add(v[1], v[5]);
432 v[2] = add(v[2], v[6]);
433 v[3] = add(v[3], v[7]);
434 v[12] = xor(v[12], v[0]);
435 v[13] = xor(v[13], v[1]);
436 v[14] = xor(v[14], v[2]);
437 v[15] = xor(v[15], v[3]);
438 v[12] = rot8(v[12]);
439 v[13] = rot8(v[13]);
440 v[14] = rot8(v[14]);
441 v[15] = rot8(v[15]);
442 v[8] = add(v[8], v[12]);
443 v[9] = add(v[9], v[13]);
444 v[10] = add(v[10], v[14]);
445 v[11] = add(v[11], v[15]);
446 v[4] = xor(v[4], v[8]);
447 v[5] = xor(v[5], v[9]);
448 v[6] = xor(v[6], v[10]);
449 v[7] = xor(v[7], v[11]);
450 v[4] = rot7(v[4]);
451 v[5] = rot7(v[5]);
452 v[6] = rot7(v[6]);
453 v[7] = rot7(v[7]);
454
455 v[0] = add(v[0], m[MSG_SCHEDULE[r][8] as usize]);
456 v[1] = add(v[1], m[MSG_SCHEDULE[r][10] as usize]);
457 v[2] = add(v[2], m[MSG_SCHEDULE[r][12] as usize]);
458 v[3] = add(v[3], m[MSG_SCHEDULE[r][14] as usize]);
459 v[0] = add(v[0], v[5]);
460 v[1] = add(v[1], v[6]);
461 v[2] = add(v[2], v[7]);
462 v[3] = add(v[3], v[4]);
463 v[15] = xor(v[15], v[0]);
464 v[12] = xor(v[12], v[1]);
465 v[13] = xor(v[13], v[2]);
466 v[14] = xor(v[14], v[3]);
467 v[15] = rot16(v[15]);
468 v[12] = rot16(v[12]);
469 v[13] = rot16(v[13]);
470 v[14] = rot16(v[14]);
471 v[10] = add(v[10], v[15]);
472 v[11] = add(v[11], v[12]);
473 v[8] = add(v[8], v[13]);
474 v[9] = add(v[9], v[14]);
475 v[5] = xor(v[5], v[10]);
476 v[6] = xor(v[6], v[11]);
477 v[7] = xor(v[7], v[8]);
478 v[4] = xor(v[4], v[9]);
479 v[5] = rot12(v[5]);
480 v[6] = rot12(v[6]);
481 v[7] = rot12(v[7]);
482 v[4] = rot12(v[4]);
483 v[0] = add(v[0], m[MSG_SCHEDULE[r][9] as usize]);
484 v[1] = add(v[1], m[MSG_SCHEDULE[r][11] as usize]);
485 v[2] = add(v[2], m[MSG_SCHEDULE[r][13] as usize]);
486 v[3] = add(v[3], m[MSG_SCHEDULE[r][15] as usize]);
487 v[0] = add(v[0], v[5]);
488 v[1] = add(v[1], v[6]);
489 v[2] = add(v[2], v[7]);
490 v[3] = add(v[3], v[4]);
491 v[15] = xor(v[15], v[0]);
492 v[12] = xor(v[12], v[1]);
493 v[13] = xor(v[13], v[2]);
494 v[14] = xor(v[14], v[3]);
495 v[15] = rot8(v[15]);
496 v[12] = rot8(v[12]);
497 v[13] = rot8(v[13]);
498 v[14] = rot8(v[14]);
499 v[10] = add(v[10], v[15]);
500 v[11] = add(v[11], v[12]);
501 v[8] = add(v[8], v[13]);
502 v[9] = add(v[9], v[14]);
503 v[5] = xor(v[5], v[10]);
504 v[6] = xor(v[6], v[11]);
505 v[7] = xor(v[7], v[8]);
506 v[4] = xor(v[4], v[9]);
507 v[5] = rot7(v[5]);
508 v[6] = rot7(v[6]);
509 v[7] = rot7(v[7]);
510 v[4] = rot7(v[4]);
511 }
512
513 #[inline(always)]
514 fn transpose_vecs(vecs: &mut [v128; DEGREE]) {
515 // Interleave 32-bit lanes. The low unpack is lanes 00/11 and the high is
516 // 22/33. Note that this doesn't split the vector into two lanes, as the
517 // AVX2 counterparts do.
518 let ab_01 = unpacklo_epi32(vecs[0], vecs[1]);
519 let ab_23 = unpackhi_epi32(vecs[0], vecs[1]);
520 let cd_01 = unpacklo_epi32(vecs[2], vecs[3]);
521 let cd_23 = unpackhi_epi32(vecs[2], vecs[3]);
522
523 // Interleave 64-bit lanes.
524 let abcd_0 = unpacklo_epi64(ab_01, cd_01);
525 let abcd_1 = unpackhi_epi64(ab_01, cd_01);
526 let abcd_2 = unpacklo_epi64(ab_23, cd_23);
527 let abcd_3 = unpackhi_epi64(ab_23, cd_23);
528
529 vecs[0] = abcd_0;
530 vecs[1] = abcd_1;
531 vecs[2] = abcd_2;
532 vecs[3] = abcd_3;
533 }
534
535 #[inline(always)]
536 unsafe fn transpose_msg_vecs(inputs: &[*const u8; DEGREE], block_offset: usize) -> [v128; 16] {
537 let mut vecs = unsafe {
538 [
539 loadu(inputs[0].add(block_offset + 0 * 4 * DEGREE)),
540 loadu(inputs[1].add(block_offset + 0 * 4 * DEGREE)),
541 loadu(inputs[2].add(block_offset + 0 * 4 * DEGREE)),
542 loadu(inputs[3].add(block_offset + 0 * 4 * DEGREE)),
543 loadu(inputs[0].add(block_offset + 1 * 4 * DEGREE)),
544 loadu(inputs[1].add(block_offset + 1 * 4 * DEGREE)),
545 loadu(inputs[2].add(block_offset + 1 * 4 * DEGREE)),
546 loadu(inputs[3].add(block_offset + 1 * 4 * DEGREE)),
547 loadu(inputs[0].add(block_offset + 2 * 4 * DEGREE)),
548 loadu(inputs[1].add(block_offset + 2 * 4 * DEGREE)),
549 loadu(inputs[2].add(block_offset + 2 * 4 * DEGREE)),
550 loadu(inputs[3].add(block_offset + 2 * 4 * DEGREE)),
551 loadu(inputs[0].add(block_offset + 3 * 4 * DEGREE)),
552 loadu(inputs[1].add(block_offset + 3 * 4 * DEGREE)),
553 loadu(inputs[2].add(block_offset + 3 * 4 * DEGREE)),
554 loadu(inputs[3].add(block_offset + 3 * 4 * DEGREE)),
555 ]
556 };
557 let (square0, rest) = vecs.split_at_mut(DEGREE);
558 let (square1, rest) = rest.split_at_mut(DEGREE);
559 let (square2, square3) = rest.split_at_mut(DEGREE);
560 transpose_vecs(square0.try_into().unwrap());
561 transpose_vecs(square1.try_into().unwrap());
562 transpose_vecs(square2.try_into().unwrap());
563 transpose_vecs(square3.try_into().unwrap());
564 vecs
565 }
566
567 #[inline(always)]
568 fn load_counters(counter: u64, increment_counter: IncrementCounter) -> (v128, v128) {
569 let mask = if increment_counter.yes() { !0 } else { 0 };
570 (
571 set4(
572 counter_low(counter + (mask & 0)),
573 counter_low(counter + (mask & 1)),
574 counter_low(counter + (mask & 2)),
575 counter_low(counter + (mask & 3)),
576 ),
577 set4(
578 counter_high(counter + (mask & 0)),
579 counter_high(counter + (mask & 1)),
580 counter_high(counter + (mask & 2)),
581 counter_high(counter + (mask & 3)),
582 ),
583 )
584 }
585
586 #[target_feature(enable = "simd128")]
587 pub unsafe fn hash4(
588 inputs: &[*const u8; DEGREE],
589 blocks: usize,
590 key: &CVWords,
591 counter: u64,
592 increment_counter: IncrementCounter,
593 flags: u8,
594 flags_start: u8,
595 flags_end: u8,
596 out: &mut [u8; DEGREE * OUT_LEN],
597 ) {
598 let mut h_vecs = [
599 set1(key[0]),
600 set1(key[1]),
601 set1(key[2]),
602 set1(key[3]),
603 set1(key[4]),
604 set1(key[5]),
605 set1(key[6]),
606 set1(key[7]),
607 ];
608 let (counter_low_vec, counter_high_vec) = load_counters(counter, increment_counter);
609 let mut block_flags = flags | flags_start;
610
611 for block in 0..blocks {
612 if block + 1 == blocks {
613 block_flags |= flags_end;
614 }
615 let block_len_vec = set1(BLOCK_LEN as u32); // full blocks only
616 let block_flags_vec = set1(block_flags as u32);
617 let msg_vecs = unsafe { transpose_msg_vecs(inputs, block * BLOCK_LEN) };
618
619 // The transposed compression function. Note that inlining this
620 // manually here improves compile times by a lot, compared to factoring
621 // it out into its own function and making it #[inline(always)]. Just
622 // guessing, it might have something to do with loop unrolling.
623 let mut v = [
624 h_vecs[0],
625 h_vecs[1],
626 h_vecs[2],
627 h_vecs[3],
628 h_vecs[4],
629 h_vecs[5],
630 h_vecs[6],
631 h_vecs[7],
632 set1(IV[0]),
633 set1(IV[1]),
634 set1(IV[2]),
635 set1(IV[3]),
636 counter_low_vec,
637 counter_high_vec,
638 block_len_vec,
639 block_flags_vec,
640 ];
641 round(&mut v, &msg_vecs, 0);
642 round(&mut v, &msg_vecs, 1);
643 round(&mut v, &msg_vecs, 2);
644 round(&mut v, &msg_vecs, 3);
645 round(&mut v, &msg_vecs, 4);
646 round(&mut v, &msg_vecs, 5);
647 round(&mut v, &msg_vecs, 6);
648 h_vecs[0] = xor(v[0], v[8]);
649 h_vecs[1] = xor(v[1], v[9]);
650 h_vecs[2] = xor(v[2], v[10]);
651 h_vecs[3] = xor(v[3], v[11]);
652 h_vecs[4] = xor(v[4], v[12]);
653 h_vecs[5] = xor(v[5], v[13]);
654 h_vecs[6] = xor(v[6], v[14]);
655 h_vecs[7] = xor(v[7], v[15]);
656
657 block_flags = flags;
658 }
659
660 let (square0, square1) = h_vecs.split_at_mut(DEGREE);
661 transpose_vecs(square0.try_into().unwrap());
662 transpose_vecs(square1.try_into().unwrap());
663 // The first four vecs now contain the first half of each output, and the
664 // second four vecs contain the second half of each output.
665 unsafe {
666 storeu(h_vecs[0], out.as_mut_ptr().add(0 * 4 * DEGREE));
667 storeu(h_vecs[4], out.as_mut_ptr().add(1 * 4 * DEGREE));
668 storeu(h_vecs[1], out.as_mut_ptr().add(2 * 4 * DEGREE));
669 storeu(h_vecs[5], out.as_mut_ptr().add(3 * 4 * DEGREE));
670 storeu(h_vecs[2], out.as_mut_ptr().add(4 * 4 * DEGREE));
671 storeu(h_vecs[6], out.as_mut_ptr().add(5 * 4 * DEGREE));
672 storeu(h_vecs[3], out.as_mut_ptr().add(6 * 4 * DEGREE));
673 storeu(h_vecs[7], out.as_mut_ptr().add(7 * 4 * DEGREE));
674 }
675 }
676
677 #[target_feature(enable = "simd128")]
678 unsafe fn hash1<const N: usize>(
679 input: &[u8; N],
680 key: &CVWords,
681 counter: u64,
682 flags: u8,
683 flags_start: u8,
684 flags_end: u8,
685 out: &mut CVBytes,
686 ) {
687 debug_assert_eq!(N % BLOCK_LEN, 0, "uneven blocks");
688 let mut cv = *key;
689 let mut block_flags = flags | flags_start;
690 let mut slice = &input[..];
691 while slice.len() >= BLOCK_LEN {
692 if slice.len() == BLOCK_LEN {
693 block_flags |= flags_end;
694 }
695 compress_in_place(
696 &mut cv,
697 (&slice[..BLOCK_LEN]).try_into().unwrap(),
698 BLOCK_LEN as u8,
699 counter,
700 block_flags,
701 );
702 block_flags = flags;
703 slice = &slice[BLOCK_LEN..];
704 }
705 *out = unsafe { core::mem::transmute(cv) };
706 }
707
708 #[target_feature(enable = "simd128")]
709 pub unsafe fn hash_many<const N: usize>(
710 mut inputs: &[&[u8; N]],
711 key: &CVWords,
712 mut counter: u64,
713 increment_counter: IncrementCounter,
714 flags: u8,
715 flags_start: u8,
716 flags_end: u8,
717 mut out: &mut [u8],
718 ) {
719 debug_assert!(out.len() >= inputs.len() * OUT_LEN, "out too short");
720 while inputs.len() >= DEGREE && out.len() >= DEGREE * OUT_LEN {
721 // Safe because the layout of arrays is guaranteed, and because the
722 // `blocks` count is determined statically from the argument type.
723 let input_ptrs: &[*const u8; DEGREE] =
724 unsafe { &*(inputs.as_ptr() as *const [*const u8; DEGREE]) };
725 let blocks = N / BLOCK_LEN;
726 unsafe {
727 hash4(
728 input_ptrs,
729 blocks,
730 key,
731 counter,
732 increment_counter,
733 flags,
734 flags_start,
735 flags_end,
736 (&mut out[..DEGREE * OUT_LEN]).try_into().unwrap(),
737 );
738 }
739 if increment_counter.yes() {
740 counter += DEGREE as u64;
741 }
742 inputs = &inputs[DEGREE..];
743 out = &mut out[DEGREE * OUT_LEN..];
744 }
745 for (&input, output) in inputs.iter().zip(out.chunks_exact_mut(OUT_LEN)) {
746 unsafe {
747 hash1(
748 input,
749 key,
750 counter,
751 flags,
752 flags_start,
753 flags_end,
754 (&mut output[..OUT_LEN]).try_into().unwrap(),
755 );
756 }
757 if increment_counter.yes() {
758 counter += 1;
759 }
760 }
761 }
762
763 #[cfg(test)]
764 mod test {
765 use super::*;
766
767 #[test]
768 fn test_transpose() {
769 #[target_feature(enable = "simd128")]
770 fn transpose_wrapper(vecs: &mut [v128; DEGREE]) {
771 transpose_vecs(vecs);
772 }
773
774 let mut matrix = [[0 as u32; DEGREE]; DEGREE];
775 for i in 0..DEGREE {
776 for j in 0..DEGREE {
777 matrix[i][j] = (i * DEGREE + j) as u32;
778 }
779 }
780
781 unsafe {
782 let mut vecs: [v128; DEGREE] = core::mem::transmute(matrix);
783 transpose_wrapper(&mut vecs);
784 matrix = core::mem::transmute(vecs);
785 }
786
787 for i in 0..DEGREE {
788 for j in 0..DEGREE {
789 // Reversed indexes from above.
790 assert_eq!(matrix[j][i], (i * DEGREE + j) as u32);
791 }
792 }
793 }
794
795 #[test]
796 fn test_compress() {
797 crate::test::test_compress_fn(compress_in_place, compress_xof);
798 }
799
800 #[test]
801 fn test_hash_many() {
802 crate::test::test_hash_many_fn(hash_many, hash_many);
803 }
804 }