diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index eeaf61c1..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""] @@ -13190,6 +13210,38 @@ 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, + Unsigned = Self::Unsigned, + Signed = Self::Signed, + Mask = Self::Mask, + >; + #[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, + Unsigned = Self::Unsigned, + Signed = Self::Signed, + Mask = Self::Mask, + >; + #[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`)."] @@ -13197,6 +13249,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 8a8f3014..9891b00e 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) @@ -1661,6 +1665,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) @@ -2055,6 +2061,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) @@ -2587,6 +2595,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) @@ -2981,6 +2991,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) @@ -3955,6 +3967,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) @@ -4323,6 +4337,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) @@ -5292,6 +5308,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) @@ -5690,6 +5708,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) @@ -6209,6 +6229,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) @@ -6609,6 +6631,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) @@ -7150,6 +7174,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) @@ -7551,6 +7577,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) @@ -8522,6 +8550,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) @@ -8885,6 +8915,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) @@ -9892,6 +9924,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) @@ -10316,6 +10350,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) @@ -10838,6 +10874,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) @@ -11248,6 +11286,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) @@ -11785,6 +11825,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) @@ -12191,6 +12233,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) @@ -13176,6 +13220,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) @@ -13545,6 +13591,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..27c95c66 100644 --- a/fearless_simd/src/traits.rs +++ b/fearless_simd/src/traits.rs @@ -333,32 +333,62 @@ 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; + /// 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 dbdc93df..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`]. @@ -542,12 +542,67 @@ 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< + 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< + 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)] + 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 d7dd5bf1..3a4b2088 100644 --- a/fearless_simd_gen/src/mk_simd_types.rs +++ b/fearless_simd_gen/src/mk_simd_types.rs @@ -734,12 +734,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) { 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) 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;