sprs/mul_acc.rs
1//! Multiply-accumulate (MAC) trait and implementations
2//! It's useful to define our own MAC trait as it's the main primitive we use
3//! in matrix products, and defining it ourselves means we can define an
4//! implementation that does not require cloning, which should prove useful
5//! when defining sparse matrices per blocks (eg BSR, BSC)
6
7use std::ops::{AddAssign, Mul};
8
9/// Trait for types that have a multiply-accumulate operation, as required
10/// in dot products and matrix products.
11///
12/// This trait is automatically implemented for numeric types that are `Copy`,
13/// however the implementation is open for more complex types, to allow them
14/// to provide the most performant implementation. For instance, we could have
15/// a default implementation for numeric types that are `Clone`, but it would
16/// make possibly unnecessary copies.
17pub trait MulAcc<A = Self, B = A> {
18 /// Multiply and accumulate in this variable, formally `*self += a * b`.
19 fn mul_acc(&mut self, a: &A, b: &B);
20}
21
22/// Default for types which supports `mul_add`
23impl<N, A, B> MulAcc<A, B> for N
24where
25 for<'x> &'x A: Mul<&'x B, Output = N>,
26 N: AddAssign<N>,
27{
28 fn mul_acc(&mut self, a: &A, b: &B) {
29 self.add_assign(a * b);
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::MulAcc;
36
37 #[test]
38 fn mul_acc_f64() {
39 let mut a = 1f64;
40 let b = 2.;
41 let c = 3.;
42 a.mul_acc(&b, &c);
43 assert_eq!(a, 7.);
44 }
45
46 #[derive(Debug, Copy, Clone, Default)]
47 struct Wrapped<T: Default + Copy + std::fmt::Debug>(T);
48
49 impl MulAcc<Wrapped<i8>, Wrapped<i16>> for Wrapped<i32> {
50 fn mul_acc(&mut self, a: &Wrapped<i8>, b: &Wrapped<i16>) {
51 self.0 = self.0 + a.0 as i32 * b.0 as i32;
52 }
53 }
54
55 #[test]
56 fn mul_acc_mixed_param_sizes() {
57 let mut a = Wrapped::<i32>(0x40000007i32);
58 let b = Wrapped::<i8>(0x20i8);
59 let c = Wrapped::<i16>(0x3000i16);
60 a.mul_acc(&b, &c);
61 assert_eq!(a.0, 0x40060007i32);
62 }
63}