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