dsp/staurating_scale: fix math

This commit is contained in:
Robert Jördens 2021-05-29 22:50:37 +02:00
parent 21ab988239
commit 2ba49258f9
1 changed files with 5 additions and 9 deletions

View File

@ -91,16 +91,12 @@ pub fn macc_i32(y0: i32, x: &[i32], a: &[i32], shift: u32) -> i32 {
/// Combine high and low i32 into a single downscaled i32, saturating the type. /// Combine high and low i32 into a single downscaled i32, saturating the type.
pub fn saturating_scale(lo: i32, hi: i32, shift: u32) -> i32 { pub fn saturating_scale(lo: i32, hi: i32, shift: u32) -> i32 {
debug_assert!(shift & 31 == shift); debug_assert!(shift & 31 == shift);
let scale = -1 << shift;
let shift_hi = 31 - shift; if hi <= scale {
debug_assert!(shift_hi & 31 == shift_hi); -i32::MAX
} else if -hi <= scale {
let over = hi >> shift;
if over < -1 {
i32::MIN
} else if over > 0 {
i32::MAX i32::MAX
} else { } else {
(lo >> shift) + (hi << shift_hi) (lo >> shift) + (hi << (31 - shift))
} }
} }