Skip to content

Commit

Permalink
Extract common code from the 17 to 128 bytes implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed Nov 1, 2024
1 parent 4c2d379 commit 53cab0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
28 changes: 28 additions & 0 deletions src/xxhash3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,34 @@ pub fn impl_1_to_3_bytes_combined(input: &[u8]) -> u32 {
| input[input.len() >> 1].into_u32() << 24
}

#[inline]
pub fn impl_17_to_128_bytes_iter(
secret: &Secret,
input: &[u8],
mut f: impl FnMut(&[u8; 16], &[u8; 16], &[[u8; 16]; 2]),
) {
let secret = secret.words_for_17_to_128();
let (secret, _) = secret.bp_as_chunks::<2>();
let (fwd, _) = input.bp_as_chunks();
let (_, bwd) = input.bp_as_rchunks();

let q = bwd.len();

if input.len() > 32 {
if input.len() > 64 {
if input.len() > 96 {
f(&fwd[3], &bwd[q - 4], &secret[3]);
}

f(&fwd[2], &bwd[q - 3], &secret[2]);
}

f(&fwd[1], &bwd[q - 2], &secret[1]);
}

f(&fwd[0], &bwd[q - 1], &secret[0]);
}

#[inline]
pub fn mix_step(data: &[u8; 16], secret: &[u8; 16], seed: u64) -> u64 {
let data_words = to_u64s(data);
Expand Down
28 changes: 4 additions & 24 deletions src/xxhash3_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,30 +296,10 @@ fn impl_17_to_128_bytes(secret: &Secret, seed: u64, input: &[u8]) -> u64 {
assert_input_range!(17..=128, input.len());
let mut acc = input.len().into_u64().wrapping_mul(PRIME64_1);

let secret = secret.words_for_17_to_128();
let (secret, _) = secret.bp_as_chunks::<2>();
let (fwd, _) = input.bp_as_chunks();
let (_, bwd) = input.bp_as_rchunks();

let q = bwd.len();

if input.len() > 32 {
if input.len() > 64 {
if input.len() > 96 {
acc = acc.wrapping_add(mix_step(&fwd[3], &secret[3][0], seed));
acc = acc.wrapping_add(mix_step(&bwd[q - 4], &secret[3][1], seed));
}

acc = acc.wrapping_add(mix_step(&fwd[2], &secret[2][0], seed));
acc = acc.wrapping_add(mix_step(&bwd[q - 3], &secret[2][1], seed));
}

acc = acc.wrapping_add(mix_step(&fwd[1], &secret[1][0], seed));
acc = acc.wrapping_add(mix_step(&bwd[q - 2], &secret[1][1], seed));
}

acc = acc.wrapping_add(mix_step(&fwd[0], &secret[0][0], seed));
acc = acc.wrapping_add(mix_step(&bwd[q - 1], &secret[0][1], seed));
impl_17_to_128_bytes_iter(secret, input, |fwd, bwd, secret| {
acc = acc.wrapping_add(mix_step(fwd, &secret[0], seed));
acc = acc.wrapping_add(mix_step(bwd, &secret[1], seed));
});

avalanche(acc)
}
Expand Down

0 comments on commit 53cab0d

Please sign in to comment.