Skip to content

Instantly share code, notes, and snippets.

@Mrnikbobjeff
Created May 6, 2024 08:34
Show Gist options
  • Save Mrnikbobjeff/794b32528df5463ff63e59654e743ad8 to your computer and use it in GitHub Desktop.
Save Mrnikbobjeff/794b32528df5463ff63e59654e743ad8 to your computer and use it in GitHub Desktop.
LessThan.cs
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public unsafe static int GreaterThanSimd_Avx2_u(int boundary,ref ReadOnlySpan<int> array)
{
Vector128<int> xmm1 = Vector128.Create(boundary);
Vector128<int> xmm2 = Vector128<int>.Zero;
Vector128<int> xmm3;
Vector128<int> xmm4;
ref int start = ref MemoryMarshal.GetReference(array);
for (nuint i = 0; i < (nuint)array.Length; i += 4)
{
xmm3 = Vector128.LoadUnsafe(ref start, i);
xmm4 = Sse2.CompareLessThan(xmm3, xmm1);
xmm2 = Avx2.Subtract(xmm2, xmm4);
}
xmm2 += Vector128.Shuffle(xmm2, Vector128.Create(1, 0, 3, 2));
Vector128<int> shuffle2 = Vector128.Shuffle(xmm2, Vector128.Create(2, 3, 0, 1));
xmm2 += shuffle2;
return xmm2[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment