From 0e11ad0b69966e7606f37c78f5e35c2cf6c2b48d Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Thu, 29 Oct 2026 11:32:21 +0100 Subject: [PATCH 1/3] Add associated types for signed/unsigned `SimdInt`s --- fearless_simd/src/generated/simd_trait.rs | 46 ++++++++++++++++++++++ fearless_simd/src/generated/simd_types.rs | 48 +++++++++++++++++++++++ fearless_simd/src/traits.rs | 25 ++++++++++++ fearless_simd_gen/src/mk_simd_trait.rs | 41 +++++++++++++++++++ fearless_simd_gen/src/mk_simd_types.rs | 23 ++++++++--- 5 files changed, 177 insertions(+), 6 deletions(-) diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index e5ec3dbc..00d99f41 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -13038,6 +13038,32 @@ pub trait SimdInt: + core::ops::Shr + core::ops::ShrAssign { + #[doc = r" Corresponding unsigned integer SIMD vector type. E.g. `u8x16` for `i8x16`."] + #[doc = r""] + #[doc = r" This type is `Self` if the SIMD vector is already unsigned."] + type Unsigned: SimdInt< + S, + Element = ::Unsigned, + ByteVector = Self::ByteVector, + >; + #[doc = r" Corresponding signed integer SIMD vector type. E.g. `i8x16` for `u8x16`."] + #[doc = r""] + #[doc = r" This type is `Self` if the SIMD vector is already signed."] + type Signed: SimdInt< + S, + Element = ::Signed, + ByteVector = Self::ByteVector, + >; + #[doc = r" Reinterpret this integer SIMD vector as its corresponding signed type."] + #[inline(always)] + fn cast_signed(self) -> Self::Signed { + self.bitcast() + } + #[doc = r" Reinterpret this integer SIMD vector as its corresponding unsigned type."] + #[inline(always)] + fn cast_unsigned(self) -> Self::Unsigned { + self.bitcast() + } #[doc = r" Convert this integer type to a floating-point type. This is a convenience method"] #[doc = r" that delegates to [`SimdCvtFloat::float_from`], and can only be called if there"] #[doc = r" actually exists a target type of the same bit width (`f32` or `f64`)."] @@ -13045,6 +13071,26 @@ pub trait SimdInt: fn to_float>(self) -> T { T::float_from(self) } + #[doc = r" Return the absolute value of `self` without any wrapping."] + #[doc = r""] + #[doc = r" If `Self` is an unsigned SIMD vector, this always produces the same result as `self.abs()`."] + #[doc = r""] + #[doc = r" ## Example"] + #[doc = r""] + #[doc = r" ```"] + #[doc = r" # use fearless_simd::*;"] + #[doc = r" # dispatch!(Level::new(), simd => {"] + #[doc = r" let i = i32x4::from_slice(simd, &[100, -100, i32::MAX, i32::MIN]);"] + #[doc = r" assert_eq!("] + #[doc = r" *i.unsigned_abs(),"] + #[doc = r" [100, 100, 2147483647, 2147483648],"] + #[doc = r" );"] + #[doc = r" # });"] + #[doc = r" ```"] + #[inline(always)] + fn unsigned_abs(self) -> Self::Unsigned { + self.abs().cast_unsigned() + } #[doc = "Return the number of ones in the binary representation of each element."] fn count_ones(self) -> Self; #[doc = "Return the number of zeros in the binary representation of each element."] diff --git a/fearless_simd/src/generated/simd_types.rs b/fearless_simd/src/generated/simd_types.rs index 1b26a420..76262189 100644 --- a/fearless_simd/src/generated/simd_types.rs +++ b/fearless_simd/src/generated/simd_types.rs @@ -752,6 +752,8 @@ impl SimdBase for i8x16 { } } impl crate::SimdInt for i8x16 { + type Unsigned = u8x16; + type Signed = i8x16; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i8x16(self) @@ -1139,6 +1141,8 @@ impl SimdBase for u8x16 { } } impl crate::SimdInt for u8x16 { + type Unsigned = u8x16; + type Signed = i8x16; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u8x16(self) @@ -1647,6 +1651,8 @@ impl SimdBase for i16x8 { } } impl crate::SimdInt for i16x8 { + type Unsigned = u16x8; + type Signed = i16x8; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i16x8(self) @@ -2041,6 +2047,8 @@ impl SimdBase for u16x8 { } } impl crate::SimdInt for u16x8 { + type Unsigned = u16x8; + type Signed = i16x8; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u16x8(self) @@ -2559,6 +2567,8 @@ impl SimdBase for i32x4 { } } impl crate::SimdInt for i32x4 { + type Unsigned = u32x4; + type Signed = i32x4; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i32x4(self) @@ -2953,6 +2963,8 @@ impl SimdBase for u32x4 { } } impl crate::SimdInt for u32x4 { + type Unsigned = u32x4; + type Signed = i32x4; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u32x4(self) @@ -3920,6 +3932,8 @@ impl SimdBase for i64x2 { } } impl crate::SimdInt for i64x2 { + type Unsigned = u64x2; + type Signed = i64x2; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i64x2(self) @@ -4288,6 +4302,8 @@ impl SimdBase for u64x2 { } } impl crate::SimdInt for u64x2 { + type Unsigned = u64x2; + type Signed = i64x2; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u64x2(self) @@ -5257,6 +5273,8 @@ impl SimdBase for i8x32 { } } impl crate::SimdInt for i8x32 { + type Unsigned = u8x32; + type Signed = i8x32; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i8x32(self) @@ -5655,6 +5673,8 @@ impl SimdBase for u8x32 { } } impl crate::SimdInt for u8x32 { + type Unsigned = u8x32; + type Signed = i8x32; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u8x32(self) @@ -6167,6 +6187,8 @@ impl SimdBase for i16x16 { } } impl crate::SimdInt for i16x16 { + type Unsigned = u16x16; + type Signed = i16x16; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i16x16(self) @@ -6567,6 +6589,8 @@ impl SimdBase for u16x16 { } } impl crate::SimdInt for u16x16 { + type Unsigned = u16x16; + type Signed = i16x16; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u16x16(self) @@ -7094,6 +7118,8 @@ impl SimdBase for i32x8 { } } impl crate::SimdInt for i32x8 { + type Unsigned = u32x8; + type Signed = i32x8; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i32x8(self) @@ -7495,6 +7521,8 @@ impl SimdBase for u32x8 { } } impl crate::SimdInt for u32x8 { + type Unsigned = u32x8; + type Signed = i32x8; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u32x8(self) @@ -8452,6 +8480,8 @@ impl SimdBase for i64x4 { } } impl crate::SimdInt for i64x4 { + type Unsigned = u64x4; + type Signed = i64x4; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i64x4(self) @@ -8815,6 +8845,8 @@ impl SimdBase for u64x4 { } } impl crate::SimdInt for u64x4 { + type Unsigned = u64x4; + type Signed = i64x4; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u64x4(self) @@ -9815,6 +9847,8 @@ impl SimdBase for i8x64 { } } impl crate::SimdInt for i8x64 { + type Unsigned = u8x64; + type Signed = i8x64; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i8x64(self) @@ -10239,6 +10273,8 @@ impl SimdBase for u8x64 { } } impl crate::SimdInt for u8x64 { + type Unsigned = u8x64; + type Signed = i8x64; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u8x64(self) @@ -10761,6 +10797,8 @@ impl SimdBase for i16x32 { } } impl crate::SimdInt for i16x32 { + type Unsigned = u16x32; + type Signed = i16x32; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i16x32(self) @@ -11171,6 +11209,8 @@ impl SimdBase for u16x32 { } } impl crate::SimdInt for u16x32 { + type Unsigned = u16x32; + type Signed = i16x32; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u16x32(self) @@ -11701,6 +11741,8 @@ impl SimdBase for i32x16 { } } impl crate::SimdInt for i32x16 { + type Unsigned = u32x16; + type Signed = i32x16; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i32x16(self) @@ -12107,6 +12149,8 @@ impl SimdBase for u32x16 { } } impl crate::SimdInt for u32x16 { + type Unsigned = u32x16; + type Signed = i32x16; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u32x16(self) @@ -13078,6 +13122,8 @@ impl SimdBase for i64x8 { } } impl crate::SimdInt for i64x8 { + type Unsigned = u64x8; + type Signed = i64x8; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_i64x8(self) @@ -13447,6 +13493,8 @@ impl SimdBase for u64x8 { } } impl crate::SimdInt for u64x8 { + type Unsigned = u64x8; + type Signed = i64x8; #[inline(always)] fn count_ones(self) -> Self { self.simd.count_ones_u64x8(self) diff --git a/fearless_simd/src/traits.rs b/fearless_simd/src/traits.rs index fc008fc1..c2cf5820 100644 --- a/fearless_simd/src/traits.rs +++ b/fearless_simd/src/traits.rs @@ -334,31 +334,56 @@ pub trait SimdIntElement: /// Its lane count depends on both the scalar type and the backend, and is /// available as `T::Native::::LEN` with [`SimdBase`] in scope. type Native: SimdInt; + + /// The corresponding unsigned integer type for this signed integer type. E.g. `u8` for `i8`. + /// + /// If `Self` is already unsigned, this type will also be `Self`. + type Unsigned: SimdIntElement; + /// The corresponding signed integer type for this unsigned integer type. E.g. `i8` for `u8`. + /// + /// If `Self` is already signed, this type will also be `Self`. + type Signed: SimdIntElement; } impl SimdIntElement for u8 { type Native = S::u8s; + type Unsigned = Self; + type Signed = i8; } impl SimdIntElement for u16 { type Native = S::u16s; + type Unsigned = Self; + type Signed = i16; } impl SimdIntElement for u32 { type Native = S::u32s; + type Unsigned = Self; + type Signed = i32; } impl SimdIntElement for u64 { type Native = S::u64s; + type Unsigned = Self; + type Signed = i64; } impl SimdIntElement for i8 { type Native = S::i8s; + type Unsigned = u8; + type Signed = Self; } impl SimdIntElement for i16 { type Native = S::i16s; + type Unsigned = u16; + type Signed = Self; } impl SimdIntElement for i32 { type Native = S::i32s; + type Unsigned = u32; + type Signed = Self; } impl SimdIntElement for i64 { type Native = S::i64s; + type Unsigned = u64; + type Signed = Self; } /// Types that can be used as elements in float SIMD vectors. diff --git a/fearless_simd_gen/src/mk_simd_trait.rs b/fearless_simd_gen/src/mk_simd_trait.rs index 08641cea..583ab2d1 100644 --- a/fearless_simd_gen/src/mk_simd_trait.rs +++ b/fearless_simd_gen/src/mk_simd_trait.rs @@ -509,12 +509,53 @@ fn mk_simd_int() -> TokenStream { pub trait SimdInt: SimdBase + Seal #(+ #op_traits)* { + /// Corresponding unsigned integer SIMD vector type. E.g. `u8x16` for `i8x16`. + /// + /// This type is `Self` if the SIMD vector is already unsigned. + type Unsigned: SimdInt::Unsigned, ByteVector = Self::ByteVector>; + /// Corresponding signed integer SIMD vector type. E.g. `i8x16` for `u8x16`. + /// + /// This type is `Self` if the SIMD vector is already signed. + type Signed: SimdInt::Signed, ByteVector = Self::ByteVector>; + + /// Reinterpret this integer SIMD vector as its corresponding signed type. + #[inline(always)] + fn cast_signed(self) -> Self::Signed { + self.bitcast() + } + /// Reinterpret this integer SIMD vector as its corresponding unsigned type. + #[inline(always)] + fn cast_unsigned(self) -> Self::Unsigned { + self.bitcast() + } + /// Convert this integer type to a floating-point type. This is a convenience method /// that delegates to [`SimdCvtFloat::float_from`], and can only be called if there /// actually exists a target type of the same bit width (`f32` or `f64`). #[inline(always)] fn to_float>(self) -> T { T::float_from(self) } + /// Return the absolute value of `self` without any wrapping. + /// + /// If `Self` is an unsigned SIMD vector, this always produces the same result as `self.abs()`. + /// + /// ## Example + /// + /// ``` + /// # use fearless_simd::*; + /// # dispatch!(Level::new(), simd => { + /// let i = i32x4::from_slice(simd, &[100, -100, i32::MAX, i32::MIN]); + /// assert_eq!( + /// *i.unsigned_abs(), + /// [100, 100, 2147483647, 2147483648], + /// ); + /// # }); + /// ``` + #[inline(always)] + fn unsigned_abs(self) -> Self::Unsigned { + self.abs().cast_unsigned() + } + #( #methods )* } } diff --git a/fearless_simd_gen/src/mk_simd_types.rs b/fearless_simd_gen/src/mk_simd_types.rs index 55798c5f..09b8df76 100644 --- a/fearless_simd_gen/src/mk_simd_types.rs +++ b/fearless_simd_gen/src/mk_simd_types.rs @@ -705,12 +705,23 @@ fn simd_vec_scalar_type_impl(ty: &VecType) -> TokenStream { let vec_trait_id = Ident::new(vec_trait, Span::call_site()); let mut types = vec![]; - if ty.scalar == ScalarType::Float { - let bits = ty.cast(ScalarType::Unsigned).rust(); - types.push(quote! { - type Bits = #bits; - }); - } + match ty.scalar { + ScalarType::Float => { + let bits = ty.cast(ScalarType::Unsigned).rust(); + types.push(quote! { + type Bits = #bits; + }); + } + ScalarType::Unsigned | ScalarType::Int => { + let unsigned = ty.cast(ScalarType::Unsigned).rust(); + let signed = ty.cast(ScalarType::Int).rust(); + types.push(quote! { + type Unsigned = #unsigned; + type Signed = #signed; + }); + } + ScalarType::Mask => unreachable!(), + }; let mut methods = vec![]; for op in vec_trait_ops_for(ty.scalar) { From a4f8d2849f03923c39b5122f9c7c0da7609d23d5 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 21 Nov 2026 01:31:35 +0100 Subject: [PATCH 2/3] Stricter generic bounds --- fearless_simd/src/generated/simd_trait.rs | 26 +++++++++++++++ fearless_simd/src/traits.rs | 11 +++++-- fearless_simd_gen/src/mk_simd_trait.rs | 40 +++++++++++++++-------- fearless_simd_tests/tests/generics.rs | 12 +++++++ 4 files changed, 73 insertions(+), 16 deletions(-) diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index 17d687ff..aa4ac8b4 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -69,6 +69,7 @@ pub trait Simd: Block = f32x4, Mask = Self::mask32s, ByteVector = Self::u8s, + Bits = Self::u32s, > + SimdCvtFloat + SimdCvtFloat + SimdWiden; @@ -79,6 +80,7 @@ pub trait Simd: Block = f64x2, Mask = Self::mask64s, ByteVector = Self::u8s, + Bits = Self::u64s, > + SimdCvtFloat + SimdCvtFloat + SimdNarrow; @@ -89,6 +91,8 @@ pub trait Simd: Block = u8x16, Mask = Self::mask8s, ByteVector = Self::u8s, + Unsigned = Self::u8s, + Signed = Self::i8s, > + SimdIntWiden; #[doc = r" A native-width SIMD vector of [`i8`]s."] type i8s: SimdInt< @@ -97,6 +101,8 @@ pub trait Simd: Block = i8x16, Mask = Self::mask8s, ByteVector = Self::u8s, + Unsigned = Self::u8s, + Signed = Self::i8s, > + SimdIntWiden + core::ops::Neg; #[doc = r" A native-width SIMD vector of [`u16`]s."] @@ -106,6 +112,8 @@ pub trait Simd: Block = u16x8, Mask = Self::mask16s, ByteVector = Self::u8s, + Unsigned = Self::u16s, + Signed = Self::i16s, > + SimdNarrow + SimdIntWiden; #[doc = r" A native-width SIMD vector of [`i16`]s."] @@ -115,6 +123,8 @@ pub trait Simd: Block = i16x8, Mask = Self::mask16s, ByteVector = Self::u8s, + Unsigned = Self::u16s, + Signed = Self::i16s, > + SimdNarrow + SimdIntWiden + core::ops::Neg; @@ -125,6 +135,8 @@ pub trait Simd: Block = u32x4, Mask = Self::mask32s, ByteVector = Self::u8s, + Unsigned = Self::u32s, + Signed = Self::i32s, > + SimdCvtTruncate + SimdNarrow + SimdIntWiden; @@ -135,6 +147,8 @@ pub trait Simd: Block = i32x4, Mask = Self::mask32s, ByteVector = Self::u8s, + Unsigned = Self::u32s, + Signed = Self::i32s, > + SimdCvtTruncate + SimdNarrow + SimdIntWiden @@ -146,6 +160,8 @@ pub trait Simd: Block = u64x2, Mask = Self::mask64s, ByteVector = Self::u8s, + Unsigned = Self::u64s, + Signed = Self::i64s, > + SimdCvtTruncate + SimdNarrow; #[doc = r" A native-width SIMD vector of [`i64`]s."] @@ -155,6 +171,8 @@ pub trait Simd: Block = i64x2, Mask = Self::mask64s, ByteVector = Self::u8s, + Unsigned = Self::u64s, + Signed = Self::i64s, > + SimdCvtTruncate + SimdNarrow + core::ops::Neg; @@ -13067,6 +13085,8 @@ pub trait SimdFloat: S, Element = ::Bits, ByteVector = Self::ByteVector, + Mask = Self::Mask, + Unsigned = Self::Bits, >; #[doc = r" Raw transmutation to an unsigned integer vector type with the same size and number of elements."] #[doc = r""] @@ -13197,6 +13217,9 @@ pub trait SimdInt: S, Element = ::Unsigned, ByteVector = Self::ByteVector, + Unsigned = Self::Unsigned, + Signed = Self::Signed, + Mask = Self::Mask, >; #[doc = r" Corresponding signed integer SIMD vector type. E.g. `i8x16` for `u8x16`."] #[doc = r""] @@ -13205,6 +13228,9 @@ pub trait SimdInt: S, Element = ::Signed, ByteVector = Self::ByteVector, + Unsigned = Self::Unsigned, + Signed = Self::Signed, + Mask = Self::Mask, >; #[doc = r" Reinterpret this integer SIMD vector as its corresponding signed type."] #[inline(always)] diff --git a/fearless_simd/src/traits.rs b/fearless_simd/src/traits.rs index c2cf5820..27c95c66 100644 --- a/fearless_simd/src/traits.rs +++ b/fearless_simd/src/traits.rs @@ -333,16 +333,21 @@ pub trait SimdIntElement: /// /// Its lane count depends on both the scalar type and the backend, and is /// available as `T::Native::::LEN` with [`SimdBase`] in scope. - type Native: SimdInt; + type Native: SimdInt< + S, + Element = Self, + Unsigned = ::Native, + Signed = ::Native, + >; /// The corresponding unsigned integer type for this signed integer type. E.g. `u8` for `i8`. /// /// If `Self` is already unsigned, this type will also be `Self`. - type Unsigned: SimdIntElement; + type Unsigned: SimdIntElement; /// The corresponding signed integer type for this unsigned integer type. E.g. `i8` for `u8`. /// /// If `Self` is already signed, this type will also be `Self`. - type Signed: SimdIntElement; + type Signed: SimdIntElement; } impl SimdIntElement for u8 { diff --git a/fearless_simd_gen/src/mk_simd_trait.rs b/fearless_simd_gen/src/mk_simd_trait.rs index d61805a7..942f2aa4 100644 --- a/fearless_simd_gen/src/mk_simd_trait.rs +++ b/fearless_simd_gen/src/mk_simd_trait.rs @@ -131,39 +131,39 @@ pub(crate) fn mk_simd_trait() -> TokenStream { /// ``` pub trait Simd: ExtractToken + Sized + Clone + Copy + Send + Sync + Debug + Seal + arch_types::ArchTypes + 'static { /// A native-width SIMD vector of [`f32`]s. - type f32s: SimdFloat, Mask = Self::mask32s, ByteVector = Self::u8s> + SimdCvtFloat + SimdCvtFloat + type f32s: SimdFloat, Mask = Self::mask32s, ByteVector = Self::u8s, Bits = Self::u32s> + SimdCvtFloat + SimdCvtFloat + SimdWiden; /// A native-width SIMD vector of [`f64`]s. - type f64s: SimdFloat, Mask = Self::mask64s, ByteVector = Self::u8s> + type f64s: SimdFloat, Mask = Self::mask64s, ByteVector = Self::u8s, Bits = Self::u64s> + SimdCvtFloat + SimdCvtFloat + SimdNarrow; /// A native-width SIMD vector of [`u8`]s. - type u8s: SimdInt, Mask = Self::mask8s, ByteVector = Self::u8s> + type u8s: SimdInt, Mask = Self::mask8s, ByteVector = Self::u8s, Unsigned = Self::u8s, Signed = Self::i8s> + SimdIntWiden; /// A native-width SIMD vector of [`i8`]s. - type i8s: SimdInt, Mask = Self::mask8s, ByteVector = Self::u8s> + type i8s: SimdInt, Mask = Self::mask8s, ByteVector = Self::u8s, Unsigned = Self::u8s, Signed = Self::i8s> + SimdIntWiden + core::ops::Neg; /// A native-width SIMD vector of [`u16`]s. - type u16s: SimdInt, Mask = Self::mask16s, ByteVector = Self::u8s> + type u16s: SimdInt, Mask = Self::mask16s, ByteVector = Self::u8s, Unsigned = Self::u16s, Signed = Self::i16s> + SimdNarrow + SimdIntWiden; /// A native-width SIMD vector of [`i16`]s. - type i16s: SimdInt, Mask = Self::mask16s, ByteVector = Self::u8s> + type i16s: SimdInt, Mask = Self::mask16s, ByteVector = Self::u8s, Unsigned = Self::u16s, Signed = Self::i16s> + SimdNarrow + SimdIntWiden + core::ops::Neg; /// A native-width SIMD vector of [`u32`]s. - type u32s: SimdInt, Mask = Self::mask32s, ByteVector = Self::u8s> + type u32s: SimdInt, Mask = Self::mask32s, ByteVector = Self::u8s, Unsigned = Self::u32s, Signed = Self::i32s> + SimdCvtTruncate + SimdNarrow + SimdIntWiden; /// A native-width SIMD vector of [`i32`]s. - type i32s: SimdInt, Mask = Self::mask32s, ByteVector = Self::u8s> + SimdCvtTruncate + type i32s: SimdInt, Mask = Self::mask32s, ByteVector = Self::u8s, Unsigned = Self::u32s, Signed = Self::i32s> + SimdCvtTruncate + SimdNarrow + SimdIntWiden + core::ops::Neg; /// A native-width SIMD vector of [`u64`]s. - type u64s: SimdInt, Mask = Self::mask64s, ByteVector = Self::u8s> + type u64s: SimdInt, Mask = Self::mask64s, ByteVector = Self::u8s, Unsigned = Self::u64s, Signed = Self::i64s> + SimdCvtTruncate + SimdNarrow; /// A native-width SIMD vector of [`i64`]s. - type i64s: SimdInt, Mask = Self::mask64s, ByteVector = Self::u8s> + type i64s: SimdInt, Mask = Self::mask64s, ByteVector = Self::u8s, Unsigned = Self::u64s, Signed = Self::i64s> + SimdCvtTruncate + SimdNarrow + core::ops::Neg; @@ -473,7 +473,7 @@ fn mk_simd_float() -> TokenStream { #(+ #op_traits)* { /// Bit representation of this SIMD float vector type. - type Bits: SimdInt::Bits, ByteVector = Self::ByteVector>; + type Bits: SimdInt::Bits, ByteVector = Self::ByteVector, Mask = Self::Mask, Unsigned = Self::Bits>; /// Raw transmutation to an unsigned integer vector type with the same size and number of elements. /// /// This is analogous to [`f32::to_bits`] and [`f64::to_bits`]. @@ -545,11 +545,25 @@ fn mk_simd_int() -> TokenStream { /// Corresponding unsigned integer SIMD vector type. E.g. `u8x16` for `i8x16`. /// /// This type is `Self` if the SIMD vector is already unsigned. - type Unsigned: SimdInt::Unsigned, ByteVector = Self::ByteVector>; + type Unsigned: SimdInt< + S, + Element = ::Unsigned, + ByteVector = Self::ByteVector, + Unsigned = Self::Unsigned, + Signed = Self::Signed, + Mask = Self::Mask, + >; /// Corresponding signed integer SIMD vector type. E.g. `i8x16` for `u8x16`. /// /// This type is `Self` if the SIMD vector is already signed. - type Signed: SimdInt::Signed, ByteVector = Self::ByteVector>; + type Signed: SimdInt< + S, + Element = ::Signed, + ByteVector = Self::ByteVector, + Unsigned = Self::Unsigned, + Signed = Self::Signed, + Mask = Self::Mask, + >; /// Reinterpret this integer SIMD vector as its corresponding signed type. #[inline(always)] diff --git a/fearless_simd_tests/tests/generics.rs b/fearless_simd_tests/tests/generics.rs index 2bfcdab8..4f5c6290 100644 --- a/fearless_simd_tests/tests/generics.rs +++ b/fearless_simd_tests/tests/generics.rs @@ -90,6 +90,18 @@ fn generic_float_bits_operations>(x: F) F::from_bits(bits & ABS_MASK) } +fn generic_int_signed_cast_roundtrip>(x: T) -> T::Signed { + x.cast_unsigned().cast_signed() +} +fn generic_int_signed_cast_idempotent>(x: T::Unsigned) -> T::Unsigned { + x.cast_unsigned() +} +fn generic_int_signed_cast_native_i32_to_u32( + x: ::Native, +) -> ::Native { + x.cast_unsigned() +} + // Ensure that a generic vector's mask can select between vectors of that type fn generic_select>(mask: V::Mask, if_true: V, if_false: V) -> V { mask.select(if_true, if_false) From 013a80ea0902d44da3cda13857196f059f3172a4 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Sat, 21 Nov 2026 02:06:14 +0100 Subject: [PATCH 3/3] Add tests for signedness casts --- .../tests/harness/ops/cast_signed_unsigned.rs | 383 ++++++++++++++++++ fearless_simd_tests/tests/harness/ops/mod.rs | 1 + 2 files changed, 384 insertions(+) create mode 100644 fearless_simd_tests/tests/harness/ops/cast_signed_unsigned.rs diff --git a/fearless_simd_tests/tests/harness/ops/cast_signed_unsigned.rs b/fearless_simd_tests/tests/harness/ops/cast_signed_unsigned.rs new file mode 100644 index 00000000..89981841 --- /dev/null +++ b/fearless_simd_tests/tests/harness/ops/cast_signed_unsigned.rs @@ -0,0 +1,383 @@ +// Copyright 2026 the Fearless_SIMD Authors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use fearless_simd::*; +use fearless_simd_dev_macros::simd_test; + +fn get_data_signed< + T: From + Copy + std::ops::Div + std::ops::AddAssign, + const N: usize, +>( + min: T, + max: T, +) -> [T; N] { + let mut arr = [T::from(0); N]; + let mut div = T::from(1); + #[allow(clippy::needless_range_loop, reason = "it's easier")] + for i in 0..N { + let x = if i.is_multiple_of(2) { min } else { max }; + arr[i] = x / div; + if !i.is_multiple_of(2) { + div += T::from(1); + } + } + arr +} +fn get_data_unsigned< + T: From + Copy + std::ops::Div + std::ops::AddAssign, + const N: usize, +>( + max: T, +) -> [T; N] { + let mut arr = [T::from(0); N]; + let mut div = T::from(1); + #[allow(clippy::needless_range_loop, reason = "it's easier")] + for i in 0..N { + let x = if i.is_multiple_of(2) { div } else { max / div }; + arr[i] = x; + if !i.is_multiple_of(2) { + div += T::from(1); + } + } + arr +} + +// One concrete test row per supported vector type. + +#[simd_test] +fn cast_unsigned_i8x16(simd: S) { + let data = get_data_signed::(i8::MIN, i8::MAX); + let x = i8x16::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i8x32(simd: S) { + let data = get_data_signed::(i8::MIN, i8::MAX); + let x = i8x32::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i8x64(simd: S) { + let data = get_data_signed::(i8::MIN, i8::MAX); + let x = i8x64::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i16x8(simd: S) { + let data = get_data_signed::(i16::MIN, i16::MAX); + let x = i16x8::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i16x16(simd: S) { + let data = get_data_signed::(i16::MIN, i16::MAX); + let x = i16x16::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i16x32(simd: S) { + let data = get_data_signed::(i16::MIN, i16::MAX); + let x = i16x32::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i32x4(simd: S) { + let data = get_data_signed::(i32::MIN, i32::MAX); + let x = i32x4::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i32x8(simd: S) { + let data = get_data_signed::(i32::MIN, i32::MAX); + let x = i32x8::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i32x16(simd: S) { + let data = get_data_signed::(i32::MIN, i32::MAX); + let x = i32x16::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i64x2(simd: S) { + let data = get_data_signed::(i64::MIN, i64::MAX); + let x = i64x2::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i64x4(simd: S) { + let data = get_data_signed::(i64::MIN, i64::MAX); + let x = i64x4::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_unsigned_i64x8(simd: S) { + let data = get_data_signed::(i64::MIN, i64::MAX); + let x = i64x8::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data.map(|v| v.cast_unsigned())); +} + +#[simd_test] +fn cast_signed_u8x16(simd: S) { + let data = get_data_unsigned::(u8::MAX); + let x = u8x16::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u8x32(simd: S) { + let data = get_data_unsigned::(u8::MAX); + let x = u8x32::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u8x64(simd: S) { + let data = get_data_unsigned::(u8::MAX); + let x = u8x64::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u16x8(simd: S) { + let data = get_data_unsigned::(u16::MAX); + let x = u16x8::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u16x16(simd: S) { + let data = get_data_unsigned::(u16::MAX); + let x = u16x16::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u16x32(simd: S) { + let data = get_data_unsigned::(u16::MAX); + let x = u16x32::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u32x4(simd: S) { + let data = get_data_unsigned::(u32::MAX); + let x = u32x4::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u32x8(simd: S) { + let data = get_data_unsigned::(u32::MAX); + let x = u32x8::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u32x16(simd: S) { + let data = get_data_unsigned::(u32::MAX); + let x = u32x16::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u64x2(simd: S) { + let data = get_data_unsigned::(u64::MAX); + let x = u64x2::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u64x4(simd: S) { + let data = get_data_unsigned::(u64::MAX); + let x = u64x4::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +#[simd_test] +fn cast_signed_u64x8(simd: S) { + let data = get_data_unsigned::(u64::MAX); + let x = u64x8::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data.map(|v| v.cast_signed())); +} + +// Now the types where casting is a noop + +#[simd_test] +fn cast_signed_i8x16(simd: S) { + let data = get_data_signed::(i8::MIN, i8::MAX); + let x = i8x16::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i8x32(simd: S) { + let data = get_data_signed::(i8::MIN, i8::MAX); + let x = i8x32::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i8x64(simd: S) { + let data = get_data_signed::(i8::MIN, i8::MAX); + let x = i8x64::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i16x8(simd: S) { + let data = get_data_signed::(i16::MIN, i16::MAX); + let x = i16x8::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i16x16(simd: S) { + let data = get_data_signed::(i16::MIN, i16::MAX); + let x = i16x16::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i16x32(simd: S) { + let data = get_data_signed::(i16::MIN, i16::MAX); + let x = i16x32::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i32x4(simd: S) { + let data = get_data_signed::(i32::MIN, i32::MAX); + let x = i32x4::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i32x8(simd: S) { + let data = get_data_signed::(i32::MIN, i32::MAX); + let x = i32x8::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i32x16(simd: S) { + let data = get_data_signed::(i32::MIN, i32::MAX); + let x = i32x16::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i64x2(simd: S) { + let data = get_data_signed::(i64::MIN, i64::MAX); + let x = i64x2::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i64x4(simd: S) { + let data = get_data_signed::(i64::MIN, i64::MAX); + let x = i64x4::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_signed_i64x8(simd: S) { + let data = get_data_signed::(i64::MIN, i64::MAX); + let x = i64x8::simd_from(simd, data); + assert_eq!(*x.cast_signed(), data); +} + +#[simd_test] +fn cast_unsigned_u8x16(simd: S) { + let data = get_data_unsigned::(u8::MAX); + let x = u8x16::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u8x32(simd: S) { + let data = get_data_unsigned::(u8::MAX); + let x = u8x32::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u8x64(simd: S) { + let data = get_data_unsigned::(u8::MAX); + let x = u8x64::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u16x8(simd: S) { + let data = get_data_unsigned::(u16::MAX); + let x = u16x8::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u16x16(simd: S) { + let data = get_data_unsigned::(u16::MAX); + let x = u16x16::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u16x32(simd: S) { + let data = get_data_unsigned::(u16::MAX); + let x = u16x32::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u32x4(simd: S) { + let data = get_data_unsigned::(u32::MAX); + let x = u32x4::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u32x8(simd: S) { + let data = get_data_unsigned::(u32::MAX); + let x = u32x8::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u32x16(simd: S) { + let data = get_data_unsigned::(u32::MAX); + let x = u32x16::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u64x2(simd: S) { + let data = get_data_unsigned::(u64::MAX); + let x = u64x2::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u64x4(simd: S) { + let data = get_data_unsigned::(u64::MAX); + let x = u64x4::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} + +#[simd_test] +fn cast_unsigned_u64x8(simd: S) { + let data = get_data_unsigned::(u64::MAX); + let x = u64x8::simd_from(simd, data); + assert_eq!(*x.cast_unsigned(), data); +} diff --git a/fearless_simd_tests/tests/harness/ops/mod.rs b/fearless_simd_tests/tests/harness/ops/mod.rs index 3278095f..3478d066 100644 --- a/fearless_simd_tests/tests/harness/ops/mod.rs +++ b/fearless_simd_tests/tests/harness/ops/mod.rs @@ -15,6 +15,7 @@ mod as_array; mod as_mut_array; mod bitcast; mod block_splat; +mod cast_signed_unsigned; mod ceil; mod clamp; mod combine;