Skip to content

Commit

Permalink
Assert in slice16 hot loop to avoid bounds checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
khrs committed Feb 22, 2023
1 parent a586f07 commit 007687a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/crc128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const fn update_slice16(
let len = bytes.len();
if reflect {
while i + 16 < len {
assert!((i + 15) < len);

let current0 = bytes[i] ^ crc as u8;
let current1 = bytes[i + 1] ^ (crc >> 8) as u8;
let current2 = bytes[i + 2] ^ (crc >> 16) as u8;
Expand Down Expand Up @@ -122,6 +124,8 @@ const fn update_slice16(
}
} else {
while i + 16 < len {
assert!((i + 15) < len);

let current0 = bytes[i] ^ (crc >> 120) as u8;
let current1 = bytes[i + 1] ^ (crc >> 112) as u8;
let current2 = bytes[i + 2] ^ (crc >> 104) as u8;
Expand Down
4 changes: 4 additions & 0 deletions src/crc16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ const fn update_slice16(
let mut i = 0;
if reflect {
while i + 16 < len {
assert!((i + 15) < len);

let current0 = bytes[i] ^ (crc as u8);
let current1 = bytes[i + 1] ^ ((crc >> 8) as u8);

Expand Down Expand Up @@ -107,6 +109,8 @@ const fn update_slice16(
}
} else {
while i + 16 < len {
assert!((i + 15) < len);

let current0 = bytes[i] ^ ((crc >> 8) as u8);
let current1 = bytes[i + 1] ^ (crc as u8);

Expand Down
13 changes: 9 additions & 4 deletions src/crc32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ const fn update_slice16(
bytes: &[u8],
) -> u32 {
let mut i = 0;
let len = bytes.len();
if reflect {
while i + 16 <= bytes.len() {
while i + 16 <= len {
assert!((i + 15) < len);

let mut current_slice = [bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]];

current_slice[0] ^= crc as u8;
Expand Down Expand Up @@ -106,13 +109,15 @@ const fn update_slice16(
}

// Last few bytes
while i < bytes.len() {
while i < len{
let table_index = ((crc ^ bytes[i] as u32) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc >> 8);
i += 1;
}
} else {
while i + 16 <= bytes.len() {
while i + 16 <= len {
assert!((i + 15) < len);

let mut current_slice = [bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]];

current_slice[0] ^= (crc >> 24) as u8;
Expand Down Expand Up @@ -141,7 +146,7 @@ const fn update_slice16(
}

// Last few bytes
while i < bytes.len() {
while i < len {
let table_index = (((crc >> 24) ^ bytes[i] as u32) & 0xFF) as usize;
crc = table[0][table_index] ^ (crc << 8);
i += 1;
Expand Down
4 changes: 4 additions & 0 deletions src/crc64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const fn update_slice16(
let len = bytes.len();
if reflect {
while i + 16 < len {
assert!((i + 15) < len);

let current0 = bytes[i] ^ crc as u8;
let current1 = bytes[i + 1] ^ (crc >> 8) as u8;
let current2 = bytes[i + 2] ^ (crc >> 16) as u8;
Expand Down Expand Up @@ -115,6 +117,8 @@ const fn update_slice16(
}
} else {
while i + 16 < len {
assert!((i + 15) < len);

let current0 = bytes[i] ^ (crc >> 56) as u8;
let current1 = bytes[i + 1] ^ (crc >> 48) as u8;
let current2 = bytes[i + 2] ^ (crc >> 40) as u8;
Expand Down
2 changes: 2 additions & 0 deletions src/crc8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const fn update_slice16(mut crc: u8, table: &[[u8; 256]; 16], bytes: &[u8]) -> u
let mut i = 0;

while i + 16 < len {
assert!((i + 15) < len);

crc = table[0][bytes[i + 15] as usize]
^ table[1][bytes[i + 14] as usize]
^ table[2][bytes[i + 13] as usize]
Expand Down

0 comments on commit 007687a

Please sign in to comment.