2021-09-18 20:31:49 -05:00
|
|
|
use crate::simd::{LaneCount, Simd, SupportedLaneCount};
|
2021-08-07 14:28:27 -05:00
|
|
|
use core::{
|
|
|
|
iter::{Product, Sum},
|
|
|
|
ops::{Add, Mul},
|
|
|
|
};
|
2021-07-23 21:54:19 -05:00
|
|
|
|
2021-06-13 12:52:44 -05:00
|
|
|
macro_rules! impl_traits {
|
2021-08-07 14:28:27 -05:00
|
|
|
{ $type:ty } => {
|
|
|
|
impl<const LANES: usize> Sum<Self> for Simd<$type, LANES>
|
2021-06-13 12:52:44 -05:00
|
|
|
where
|
2021-07-23 21:54:19 -05:00
|
|
|
LaneCount<LANES>: SupportedLaneCount,
|
2021-06-13 12:52:44 -05:00
|
|
|
{
|
2021-08-07 14:28:27 -05:00
|
|
|
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
|
|
|
|
iter.fold(Simd::splat(0 as $type), Add::add)
|
2021-06-13 12:52:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 20:31:49 -05:00
|
|
|
impl<const LANES: usize> Product<Self> for Simd<$type, LANES>
|
2021-06-13 12:52:44 -05:00
|
|
|
where
|
2021-07-23 21:54:19 -05:00
|
|
|
LaneCount<LANES>: SupportedLaneCount,
|
2021-06-13 12:52:44 -05:00
|
|
|
{
|
2021-08-07 14:28:27 -05:00
|
|
|
fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
|
|
|
|
iter.fold(Simd::splat(1 as $type), Mul::mul)
|
2021-06-13 12:52:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:28:27 -05:00
|
|
|
impl<'a, const LANES: usize> Sum<&'a Self> for Simd<$type, LANES>
|
2021-06-13 13:00:47 -05:00
|
|
|
where
|
2021-07-23 21:54:19 -05:00
|
|
|
LaneCount<LANES>: SupportedLaneCount,
|
2021-06-13 13:00:47 -05:00
|
|
|
{
|
2021-08-07 14:28:27 -05:00
|
|
|
fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
|
|
|
|
iter.fold(Simd::splat(0 as $type), Add::add)
|
2021-06-13 13:00:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:28:27 -05:00
|
|
|
impl<'a, const LANES: usize> Product<&'a Self> for Simd<$type, LANES>
|
2021-06-13 13:00:47 -05:00
|
|
|
where
|
2021-07-23 21:54:19 -05:00
|
|
|
LaneCount<LANES>: SupportedLaneCount,
|
2021-06-13 13:00:47 -05:00
|
|
|
{
|
2021-08-07 14:28:27 -05:00
|
|
|
fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
|
|
|
|
iter.fold(Simd::splat(1 as $type), Mul::mul)
|
2021-06-13 13:00:47 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-13 12:52:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 14:28:27 -05:00
|
|
|
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 }
|