]> git.lizzy.rs Git - rust.git/blob - library/portable-simd/crates/core_simd/src/iter.rs
Merge commit '370c397ec9169809e5ad270079712e0043514240' into sync_cg_clif-2022-03-20
[rust.git] / library / portable-simd / crates / core_simd / src / iter.rs
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 LANES: usize> Sum<Self> for Simd<$type, LANES>
10         where
11             LaneCount<LANES>: SupportedLaneCount,
12         {
13             fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
14                 iter.fold(Simd::splat(0 as $type), Add::add)
15             }
16         }
17
18         impl<const LANES: usize> Product<Self> for Simd<$type, LANES>
19         where
20             LaneCount<LANES>: SupportedLaneCount,
21         {
22             fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
23                 iter.fold(Simd::splat(1 as $type), Mul::mul)
24             }
25         }
26
27         impl<'a, const LANES: usize> Sum<&'a Self> for Simd<$type, LANES>
28         where
29             LaneCount<LANES>: SupportedLaneCount,
30         {
31             fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
32                 iter.fold(Simd::splat(0 as $type), Add::add)
33             }
34         }
35
36         impl<'a, const LANES: usize> Product<&'a Self> for Simd<$type, LANES>
37         where
38             LaneCount<LANES>: SupportedLaneCount,
39         {
40             fn product<I: Iterator<Item = &'a Self>>(iter: I) -> Self {
41                 iter.fold(Simd::splat(1 as $type), Mul::mul)
42             }
43         }
44     }
45 }
46
47 impl_traits! { f32 }
48 impl_traits! { f64 }
49 impl_traits! { u8 }
50 impl_traits! { u16 }
51 impl_traits! { u32 }
52 impl_traits! { u64 }
53 impl_traits! { usize }
54 impl_traits! { i8 }
55 impl_traits! { i16 }
56 impl_traits! { i32 }
57 impl_traits! { i64 }
58 impl_traits! { isize }