Skip to content

Commit 96f0f5d

Browse files
committed
Implement Sum/Product over references
1 parent bdcccba commit 96f0f5d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

crates/core_simd/src/iter.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,42 @@ macro_rules! impl_traits {
3535
iter.product::<crate::$type<LANES>>().horizontal_product()
3636
}
3737
}
38+
39+
impl<'a, const LANES: usize> core::iter::Sum<&'a Self> for crate::$type<LANES>
40+
where
41+
Self: crate::LanesAtMost32,
42+
{
43+
fn sum<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
44+
iter.fold(Default::default(), core::ops::Add::add)
45+
}
46+
}
47+
48+
impl<'a, const LANES: usize> core::iter::Product<&'a Self> for crate::$type<LANES>
49+
where
50+
Self: crate::LanesAtMost32,
51+
{
52+
fn product<I: core::iter::Iterator<Item = &'a Self>>(iter: I) -> Self {
53+
iter.fold(Default::default(), core::ops::Mul::mul)
54+
}
55+
}
56+
57+
impl<'a, const LANES: usize> core::iter::Sum<&'a crate::$type<LANES>> for $scalar
58+
where
59+
crate::$type<LANES>: crate::LanesAtMost32,
60+
{
61+
fn sum<I: core::iter::Iterator<Item = &'a crate::$type<LANES>>>(iter: I) -> Self {
62+
iter.sum::<crate::$type<LANES>>().horizontal_sum()
63+
}
64+
}
65+
66+
impl<'a, const LANES: usize> core::iter::Product<&'a crate::$type<LANES>> for $scalar
67+
where
68+
crate::$type<LANES>: crate::LanesAtMost32,
69+
{
70+
fn product<I: core::iter::Iterator<Item = &'a crate::$type<LANES>>>(iter: I) -> Self {
71+
iter.product::<crate::$type<LANES>>().horizontal_product()
72+
}
73+
}
3874
}
3975
}
4076

0 commit comments

Comments
 (0)