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]
        }
    }
相关推荐
倒头就睡的小比特20 小时前
算法竞赛C++常用的STL
c++·算法
weilx123420 小时前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!20 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读21 小时前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
Smileyqp沛沛21 小时前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车1 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光1 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
吞下星星的少年·-·1 天前
C++ 萌新语法入门篇
c++·算法比赛
霍霍的袁1 天前
【C++】map 和 set 的使用 | 从用法到底层
开发语言·c++·学习·visual studio
another heaven1 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法