From 1805961d5dade1ed90361a8de6bc49b7c2d4a928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Thu, 25 Feb 2021 18:01:53 +0100 Subject: [PATCH] macc_i32: move to tools --- dsp/src/iir_int.rs | 14 ++------------ dsp/src/tools.rs | 11 +++++++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/dsp/src/iir_int.rs b/dsp/src/iir_int.rs index 02fdeb1..ef0c1dc 100644 --- a/dsp/src/iir_int.rs +++ b/dsp/src/iir_int.rs @@ -1,3 +1,4 @@ +use super::tools::macc_i32; use core::f64::consts::PI; use miniconf::StringSet; 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 /// /// See `dsp::iir::IIR` for general implementation details. @@ -86,7 +76,7 @@ impl IIR { // Store x0 x0 x1 x2 y1 y2 xy[0] = x0; // 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 // let y0 = y0.max(self.y_min).min(self.y_max); // Store y0 x0 x1 y0 y1 y2 diff --git a/dsp/src/tools.rs b/dsp/src/tools.rs index 378734f..6fc2b12 100644 --- a/dsp/src/tools.rs +++ b/dsp/src/tools.rs @@ -77,6 +77,17 @@ where .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. pub fn saturating_scale(lo: i32, hi: i32, shift: u32) -> i32 { debug_assert!(shift & 31 == shift);