Skip to content

Commit 0a1e745

Browse files
committed
Return unsigned integers from some signed integer functions
1 parent c948b70 commit 0a1e745

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

crates/core_simd/src/elements/int.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::sealed::Sealed;
22
use 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

@@ -200,20 +204,20 @@ pub trait SimdInt: Copy + Sealed {
200204
fn reverse_bits(self) -> Self;
201205

202206
/// Returns the number of leading zeros in the binary representation of each element.
203-
fn leading_zeros(self) -> Self;
207+
fn leading_zeros(self) -> Self::Unsigned;
204208

205209
/// Returns the number of trailing zeros in the binary representation of each element.
206-
fn trailing_zeros(self) -> Self;
210+
fn trailing_zeros(self) -> Self::Unsigned;
207211

208212
/// Returns the number of leading ones in the binary representation of each element.
209-
fn leading_ones(self) -> Self;
213+
fn leading_ones(self) -> Self::Unsigned;
210214

211215
/// Returns the number of trailing ones in the binary representation of each element.
212-
fn trailing_ones(self) -> Self;
216+
fn trailing_ones(self) -> Self::Unsigned;
213217
}
214218

215219
macro_rules! impl_trait {
216-
{ $($ty:ty),* } => {
220+
{ $($ty:ident ($unsigned:ident)),* } => {
217221
$(
218222
impl<const LANES: usize> Sealed for Simd<$ty, LANES>
219223
where
@@ -227,6 +231,7 @@ macro_rules! impl_trait {
227231
{
228232
type Mask = Mask<<$ty as SimdElement>::Mask, LANES>;
229233
type Scalar = $ty;
234+
type Unsigned = Simd<$unsigned, LANES>;
230235
type Cast<T: SimdElement> = Simd<T, LANES>;
231236

232237
#[inline]
@@ -340,29 +345,27 @@ macro_rules! impl_trait {
340345
}
341346

342347
#[inline]
343-
fn leading_zeros(self) -> Self {
344-
// Safety: `self` is an integer vector
345-
unsafe { intrinsics::simd_ctlz(self) }
348+
fn leading_zeros(self) -> Self::Unsigned {
349+
self.cast::<$unsigned>().leading_zeros()
346350
}
347351

348352
#[inline]
349-
fn trailing_zeros(self) -> Self {
350-
// Safety: `self` is an integer vector
351-
unsafe { intrinsics::simd_cttz(self) }
353+
fn trailing_zeros(self) -> Self::Unsigned {
354+
self.cast::<$unsigned>().trailing_zeros()
352355
}
353356

354357
#[inline]
355-
fn leading_ones(self) -> Self {
356-
(!self).leading_zeros()
358+
fn leading_ones(self) -> Self::Unsigned {
359+
self.cast::<$unsigned>().leading_ones()
357360
}
358361

359362
#[inline]
360-
fn trailing_ones(self) -> Self {
361-
(!self).trailing_zeros()
363+
fn trailing_ones(self) -> Self::Unsigned {
364+
self.cast::<$unsigned>().trailing_ones()
362365
}
363366
}
364367
)*
365368
}
366369
}
367370

368-
impl_trait! { i8, i16, i32, i64, isize }
371+
impl_trait! { i8 (u8), i16 (u16), i32 (u32), i64 (u64), isize (usize) }

crates/core_simd/tests/ops_macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -213,31 +213,31 @@ macro_rules! impl_common_integer_tests {
213213
fn leading_zeros<const LANES: usize>() {
214214
test_helpers::test_unary_elementwise(
215215
&$vector::<LANES>::leading_zeros,
216-
&|x| x.leading_zeros() as $scalar,
216+
&|x| x.leading_zeros() as _,
217217
&|_| true,
218218
)
219219
}
220220

221221
fn trailing_zeros<const LANES: usize>() {
222222
test_helpers::test_unary_elementwise(
223223
&$vector::<LANES>::trailing_zeros,
224-
&|x| x.trailing_zeros() as $scalar,
224+
&|x| x.trailing_zeros() as _,
225225
&|_| true,
226226
)
227227
}
228228

229229
fn leading_ones<const LANES: usize>() {
230230
test_helpers::test_unary_elementwise(
231231
&$vector::<LANES>::leading_ones,
232-
&|x| x.leading_ones() as $scalar,
232+
&|x| x.leading_ones() as _,
233233
&|_| true,
234234
)
235235
}
236236

237237
fn trailing_ones<const LANES: usize>() {
238238
test_helpers::test_unary_elementwise(
239239
&$vector::<LANES>::trailing_ones,
240-
&|x| x.trailing_ones() as $scalar,
240+
&|x| x.trailing_ones() as _,
241241
&|_| true,
242242
)
243243
}

0 commit comments

Comments
 (0)