1 | use crate::simd::{LaneCount, Simd, SupportedLaneCount}; |
2 | use core::{ |
3 | iter::{Product, Sum}, |
4 | ops::{Add, Mul}, |
5 | }; |
6 | |
7 | macro_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 | |
51 | impl_traits! { f32 } |
52 | impl_traits! { f64 } |
53 | impl_traits! { u8 } |
54 | impl_traits! { u16 } |
55 | impl_traits! { u32 } |
56 | impl_traits! { u64 } |
57 | impl_traits! { usize } |
58 | impl_traits! { i8 } |
59 | impl_traits! { i16 } |
60 | impl_traits! { i32 } |
61 | impl_traits! { i64 } |
62 | impl_traits! { isize } |
63 | |