diff --git a/fearless_simd/src/generated/avx512.rs b/fearless_simd/src/generated/avx512.rs index b159741d..5a880cf3 100644 --- a/fearless_simd/src/generated/avx512.rs +++ b/fearless_simd/src/generated/avx512.rs @@ -23118,6 +23118,118 @@ impl Simd for Avx512 { simd: self, } } + #[inline(always)] + fn cast_mask8x16_to_mask16x16(self, from: mask8x16) -> mask16x16 { + mask16x16 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask8x16_to_mask32x16(self, from: mask8x16) -> mask32x16 { + mask32x16 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask16x8_to_mask32x8(self, from: mask16x8) -> mask32x8 { + mask32x8 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask16x8_to_mask64x8(self, from: mask16x8) -> mask64x8 { + mask64x8 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask32x4_to_mask64x4(self, from: mask32x4) -> mask64x4 { + mask64x4 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask8x32_to_mask16x32(self, from: mask8x32) -> mask16x32 { + mask16x32 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask16x16_to_mask8x16(self, from: mask16x16) -> mask8x16 { + mask8x16 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask16x16_to_mask32x16(self, from: mask16x16) -> mask32x16 { + mask32x16 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask32x8_to_mask16x8(self, from: mask32x8) -> mask16x8 { + mask16x8 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask32x8_to_mask64x8(self, from: mask32x8) -> mask64x8 { + mask64x8 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask64x4_to_mask32x4(self, from: mask64x4) -> mask32x4 { + mask32x4 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask16x32_to_mask8x32(self, from: mask16x32) -> mask8x32 { + mask8x32 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask32x16_to_mask8x16(self, from: mask32x16) -> mask8x16 { + mask8x16 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask32x16_to_mask16x16(self, from: mask32x16) -> mask16x16 { + mask16x16 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask64x8_to_mask16x8(self, from: mask64x8) -> mask16x8 { + mask16x8 { + val: from.val, + simd: from.simd, + } + } + #[inline(always)] + fn cast_mask64x8_to_mask32x8(self, from: mask64x8) -> mask32x8 { + mask32x8 { + val: from.val, + simd: from.simd, + } + } } impl SimdFrom<__mmask16, S> for mask8x16 { #[inline(always)] diff --git a/fearless_simd/src/generated/simd_trait.rs b/fearless_simd/src/generated/simd_trait.rs index e5ec3dbc..d46ee3e0 100644 --- a/fearless_simd/src/generated/simd_trait.rs +++ b/fearless_simd/src/generated/simd_trait.rs @@ -12282,6 +12282,142 @@ pub trait Simd: let (b0, b1) = self.split_mask64x8(b); self.combine_mask32x8(self.narrow_mask64x4(a0, a1), self.narrow_mask64x4(b0, b1)) } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask8x16` to the boolean lanes of `mask16x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask8x16_to_mask16x16(self, from: mask8x16) -> mask16x16 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask8x16(from); + let from = self.combine_mask16x8(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask8x16` to the boolean lanes of `mask32x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask8x16_to_mask32x16(self, from: mask8x16) -> mask32x16 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask8x16(from); + let from = self.combine_mask16x8(low, high); + let (low, high) = self.widen_mask16x16(from); + let from = self.combine_mask32x8(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask16x8` to the boolean lanes of `mask32x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask16x8_to_mask32x8(self, from: mask16x8) -> mask32x8 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask16x8(from); + let from = self.combine_mask32x4(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask16x8` to the boolean lanes of `mask64x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask16x8_to_mask64x8(self, from: mask16x8) -> mask64x8 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask16x8(from); + let from = self.combine_mask32x4(low, high); + let (low, high) = self.widen_mask32x8(from); + let from = self.combine_mask64x4(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask32x4` to the boolean lanes of `mask64x4`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask32x4_to_mask64x4(self, from: mask32x4) -> mask64x4 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask32x4(from); + let from = self.combine_mask64x2(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask8x32` to the boolean lanes of `mask16x32`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask8x32_to_mask16x32(self, from: mask8x32) -> mask16x32 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask8x32(from); + let from = self.combine_mask16x16(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask16x16` to the boolean lanes of `mask8x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask16x16_to_mask8x16(self, from: mask16x16) -> mask8x16 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask16x16(from); + let from = self.narrow_mask16x8(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask16x16` to the boolean lanes of `mask32x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask16x16_to_mask32x16(self, from: mask16x16) -> mask32x16 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask16x16(from); + let from = self.combine_mask32x8(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask32x8` to the boolean lanes of `mask16x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask32x8_to_mask16x8(self, from: mask32x8) -> mask16x8 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask32x8(from); + let from = self.narrow_mask32x4(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask32x8` to the boolean lanes of `mask64x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask32x8_to_mask64x8(self, from: mask32x8) -> mask64x8 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.widen_mask32x8(from); + let from = self.combine_mask64x4(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask64x4` to the boolean lanes of `mask32x4`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask64x4_to_mask32x4(self, from: mask64x4) -> mask32x4 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask64x4(from); + let from = self.narrow_mask64x2(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask16x32` to the boolean lanes of `mask8x32`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask16x32_to_mask8x32(self, from: mask16x32) -> mask8x32 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask16x32(from); + let from = self.narrow_mask16x16(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask32x16` to the boolean lanes of `mask8x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask32x16_to_mask8x16(self, from: mask32x16) -> mask8x16 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask32x16(from); + let from = self.narrow_mask32x8(low, high); + let (low, high) = self.split_mask16x16(from); + let from = self.narrow_mask16x8(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask32x16` to the boolean lanes of `mask16x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask32x16_to_mask16x16(self, from: mask32x16) -> mask16x16 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask32x16(from); + let from = self.narrow_mask32x8(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask64x8` to the boolean lanes of `mask16x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask64x8_to_mask16x8(self, from: mask64x8) -> mask16x8 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask64x8(from); + let from = self.narrow_mask64x4(low, high); + let (low, high) = self.split_mask32x8(from); + let from = self.narrow_mask32x4(low, high); + from + } + #[inline(always)] + #[doc = "Convert the boolean lanes of `mask64x8` to the boolean lanes of `mask32x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + fn cast_mask64x8_to_mask32x8(self, from: mask64x8) -> mask32x8 { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + let (low, high) = self.split_mask64x8(from); + let from = self.narrow_mask64x4(low, high); + from + } } pub(crate) mod arch_types { use crate::transmute::SimdPod; diff --git a/fearless_simd/src/generated/simd_types.rs b/fearless_simd/src/generated/simd_types.rs index 1b26a420..f4bafe4c 100644 --- a/fearless_simd/src/generated/simd_types.rs +++ b/fearless_simd/src/generated/simd_types.rs @@ -1340,6 +1340,20 @@ impl MaskWiden for mask8x16 { self.simd.widen_mask8x16(self) } } +impl From> for mask8x16 { + #[doc = "Convert the boolean lanes of `mask16x16` to the boolean lanes of `mask8x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask16x16) -> Self { + value.simd.cast_mask16x16_to_mask8x16(value) + } +} +impl From> for mask8x16 { + #[doc = "Convert the boolean lanes of `mask32x16` to the boolean lanes of `mask8x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask32x16) -> Self { + value.simd.cast_mask32x16_to_mask8x16(value) + } +} impl ExtractToken for i16x8 { type S = S; #[inline] @@ -2264,6 +2278,20 @@ impl MaskNarrow for mask16x8 { self.simd.narrow_mask16x8(self, high) } } +impl From> for mask16x8 { + #[doc = "Convert the boolean lanes of `mask32x8` to the boolean lanes of `mask16x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask32x8) -> Self { + value.simd.cast_mask32x8_to_mask16x8(value) + } +} +impl From> for mask16x8 { + #[doc = "Convert the boolean lanes of `mask64x8` to the boolean lanes of `mask16x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask64x8) -> Self { + value.simd.cast_mask64x8_to_mask16x8(value) + } +} impl ExtractToken for i32x4 { type S = S; #[inline] @@ -3188,6 +3216,13 @@ impl MaskNarrow for mask32x4 { self.simd.narrow_mask32x4(self, high) } } +impl From> for mask32x4 { + #[doc = "Convert the boolean lanes of `mask64x4` to the boolean lanes of `mask32x4`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask64x4) -> Self { + value.simd.cast_mask64x4_to_mask32x4(value) + } +} impl ExtractToken for f64x2 { type S = S; #[inline] @@ -5851,6 +5886,13 @@ impl MaskWiden for mask8x32 { self.simd.widen_mask8x32(self) } } +impl From> for mask8x32 { + #[doc = "Convert the boolean lanes of `mask16x32` to the boolean lanes of `mask8x32`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask16x32) -> Self { + value.simd.cast_mask16x32_to_mask8x32(value) + } +} impl ExtractToken for i16x16 { type S = S; #[inline] @@ -6787,6 +6829,20 @@ impl MaskNarrow for mask16x16 { self.simd.narrow_mask16x16(self, high) } } +impl From> for mask16x16 { + #[doc = "Convert the boolean lanes of `mask8x16` to the boolean lanes of `mask16x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask8x16) -> Self { + value.simd.cast_mask8x16_to_mask16x16(value) + } +} +impl From> for mask16x16 { + #[doc = "Convert the boolean lanes of `mask32x16` to the boolean lanes of `mask16x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask32x16) -> Self { + value.simd.cast_mask32x16_to_mask16x16(value) + } +} impl ExtractToken for i32x8 { type S = S; #[inline] @@ -7725,6 +7781,20 @@ impl MaskNarrow for mask32x8 { self.simd.narrow_mask32x8(self, high) } } +impl From> for mask32x8 { + #[doc = "Convert the boolean lanes of `mask16x8` to the boolean lanes of `mask32x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask16x8) -> Self { + value.simd.cast_mask16x8_to_mask32x8(value) + } +} +impl From> for mask32x8 { + #[doc = "Convert the boolean lanes of `mask64x8` to the boolean lanes of `mask32x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask64x8) -> Self { + value.simd.cast_mask64x8_to_mask32x8(value) + } +} impl ExtractToken for f64x4 { type S = S; #[inline] @@ -9012,6 +9082,13 @@ impl MaskNarrow for mask64x4 { self.simd.narrow_mask64x4(self, high) } } +impl From> for mask64x4 { + #[doc = "Convert the boolean lanes of `mask32x4` to the boolean lanes of `mask64x4`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask32x4) -> Self { + value.simd.cast_mask32x4_to_mask64x4(value) + } +} impl ExtractToken for f32x16 { type S = S; #[inline] @@ -11384,6 +11461,13 @@ impl MaskNarrow for mask16x32 { self.simd.narrow_mask16x32(self, high) } } +impl From> for mask16x32 { + #[doc = "Convert the boolean lanes of `mask8x32` to the boolean lanes of `mask16x32`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask8x32) -> Self { + value.simd.cast_mask8x32_to_mask16x32(value) + } +} impl ExtractToken for i32x16 { type S = S; #[inline] @@ -12332,6 +12416,20 @@ impl MaskNarrow for mask32x16 { self.simd.narrow_mask32x16(self, high) } } +impl From> for mask32x16 { + #[doc = "Convert the boolean lanes of `mask8x16` to the boolean lanes of `mask32x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask8x16) -> Self { + value.simd.cast_mask8x16_to_mask32x16(value) + } +} +impl From> for mask32x16 { + #[doc = "Convert the boolean lanes of `mask16x16` to the boolean lanes of `mask32x16`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask16x16) -> Self { + value.simd.cast_mask16x16_to_mask32x16(value) + } +} impl ExtractToken for f64x8 { type S = S; #[inline] @@ -13637,3 +13735,17 @@ impl MaskNarrow for mask64x8 { self.simd.narrow_mask64x8(self, high) } } +impl From> for mask64x8 { + #[doc = "Convert the boolean lanes of `mask16x8` to the boolean lanes of `mask64x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask16x8) -> Self { + value.simd.cast_mask16x8_to_mask64x8(value) + } +} +impl From> for mask64x8 { + #[doc = "Convert the boolean lanes of `mask32x8` to the boolean lanes of `mask64x8`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior."] + #[inline(always)] + fn from(value: mask32x8) -> Self { + value.simd.cast_mask32x8_to_mask64x8(value) + } +} diff --git a/fearless_simd_gen/src/generic.rs b/fearless_simd_gen/src/generic.rs index 3fab5dbe..f6c01186 100644 --- a/fearless_simd_gen/src/generic.rs +++ b/fearless_simd_gen/src/generic.rs @@ -2,12 +2,12 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use proc_macro2::{Ident, Literal, Span, TokenStream}; -use quote::{ToTokens, quote}; +use quote::{ToTokens, format_ident, quote}; use crate::{ level::Level, ops::{ElementDirection, Op, OpSig, SlideGranularity}, - types::{ScalarType, VecType}, + types::{ScalarType, VecType, simd_mask_types}, }; pub(crate) fn generic_op_name(op: &str, ty: &VecType) -> Ident { @@ -1340,3 +1340,92 @@ pub(crate) fn generic_clamp_int(method_sig: TokenStream) -> TokenStream { } } } + +/// A generic implementation for mask casts using integer narrowing and widening +pub(crate) fn generic_mask_cast_body(from_ty: &VecType, to_ty: &VecType) -> TokenStream { + let mut narrow_or_widen = TokenStream::new(); + + let mut from_ty = *from_ty; + while from_ty != *to_ty { + if from_ty.scalar_bits > to_ty.scalar_bits { + let new_from_ty = VecType::new(ScalarType::Mask, from_ty.scalar_bits / 2, from_ty.len); + + let split = format_ident!("split_{}", from_ty.rust_name()); + let narrow = format_ident!("narrow_mask{}x{}", from_ty.scalar_bits, from_ty.len / 2); + narrow_or_widen.extend(quote! { + let (low, high) = self.#split(from); + let from = self.#narrow(low, high); + }); + + from_ty = new_from_ty; + } else { + let new_from_ty = VecType::new(ScalarType::Mask, from_ty.scalar_bits * 2, from_ty.len); + + let widen = format_ident!("widen_{}", from_ty.rust_name()); + let combine = format_ident!( + "combine_mask{}x{}", + new_from_ty.scalar_bits, + new_from_ty.len / 2 + ); + narrow_or_widen.extend(quote! { + let (low, high) = self.#widen(from); + let from = self.#combine(low, high); + }); + + from_ty = new_from_ty; + } + } + + quote! { + #![allow(clippy::let_and_return, reason = "makes code gen easier")] + #narrow_or_widen + from + } +} +/// Allows levels to overwrite the default mask conversion implementations. +/// Return `None` to use the generic implementation. +pub(crate) fn generic_mask_casts_methods( + emit_docs: bool, + mut impl_fn: impl FnMut(&VecType, &VecType) -> Option, +) -> Vec { + let mut methods = Vec::new(); + + for from_ty in simd_mask_types() { + for to_ty in simd_mask_types() { + if from_ty == to_ty || from_ty.len != to_ty.len { + continue; + } + + let from_name = from_ty.rust(); + let to_name = to_ty.rust(); + let op = format_ident!("cast_{}_to_{}", from_ty.rust_name(), to_ty.rust_name()); + + if let Some(inner) = impl_fn(from_ty, to_ty) { + let doc = if emit_docs { + generic_mask_doc(from_ty, to_ty) + } else { + TokenStream::new() + }; + + methods.push(quote! { + #doc + fn #op(self, from: #from_name) -> #to_name { + #inner + } + }); + } + } + } + + methods +} +pub(crate) fn generic_mask_doc(from_ty: &VecType, to_ty: &VecType) -> TokenStream { + let doc = format!( + "Convert the boolean lanes of `{}` to the boolean lanes of `{}`.\n\nMasks constructed from integer lanes other than 0 or -1 have unspecified behavior.", + from_ty.rust_name(), + to_ty.rust_name() + ); + quote! { + #[doc = #doc] + } +} diff --git a/fearless_simd_gen/src/level.rs b/fearless_simd_gen/src/level.rs index 799ced0c..bfceab7d 100644 --- a/fearless_simd_gen/src/level.rs +++ b/fearless_simd_gen/src/level.rs @@ -5,7 +5,7 @@ use proc_macro2::{Ident, Span, TokenStream}; use quote::{format_ident, quote}; use crate::{ - generic::generic_op, + generic::{generic_mask_casts_methods, generic_op}, ops::{Op, ops_for_type}, types::{SIMD_TYPES, ScalarType, VecType, type_imports}, }; @@ -85,6 +85,13 @@ pub(crate) trait Level { fn custom_mask_array_conversion(&self, _vec_ty: &VecType) -> Option { None } + /// Override the default mask casts implementation for the given mask types. + /// This should return only the body of the conversion implementation, not the full method. + /// The input parameter is called `from`. + fn custom_mask_cast(&self, from_ty: &VecType, to_ty: &VecType) -> Option { + assert_eq!(from_ty.len, to_ty.len); + None + } fn impl_arch_types(&self) -> TokenStream { let mut assoc_types = vec![]; @@ -169,6 +176,10 @@ pub(crate) trait Level { } } + methods.extend(generic_mask_casts_methods(false, |from_ty, to_ty| { + self.custom_mask_cast(from_ty, to_ty) + })); + let vectorize_body = if let Some(target_features) = self.enabled_target_features() { let vectorize = format_ident!("vectorize_{}", self.name().to_ascii_lowercase()); quote! { diff --git a/fearless_simd_gen/src/mk_simd_trait.rs b/fearless_simd_gen/src/mk_simd_trait.rs index 08641cea..f71edd86 100644 --- a/fearless_simd_gen/src/mk_simd_trait.rs +++ b/fearless_simd_gen/src/mk_simd_trait.rs @@ -5,7 +5,10 @@ use proc_macro2::{Ident, Literal, Span, TokenStream}; use quote::{format_ident, quote}; use crate::{ - generic::{byte_swizzle_op, generic_op, generic_op_name, reversed_compare_op}, + generic::{ + byte_swizzle_op, generic_mask_cast_body, generic_mask_casts_methods, generic_op, + generic_op_name, reversed_compare_op, + }, ops::{ CoreOpTrait, OpKind, OpSig, TyFlavor, base_trait_ops, ops_for_type, overloaded_ops_for, vec_trait_ops_for, @@ -16,6 +19,7 @@ use crate::{ pub(crate) fn mk_simd_trait() -> TokenStream { let imports = type_imports(); let mut methods = vec![]; + // Float methods for vec_ty in SIMD_TYPES { for op in ops_for_type(vec_ty) { @@ -74,6 +78,18 @@ pub(crate) fn mk_simd_trait() -> TokenStream { } } } + + // mask casts + let mask_casts = generic_mask_casts_methods(true, |from_ty, to_ty| { + Some(generic_mask_cast_body(from_ty, to_ty)) + }); + methods.extend(quote! { + #( + #[inline(always)] + #mask_casts + )* + }); + let mut code = quote! { use core::fmt::Debug; use crate::{seal::Seal, Level, ExtractToken, SimdElement, SimdIntElement, SimdFloatElement, SimdFrom, SimdInto, SimdCvtTruncate, SimdCvtFloat, SimdWiden, SimdIntWiden, SimdNarrow, MaskWiden, MaskNarrow, Select, Bytes}; diff --git a/fearless_simd_gen/src/mk_simd_types.rs b/fearless_simd_gen/src/mk_simd_types.rs index 55798c5f..d7dd5bf1 100644 --- a/fearless_simd_gen/src/mk_simd_types.rs +++ b/fearless_simd_gen/src/mk_simd_types.rs @@ -5,13 +5,13 @@ use proc_macro2::{Ident, Literal, Span, TokenStream}; use quote::{format_ident, quote}; use crate::{ - generic::{generic_op_name, unrolled_array}, + generic::{generic_mask_doc, generic_op_name, unrolled_array}, ops::{ F32_TO_I32, F32_TO_I32_PRECISE, F32_TO_U32, F32_TO_U32_PRECISE, F64_TO_I64, F64_TO_I64_PRECISE, F64_TO_U64, F64_TO_U64_PRECISE, I32_TO_F32, I64_TO_F64, Op, OpSig, TyFlavor, U32_TO_F32, U64_TO_F64, base_trait_ops, vec_trait_ops_for, }, - types::{SIMD_TYPES, ScalarType, VecType}, + types::{SIMD_TYPES, ScalarType, VecType, simd_mask_types}, }; pub(crate) fn mk_simd_types() -> TokenStream { @@ -45,6 +45,7 @@ pub(crate) fn mk_simd_types() -> TokenStream { let splat = Ident::new(&format!("splat_{}", ty.rust_name()), Span::call_site()); let impl_block = simd_mask_impl(ty); let conversions = mask_widen_narrow_impls(ty); + let from_impls = mask_from_impls(ty); let mask_from_array = format_ident!("{}_from_array", ty.rust_name()); let mask_to_array = format_ident!("{}_to_array", ty.rust_name()); result.extend(quote! { @@ -100,6 +101,7 @@ pub(crate) fn mk_simd_types() -> TokenStream { #impl_block #conversions + #from_impls }); continue; } @@ -453,6 +455,33 @@ fn mask_widen_narrow_impls(ty: &VecType) -> TokenStream { quote! { #widen #narrow } } +fn mask_from_impls(ty: &VecType) -> TokenStream { + let name = ty.rust(); + + let mut impls = Vec::new(); + for from_ty in simd_mask_types().filter(|t| t.len == ty.len) { + if from_ty == ty { + continue; // skip reflexive + } + + let from_name = from_ty.rust(); + let cast_op = format_ident!("cast_{}_to_{}", from_ty.rust_name(), ty.rust_name()); + let docs = generic_mask_doc(from_ty, ty); + + impls.push(quote! { + impl From<#from_name> for #name { + #docs + #[inline(always)] + fn from(value: #from_name) -> Self { + value.simd.#cast_op(value) + } + } + }); + } + + quote! { #(#impls)* } +} + fn simd_mask_impl(ty: &VecType) -> TokenStream { let name = ty.rust(); let int_ty = ty.cast(ScalarType::Int); diff --git a/fearless_simd_gen/src/mk_x86.rs b/fearless_simd_gen/src/mk_x86.rs index 0e1ae5eb..5fb6eda2 100644 --- a/fearless_simd_gen/src/mk_x86.rs +++ b/fearless_simd_gen/src/mk_x86.rs @@ -109,6 +109,22 @@ impl Level for X86 { Some(self.avx512_mask_array_conversion(vec_ty)) } + fn custom_mask_cast(&self, from_ty: &VecType, to_ty: &VecType) -> Option { + if *self != Self::Avx512 { + return None; + } + // AVX512 masks are bitmasks, so masks of the same length are all the same. + // This makes mask casts no-ops. + assert_eq!(from_ty.len, to_ty.len); + let to_name = to_ty.rust(); + Some(quote! { + #to_name { + val: from.val, + simd: from.simd, + } + }) + } + fn token_doc(&self) -> &'static str { match self { Self::Sse2 => { diff --git a/fearless_simd_gen/src/types.rs b/fearless_simd_gen/src/types.rs index bf7bd110..31d525a9 100644 --- a/fearless_simd_gen/src/types.rs +++ b/fearless_simd_gen/src/types.rs @@ -341,3 +341,7 @@ pub(crate) fn type_imports() -> TokenStream { use crate::{ #( #imports ),* }; } } + +pub(crate) fn simd_mask_types() -> impl Iterator { + SIMD_TYPES.iter().filter(|ty| ty.scalar == ScalarType::Mask) +} diff --git a/fearless_simd_tests/tests/harness/ops/mask_casts.rs b/fearless_simd_tests/tests/harness/ops/mask_casts.rs new file mode 100644 index 00000000..6bf41d37 --- /dev/null +++ b/fearless_simd_tests/tests/harness/ops/mask_casts.rs @@ -0,0 +1,101 @@ +// Copyright 2026 the Fearless_SIMD Authors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +use fearless_simd::*; +use fearless_simd_dev_macros::simd_test; + +// One concrete test row per supported vector type. + +fn test_a_to_b(simd: S) +where + A: SimdMask, + B: SimdMask + From, +{ + let n = A::LEN; + let mut rng = fastrand::Rng::with_seed(0xE397_FACE_F768_F459); + for _ in 0..100 { + let bits = rng.u64(0..(1 << n)); + let a: A = A::from_bitmask(simd, bits); + assert_eq!(B::from(a).to_bitmask(), bits); + } +} + +#[simd_test] +fn cast_mask8x16_to_mask16x16(simd: S) { + test_a_to_b::, mask16x16>(simd); +} + +#[simd_test] +fn cast_mask8x16_to_mask32x16(simd: S) { + test_a_to_b::, mask32x16>(simd); +} + +#[simd_test] +fn cast_mask16x8_to_mask32x8(simd: S) { + test_a_to_b::, mask32x8>(simd); +} + +#[simd_test] +fn cast_mask16x8_to_mask64x8(simd: S) { + test_a_to_b::, mask64x8>(simd); +} + +#[simd_test] +fn cast_mask32x4_to_mask64x4(simd: S) { + test_a_to_b::, mask64x4>(simd); +} + +#[simd_test] +fn cast_mask8x32_to_mask16x32(simd: S) { + test_a_to_b::, mask16x32>(simd); +} + +#[simd_test] +fn cast_mask16x16_to_mask8x16(simd: S) { + test_a_to_b::, mask8x16>(simd); +} + +#[simd_test] +fn cast_mask16x16_to_mask32x16(simd: S) { + test_a_to_b::, mask32x16>(simd); +} + +#[simd_test] +fn cast_mask32x8_to_mask16x8(simd: S) { + test_a_to_b::, mask16x8>(simd); +} + +#[simd_test] +fn cast_mask32x8_to_mask64x8(simd: S) { + test_a_to_b::, mask64x8>(simd); +} + +#[simd_test] +fn cast_mask64x4_to_mask32x4(simd: S) { + test_a_to_b::, mask32x4>(simd); +} + +#[simd_test] +fn cast_mask16x32_to_mask8x32(simd: S) { + test_a_to_b::, mask8x32>(simd); +} + +#[simd_test] +fn cast_mask32x16_to_mask8x16(simd: S) { + test_a_to_b::, mask8x16>(simd); +} + +#[simd_test] +fn cast_mask32x16_to_mask16x16(simd: S) { + test_a_to_b::, mask16x16>(simd); +} + +#[simd_test] +fn cast_mask64x8_to_mask16x8(simd: S) { + test_a_to_b::, mask16x8>(simd); +} + +#[simd_test] +fn cast_mask64x8_to_mask32x8(simd: S) { + test_a_to_b::, mask32x8>(simd); +} diff --git a/fearless_simd_tests/tests/harness/ops/mod.rs b/fearless_simd_tests/tests/harness/ops/mod.rs index 2a24c4aa..3278095f 100644 --- a/fearless_simd_tests/tests/harness/ops/mod.rs +++ b/fearless_simd_tests/tests/harness/ops/mod.rs @@ -49,6 +49,7 @@ mod interleave; mod load_array; mod load_array_ref; mod load_four_interleaved; +mod mask_casts; mod mask_from_to_vector; mod max; mod max_precise;