]> git.lizzy.rs Git - rust.git/blob - crates/core_simd/src/reduction.rs
Fix mask ops
[rust.git] / crates / core_simd / src / reduction.rs
1 macro_rules! impl_integer_reductions {
2     { $name:ident, $scalar:ty } => {
3         impl<const LANES: usize> crate::$name<LANES>
4         where
5             Self: crate::LanesAtMost32
6         {
7             /// Produces the sum of the lanes of the vector, with wrapping addition.
8             #[inline]
9             pub fn wrapping_sum(self) -> $scalar {
10                 unsafe { crate::intrinsics::simd_reduce_add_ordered(self, 0) }
11             }
12
13             /// Produces the sum of the lanes of the vector, with wrapping multiplication.
14             #[inline]
15             pub fn wrapping_product(self) -> $scalar {
16                 unsafe { crate::intrinsics::simd_reduce_mul_ordered(self, 1) }
17             }
18
19             /// Sequentially performs bitwise "and" between the lanes of the vector.
20             #[inline]
21             pub fn and_lanes(self) -> $scalar {
22                 unsafe { crate::intrinsics::simd_reduce_and(self) }
23             }
24
25             /// Sequentially performs bitwise "or" between the lanes of the vector.
26             #[inline]
27             pub fn or_lanes(self) -> $scalar {
28                 unsafe { crate::intrinsics::simd_reduce_or(self) }
29             }
30
31             /// Sequentially performs bitwise "xor" between the lanes of the vector.
32             #[inline]
33             pub fn xor_lanes(self) -> $scalar {
34                 unsafe { crate::intrinsics::simd_reduce_xor(self) }
35             }
36
37             /// Returns the maximum lane in the vector.
38             #[inline]
39             pub fn max_lane(self) -> $scalar {
40                 unsafe { crate::intrinsics::simd_reduce_max(self) }
41             }
42
43             /// Returns the minimum lane in the vector.
44             #[inline]
45             pub fn min_lane(self) -> $scalar {
46                 unsafe { crate::intrinsics::simd_reduce_min(self) }
47             }
48         }
49     }
50 }
51
52 macro_rules! impl_float_reductions {
53     { $name:ident, $scalar:ty } => {
54         impl<const LANES: usize> crate::$name<LANES>
55         where
56             Self: crate::LanesAtMost32
57         {
58             /// Produces the sum of the lanes of the vector.
59             #[inline]
60             pub fn sum(self) -> $scalar {
61                 unsafe { crate::intrinsics::simd_reduce_add_ordered(self, 0.) }
62             }
63
64             /// Produces the sum of the lanes of the vector.
65             #[inline]
66             pub fn product(self) -> $scalar {
67                 unsafe { crate::intrinsics::simd_reduce_mul_ordered(self, 1.) }
68             }
69
70             /// Returns the maximum lane in the vector.
71             #[inline]
72             pub fn max_lane(self) -> $scalar {
73                 unsafe { crate::intrinsics::simd_reduce_max(self) }
74             }
75
76             /// Returns the minimum lane in the vector.
77             #[inline]
78             pub fn min_lane(self) -> $scalar {
79                 unsafe { crate::intrinsics::simd_reduce_min(self) }
80             }
81         }
82     }
83 }
84
85 macro_rules! impl_full_mask_reductions {
86     { $name:ident, $inner:ident } => {
87         impl<const LANES: usize> crate::$name<LANES>
88         where
89             crate::$inner<LANES>: crate::LanesAtMost32
90         {
91             /// Returns true if any lane is set, or false otherwise.
92             #[inline]
93             pub fn any(self) -> bool {
94                 unsafe { crate::intrinsics::simd_reduce_any(self.to_int()) }
95             }
96
97             /// Returns true if all lanes are set, or false otherwise.
98             #[inline]
99             pub fn all(self) -> bool {
100                 unsafe { crate::intrinsics::simd_reduce_all(self.to_int()) }
101             }
102         }
103     }
104 }
105
106 macro_rules! impl_opaque_mask_reductions {
107     { $name:ident, $inner:ident, $bits_ty:ident } => {
108         impl<const LANES: usize> $name<LANES>
109         where
110             $bits_ty<LANES>: crate::LanesAtMost32
111         {
112             /// Returns true if any lane is set, or false otherwise.
113             #[inline]
114             pub fn any(self) -> bool {
115                 self.0.any()
116             }
117
118             /// Returns true if all lanes are set, or false otherwise.
119             #[inline]
120             pub fn all(self) -> bool {
121                 self.0.all()
122             }
123         }
124     }
125 }
126
127 impl<const LANES: usize> crate::BitMask<LANES>
128 where
129     crate::BitMask<LANES>: crate::LanesAtMost32,
130 {
131     /// Returns true if any lane is set, or false otherwise.
132     #[inline]
133     pub fn any(self) -> bool {
134         self != Self::splat(false)
135     }
136
137     /// Returns true if all lanes are set, or false otherwise.
138     #[inline]
139     pub fn all(self) -> bool {
140         self == Self::splat(true)
141     }
142 }