1
1
use super :: sealed:: Sealed ;
2
2
use crate :: simd:: {
3
- intrinsics, LaneCount , Mask , Simd , SimdCast , SimdElement , SimdPartialOrd , SupportedLaneCount ,
3
+ intrinsics, LaneCount , Mask , Simd , SimdCast , SimdElement , SimdPartialOrd , SimdUint ,
4
+ SupportedLaneCount ,
4
5
} ;
5
6
6
7
/// Operations on SIMD vectors of signed integers.
@@ -11,6 +12,9 @@ pub trait SimdInt: Copy + Sealed {
11
12
/// Scalar type contained by this SIMD vector type.
12
13
type Scalar ;
13
14
15
+ /// A SIMD vector of unsigned integers with the same element size.
16
+ type Unsigned ;
17
+
14
18
/// A SIMD vector with a different element type.
15
19
type Cast < T : SimdElement > ;
16
20
@@ -191,10 +195,29 @@ pub trait SimdInt: Copy + Sealed {
191
195
192
196
/// Returns the cumulative bitwise "xor" across the lanes of the vector.
193
197
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 ;
194
217
}
195
218
196
219
macro_rules! impl_trait {
197
- { $( $ty: ty ) ,* } => {
220
+ { $( $ty: ident ( $unsigned : ident ) ) ,* } => {
198
221
$(
199
222
impl <const LANES : usize > Sealed for Simd <$ty, LANES >
200
223
where
@@ -208,6 +231,7 @@ macro_rules! impl_trait {
208
231
{
209
232
type Mask = Mask <<$ty as SimdElement >:: Mask , LANES >;
210
233
type Scalar = $ty;
234
+ type Unsigned = Simd <$unsigned, LANES >;
211
235
type Cast <T : SimdElement > = Simd <T , LANES >;
212
236
213
237
#[ inline]
@@ -307,9 +331,41 @@ macro_rules! impl_trait {
307
331
// Safety: `self` is an integer vector
308
332
unsafe { intrinsics:: simd_reduce_xor( self ) }
309
333
}
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
+ }
310
366
}
311
367
) *
312
368
}
313
369
}
314
370
315
- impl_trait ! { i8 , i16 , i32 , i64 , isize }
371
+ impl_trait ! { i8 ( u8 ) , i16 ( u16 ) , i32 ( u32 ) , i64 ( u64 ) , isize ( usize ) }
0 commit comments