System.Numerics.Vector.ConditionalSelect有什么用?

What is System.Numerics.Vector.ConditionalSelect used for?

谁能举例说明一下好吗what/when/howSystem.Numerics.Vector.ConditionalSelect可以用吗

我对 docs 了解不多。

当使用 ConditionalSelect 的 4 字节重载时,condition 参数实际上是 Vector<int>,例如带有 Vector<int>Vector<float> 参数的参数。 condition 在使用 8 字节版本等时是 Vector<long>

为了扩展@Hans 在您的问题中的评论,condition 的意思类似于:double c = cond == -1 ? a : b;。也就是说,当cond == -1时,它选择左边的值。当 cond == 0 时,它会选择正确的值。

cond是另外一回事时,我看到了一些我还不是特别了解的结果,并没有真正研究。

class Program
{
    static void Main(string[] args)
    {
        //Length depends on your Vector<int>.Count. In my computer it is 4
        Vector<int> vector1 = new Vector<int>(4); //vector1 == {<4, 4, 4, 4>}
        Vector<int> vector2 = new Vector<int>(5); //vector2 == {<5, 5, 5, 5>}
        Vector<int> mask = Vector.GreaterThan(vector1, vector2); //mask == {<0, 0, 0, 0>}
        Vector<int> selected = Vector.ConditionalSelect(mask, vector1, vector2); //selected == {<5, 5, 5, 5>}

        vector1 = new Vector<int>(4); //vector1 == {<4, 4, 4, 4>}
        vector2 = new Vector<int>(3); //vector2 == {<3, 3, 3, 3>}
        mask = Vector.GreaterThan(vector1, vector2); //mask == {<-1, -1, -1, -1>}
        selected = Vector.ConditionalSelect(mask, vector1, vector2); //selected == {<4, 4, 4, 4>}

        mask = new Vector<int>(123); //mask == {<123, 123, 123, 123>}
        selected = Vector.ConditionalSelect(mask, vector1, vector2); //selected == {<0, 0, 0, 0>}

        mask = new Vector<int>(4);
        selected = Vector.ConditionalSelect(mask, vector1, vector2); //selected == {<7, 7, 7, 7>}
    }
}