1 use crate::simd::{intrinsics, LaneCount, Mask, Simd, SimdPartialEq, SupportedLaneCount};
3 /// Parallel `PartialOrd`.
4 pub trait SimdPartialOrd: SimdPartialEq {
5 /// Test if each lane is less than the corresponding lane in `other`.
6 #[must_use = "method returns a new mask and does not mutate the original value"]
7 fn simd_lt(self, other: Self) -> Self::Mask;
9 /// Test if each lane is less than or equal to the corresponding lane in `other`.
10 #[must_use = "method returns a new mask and does not mutate the original value"]
11 fn simd_le(self, other: Self) -> Self::Mask;
13 /// Test if each lane is greater than the corresponding lane in `other`.
14 #[must_use = "method returns a new mask and does not mutate the original value"]
15 fn simd_gt(self, other: Self) -> Self::Mask;
17 /// Test if each lane is greater than or equal to the corresponding lane in `other`.
18 #[must_use = "method returns a new mask and does not mutate the original value"]
19 fn simd_ge(self, other: Self) -> Self::Mask;
23 pub trait SimdOrd: SimdPartialOrd {
24 /// Returns the lane-wise maximum with `other`.
25 #[must_use = "method returns a new vector and does not mutate the original value"]
26 fn simd_max(self, other: Self) -> Self;
28 /// Returns the lane-wise minimum with `other`.
29 #[must_use = "method returns a new vector and does not mutate the original value"]
30 fn simd_min(self, other: Self) -> Self;
32 /// Restrict each lane to a certain interval.
34 /// For each lane, returns `max` if `self` is greater than `max`, and `min` if `self` is
35 /// less than `min`. Otherwise returns `self`.
39 /// Panics if `min > max` on any lane.
40 #[must_use = "method returns a new vector and does not mutate the original value"]
41 fn simd_clamp(self, min: Self, max: Self) -> Self;
44 macro_rules! impl_integer {
45 { $($integer:ty),* } => {
47 impl<const LANES: usize> SimdPartialOrd for Simd<$integer, LANES>
49 LaneCount<LANES>: SupportedLaneCount,
52 fn simd_lt(self, other: Self) -> Self::Mask {
53 // Safety: `self` is a vector, and the result of the comparison
54 // is always a valid mask.
55 unsafe { Mask::from_int_unchecked(intrinsics::simd_lt(self, other)) }
59 fn simd_le(self, other: Self) -> Self::Mask {
60 // Safety: `self` is a vector, and the result of the comparison
61 // is always a valid mask.
62 unsafe { Mask::from_int_unchecked(intrinsics::simd_le(self, other)) }
66 fn simd_gt(self, other: Self) -> Self::Mask {
67 // Safety: `self` is a vector, and the result of the comparison
68 // is always a valid mask.
69 unsafe { Mask::from_int_unchecked(intrinsics::simd_gt(self, other)) }
73 fn simd_ge(self, other: Self) -> Self::Mask {
74 // Safety: `self` is a vector, and the result of the comparison
75 // is always a valid mask.
76 unsafe { Mask::from_int_unchecked(intrinsics::simd_ge(self, other)) }
80 impl<const LANES: usize> SimdOrd for Simd<$integer, LANES>
82 LaneCount<LANES>: SupportedLaneCount,
85 fn simd_max(self, other: Self) -> Self {
86 self.simd_lt(other).select(other, self)
90 fn simd_min(self, other: Self) -> Self {
91 self.simd_gt(other).select(other, self)
95 fn simd_clamp(self, min: Self, max: Self) -> Self {
97 min.simd_le(max).all(),
98 "each lane in `min` must be less than or equal to the corresponding lane in `max`",
100 self.simd_max(min).simd_min(max)
107 impl_integer! { u8, u16, u32, u64, usize, i8, i16, i32, i64, isize }
109 macro_rules! impl_float {
110 { $($float:ty),* } => {
112 impl<const LANES: usize> SimdPartialOrd for Simd<$float, LANES>
114 LaneCount<LANES>: SupportedLaneCount,
117 fn simd_lt(self, other: Self) -> Self::Mask {
118 // Safety: `self` is a vector, and the result of the comparison
119 // is always a valid mask.
120 unsafe { Mask::from_int_unchecked(intrinsics::simd_lt(self, other)) }
124 fn simd_le(self, other: Self) -> Self::Mask {
125 // Safety: `self` is a vector, and the result of the comparison
126 // is always a valid mask.
127 unsafe { Mask::from_int_unchecked(intrinsics::simd_le(self, other)) }
131 fn simd_gt(self, other: Self) -> Self::Mask {
132 // Safety: `self` is a vector, and the result of the comparison
133 // is always a valid mask.
134 unsafe { Mask::from_int_unchecked(intrinsics::simd_gt(self, other)) }
138 fn simd_ge(self, other: Self) -> Self::Mask {
139 // Safety: `self` is a vector, and the result of the comparison
140 // is always a valid mask.
141 unsafe { Mask::from_int_unchecked(intrinsics::simd_ge(self, other)) }
148 impl_float! { f32, f64 }
150 macro_rules! impl_mask {
151 { $($integer:ty),* } => {
153 impl<const LANES: usize> SimdPartialOrd for Mask<$integer, LANES>
155 LaneCount<LANES>: SupportedLaneCount,
158 fn simd_lt(self, other: Self) -> Self::Mask {
159 // Safety: `self` is a vector, and the result of the comparison
160 // is always a valid mask.
161 unsafe { Self::from_int_unchecked(intrinsics::simd_lt(self.to_int(), other.to_int())) }
165 fn simd_le(self, other: Self) -> Self::Mask {
166 // Safety: `self` is a vector, and the result of the comparison
167 // is always a valid mask.
168 unsafe { Self::from_int_unchecked(intrinsics::simd_le(self.to_int(), other.to_int())) }
172 fn simd_gt(self, other: Self) -> Self::Mask {
173 // Safety: `self` is a vector, and the result of the comparison
174 // is always a valid mask.
175 unsafe { Self::from_int_unchecked(intrinsics::simd_gt(self.to_int(), other.to_int())) }
179 fn simd_ge(self, other: Self) -> Self::Mask {
180 // Safety: `self` is a vector, and the result of the comparison
181 // is always a valid mask.
182 unsafe { Self::from_int_unchecked(intrinsics::simd_ge(self.to_int(), other.to_int())) }
186 impl<const LANES: usize> SimdOrd for Mask<$integer, LANES>
188 LaneCount<LANES>: SupportedLaneCount,
191 fn simd_max(self, other: Self) -> Self {
192 self.simd_gt(other).select_mask(other, self)
196 fn simd_min(self, other: Self) -> Self {
197 self.simd_lt(other).select_mask(other, self)
201 fn simd_clamp(self, min: Self, max: Self) -> Self {
203 min.simd_le(max).all(),
204 "each lane in `min` must be less than or equal to the corresponding lane in `max`",
206 self.simd_max(min).simd_min(max)
213 impl_mask! { i8, i16, i32, i64, isize }