Merge pull request #280 from quartiq/feature/rj/saturating-complex

Feature/rj/saturating complex
This commit is contained in:
Robert Jördens 2021-02-22 16:55:46 +01:00 committed by GitHub
commit 1f19b65584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 1 deletions

View File

@ -8,6 +8,8 @@ pub trait ComplexExt<T, U> {
fn abs_sqr(&self) -> U; fn abs_sqr(&self) -> U;
fn log2(&self) -> T; fn log2(&self) -> T;
fn arg(&self) -> T; fn arg(&self) -> T;
fn saturating_add(&self, other: Self) -> Self;
fn saturating_sub(&self, other: Self) -> Self;
} }
impl ComplexExt<i32, u32> for Complex<i32> { impl ComplexExt<i32, u32> for Complex<i32> {
@ -23,7 +25,7 @@ impl ComplexExt<i32, u32> for Complex<i32> {
/// ``` /// ```
fn from_angle(angle: i32) -> Self { fn from_angle(angle: i32) -> Self {
let (c, s) = cossin(angle); let (c, s) = cossin(angle);
Self { re: c, im: s } Self::new(c, s)
} }
/// Return the absolute square (the squared magnitude). /// Return the absolute square (the squared magnitude).
@ -85,6 +87,20 @@ impl ComplexExt<i32, u32> for Complex<i32> {
fn arg(&self) -> i32 { fn arg(&self) -> i32 {
atan2(self.im, self.re) atan2(self.im, self.re)
} }
fn saturating_add(&self, other: Self) -> Self {
Self::new(
self.re.saturating_add(other.re),
self.im.saturating_add(other.im),
)
}
fn saturating_sub(&self, other: Self) -> Self {
Self::new(
self.re.saturating_sub(other.re),
self.im.saturating_sub(other.im),
)
}
} }
/// Full scale fixed point multiplication. /// Full scale fixed point multiplication.