193: unwrap: comments, names r=jordens a=jordens



Co-authored-by: Robert Jördens <rj@quartiq.de>
This commit is contained in:
bors[bot] 2020-12-07 12:47:31 +00:00 committed by GitHub
commit 80e92b6949
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 8 deletions

View File

@ -18,14 +18,14 @@ pub fn overflowing_sub(y: i32, x: i32) -> (i32, i8) {
/// Overflow unwrapper. /// Overflow unwrapper.
/// ///
/// This is unwrapping as in the phase unwrapping context, not unwrapping as /// This is unwrapping as in the phase and overflow unwrapping context, not
/// in the `Result`/`Option` context. /// unwrapping as in the `Result`/`Option` context.
#[derive(Copy, Clone, Default, Deserialize, Serialize)] #[derive(Copy, Clone, Default, Deserialize, Serialize)]
pub struct Unwrapper { pub struct Unwrapper {
// last input // last input
x: i32, x: i32,
// last wraps // last wraps
v: i32, w: i32,
} }
impl Unwrapper { impl Unwrapper {
@ -35,13 +35,13 @@ impl Unwrapper {
/// * `x`: New sample /// * `x`: New sample
/// ///
/// Returns: /// Returns:
/// A tuple containing the (wrapped) difference `x - x_old` and the signed /// A tuple containing the (wrapped) difference `x - x_old` and the
/// number of wraps accumulated by `x`. /// signed number of wraps accumulated by the new sample.
pub fn update(&mut self, x: i32) -> (i32, i32) { pub fn update(&mut self, x: i32) -> (i32, i32) {
let (dx, v) = overflowing_sub(x, self.x); let (dx, dw) = overflowing_sub(x, self.x);
self.x = x; self.x = x;
self.v = self.v.saturating_add(v as i32); self.w = self.w.wrapping_add(dw as i32);
(dx, self.v) (dx, self.w)
} }
} }