Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Vector256.Shuffle work in .Net 7+?

Avx2.Shuffle uses _mm256_shuffle_epi8 to shuffle bytes within 128-bit lanes depending on a mask, zeroing values if the final bit is set.

How does Vector256.Shuffle work? Unlike Avx2.Shuffle it will zero if the shuffle control mask value exceeds the number of elements. In addition it supports cross lane shuffling.

I'm guessing it uses a combination of checking for a constant shuffle mask, using _mm256_cmpgt_epi8 with masking to zero elements and _mm256_permutevar8x32_epi32 when cross lane shuffling is required.

How would I see what IL code is emitted at runtime? I don't think SharpLab does - my example.

edit I've never looked at the jit source code before but link appears to show that the jit will check if any shuffle indices exceed its lane, falling back to the manual version if indices do fall out of bounds, otherwise it will use the relevant Avx instruction. This is my best guess, I haven't the faintest idea how the jit works.

like image 549
DaemonFire Avatar asked Nov 23 '25 15:11

DaemonFire


1 Answers

Currently, the Shuffle method in .Net7, always uses scalar fallback code. I've tested it on both X86 and Arm platforms and found that it is not hardware accelerated.

To solve the problem of the Shuffle method not being hardware accelerated, I developed the VectorTraits library. It supports both X86 and Arm platforms.

  • X86 platform: Use _mm256_shuffle_epi8 and other instructions.
  • Arm platforms: Use vqvtbl1q_u8 and other instructions.

The methods is as follows.

  • Vector256s.Shuffle: Shuffle and clear. If the indices value is out of range, the element will be cleared.
  • Vector256s.YShuffleInsert: Shuffle and insert. If the index value is out of range, the elements of the background vector will be inserted.
  • Vector256s.YShuffleKernel: Only shuffle. If the index value is out of range, the result is undefined.

Not only for Vector256, but also for Vector128 and Vector, this library provides a Shuffle method.

NuGet: https://www.nuget.org/packages/VectorTraits

like image 85
zyl910 Avatar answered Nov 25 '25 05:11

zyl910



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!