core/portable-simd/crates/core_simd/src/
iter.rs

1use crate::simd::{LaneCount, Simd, SupportedLaneCount};
2use core::{
3    iter::{Product, Sum},
4    ops::{Add, Mul},
5};
6
7macro_rules! impl_traits {
8    { $type:ty } => {
9        impl<const N: usize> Sum<Self> for Simd<$type, N>
10        where
11            LaneCount<N>: SupportedLaneCount,
12        {
13            #[inline]
14            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
15                iter.fold(Simd::splat(0 as $type), Add::add)
16            }
17        }
18
19        impl<const N: usize> Product<Self> for Simd<$type, N>
20        where
21            LaneCount<N>: SupportedLaneCount,
22        {
23            #[inline]
24            fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
25                iter.fold(Simd::splat(1 as $type), Mul::mul)
26            }
27        }
28
29        impl<'a, const N: usize> Sum<&'a Self> for Simd<$type, N>
30        where
31            LaneCount<N>: SupportedLaneCount,
32        {
33            #[inline]
34            fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
35                iter.fold(Simd::splat(0 as $type), Add::add)
36            }
37        }
38
39        impl<'a, const N: usize> Product<&'a Self> for Simd<$type, N>
40        where
41            LaneCount<N>: SupportedLaneCount,
42        {
43            #[inline]
44            fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
45                iter.fold(Simd::splat(1 as $type), Mul::mul)
46            }
47        }
48    }
49}
50
51impl_traits! { f32 }
52impl_traits! { f64 }
53impl_traits! { u8 }
54impl_traits! { u16 }
55impl_traits! { u32 }
56impl_traits! { u64 }
57impl_traits! { usize }
58impl_traits! { i8 }
59impl_traits! { i16 }
60impl_traits! { i32 }
61impl_traits! { i64 }
62impl_traits! { isize }