File tree 3 files changed +49
-0
lines changed
3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -219,6 +219,12 @@ pub trait SimdInt: Copy + Sealed {
219
219
/// The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
220
220
fn reverse_bits ( self ) -> Self ;
221
221
222
+ /// Returns the number of ones in the binary representation of each element.
223
+ fn count_ones ( self ) -> Self :: Unsigned ;
224
+
225
+ /// Returns the number of zeros in the binary representation of each element.
226
+ fn count_zeros ( self ) -> Self :: Unsigned ;
227
+
222
228
/// Returns the number of leading zeros in the binary representation of each element.
223
229
fn leading_zeros ( self ) -> Self :: Unsigned ;
224
230
@@ -367,6 +373,16 @@ macro_rules! impl_trait {
367
373
unsafe { core:: intrinsics:: simd:: simd_bitreverse( self ) }
368
374
}
369
375
376
+ #[ inline]
377
+ fn count_ones( self ) -> Self :: Unsigned {
378
+ self . cast:: <$unsigned>( ) . count_ones( )
379
+ }
380
+
381
+ #[ inline]
382
+ fn count_zeros( self ) -> Self :: Unsigned {
383
+ self . cast:: <$unsigned>( ) . count_zeros( )
384
+ }
385
+
370
386
#[ inline]
371
387
fn leading_zeros( self ) -> Self :: Unsigned {
372
388
self . cast:: <$unsigned>( ) . leading_zeros( )
Original file line number Diff line number Diff line change @@ -101,6 +101,12 @@ pub trait SimdUint: Copy + Sealed {
101
101
/// The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
102
102
fn reverse_bits ( self ) -> Self ;
103
103
104
+ /// Returns the number of ones in the binary representation of each element.
105
+ fn count_ones ( self ) -> Self ;
106
+
107
+ /// Returns the number of zeros in the binary representation of each element.
108
+ fn count_zeros ( self ) -> Self ;
109
+
104
110
/// Returns the number of leading zeros in the binary representation of each element.
105
111
fn leading_zeros ( self ) -> Self ;
106
112
@@ -215,6 +221,17 @@ macro_rules! impl_trait {
215
221
unsafe { core:: intrinsics:: simd:: simd_bitreverse( self ) }
216
222
}
217
223
224
+ #[ inline]
225
+ fn count_ones( self ) -> Self {
226
+ // Safety: `self` is an integer vector
227
+ unsafe { core:: intrinsics:: simd:: simd_ctpop( self ) }
228
+ }
229
+
230
+ #[ inline]
231
+ fn count_zeros( self ) -> Self {
232
+ ( !self ) . count_ones( )
233
+ }
234
+
218
235
#[ inline]
219
236
fn leading_zeros( self ) -> Self {
220
237
// Safety: `self` is an integer vector
Original file line number Diff line number Diff line change @@ -216,6 +216,22 @@ macro_rules! impl_common_integer_tests {
216
216
)
217
217
}
218
218
219
+ fn count_ones<const LANES : usize >( ) {
220
+ test_helpers:: test_unary_elementwise(
221
+ & $vector:: <LANES >:: count_ones,
222
+ & |x| x. count_ones( ) as _,
223
+ & |_| true ,
224
+ )
225
+ }
226
+
227
+ fn count_zeros<const LANES : usize >( ) {
228
+ test_helpers:: test_unary_elementwise(
229
+ & $vector:: <LANES >:: count_zeros,
230
+ & |x| x. count_zeros( ) as _,
231
+ & |_| true ,
232
+ )
233
+ }
234
+
219
235
fn leading_zeros<const LANES : usize >( ) {
220
236
test_helpers:: test_unary_elementwise(
221
237
& $vector:: <LANES >:: leading_zeros,
You can’t perform that action at this time.
0 commit comments