11use super :: sealed:: Sealed ;
22use crate :: simd:: {
3- intrinsics, LaneCount , Mask , Simd , SimdCast , SimdElement , SimdPartialOrd , SupportedLaneCount ,
3+ intrinsics, LaneCount , Mask , Simd , SimdCast , SimdElement , SimdPartialOrd , SimdUint ,
4+ SupportedLaneCount ,
45} ;
56
67/// Operations on SIMD vectors of signed integers.
@@ -11,6 +12,9 @@ pub trait SimdInt: Copy + Sealed {
1112 /// Scalar type contained by this SIMD vector type.
1213 type Scalar ;
1314
15+ /// A SIMD vector of unsigned integers with the same element size.
16+ type Unsigned ;
17+
1418 /// A SIMD vector with a different element type.
1519 type Cast < T : SimdElement > ;
1620
@@ -191,10 +195,29 @@ pub trait SimdInt: Copy + Sealed {
191195
192196 /// Returns the cumulative bitwise "xor" across the lanes of the vector.
193197 fn reduce_xor ( self ) -> Self :: Scalar ;
198+
199+ /// Reverses the byte order of each element.
200+ fn swap_bytes ( self ) -> Self ;
201+
202+ /// Reverses the order of bits in each elemnent.
203+ /// The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
204+ fn reverse_bits ( self ) -> Self ;
205+
206+ /// Returns the number of leading zeros in the binary representation of each element.
207+ fn leading_zeros ( self ) -> Self :: Unsigned ;
208+
209+ /// Returns the number of trailing zeros in the binary representation of each element.
210+ fn trailing_zeros ( self ) -> Self :: Unsigned ;
211+
212+ /// Returns the number of leading ones in the binary representation of each element.
213+ fn leading_ones ( self ) -> Self :: Unsigned ;
214+
215+ /// Returns the number of trailing ones in the binary representation of each element.
216+ fn trailing_ones ( self ) -> Self :: Unsigned ;
194217}
195218
196219macro_rules! impl_trait {
197- { $( $ty: ty ) ,* } => {
220+ { $( $ty: ident ( $unsigned : ident ) ) ,* } => {
198221 $(
199222 impl <const LANES : usize > Sealed for Simd <$ty, LANES >
200223 where
@@ -208,6 +231,7 @@ macro_rules! impl_trait {
208231 {
209232 type Mask = Mask <<$ty as SimdElement >:: Mask , LANES >;
210233 type Scalar = $ty;
234+ type Unsigned = Simd <$unsigned, LANES >;
211235 type Cast <T : SimdElement > = Simd <T , LANES >;
212236
213237 #[ inline]
@@ -307,9 +331,41 @@ macro_rules! impl_trait {
307331 // Safety: `self` is an integer vector
308332 unsafe { intrinsics:: simd_reduce_xor( self ) }
309333 }
334+
335+ #[ inline]
336+ fn swap_bytes( self ) -> Self {
337+ // Safety: `self` is an integer vector
338+ unsafe { intrinsics:: simd_bswap( self ) }
339+ }
340+
341+ #[ inline]
342+ fn reverse_bits( self ) -> Self {
343+ // Safety: `self` is an integer vector
344+ unsafe { intrinsics:: simd_bitreverse( self ) }
345+ }
346+
347+ #[ inline]
348+ fn leading_zeros( self ) -> Self :: Unsigned {
349+ self . cast:: <$unsigned>( ) . leading_zeros( )
350+ }
351+
352+ #[ inline]
353+ fn trailing_zeros( self ) -> Self :: Unsigned {
354+ self . cast:: <$unsigned>( ) . trailing_zeros( )
355+ }
356+
357+ #[ inline]
358+ fn leading_ones( self ) -> Self :: Unsigned {
359+ self . cast:: <$unsigned>( ) . leading_ones( )
360+ }
361+
362+ #[ inline]
363+ fn trailing_ones( self ) -> Self :: Unsigned {
364+ self . cast:: <$unsigned>( ) . trailing_ones( )
365+ }
310366 }
311367 ) *
312368 }
313369}
314370
315- impl_trait ! { i8 , i16 , i32 , i64 , isize }
371+ impl_trait ! { i8 ( u8 ) , i16 ( u16 ) , i32 ( u32 ) , i64 ( u64 ) , isize ( usize ) }
0 commit comments