Skip to content

Instantly share code, notes, and snippets.

@danmoseley
Last active May 20, 2022 02:58
Show Gist options
  • Save danmoseley/faa3b6fcafad8fbdf6e44f62dfe006b4 to your computer and use it in GitHub Desktop.
Save danmoseley/faa3b6fcafad8fbdf6e44f62dfe006b4 to your computer and use it in GitHub Desktop.
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
Vector128<byte> value = Vector128.Create((byte)0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Vector128<byte> mask = Vector128.Create((byte)0b01000100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
// PSHUFB -- result[i] = (mask[i] & 0x80) ? 0 : value[mask[i] & 15]
Vector128<byte> result1 = Ssse3.Shuffle(value, mask);
// "Generic" (?) shuffle -- result[i] = (mask[i] < 16 ? 0 : value[mask[i]])
Vector128<byte> result2 = Vector128.Shuffle(value, mask);
Console.WriteLine(result1); // <3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>
Console.WriteLine(result2); // <0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment