Skip to content

Add mask<->vector conversion methods - #400

Open
valadaptive wants to merge 2 commits into
linebender:mainfrom
valadaptive:mask-vector-conversion
Open

valadaptive wants to merge 2 commits into
linebender:mainfrom
valadaptive:mask-vector-conversion

Conversation

@valadaptive

Copy link
Copy Markdown
Contributor

Resolves #398.

This PR adds methods for converting masks to and from vectors.

The SimdMask trait now has an Ints associated type, which represents the mask's corresponding integer vector type.

For AVX-512, these are implemented using movm_epi[N] and movepi[N]_mask. For all other instruction sets, the mask types are the same as the integer vector types, so all we have to do is re-wrap them in the proper type.

I'm not attached to any of the names here (Ints, from_vector, to_vector) and am willing to change them if anyone has a better idea.

I'm also not sure if we should provide corresponding from_mask or to_mask functions on the corresponding integer vector types; I think that would require adding a new SimdSignedInt type or implementing this functionality for both signed and unsigned int vector types.

I also implemented these as associated methods on SimdMask but haven't added any sort of conversion trait. In general, I prefer APIs where the types are concrete if possible (no wide-ranging conversion trait) because it helps with type inference and reduces the number of "turbofish" I have to type out.

@Shnatsel

Copy link
Copy Markdown
Contributor

The direction looks good to me, I'll take a closer look later. Thanks!

@valadaptive
valadaptive force-pushed the mask-vector-conversion branch from 6590b49 to d433103 Compare September 30, 2026 06:58
@Shnatsel

Copy link
Copy Markdown
Contributor

std::simd calls these from_simd/to_simd: rust-lang/portable-simd#478

We generally try to align with std::simd on naming, because that's what other libs are also doing and it makes porting across the whole ecosystem really easy. But in our case to_simd sounds like it's going to give you the Simd token, so I'm not sure what the best option is.

@Shnatsel Shnatsel left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a change to specify more generic bounds, cargo-semver-checks is happy with it. PTAL.

We need to make a call on whether to align with std::simd and call this to_simd or keep the to_vector, I'm OK with either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API for converting masks to and from integer vectors

2 participants