Skip to content

Conversation

87flowers
Copy link

Implemented a 64 bit swizzle_dyn for AVX2.

Use case: Encountered this while implementing a chess move generator in rust with portable simd.

Would like an alternative suggestion for use of mem::transmute here.

use x86::_mm256_permute2x128_si256 as avx2_cross_shuffle;
use x86::_mm256_shuffle_epi8 as avx2_half_pshufb;
let high = Simd::splat(64u8);
// SAFETY: Caller promised AVX2
Copy link
Member

@programmerjake programmerjake Sep 9, 2025

Choose a reason for hiding this comment

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

you should probably add more safety comments, e.g. answer why is the transmute sound?

Copy link
Author

Choose a reason for hiding this comment

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

Added one for the transmute. Can't really think of anywhere else where its required.

@jhorstmann
Copy link

The concat swizzle from #335 would be real nice to replace that transmute.

@87flowers
Copy link
Author

@jhorstmann Agreed.

@@ -220,6 +220,8 @@ unsafe fn avx2_pshufb512(bytes: Simd<u8, 64>, idxs: Simd<u8, 64>) -> Simd<u8, 64

let z0 = half_swizzler(bytes0, bytes1, idxs0);
let z1 = half_swizzler(bytes0, bytes1, idxs1);

// SAFETY: Concatenation of two 32-element vectors to one 64-element vector
Copy link
Member

Choose a reason for hiding this comment

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

this says what your doing, what it should say is why it's safe to use transmute like this. e.g.:
[Simd<u8, 32>; 2] and Simd<u8, 64> both have the same size (64) and no padding bytes, so transmuting is safe

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.

3 participants