Tag: 矢量矢量

使用Vector 运行比经典循环慢的SIMD矢量化C#代码

我已经看过一些文章,描述了Vector是如何启用SIMD并使用JIT内在函数实现的,因此编译器在使用时会正确输出AVS / SSE / …指令,允许比经典线性循环更快的代码( 这里的例子)。 我决定尝试重写一个方法,我必须看看我是否设法获得了一些加速,但到目前为止我失败了,矢量化代码的运行速度比原来快3倍,而且我不确定为什么。 以下是两个版本的方法,检查两个Span实例是否具有相同位置的所有项对,它们相对于阈值共享相同的位置。 // Classic implementation public static unsafe bool MatchElementwiseThreshold(this Span x1, Span x2, float threshold) { fixed (float* px1 = &x1.DangerousGetPinnableReference(), px2 = &x2.DangerousGetPinnableReference()) for (int i = 0; i threshold != px2[i] > threshold) return false; return true; } // Vectorized public static unsafe bool MatchElementwiseThresholdSIMD(this Span […]