1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! x86-64 kernels of the long path, one module per vector width, all equal to `spec`.
//! AVX2 and AVX-512 keep the 64-word state in registers, in the one-shot functions and
//! in `absorb`, which serves `Stream` over caller-owned state; SSE2 keeps it in a 64-byte
//! aligned array.
use super::spec::{fold, Words, BLOCK, CHAINS, CT, QT, S, S2L, STRIPE};
use core::arch::x86_64::*;
macro_rules! width {
($m:ident, $feat:literal, $reg:expr, $smallm:ident, $small_max:expr, $mid_max:expr, $v:ty, $W:expr, $loadu:ident, $storeu:ident, $storem:ident, $xor:ident, $add:ident, $mul:ident, $srl32:ident, $set1:ident) => {
pub mod $m {
use super::*;
/// Registers per 64-byte stripe.
const R: usize = 64 / $W;
/// 64-bit words per register.
const WPR: usize = $W / 8;
/// Up to this many leading stripes the one-shot functions use `small`.
const SMALL_MAX: usize = $small_max;
/// Up to this many they use the `chained` of the module that provides `small`.
const MID_MAX: usize = $mid_max;
/// Without the state in registers: up to this many whole blocks run two chains
/// at a time from start to merge.
#[allow(dead_code)]
const FUSE_MAX: usize = 4;
/// `x = a ^ w; x + lo32(x) * hi32(x)` on one register.
#[inline(always)]
unsafe fn step(a: $v, w: $v) -> $v {
let x = $xor(a, w);
$add(x, $mul(x, $srl32(x)))
}
#[inline(always)]
unsafe fn ld(p: *const u64, r: usize) -> $v {
$loadu(p.add(r * WPR) as *const $v)
}
#[inline(always)]
unsafe fn ldb(p: *const u8, r: usize) -> $v {
$loadu(p.add(r * $W) as *const $v)
}
/// Chain `c`'s start state `CT + ks`, register `r`.
#[inline(always)]
unsafe fn start(c: usize, r: usize, ksv: $v) -> $v {
$add(ld(CT.as_ptr().add(8 * c), r), ksv)
}
/// Chain `c`'s merge constants, register `r`. Loops, never
/// `core::array::from_fn`, in this module: a closure does not inherit the
/// function's target features, so without AVX in the build its intrinsics go
/// through memory (+360 cycles per call).
#[inline(always)]
unsafe fn q(c: usize, r: usize) -> $v {
ld(QT.as_ptr().add(8 * c), r)
}
/// The folds of `spec::finish_lanes` from the merged lanes in registers. The
/// XOR with the secrets is done on the vectors, in lane order, so the eight
/// words leave through plain extractions: with the XOR on the scalar side the
/// compiler vectorized it in multiply order and paid in shuffles (16 cycles
/// on the path at 128 B). No memory in between either: a 512-bit store is not
/// forwarded to the 64-bit loads that follow (+10 cycles).
#[inline(always)]
unsafe fn fin<const HI: bool>(mv: &[$v; R], ks: u64, n: usize) -> u128 {
let mut x = [0u64; 8];
for r in 0..R {
$storem(&mut x[r * WPR..r * WPR + WPR], $xor(mv[r], ld(S.as_ptr(), r)));
}
let h = fold(x[0], x[1]) ^ fold(x[2], x[3]) ^ fold(x[4], x[5]) ^ fold(x[6], x[7]);
let lo = fold(h ^ n as u64, ks ^ S[8]);
if !HI {
return lo as u128;
}
let mut y = [0u64; 8];
for r in 0..R {
$storem(&mut y[r * WPR..r * WPR + WPR], $xor(mv[r], ld(S2L.as_ptr(), r)));
}
let h2 = fold(y[0], y[2]) ^ fold(y[1], y[3]) ^ fold(y[4], y[6]) ^ fold(y[5], y[7]);
let hi = fold(h2 ^ n as u64, ks ^ S[18]);
lo as u128 | (hi as u128) << 64
}
/// `m <= SMALL_MAX` leading stripes: stripe step and merge step per chain,
/// no state. Latency-bound, so the AVX-512 backend uses the AVX2 module's
/// copy: two ymm chains in flight beat one double-pumped zmm on the test CPU.
#[inline(always)]
#[allow(dead_code)] // unused in the avx512 module itself
pub(super) unsafe fn small<const HI: bool>(p: *const u8, n: usize, m: usize, ks: u64) -> u128 {
let (ksv, nv) = ($set1(ks as i64), $set1(n as i64));
let mut mv = [$set1(0); R];
for s in 0..m {
for r in 0..R {
mv[r] = $xor(mv[r], step(step(start(s, r, ksv), ldb(p.add(s * 64), r)), q(s, r)));
}
}
for r in 0..R {
mv[r] = $xor(mv[r], step(step(start(m, r, ksv), $xor(ldb(p.add(n - 64), r), nv)), q(m, r)));
}
fin::<HI>(&mv, ks, n)
}
/// With the chain states in registers: the `rem < 8` leading stripes at
/// `lead`, the closing stripe at `closing` into chain `m mod 8`, the merge
/// over the chains that received data.
#[inline(always)]
#[allow(dead_code)] // the SSE2 module keeps its state in memory
unsafe fn close(a: &mut [[$v; R]; CHAINS], lead: *const u8, rem: usize, closing: *const u8, m: usize, n: usize) -> [$v; R] {
for c in 0..CHAINS {
if c < rem {
for r in 0..R {
a[c][r] = step(a[c][r], ldb(lead.add(c * 64), r));
}
}
}
let nv = $set1(n as i64);
for c in 0..CHAINS {
if c == m % CHAINS {
for r in 0..R {
a[c][r] = step(a[c][r], $xor(ldb(closing, r), nv));
}
}
}
let used = (m + 1).min(CHAINS);
let mut mv = [$set1(0); R];
for c in 0..CHAINS {
if c < used {
for r in 0..R {
mv[r] = $xor(mv[r], step(a[c][r], q(c, r)));
}
}
}
mv
}
/// The memory form of `close` over a 64-word state at `st`.
#[inline(always)]
unsafe fn close_mem(st: *mut u64, lead: *const u8, rem: usize, closing: *const u8, m: usize, n: usize) -> [$v; R] {
for c in 0..rem {
let sp = st.add(8 * c);
for r in 0..R {
$storeu(sp.add(r * WPR) as *mut $v, step(ld(sp, r), ldb(lead.add(c * 64), r)));
}
}
let nv = $set1(n as i64);
let sp = st.add(8 * (m % CHAINS));
for r in 0..R {
$storeu(sp.add(r * WPR) as *mut $v, step(ld(sp, r), $xor(ldb(closing, r), nv)));
}
let used = (m + 1).min(CHAINS);
let mut mv = [$set1(0); R];
for c in 0..used {
for r in 0..R {
mv[r] = $xor(mv[r], step(ld(st.add(8 * c), r), q(c, r)));
}
}
mv
}
/// The hash of `n >= 65` bytes at `p`, inlined into `hash64` / `hash128` so
/// the merged lanes reach the folds in registers. Three regimes by the number
/// of leading stripes: `small`, then the chained form of the narrower module
/// (on the test CPU ymm chains give the throughput of zmm and 11 cycles less latency
/// up to 448 B), then this module's own chained form.
#[inline(always)]
unsafe fn hash<const HI: bool>(p: *const u8, n: usize, ks: u64) -> u128 {
debug_assert!(n >= 65);
let m = (n - 1) / STRIPE;
if m <= SMALL_MAX {
return super::$smallm::small::<HI>(p, n, m, ks);
}
if m <= MID_MAX {
return super::$smallm::chained::<HI>(p, n, m, ks);
}
chained::<HI>(p, n, m, ks)
}
/// All eight chains started, whole blocks absorbed, then `close`.
#[inline(always)]
pub(super) unsafe fn chained<const HI: bool>(p: *const u8, n: usize, m: usize, ks: u64) -> u128 {
let ksv = $set1(ks as i64);
let mv = if $reg {
let mut a = [[$set1(0); R]; CHAINS];
for c in 0..CHAINS {
for r in 0..R {
a[c][r] = start(c, r, ksv);
}
}
let mut s = 0;
while s + CHAINS <= m {
for c in 0..CHAINS {
for r in 0..R {
a[c][r] = step(a[c][r], ldb(p.add((s + c) * 64), r));
}
}
s += CHAINS;
}
close(&mut a, p.add(s * 64), m - s, p.add(n - 64), m, n)
} else {
// The state does not fit in registers.
let nb = m / CHAINS;
let rem = m - nb * CHAINS;
if nb <= FUSE_MAX {
// A few blocks: chains are independent until the merge (chain c
// sees only stripes c mod 8), so two chains at a time run from
// start to merge with their lanes in registers and no state
// array. One chain at a time would be latency bound (four
// registers in flight), and the state array costs 64 words of
// stores and loads at each end: 513 B took 167 cycles against
// 111 for 512.
let nv = $set1(n as i64);
let mut mv = [$set1(0); R];
let mut c = 0;
while c < CHAINS {
let mut a = [[$set1(0); R]; 2];
for k in 0..2 {
for r in 0..R {
a[k][r] = start(c + k, r, ksv);
}
}
for b in 0..nb {
for k in 0..2 {
for r in 0..R {
a[k][r] = step(a[k][r], ldb(p.add((b * CHAINS + c + k) * 64), r));
}
}
}
for k in 0..2 {
if c + k < rem {
for r in 0..R {
a[k][r] = step(a[k][r], ldb(p.add((nb * CHAINS + c + k) * 64), r));
}
}
if c + k == rem {
for r in 0..R {
a[k][r] = step(a[k][r], $xor(ldb(p.add(n - 64), r), nv));
}
}
for r in 0..R {
mv[r] = $xor(mv[r], step(a[k][r], q(c + k, r)));
}
}
c += 2;
}
mv
} else {
let mut st = Words([0u64; 64]);
let st = st.0.as_mut_ptr();
for c in 0..CHAINS {
for r in 0..R {
$storeu(st.add(8 * c + r * WPR) as *mut $v, start(c, r, ksv));
}
}
let mut s = 0;
while s + CHAINS <= m {
for c in 0..CHAINS {
let sp = st.add(8 * c);
for r in 0..R {
$storeu(sp.add(r * WPR) as *mut $v, step(ld(sp, r), ldb(p.add((s + c) * 64), r)));
}
}
s += CHAINS;
}
close_mem(st, p.add(s * 64), m - s, p.add(n - 64), m, n)
}
};
fin::<HI>(&mv, ks, n)
}
/// 64-bit hash of `n >= 65` bytes at `p` (`ks` from `spec::ks`).
#[target_feature(enable = $feat)]
pub unsafe fn hash64(p: *const u8, n: usize, ks: u64) -> u64 {
hash::<false>(p, n, ks) as u64
}
/// 128-bit hash of `n >= 65` bytes at `p`.
#[target_feature(enable = $feat)]
pub unsafe fn hash128(p: *const u8, n: usize, ks: u64) -> u128 {
hash::<true>(p, n, ks)
}
/// Streaming: absorb `nblocks` whole leading blocks at `p` into `st`.
#[target_feature(enable = $feat)]
pub unsafe fn absorb(st: &mut Words, p: *const u8, nblocks: usize) {
let st = st.0.as_mut_ptr();
if $reg {
// state in registers for the whole call: with a load and a store per
// stripe, store forwarding sits on every chain's dependency path
let mut a = [[$set1(0); R]; CHAINS];
for c in 0..CHAINS {
for r in 0..R {
a[c][r] = ld(st.add(8 * c), r);
}
}
for b in 0..nblocks {
for c in 0..CHAINS {
for r in 0..R {
a[c][r] = step(a[c][r], ldb(p.add(b * BLOCK + c * 64), r));
}
}
}
for c in 0..CHAINS {
for r in 0..R {
$storeu(st.add(8 * c + r * WPR) as *mut $v, a[c][r]);
}
}
} else {
for b in 0..nblocks {
for c in 0..CHAINS {
let sp = st.add(8 * c);
for r in 0..R {
$storeu(sp.add(r * WPR) as *mut $v, step(ld(sp, r), ldb(p.add(b * BLOCK + c * 64), r)));
}
}
}
}
}
/// Streaming: the 128-bit hash from `st`, the `r < 8` remaining leading stripes
/// at `leading` and the closing stripe at `closing` (`spec::finish`).
#[target_feature(enable = $feat)]
pub unsafe fn finish(st: &Words, leading: *const u8, r: usize, closing: *const u8, n: usize, ks: u64) -> u128 {
let m = (n - 1) / STRIPE;
let mut w = Words(st.0);
let mv = close_mem(w.0.as_mut_ptr(), leading, r, closing, m, n);
fin::<true>(&mv, ks, n)
}
}
};
}
#[inline(always)]
unsafe fn storem_128(out: &mut [u64], v: __m128i) {
// SSE2 only: `_mm_extract_epi64` would be SSE4.1
out[0] = _mm_cvtsi128_si64(v) as u64;
out[1] = _mm_cvtsi128_si64(_mm_unpackhi_epi64(v, v)) as u64;
}
/// Both words of an xmm register to general registers, as two instructions the compiler
/// cannot regroup: left to itself it gathers even and odd words with `vpunpck` and
/// `vpermq` first, on the critical path of every hash under 513 bytes.
#[inline(always)]
unsafe fn storem_128v(out: &mut [u64], v: __m128i) {
#[cfg(not(any(miri, kani)))]
{
let (a, b): (u64, u64);
core::arch::asm!("vmovq {a}, {v}", "vpextrq {b}, {v}, 1", v = in(xmm_reg) v, a = out(reg) a, b = out(reg) b, options(pure, nomem, nostack, preserves_flags));
out[0] = a;
out[1] = b;
}
#[cfg(any(miri, kani))]
storem_128(out, v);
}
#[inline(always)]
unsafe fn storem_256(out: &mut [u64], v: __m256i) {
storem_128v(&mut out[..2], _mm256_castsi256_si128(v));
storem_128v(&mut out[2..], _mm256_extracti128_si256::<1>(v));
}
#[inline(always)]
unsafe fn storem_512(out: &mut [u64], v: __m512i) {
storem_256(&mut out[..4], _mm512_castsi512_si256(v));
storem_256(&mut out[4..], _mm512_extracti64x4_epi64::<1>(v));
}
#[inline(always)]
unsafe fn srl32_128(x: __m128i) -> __m128i {
_mm_srli_epi64::<32>(x)
}
#[inline(always)]
unsafe fn srl32_256(x: __m256i) -> __m256i {
_mm256_srli_epi64::<32>(x)
}
#[inline(always)]
unsafe fn srl32_512(x: __m512i) -> __m512i {
_mm512_srli_epi64::<32>(x)
}
// BMI2 with the AVX kernels: the inlined folds become `mulx`, without the register
// shuffling around `mul`. Every CPU with AVX2 has it; the dispatcher checks both.
width!(sse2, "sse2", false, sse2, 7, 0, __m128i, 16, _mm_loadu_si128, _mm_storeu_si128, storem_128, _mm_xor_si128, _mm_add_epi64, _mm_mul_epu32, srl32_128, _mm_set1_epi64x);
width!(avx2, "avx2,bmi2", true, avx2, 3, 0, __m256i, 32, _mm256_loadu_si256, _mm256_storeu_si256, storem_256, _mm256_xor_si256, _mm256_add_epi64, _mm256_mul_epu32, srl32_256, _mm256_set1_epi64x);
width!(avx512, "avx512f,avx512vl,avx2,bmi2", true, avx2, 3, 6, __m512i, 64, _mm512_loadu_si512, _mm512_storeu_si512, storem_512, _mm512_xor_si512, _mm512_add_epi64, _mm512_mul_epu32, srl32_512, _mm512_set1_epi64);