Skip to main content

core_simd/simd/cmp/
ord.rs

1use crate::simd::{
2    Mask, Select, Simd,
3    cmp::SimdPartialEq,
4    ptr::{SimdConstPtr, SimdMutPtr},
5};
6
7/// Parallel `PartialOrd`.
8pub trait SimdPartialOrd: SimdPartialEq {
9    /// Test if each element is less than the corresponding element in `other`.
10    #[must_use = "method returns a new mask and does not mutate the original value"]
11    fn simd_lt(self, other: Self) -> Self::Mask;
12
13    /// Test if each element is less than or equal to the corresponding element in `other`.
14    #[must_use = "method returns a new mask and does not mutate the original value"]
15    fn simd_le(self, other: Self) -> Self::Mask;
16
17    /// Test if each element is greater than the corresponding element in `other`.
18    #[must_use = "method returns a new mask and does not mutate the original value"]
19    fn simd_gt(self, other: Self) -> Self::Mask;
20
21    /// Test if each element is greater than or equal to the corresponding element in `other`.
22    #[must_use = "method returns a new mask and does not mutate the original value"]
23    fn simd_ge(self, other: Self) -> Self::Mask;
24}
25
26/// Parallel `Ord`.
27pub trait SimdOrd: SimdPartialOrd {
28    /// Returns the element-wise maximum with `other`.
29    #[must_use = "method returns a new vector and does not mutate the original value"]
30    fn simd_max(self, other: Self) -> Self;
31
32    /// Returns the element-wise minimum with `other`.
33    #[must_use = "method returns a new vector and does not mutate the original value"]
34    fn simd_min(self, other: Self) -> Self;
35
36    /// Restrict each element to a certain interval.
37    ///
38    /// For each element, returns `max` if `self` is greater than `max`, and `min` if `self` is
39    /// less than `min`. Otherwise returns `self`.
40    ///
41    /// # Panics
42    ///
43    /// Panics if `min > max` on any element.
44    #[must_use = "method returns a new vector and does not mutate the original value"]
45    fn simd_clamp(self, min: Self, max: Self) -> Self;
46}
47
48macro_rules! impl_integer {
49    { $($integer:ty),* } => {
50        $(
51        impl<const N: usize> SimdPartialOrd for Simd<$integer, N>
52        {
53            #[inline]
54            fn simd_lt(self, other: Self) -> Self::Mask {
55                // Safety: `self` is a vector, and the result of the comparison
56                // is always a valid mask.
57                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_lt(self, other)) }
58            }
59
60            #[inline]
61            fn simd_le(self, other: Self) -> Self::Mask {
62                // Safety: `self` is a vector, and the result of the comparison
63                // is always a valid mask.
64                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_le(self, other)) }
65            }
66
67            #[inline]
68            fn simd_gt(self, other: Self) -> Self::Mask {
69                // Safety: `self` is a vector, and the result of the comparison
70                // is always a valid mask.
71                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_gt(self, other)) }
72            }
73
74            #[inline]
75            fn simd_ge(self, other: Self) -> Self::Mask {
76                // Safety: `self` is a vector, and the result of the comparison
77                // is always a valid mask.
78                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_ge(self, other)) }
79            }
80        }
81
82        impl<const N: usize> SimdOrd for Simd<$integer, N>
83        {
84            #[inline]
85            fn simd_max(self, other: Self) -> Self {
86                self.simd_lt(other).select(other, self)
87            }
88
89            #[inline]
90            fn simd_min(self, other: Self) -> Self {
91                self.simd_gt(other).select(other, self)
92            }
93
94            #[inline]
95            #[track_caller]
96            fn simd_clamp(self, min: Self, max: Self) -> Self {
97                assert!(
98                    min.simd_le(max).all(),
99                    "each element in `min` must be less than or equal to the corresponding element in `max`",
100                );
101                self.simd_max(min).simd_min(max)
102            }
103        }
104        )*
105    }
106}
107
108impl_integer! { u8, u16, u32, u64, usize, i8, i16, i32, i64, isize }
109
110macro_rules! impl_float {
111    { $($float:ty),* } => {
112        $(
113        impl<const N: usize> SimdPartialOrd for Simd<$float, N>
114        {
115            #[inline]
116            fn simd_lt(self, other: Self) -> Self::Mask {
117                // Safety: `self` is a vector, and the result of the comparison
118                // is always a valid mask.
119                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_lt(self, other)) }
120            }
121
122            #[inline]
123            fn simd_le(self, other: Self) -> Self::Mask {
124                // Safety: `self` is a vector, and the result of the comparison
125                // is always a valid mask.
126                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_le(self, other)) }
127            }
128
129            #[inline]
130            fn simd_gt(self, other: Self) -> Self::Mask {
131                // Safety: `self` is a vector, and the result of the comparison
132                // is always a valid mask.
133                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_gt(self, other)) }
134            }
135
136            #[inline]
137            fn simd_ge(self, other: Self) -> Self::Mask {
138                // Safety: `self` is a vector, and the result of the comparison
139                // is always a valid mask.
140                unsafe { Mask::from_simd_unchecked(core::intrinsics::simd::simd_ge(self, other)) }
141            }
142        }
143        )*
144    }
145}
146
147impl_float! { f16, f32, f64 }
148
149macro_rules! impl_mask {
150    { $($integer:ty),* } => {
151        $(
152        impl<const N: usize> SimdPartialOrd for Mask<$integer, N>
153        {
154            #[inline]
155            fn simd_lt(self, other: Self) -> Self::Mask {
156                !self & other
157            }
158
159            #[inline]
160            fn simd_le(self, other: Self) -> Self::Mask {
161                !self | other
162            }
163
164            #[inline]
165            fn simd_gt(self, other: Self) -> Self::Mask {
166                self & !other
167            }
168
169            #[inline]
170            fn simd_ge(self, other: Self) -> Self::Mask {
171                self | !other
172            }
173        }
174
175        impl<const N: usize> SimdOrd for Mask<$integer, N>
176        {
177            #[inline]
178            fn simd_max(self, other: Self) -> Self {
179                self | other
180            }
181
182            #[inline]
183            fn simd_min(self, other: Self) -> Self {
184                self & other
185            }
186
187            #[inline]
188            #[track_caller]
189            fn simd_clamp(self, min: Self, max: Self) -> Self {
190                assert!(
191                    min.simd_le(max).all(),
192                    "each element in `min` must be less than or equal to the corresponding element in `max`",
193                );
194                self.simd_max(min).simd_min(max)
195            }
196        }
197        )*
198    }
199}
200
201impl_mask! { i8, i16, i32, i64, isize }
202
203impl<T, const N: usize> SimdPartialOrd for Simd<*const T, N> {
204    #[inline]
205    fn simd_lt(self, other: Self) -> Self::Mask {
206        self.addr().simd_lt(other.addr())
207    }
208
209    #[inline]
210    fn simd_le(self, other: Self) -> Self::Mask {
211        self.addr().simd_le(other.addr())
212    }
213
214    #[inline]
215    fn simd_gt(self, other: Self) -> Self::Mask {
216        self.addr().simd_gt(other.addr())
217    }
218
219    #[inline]
220    fn simd_ge(self, other: Self) -> Self::Mask {
221        self.addr().simd_ge(other.addr())
222    }
223}
224
225impl<T, const N: usize> SimdOrd for Simd<*const T, N> {
226    #[inline]
227    fn simd_max(self, other: Self) -> Self {
228        self.simd_lt(other).select(other, self)
229    }
230
231    #[inline]
232    fn simd_min(self, other: Self) -> Self {
233        self.simd_gt(other).select(other, self)
234    }
235
236    #[inline]
237    #[track_caller]
238    fn simd_clamp(self, min: Self, max: Self) -> Self {
239        assert!(
240            min.simd_le(max).all(),
241            "each element in `min` must be less than or equal to the corresponding element in `max`",
242        );
243        self.simd_max(min).simd_min(max)
244    }
245}
246
247impl<T, const N: usize> SimdPartialOrd for Simd<*mut T, N> {
248    #[inline]
249    fn simd_lt(self, other: Self) -> Self::Mask {
250        self.addr().simd_lt(other.addr())
251    }
252
253    #[inline]
254    fn simd_le(self, other: Self) -> Self::Mask {
255        self.addr().simd_le(other.addr())
256    }
257
258    #[inline]
259    fn simd_gt(self, other: Self) -> Self::Mask {
260        self.addr().simd_gt(other.addr())
261    }
262
263    #[inline]
264    fn simd_ge(self, other: Self) -> Self::Mask {
265        self.addr().simd_ge(other.addr())
266    }
267}
268
269impl<T, const N: usize> SimdOrd for Simd<*mut T, N> {
270    #[inline]
271    fn simd_max(self, other: Self) -> Self {
272        self.simd_lt(other).select(other, self)
273    }
274
275    #[inline]
276    fn simd_min(self, other: Self) -> Self {
277        self.simd_gt(other).select(other, self)
278    }
279
280    #[inline]
281    #[track_caller]
282    fn simd_clamp(self, min: Self, max: Self) -> Self {
283        assert!(
284            min.simd_le(max).all(),
285            "each element in `min` must be less than or equal to the corresponding element in `max`",
286        );
287        self.simd_max(min).simd_min(max)
288    }
289}