Skip to content

Commit

Permalink
Merge pull request #107 from okaneco/revert-clamps
Browse files Browse the repository at this point in the history
Revert `clamp` in favor of `max(0).min(255)` in `vp8.rs`
  • Loading branch information
kornelski authored Aug 21, 2024
2 parents e6e4b43 + 8491090 commit ca1fc02
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/vp8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,11 @@ fn mulhi(v: u8, coeff: u16) -> i32 {
/// }
/// }
/// ```
// Clippy suggests the clamp method, but it seems to optimize worse as of rustc 1.82.0 nightly.
#[allow(clippy::manual_clamp)]
fn clip(v: i32) -> u8 {
const YUV_FIX2: i32 = 6;
(v >> YUV_FIX2).clamp(0, 255) as u8
(v >> YUV_FIX2).max(0).min(255) as u8
}

#[derive(Clone, Copy, Default)]
Expand Down Expand Up @@ -2310,11 +2312,14 @@ fn avg2(this: u8, right: u8) -> u8 {

// Only 16 elements from rblock are used to add residue, so it is restricted to 16 elements
// to enable SIMD and other optimizations.
//
// Clippy suggests the clamp method, but it seems to optimize worse as of rustc 1.82.0 nightly.
#[allow(clippy::manual_clamp)]
fn add_residue(pblock: &mut [u8], rblock: &[i32; 16], y0: usize, x0: usize, stride: usize) {
let mut pos = y0 * stride + x0;
for row in rblock.chunks(4) {
for (p, &a) in pblock[pos..][..4].iter_mut().zip(row.iter()) {
*p = (a + i32::from(*p)).clamp(0, 255) as u8;
*p = (a + i32::from(*p)).max(0).min(255) as u8;
}
pos += stride;
}
Expand Down Expand Up @@ -2397,6 +2402,8 @@ fn predict_dcpred(a: &mut [u8], size: usize, stride: usize, above: bool, left: b
}
}

// Clippy suggests the clamp method, but it seems to optimize worse as of rustc 1.82.0 nightly.
#[allow(clippy::manual_clamp)]
fn predict_tmpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize) {
// The formula for tmpred is:
// X_ij = L_i + A_j - P (i, j=0, 1, 2, 3)
Expand Down Expand Up @@ -2426,7 +2433,7 @@ fn predict_tmpred(a: &mut [u8], size: usize, x0: usize, y0: usize, stride: usize
x_block[y * stride + 1..][..size]
.iter_mut()
.zip(above_slice)
.for_each(|(cur, &abv)| *cur = (left_minus_p + i32::from(abv)).clamp(0, 255) as u8);
.for_each(|(cur, &abv)| *cur = (left_minus_p + i32::from(abv)).max(0).min(255) as u8);
}
}

Expand Down

0 comments on commit ca1fc02

Please sign in to comment.