|
66 | 66 | unsafe { Mask::from_int_unchecked(intrinsics::simd_ge(self, other)) }
|
67 | 67 | }
|
68 | 68 | }
|
| 69 | + |
| 70 | +macro_rules! impl_ord_methods_vector { |
| 71 | + { $type:ty } => { |
| 72 | + impl<const LANES: usize> Simd<$type, LANES> |
| 73 | + where |
| 74 | + LaneCount<LANES>: SupportedLaneCount, |
| 75 | + { |
| 76 | + /// Returns the lane-wise minimum with `other`. |
| 77 | + #[must_use = "method returns a new vector and does not mutate the original value"] |
| 78 | + #[inline] |
| 79 | + pub fn min(self, other: Self) -> Self { |
| 80 | + self.lanes_gt(other).select(other, self) |
| 81 | + } |
| 82 | + |
| 83 | + /// Returns the lane-wise maximum with `other`. |
| 84 | + #[must_use = "method returns a new vector and does not mutate the original value"] |
| 85 | + #[inline] |
| 86 | + pub fn max(self, other: Self) -> Self { |
| 87 | + self.lanes_lt(other).select(other, self) |
| 88 | + } |
| 89 | + |
| 90 | + /// Restrict each lane to a certain interval. |
| 91 | + /// |
| 92 | + /// For each lane, returns `max` if `self` is greater than `max`, and `min` if `self` is |
| 93 | + /// less than `min`. Otherwise returns `self`. |
| 94 | + /// |
| 95 | + /// # Panics |
| 96 | + /// |
| 97 | + /// Panics if `min > max` on any lane. |
| 98 | + #[must_use = "method returns a new vector and does not mutate the original value"] |
| 99 | + #[inline] |
| 100 | + pub fn clamp(self, min: Self, max: Self) -> Self { |
| 101 | + assert!( |
| 102 | + min.lanes_le(max).all(), |
| 103 | + "each lane in `min` must be less than or equal to the corresponding lane in `max`", |
| 104 | + ); |
| 105 | + self.max(min).min(max) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl_ord_methods_vector!(i8); |
| 112 | +impl_ord_methods_vector!(i16); |
| 113 | +impl_ord_methods_vector!(i32); |
| 114 | +impl_ord_methods_vector!(i64); |
| 115 | +impl_ord_methods_vector!(isize); |
| 116 | +impl_ord_methods_vector!(u8); |
| 117 | +impl_ord_methods_vector!(u16); |
| 118 | +impl_ord_methods_vector!(u32); |
| 119 | +impl_ord_methods_vector!(u64); |
| 120 | +impl_ord_methods_vector!(usize); |
0 commit comments