]> git.lizzy.rs Git - rust.git/blob - crates/core_simd/src/reduction.rs
Revert i586 fix, fix test instead
[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
59             /// Produces the sum of the lanes of the vector.
60             #[inline]
61             pub fn sum(self) -> $scalar {
62                 unsafe { crate::intrinsics::simd_reduce_add_ordered(self, 0.) }
63             }
64
65             /// Produces the sum of the lanes of the vector.
66             #[inline]
67             pub fn product(self) -> $scalar {
68                 unsafe { crate::intrinsics::simd_reduce_mul_ordered(self, 1.) }
69             }
70
71             /// Returns the maximum lane in the vector.
72             ///
73             /// Returns values based on equality, so a vector containing both `0.` and `-0.` may
74             /// return either.  This function will not return `NaN` unless all lanes are `NaN`.
75             #[inline]
76             pub fn max_lane(self) -> $scalar {
77                 unsafe { crate::intrinsics::simd_reduce_max(self) }
78             }
79
80             /// Returns the minimum lane in the vector.
81             ///
82             /// Returns values based on equality, so a vector containing both `0.` and `-0.` may
83             /// return either.  This function will not return `NaN` unless all lanes are `NaN`.
84             #[inline]
85             pub fn min_lane(self) -> $scalar {
86                 unsafe { crate::intrinsics::simd_reduce_min(self) }
87             }
88         }
89     }
90 }
91
92 macro_rules! impl_full_mask_reductions {
93     { $name:ident, $inner:ident } => {
94         impl<const LANES: usize> crate::$name<LANES>
95         where
96             crate::$inner<LANES>: crate::LanesAtMost32
97         {
98             /// Returns true if any lane is set, or false otherwise.
99             #[inline]
100             pub fn any(self) -> bool {
101                 unsafe { crate::intrinsics::simd_reduce_any(self.to_int()) }
102             }
103
104             /// Returns true if all lanes are set, or false otherwise.
105             #[inline]
106             pub fn all(self) -> bool {
107                 unsafe { crate::intrinsics::simd_reduce_all(self.to_int()) }
108             }
109         }
110     }
111 }
112
113 macro_rules! impl_opaque_mask_reductions {
114     { $name:ident, $inner:ident, $bits_ty:ident } => {
115         impl<const LANES: usize> $name<LANES>
116         where
117             $bits_ty<LANES>: crate::LanesAtMost32
118         {
119             /// Returns true if any lane is set, or false otherwise.
120             #[inline]
121             pub fn any(self) -> bool {
122                 self.0.any()
123             }
124
125             /// Returns true if all lanes are set, or false otherwise.
126             #[inline]
127             pub fn all(self) -> bool {
128                 self.0.all()
129             }
130         }
131     }
132 }
133
134 impl<const LANES: usize> crate::BitMask<LANES>
135 where
136     crate::BitMask<LANES>: crate::LanesAtMost32,
137 {
138     /// Returns true if any lane is set, or false otherwise.
139     #[inline]
140     pub fn any(self) -> bool {
141         self != Self::splat(false)
142     }
143
144     /// Returns true if all lanes are set, or false otherwise.
145     #[inline]
146     pub fn all(self) -> bool {
147         self == Self::splat(true)
148     }
149 }