Skip to content

Commit

Permalink
Optimize devli to speed up jpeg reading (#122)
Browse files Browse the repository at this point in the history
* optimize devli not to branch

* added test
  • Loading branch information
mcroomp authored Nov 26, 2024
1 parent 2545e7e commit d7b80ab
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,31 @@ pub fn buffer_prefix_matches_marker<const BS: usize, const MS: usize>(

#[inline(always)]
pub const fn devli(s: u8, value: u16) -> i16 {
if s == 0 {
let shifted = 1 << s;

if value >= (shifted >> 1) {
value as i16
} else if value < (1 << (s as u16 - 1)) {
value as i16 + (-1 << s as i16) + 1
} else {
value as i16
(value + !(shifted - 1) + 1) as i16
}
}

/// check to make sure the behavior hasn't changed even with the optimization
#[test]
fn devli_test() {
for s in 0u8..15 {
for value in 0..(1 << s) {
assert_eq!(
devli(s, value) as i16,
if s == 0 {
value as i16
} else if value < (1 << (s as u16 - 1)) {
value as i16 + (-1 << s as i16) + 1
} else {
value as i16
}
);
}
}
}

Expand Down

0 comments on commit d7b80ab

Please sign in to comment.