Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Created November 19, 2025 23:22
Show Gist options
  • Select an option

  • Save raphlinus/bf56a593724ff1ad3c6049a7320b3726 to your computer and use it in GitHub Desktop.

Select an option

Save raphlinus/bf56a593724ff1ad3c6049a7320b3726 to your computer and use it in GitHub Desktop.
Experiment with widen/narrow associated types
commit 49309841ba5cb7db0989a87c0a1d4b2947f02314
Merge: 3abe852 de4ec3f
Author: Raph Levien <[email protected]>
Date: Wed Jun 11 08:12:42 2025 -0700
On gen2: half experiment
diff --cc fearless_simd/src/generated/simd_trait.rs
index 875ccad,875ccad..544068b
--- a/fearless_simd/src/generated/simd_trait.rs
+++ b/fearless_simd/src/generated/simd_trait.rs
@@@ -7,16 -7,16 +7,17 @@@ use crate::
};
/// TODO: docstring
pub trait Simd: Sized + Clone + Copy + Send + Sync + Seal + 'static {
-- type f32s: SimdFloat<f32, Self, Block = f32x4<Self>>;
++ type f32s: SimdFloat<f32, Self, Block = f32x4<Self>> + crate::SimdF32Full<Self>;
type u8s: SimdInt<u8, Self, Block = u8x16<Self>>;
type i8s: SimdInt<i8, Self, Block = i8x16<Self>>;
-- type u16s: SimdInt<u16, Self, Block = u16x8<Self>>;
++ type u16s: SimdInt<u16, Self, Block = u16x8<Self>> + crate::SimdFull<u16, Self>;
type i16s: SimdInt<i16, Self, Block = i16x8<Self>>;
type u32s: SimdInt<u32, Self, Block = u32x4<Self>>;
type i32s: SimdInt<i32, Self, Block = i32x4<Self>>;
type mask8s: SimdMask<i8, Self, Block = mask8x16<Self>>;
type mask16s: SimdMask<i16, Self, Block = mask16x8<Self>>;
type mask32s: SimdMask<i32, Self, Block = mask32x4<Self>>;
++ fn cvt_f32s_u16(self, a: Self::f32s) -> <Self::u16s as crate::SimdFull<u16, Self>>::Half;
fn level(self) -> Level;
/// Call function with CPU features enabled.
///
@@@ -601,3 -601,3 +602,12 @@@ pub trait SimdMask
fn unzip(self, rhs: impl SimdInto<Self, S>) -> (Self, Self);
fn simd_eq(self, rhs: impl SimdInto<Self, S>) -> Self::Mask;
}
++pub trait SimdFull<Element: SimdElement, S: Simd>: SimdBase<Element, S> {
++ type Half: SimdHalf<Element, S, Full = Self>;
++}
++pub trait SimdHalf<Element: SimdElement, S: Simd>: SimdBase<Element, S> {
++ type Full: SimdFull<Element, S, Half = Self>;
++}
++pub trait SimdF32Full<S: Simd>: SimdFull<f32, S> {
++ fn cvt_u16(self) -> <S::u16s as SimdFull<u16, S>>::Half;
++}
diff --cc fearless_simd/src/generated/simd_types.rs
index 07a52c0,07a52c0..5ab5acb
--- a/fearless_simd/src/generated/simd_types.rs
+++ b/fearless_simd/src/generated/simd_types.rs
@@@ -3193,3 -3193,3 +3193,10 @@@ impl<S: Simd> crate::SimdMask<i32, S> f
self.simd.simd_eq_mask32x8(self, rhs.simd_into(self.simd))
}
}
++
++impl<S: Simd> crate::SimdFull<f32, S> for f32x8<S> {
++ type Half = f32x4<S>;
++}
++impl<S: Simd> crate::SimdHalf<f32, S> for f32x4<S> {
++ type Full = f32x8<S>;
++}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment