怎么知道自己电脑的CPU支不支持AVX指令集?以及如何使用这些指令集?

没装 PowerShell 的可以先装一个。(没装 .NET 的也可以先装一个。)

dotnet tool install --global PowerShell

然后运行

pwsh -c "[System.Runtime.Intrinsics.X86.Avx]::IsSupported"

会显示“True”or“False”。

开发也很简单。例如,我们可以用 AVX 来做 4x4 的双精度矩阵乘法。

// 示例代码,仅作参考。
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

struct Matrix4x4 {
    Vector256<double> _Row0;
    Vector256<double> _Row1;
    Vector256<double> _Row2;
    Vector256<double> _Row3;

    public static unsafe Matrix4x4 operator *(Matrix4x4 first, Matrix4x4 second) {
        var row = (double*)&first._Row0;
        first._Row0 =
            Avx.Add(Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(0 + row), second._Row0),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(1 + row), second._Row1)),
                    Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(2 + row), second._Row2),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(3 + row), second._Row3)));

        row = (double*)&first._Row1;
        first._Row1 =
            Avx.Add(Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(0 + row), second._Row0),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(1 + row), second._Row1)),
                    Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(2 + row), second._Row2),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(3 + row), second._Row3)));

        row = (double*)&first._Row2;
        first._Row2 =
            Avx.Add(Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(0 + row), second._Row0),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(1 + row), second._Row1)),
                    Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(2 + row), second._Row2),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(3 + row), second._Row3)));

        row = (double*)&first._Row3;
        first._Row3 =
            Avx.Add(Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(0 + row), second._Row0),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(1 + row), second._Row1)),
                    Avx.Add(Avx.Multiply(Avx.BroadcastScalarToVector256(2 + row), second._Row2),
                            Avx.Multiply(Avx.BroadcastScalarToVector256(3 + row), second._Row3)));

        return first;
    }
}

推荐个网站查指令:

x86/x64 SIMD Instruction List (SSE to AVX512)www.officedaytime.com