C++ SSE/AVX/SHA/AES指令集检查,用于程序定向优化。

结构定义:

cpp 复制代码
    class CPU final
    {
    public:
        static bool             has_sse2;
        static bool             has_sse3;
        static bool             has_ssse3;
        static bool             has_sse4_1;
        static bool             has_sse4_2;
        static bool             has_avx;
        static bool             has_avx2;
        static bool             has_avx512f;
        static bool             has_sse;
        static bool             has_aes;
        static bool             has_sha;

    public:
        static void             cpuid(unsigned int func, unsigned int& eax, unsigned int& ebx, unsigned int& ecx, unsigned int& edx) noexcept;
    };

检测实现:

cpp 复制代码
    bool CPU::has_sse       = false;
    bool CPU::has_sse2      = false;
    bool CPU::has_sse3      = false;
    bool CPU::has_ssse3     = false;
    bool CPU::has_sse4_1    = false;
    bool CPU::has_sse4_2    = false;
    bool CPU::has_avx       = false;
    bool CPU::has_avx2      = false;
    bool CPU::has_avx512f   = false;
    bool CPU::has_aes       = false;
    bool CPU::has_sha       = false;

    void CPU_detect() noexcept
    {
        unsigned int eax, ebx, ecx, edx;
        
        // 获取基本最大level
        CPU::cpuid(0, eax, ebx, ecx, edx);
        unsigned int max_base = eax;
        
        // 检测基本功能1
        if (max_base >= 1) {
            CPU::cpuid(1, eax, ebx, ecx, edx);

            // 根据映射表设置特性标志
            CPU::has_sse = (edx & (1 << 25)) != 0;    // SSE - EDX[25]
            CPU::has_sse2 = (edx & (1 << 26)) != 0;  // SSE2 - EDX[26]
            CPU::has_sse3 = (ecx & (1 << 0)) != 0;   // SSE3 - ECX[0]
            CPU::has_aes = (ecx & (1 << 25)) != 0;   // AES - ECX[25]
            CPU::has_avx = (ecx & (1 << 28)) != 0;   // AVX - ECX[28]
        }

        // 检测扩展功能7
        CPU::cpuid(0, eax, ebx, ecx, edx); // 重新获取最大level
        if (eax >= 7) { 
            // EAX=7, ECX=0 for extended features
            int cpus[4];
            __cpuidex(cpus, 7, 0);
            ebx = cpus[1];

            // Check bit 5 of EBX for AVX2 support
            // GCC -> __builtin_cpu_supports("avx2")
            CPU::has_avx2 = (ebx & (1 << 5)) != 0;      // AVX2 - EBX[5]
            CPU::has_avx512f = (ebx & (1 << 16)) != 0;  // AVX512F - EBX[16]
            CPU::has_sha = (ebx & (1 << 29)) != 0;      // SHA - EBX[29]
        }

        // 补充检测SSSE3, SSE4.1, SSE4.2(在基本功能1中)
        if (max_base >= 1) {
            CPU::cpuid(1, eax, ebx, ecx, edx);
            CPU::has_ssse3 = (ecx & (1 << 9)) != 0;   // SSSE3 - ECX[9]
            CPU::has_sse4_1 = (ecx & (1 << 19)) != 0; // SSE4.1 - ECX[19]
            CPU::has_sse4_2 = (ecx & (1 << 20)) != 0; // SSE4.2 - ECX[20]
        }
    }
相关推荐
代码地平线4 分钟前
C++类与对象(中):默认成员函数与日期类实战
c++·笔记·算法
hansang_IR8 分钟前
【题解】可持久化区间仿射区间和(Persistent Range Affine Range Sum)
c++·算法·线段树
万山寒15 分钟前
python依赖包导出离线安装到没有外网的服务器
服务器·开发语言·python
瑞码空间33 分钟前
01背包:动态规划的Hello World
c++·算法·0/1背包问题
特立独行的猫a38 分钟前
我用 Rust + Tauri 2 复刻了 N_m3u8DL-CLI:一个 m3u8 多线程下载器
开发语言·后端·rust·m3u8·视频下载器·m3u8dl-cli
程序员雷欧41 分钟前
LongAdder
开发语言·python
鱼子星_1 小时前
【C++】反向迭代器:反向迭代器的底层认识与模拟实现
开发语言·c++·笔记·stl
名字还没想好☜1 小时前
Java 用 MethodHandle 替代反射:调用性能实测、invokeExact 的坑与缓存
java·开发语言·缓存·反射·methodhandle
SMF19191 小时前
【Linux】完美解决缩略图工具gm调用java.io.FileNotFoundException: gm问题
java·开发语言·python
郝学胜-神的一滴1 小时前
[简化版 GAMES 104] 现代游戏引擎 05:游戏引擎世界构建核心机制深度解析
c++·程序人生·unity·游戏引擎·计算机图形学·opengl