Skip to content

Commit bdcccba

Browse files
committed
Implement Sum/Product traits
1 parent 57e67c9 commit bdcccba

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

crates/core_simd/src/iter.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
macro_rules! impl_traits {
2+
{ $type:ident, $scalar:ty } => {
3+
impl<const LANES: usize> core::iter::Sum<Self> for crate::$type<LANES>
4+
where
5+
Self: crate::LanesAtMost32,
6+
{
7+
fn sum<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
8+
iter.fold(Default::default(), core::ops::Add::add)
9+
}
10+
}
11+
12+
impl<const LANES: usize> core::iter::Product<Self> for crate::$type<LANES>
13+
where
14+
Self: crate::LanesAtMost32,
15+
{
16+
fn product<I: core::iter::Iterator<Item = Self>>(iter: I) -> Self {
17+
iter.fold(Default::default(), core::ops::Mul::mul)
18+
}
19+
}
20+
21+
impl<const LANES: usize> core::iter::Sum<crate::$type<LANES>> for $scalar
22+
where
23+
crate::$type<LANES>: crate::LanesAtMost32,
24+
{
25+
fn sum<I: core::iter::Iterator<Item = crate::$type<LANES>>>(iter: I) -> Self {
26+
iter.sum::<crate::$type<LANES>>().horizontal_sum()
27+
}
28+
}
29+
30+
impl<const LANES: usize> core::iter::Product<crate::$type<LANES>> for $scalar
31+
where
32+
crate::$type<LANES>: crate::LanesAtMost32,
33+
{
34+
fn product<I: core::iter::Iterator<Item = crate::$type<LANES>>>(iter: I) -> Self {
35+
iter.product::<crate::$type<LANES>>().horizontal_product()
36+
}
37+
}
38+
}
39+
}
40+
41+
impl_traits! { SimdF32, f32 }
42+
impl_traits! { SimdF64, f64 }
43+
impl_traits! { SimdU8, u8 }
44+
impl_traits! { SimdU16, u16 }
45+
impl_traits! { SimdU32, u32 }
46+
impl_traits! { SimdU64, u64 }
47+
impl_traits! { SimdUsize, usize }
48+
impl_traits! { SimdI8, i8 }
49+
impl_traits! { SimdI16, i16 }
50+
impl_traits! { SimdI32, i32 }
51+
impl_traits! { SimdI64, i64 }
52+
impl_traits! { SimdIsize, isize }

crates/core_simd/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use to_bytes::ToBytes;
2222
mod comparisons;
2323
mod fmt;
2424
mod intrinsics;
25+
mod iter;
2526
mod ops;
2627
mod round;
2728

0 commit comments

Comments
 (0)