From 032f385e2fda5821ec3802186f1c8503f40a1ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Hoz=C3=A1k?= <5235838+rhzx86@users.noreply.github.com> Date: Mon, 17 Jul 2023 20:40:24 +0200 Subject: [PATCH] Implement Sum and Product traits for fixed point numbers This makes it so simd_horizontal_sum and simd_horizontal_product can be implemented for fixed point numbers. --- src/scalar/fixed_impl.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/scalar/fixed_impl.rs b/src/scalar/fixed_impl.rs index b7709e8..4c8d007 100644 --- a/src/scalar/fixed_impl.rs +++ b/src/scalar/fixed_impl.rs @@ -16,6 +16,7 @@ use std::hash::{Hash, Hasher}; use std::ops::{ Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign, }; +use std::iter::{Sum, Product}; macro_rules! impl_fixed_type( ($($FixedI: ident, $Int: ident, $LeEqDim: ident, $LeEqDim1: ident, $LeEqDim2: ident, $LeEqDim3: ident, $LeEqDim4: ident;)*) => {$( @@ -138,6 +139,42 @@ macro_rules! impl_fixed_type( } } + impl Sum for $FixedI { + fn sum>(iter: I) -> Self { + iter.fold( + Self::zero(), + |a, b| a + b, + ) + } + } + + impl<'a, Fract: $LeEqDim> Sum<&'a $FixedI> for $FixedI { + fn sum>(iter: I) -> Self { + iter.fold( + Self::zero(), + |a, b| a + *b, + ) + } + } + + impl Product for $FixedI { + fn product>(iter: I) -> Self { + iter.fold( + Self::one(), + |a, b| a * b, + ) + } + } + + impl<'a, Fract: $LeEqDim> Product<&'a $FixedI > for $FixedI { + fn product>(iter: I) -> Self { + iter.fold( + Self::one(), + |a, b| a * *b, + ) + } + } + impl Mul for $FixedI { type Output = Self; #[inline(always)]