Skip to content

Commit

Permalink
Use full hash bytes and use fixed endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Jul 15, 2024
1 parent 38b97bc commit 311050d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
15 changes: 10 additions & 5 deletions compiler/rustc_data_structures/src/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,16 @@ impl FromStableHash for Fingerprint {

#[inline]
fn from(hash: Self::Hash) -> Self {
let bytes = hash.as_bytes();
Fingerprint(
u64::from_ne_bytes(bytes[0..8].try_into().unwrap()),
u64::from_ne_bytes(bytes[8..16].try_into().unwrap()),
)
let bytes: &[u8; 32] = hash.as_bytes();

let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
Fingerprint(p0.wrapping_mul(3).wrapping_add(p1), p2.wrapping_mul(3).wrapping_add(p3))
}
}

Expand Down
27 changes: 25 additions & 2 deletions compiler/rustc_data_structures/src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ impl FromStableHash for Hash64 {
#[inline]
fn from(hash: Self::Hash) -> Self {
let bytes = hash.as_bytes();
Self { inner: u64::from_ne_bytes(bytes[0..8].try_into().unwrap()) }

let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
let m0 = p0.wrapping_mul(3).wrapping_add(p1);
let m1 = p2.wrapping_mul(3).wrapping_add(p3);
let h = m0.wrapping_mul(3).wrapping_add(m1);

Self { inner: h }
}
}

Expand Down Expand Up @@ -130,7 +142,18 @@ impl FromStableHash for Hash128 {
#[inline]
fn from(hash: Self::Hash) -> Self {
let bytes = hash.as_bytes();
Self { inner: u128::from_ne_bytes(bytes[0..16].try_into().unwrap()) }

let p0 = u64::from_le_bytes(bytes[0..8].try_into().unwrap());
let p1 = u64::from_le_bytes(bytes[8..16].try_into().unwrap());
let p2 = u64::from_le_bytes(bytes[16..24].try_into().unwrap());
let p3 = u64::from_le_bytes(bytes[24..32].try_into().unwrap());

// See https://stackoverflow.com/a/27952689 on why this function is
// implemented this way.
let upper = p0.wrapping_mul(3).wrapping_add(p1);
let lower = p2.wrapping_mul(3).wrapping_add(p3);

Self { inner: u128::from(lower) | (u128::from(upper) << 64) }
}
}

Expand Down

0 comments on commit 311050d

Please sign in to comment.