1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
use core::{
    iter::{Product, Sum},
    ops::{Add, Mul},
};

macro_rules! impl_traits {
    { $type:ty } => {
        impl<const N: usize> Sum<Self> for Simd<$type, N>
        where
            LaneCount<N>: SupportedLaneCount,
        {
            #[inline]
            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
                iter.fold(Simd::splat(0 as $type), Add::add)
            }
        }

        impl<const N: usize> Product<Self> for Simd<$type, N>
        where
            LaneCount<N>: SupportedLaneCount,
        {
            #[inline]
            fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
                iter.fold(Simd::splat(1 as $type), Mul::mul)
            }
        }

        impl<'a, const N: usize> Sum<&'a Self> for Simd<$type, N>
        where
            LaneCount<N>: SupportedLaneCount,
        {
            #[inline]
            fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
                iter.fold(Simd::splat(0 as $type), Add::add)
            }
        }

        impl<'a, const N: usize> Product<&'a Self> for Simd<$type, N>
        where
            LaneCount<N>: SupportedLaneCount,
        {
            #[inline]
            fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
                iter.fold(Simd::splat(1 as $type), Mul::mul)
            }
        }
    }
}

impl_traits! { f32 }
impl_traits! { f64 }
impl_traits! { u8 }
impl_traits! { u16 }
impl_traits! { u32 }
impl_traits! { u64 }
impl_traits! { usize }
impl_traits! { i8 }
impl_traits! { i16 }
impl_traits! { i32 }
impl_traits! { i64 }
impl_traits! { isize }