首页 > 解决方案 > Intrinsics - 只获取所有匹配的索引

问题描述

我正在比较两个向量using System.Runtime.Intrinsics,并想知道仅获取所有匹配索引的最快方法。

目前我的代码如下所示:

static void CompareVectors(Vector128<byte> first, Vector128<byte> second)
{
    var matches = Avx2.MoveMask(Avx2.CompareEqual(first, second));
    if (matches == 0) return;
    var firstIndex = BitOps.TrailingZeroCount(matches) / sizeof(byte);
    for (var j = firstIndex; j < Vector128<sbyte>.Count; j++)
    {
        var matchingValue = first.GetElement(j);
        //do stuff
    }
}

问题

如果第一个索引匹配,它会遍历向量中的所有元素,即使只有第一个索引是匹配的。

我怎样才能获得所有匹配的索引只是为了加快速度?

标签: c#.net-coreintrinsics

解决方案


推荐阅读