@@ -67,36 +67,54 @@ where
67
67
}
68
68
}
69
69
70
- macro_rules! impl_min_max_vector {
70
+ macro_rules! impl_ord_methods_vector {
71
71
{ $type: ty } => {
72
72
impl <const LANES : usize > Simd <$type, LANES >
73
73
where
74
74
LaneCount <LANES >: SupportedLaneCount ,
75
75
{
76
- /// Returns the lane-wise minimum with other
76
+ /// Returns the lane-wise minimum with ` other`.
77
77
#[ must_use = "method returns a new vector and does not mutate the original value" ]
78
78
#[ inline]
79
79
pub fn min( self , other: Self ) -> Self {
80
80
self . lanes_gt( other) . select( other, self )
81
81
}
82
82
83
- /// Returns the lane-wise maximum with other
83
+ /// Returns the lane-wise maximum with ` other`.
84
84
#[ must_use = "method returns a new vector and does not mutate the original value" ]
85
85
#[ inline]
86
86
pub fn max( self , other: Self ) -> Self {
87
87
self . lanes_lt( other) . select( other, self )
88
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
+ }
89
107
}
90
108
}
91
109
}
92
110
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