macc_i32: move to tools

This commit is contained in:
Robert Jördens 2021-02-25 18:01:53 +01:00
parent 83496205b3
commit 1805961d5d
2 changed files with 13 additions and 12 deletions

View File

@ -1,3 +1,4 @@
use super::tools::macc_i32;
use core::f64::consts::PI; use core::f64::consts::PI;
use miniconf::StringSet; use miniconf::StringSet;
use serde::Deserialize; use serde::Deserialize;
@ -40,17 +41,6 @@ impl Coeff for Vec5 {
} }
} }
fn macc(y0: i32, x: &[i32], a: &[i32], shift: u32) -> i32 {
// Rounding bias, half up
let y0 = ((y0 as i64) << shift) + (1 << (shift - 1));
let y = x
.iter()
.zip(a)
.map(|(x, a)| *x as i64 * *a as i64)
.fold(y0, |y, xa| y + xa);
(y >> shift) as i32
}
/// Integer biquad IIR /// Integer biquad IIR
/// ///
/// See `dsp::iir::IIR` for general implementation details. /// See `dsp::iir::IIR` for general implementation details.
@ -86,7 +76,7 @@ impl IIR {
// Store x0 x0 x1 x2 y1 y2 // Store x0 x0 x1 x2 y1 y2
xy[0] = x0; xy[0] = x0;
// Compute y0 by multiply-accumulate // Compute y0 by multiply-accumulate
let y0 = macc(0, xy, &self.ba, IIR::SHIFT); let y0 = macc_i32(0, xy, &self.ba, IIR::SHIFT);
// Limit y0 // Limit y0
// let y0 = y0.max(self.y_min).min(self.y_max); // let y0 = y0.max(self.y_min).min(self.y_max);
// Store y0 x0 x1 y0 y1 y2 // Store y0 x0 x1 y0 y1 y2

View File

@ -77,6 +77,17 @@ where
.fold(y0, |y, xa| y + xa) .fold(y0, |y, xa| y + xa)
} }
pub fn macc_i32(y0: i32, x: &[i32], a: &[i32], shift: u32) -> i32 {
// Rounding bias, half up
let y0 = ((y0 as i64) << shift) + (1 << (shift - 1));
let y = x
.iter()
.zip(a)
.map(|(x, a)| *x as i64 * *a as i64)
.fold(y0, |y, xa| y + xa);
(y >> shift) as 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);