Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert in slice16 hot loop to avoid bounds checks. #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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