Skip to content

Commit

Permalink
ne -> le
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Mar 24, 2024
1 parent e51957a commit 24ffc8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
27 changes: 14 additions & 13 deletions src/xxh32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn finalize(mut input: u32, data: &[u8], is_aligned: bool) -> u32 {
let (chunks, remainder) = slice_chunks::<4>(data);
for chunk in chunks {
input = input.wrapping_add(
u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]).wrapping_mul(PRIME_3)
u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]).to_le().wrapping_mul(PRIME_3)
);
input = input.rotate_left(17).wrapping_mul(PRIME_4);
}
Expand Down Expand Up @@ -53,6 +53,17 @@ const fn init_v(seed: u32) -> (u32, u32, u32, u32) {
)
}

macro_rules! round_loop {
($input:ident => $($v:tt)+) => {
for chunk in $input {
$($v)+.0 = round($($v)+.0, u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]).to_le());
$($v)+.1 = round($($v)+.1, u32::from_ne_bytes([chunk[4], chunk[5], chunk[6], chunk[7]]).to_le());
$($v)+.2 = round($($v)+.2, u32::from_ne_bytes([chunk[8], chunk[9], chunk[10], chunk[11]]).to_le());
$($v)+.3 = round($($v)+.3, u32::from_ne_bytes([chunk[12], chunk[13], chunk[14], chunk[15]]).to_le());
}
}
}

///Returns hash for the provided input
pub fn xxh32(mut input: &[u8], seed: u32) -> u32 {
let mut result = input.len() as u32;
Expand All @@ -62,12 +73,7 @@ pub fn xxh32(mut input: &[u8], seed: u32) -> u32 {

let (chunks, remainder) = slice_chunks::<CHUNK_SIZE>(input);

for chunk in chunks {
v.0 = round(v.0, u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
v.1 = round(v.1, u32::from_ne_bytes([chunk[4], chunk[5], chunk[6], chunk[7]]));
v.2 = round(v.2, u32::from_ne_bytes([chunk[8], chunk[9], chunk[10], chunk[11]]));
v.3 = round(v.3, u32::from_ne_bytes([chunk[12], chunk[13], chunk[14], chunk[15]]));
}
round_loop!(chunks => v);
input = remainder;

result = result.wrapping_add(
Expand Down Expand Up @@ -141,12 +147,7 @@ impl Xxh32 {
}

let (chunks, remainder) = slice_chunks::<CHUNK_SIZE>(input);
for chunk in chunks {
self.v.0 = round(self.v.0, u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
self.v.1 = round(self.v.1, u32::from_ne_bytes([chunk[4], chunk[5], chunk[6], chunk[7]]));
self.v.2 = round(self.v.2, u32::from_ne_bytes([chunk[8], chunk[9], chunk[10], chunk[11]]));
self.v.3 = round(self.v.3, u32::from_ne_bytes([chunk[12], chunk[13], chunk[14], chunk[15]]));
}
round_loop!(chunks => self.v);

if remainder.len() > 0 {
unsafe {
Expand Down
8 changes: 4 additions & 4 deletions src/xxh64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ const fn init_v(seed: u64) -> (u64, u64, u64, u64) {
macro_rules! round_loop {
($input:ident => $($v:tt)+) => {
for chunk in $input {
$($v)+.0 = round($($v)+.0, u64::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]]));
$($v)+.1 = round($($v)+.1, u64::from_ne_bytes([chunk[8], chunk[9], chunk[10], chunk[11], chunk[12], chunk[13], chunk[14], chunk[15]]));
$($v)+.2 = round($($v)+.2, u64::from_ne_bytes([chunk[16], chunk[17], chunk[18], chunk[19], chunk[20], chunk[21], chunk[22], chunk[23]]));
$($v)+.3 = round($($v)+.3, u64::from_ne_bytes([chunk[24], chunk[25], chunk[26], chunk[27], chunk[28], chunk[29], chunk[30], chunk[31]]));
$($v)+.0 = round($($v)+.0, u64::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]]).to_le());
$($v)+.1 = round($($v)+.1, u64::from_ne_bytes([chunk[8], chunk[9], chunk[10], chunk[11], chunk[12], chunk[13], chunk[14], chunk[15]]).to_le());
$($v)+.2 = round($($v)+.2, u64::from_ne_bytes([chunk[16], chunk[17], chunk[18], chunk[19], chunk[20], chunk[21], chunk[22], chunk[23]]).to_le());
$($v)+.3 = round($($v)+.3, u64::from_ne_bytes([chunk[24], chunk[25], chunk[26], chunk[27], chunk[28], chunk[29], chunk[30], chunk[31]]).to_le());
}
}
}
Expand Down

0 comments on commit 24ffc8f

Please sign in to comment.