Skip to content

Commit

Permalink
Simplify trait bounds on FletcherAccumulator (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbangelo authored Sep 5, 2024
1 parent a5947c7 commit 1a4badc
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,18 @@ use core::{
ops::{Add, AddAssign, BitAnd, BitOr, Shl, Shr},
};

/// Base set of values and
/// Base set of values and operations needed for our implementation
pub trait FletcherAccumulator:
Sized
+ Copy
+ Default
+ From<Self::InputType>
+ Add
+ From<<Self as Add>::Output>
+ Add<Output = Self>
+ AddAssign
+ BitAnd
+ From<<Self as BitAnd>::Output>
+ BitOr
+ From<<Self as BitOr>::Output>
+ Shl<u16>
+ From<<Self as Shl<u16>>::Output>
+ Shr<u16>
+ From<<Self as Shr<u16>>::Output>
+ BitAnd<Output = Self>
+ BitOr<Output = Self>
+ Shl<u16, Output = Self>
+ Shr<u16, Output = Self>
+ PartialEq
{
type InputType: Copy;
Expand Down Expand Up @@ -141,17 +136,17 @@ where
/// This function assumes that the accumulators have already
/// been fully reduced.
fn combine(lower: T, upper: T) -> T {
(lower | (upper << T::SHIFT_AMOUNT).into()).into()
lower | (upper << T::SHIFT_AMOUNT)
}

/// Reduces the accumulator value
///
/// This function needs to reduce the accumulator value in a manner
/// that rounds the value according to one's compliment math.
fn reduce(value: T) -> T {
let lhs: T = (value & T::BIT_MASK).into();
let rhs: T = (value >> T::SHIFT_AMOUNT).into();
let result: T = (lhs + rhs).into();
let lhs: T = value & T::BIT_MASK;
let rhs: T = value >> T::SHIFT_AMOUNT;
let result: T = lhs + rhs;
if result == T::BIT_MASK {
T::default()
} else {
Expand Down

0 comments on commit 1a4badc

Please sign in to comment.