@@ -67,36 +67,54 @@ where
6767 }
6868}
6969
70- macro_rules! impl_min_max_vector {
70+ macro_rules! impl_ord_methods_vector {
7171 { $type: ty } => {
7272 impl <const LANES : usize > Simd <$type, LANES >
7373 where
7474 LaneCount <LANES >: SupportedLaneCount ,
7575 {
76- /// Returns the lane-wise minimum with other
76+ /// Returns the lane-wise minimum with ` other`.
7777 #[ must_use = "method returns a new vector and does not mutate the original value" ]
7878 #[ inline]
7979 pub fn min( self , other: Self ) -> Self {
8080 self . lanes_gt( other) . select( other, self )
8181 }
8282
83- /// Returns the lane-wise maximum with other
83+ /// Returns the lane-wise maximum with ` other`.
8484 #[ must_use = "method returns a new vector and does not mutate the original value" ]
8585 #[ inline]
8686 pub fn max( self , other: Self ) -> Self {
8787 self . lanes_lt( other) . select( other, self )
8888 }
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+ }
89107 }
90108 }
91109}
92110
93- impl_min_max_vector ! ( i8 ) ;
94- impl_min_max_vector ! ( i16 ) ;
95- impl_min_max_vector ! ( i32 ) ;
96- impl_min_max_vector ! ( i64 ) ;
97- impl_min_max_vector ! ( isize ) ;
98- impl_min_max_vector ! ( u8 ) ;
99- impl_min_max_vector ! ( u16 ) ;
100- impl_min_max_vector ! ( u32 ) ;
101- impl_min_max_vector ! ( u64 ) ;
102- impl_min_max_vector ! ( usize ) ;
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