单个文件实现cpu的信息检测:ruapu.h的学习笔记

这篇文章介绍了GitHub上的ruapu项目,一个由nihui开发的单文件库,用于检测CPU的指令集支持。文章详细描述了ruapu.h中的初始化函数ruapu_init和检测函数ruapu_supports的使用,以及在不同平台上的实现细节,包括Windows、Android、Linux和macOS的特性和兼容性处理。
摘要由CSDN通过智能技术生成

ruapu.h的使用

  • "ruapu.h"主要提供了两个函数 ruapu_init 和 ruapu_supports,分别用于初始化和检测指令集支持。
// 使用示例见:https://github1s.com/nihui/ruapu/blob/master/main.c#L14-L16
void ruapu_init();
int ruapu_supports(const char* isa);

使用方式

#define RUAPU_IMPLEMENTATION
#include "ruapu.h"
  • 需要加上 #define RUAPU_IMPLEMENTATION是因为"ruapu.h"的结构如下(一般来说头文件只包含声明,所以要这样处理)
#ifndef RUAPU_H
#define RUAPU_H

void ruapu_init();
int ruapu_supports(const char* isa);

#ifdef RUAPU_IMPLEMENTATION
  // 两个函数的实现
#endif // RUAPU_IMPLEMENTATION

实现部分

#include <setjmp.h>
#include <string.h>

Windows 平台的ruapu_detect_isa()实现

#if defined _WIN32
#include <windows.h>

#if WINAPI_FAMILY == WINAPI_FAMILY_APP
// uwp does not support veh :(   UWP平台不支持VEH(Vectored Exception Handling)
#if defined (_MSC_VER)
#pragma message("warning: ruapu does not support UWP yet.")
#else
#warning ruapu does not support UWP yet.
#endif
static int ruapu_detect_isa(const void* some_inst)// 该函数总是返回0
{
    (void)some_inst;
    return 0;
}
#else // WINAPI_FAMILY == WINAPI_FAMILY_APP
// 如果不是UWP平台,这里放置非UWP平台的ruapu_detect_isa()实现
#endif // WINAPI_FAMILY == WINAPI_FAMILY_APP

Android、Linux、和 macOS 平台的ruapu_detect_isa()实现

#elif defined __ANDROID__ || defined __linux__ || defined __APPLE__
#include <signal.h>
// ruapu_detect_isa()通过模拟异常处理的方式,检测特定指令集的支持情况,当执行包含该指令集的代码时,通过捕捉非法指令信号来判断是否支持
#endif // defined _WIN32 || defined __ANDROID__ || defined __linux__ || defined __APPLE__

ruapu_detect_isa()在Android、Linux、和 macOS 平台实现的具体代码

#include <signal.h>

// 全局变量,用于标志是否捕获到 SIGILL 信号
static int g_ruapu_sigill_caught = 0;

// 全局变量,用于保存跳转的上下文信息
static sigjmp_buf g_ruapu_jmpbuf;

// 定义一个函数指针类型,表示一些指令的执行函数
typedef void (*ruapu_some_inst)();

// SIGILL 信号捕获处理函数
static void ruapu_catch_sigill(int signo, siginfo_t* si, void* data)
{
    // 防止未使用的参数警告
    (void)signo;
    (void)si;
    (void)data;

    // 标志捕获到 SIGILL 信号
    g_ruapu_sigill_caught = 1;
    
    // 跳转到之前保存的上下文信息,值为 -1 表示异常跳转
    siglongjmp(g_ruapu_jmpbuf, -1);
}

// 检测指令集支持的函数,输入是一个指令组成的数组比如:{0xc5, 0xfc, 0x54, 0xc0, 0xC3 },关于数组的来源可以先看下面的部分
static int ruapu_detect_isa(ruapu_some_inst some_inst)
{
    // 初始化捕获到 SIGILL 的标志为 0
    g_ruapu_sigill_caught = 0;

    // 定义 sigaction 结构体,用于设置信号处理函数
    struct sigaction sa = { 0 };
    struct sigaction old_sa;

    // 设置信号处理函数为 ruapu_catch_sigill
    sa.sa_flags = SA_ONSTACK | SA_RESTART | SA_SIGINFO;
    sa.sa_sigaction = ruapu_catch_sigill;

    // 设置 SIGILL 信号的处理函数,并保存之前的处理函数信息
    sigaction(SIGILL, &sa, &old_sa);

    // 通过 setjmp 在此处保存当前上下文信息,并返回 0 表示正常执行
    if (sigsetjmp(g_ruapu_jmpbuf, 1) == 0)
    {
        // 执行一些指令,如果有非法指令,会跳转到 catch 处理
        some_inst();
    }

    // 恢复 SIGILL 信号的原始处理函数
    sigaction(SIGILL, &old_sa, NULL);

    // 返回捕获到 SIGILL 信号的标志,如果捕获到返回 0,否则返回 1
    return g_ruapu_sigill_caught ? 0 : 1;
}

RUAPU_INSTCODE宏

  • 然后是为不同平台定义RUAPU_INSTCODE宏,比如在windows平台(_WIN32),x86_64架构(__x86_64__),Microsoft Visual Studio 编译器(_MSC_VER
#define RUAPU_INSTCODE(isa, ...) __pragma(section(".text")) __declspec(allocate(".text")) static unsigned char ruapu_some_##isa[] = { __VA_ARGS__, 0xc3 };
  • 展开为:
#define RUAPU_INSTCODE(isa, ...) \
    __pragma(section(".text"))    \
    __declspec(allocate(".text")) \
    static unsigned char ruapu_some_##isa[] = { __VA_ARGS__, 0xc3 };
  • 使用这个宏调用RUAPU_INSTCODE(avx, 0xc5, 0xfc, 0x54, 0xc0);将会被预处理器替换成以下代码:
__pragma(section(".text")) 
__declspec(allocate(".text")) 
static unsigned char ruapu_some_AVX[] = {0xc5, 0xfc, 0x54, 0xc0, 0xC3 }; // ## 是C语言宏中的连接操作符
  • 然后就能创建了一堆名为 ruapu_some_isa 的静态无符号字符数组
RUAPU_INSTCODE(mmx, 0x0f, 0xdb, 0xc0) // pand mm0,mm0
RUAPU_INSTCODE(sse, 0x0f, 0x54, 0xc0) // andps xmm0,xmm0
RUAPU_INSTCODE(avx, 0xc5, 0xfc, 0x54, 0xc0);// vandps ymm0,ymm0,ymm0

RUAPU_ISAENTRY宏

  • 然后用RUAPU_ISAENTRY宏来定义了g_ruapu_isa_map的结构体数组
struct ruapu_isa_entry
{
    const char* isa;
    ruapu_some_inst inst;
    int capable;
};

#define RUAPU_ISAENTRY(isa) { #isa, (ruapu_some_inst)ruapu_some_##isa, 0 },

struct ruapu_isa_entry g_ruapu_isa_map[] = {

RUAPU_ISAENTRY(mmx)
RUAPU_ISAENTRY(sse)
RUAPU_ISAENTRY(sse2)
RUAPU_ISAENTRY(sse3)
RUAPU_ISAENTRY(ssse3)
RUAPU_ISAENTRY(sse41)

}

两个函数的最终实现

void ruapu_init()
{
    // 如果是在支持的操作系统下(Windows、Android、Linux、macOS等)
#if defined _WIN32 || defined __ANDROID__ || defined __linux__ || defined __APPLE__
    // 遍历 g_ruapu_isa_map 数组
    for (size_t i = 0; i < sizeof(g_ruapu_isa_map) / sizeof(g_ruapu_isa_map[0]); i++)
    {
        // 调用 ruapu_detect_isa 函数检测指令集支持情况,并将结果存储在 g_ruapu_isa_map[i].capable 中
        g_ruapu_isa_map[i].capable = ruapu_detect_isa(g_ruapu_isa_map[i].inst);
    }
#else
    // 如果操作系统不是上述支持的操作系统
    // 在此处初始化 g_ruapu_isa_map 数组,默认全部为零
    // 仍然可以使用 ruapu_some_XYZ() 函数
    // 但是你需要自己处理信号
    // 发出编译警告,说明 ruapu 还不支持当前裸机操作系统
#warning ruapu does not support your baremetal os yet
#endif
}

int ruapu_supports(const char* isa)
{
    // 遍历 g_ruapu_isa_map 数组
    for (size_t i = 0; i < sizeof(g_ruapu_isa_map) / sizeof(g_ruapu_isa_map[0]); i++)
    {
        // 检查是否有与输入指令集名称匹配的条目
        if (strcmp(g_ruapu_isa_map[i].isa, isa) == 0)
        {
            // 如果找到匹配的指令集,返回相应的 capable 值
            return g_ruapu_isa_map[i].capable;
        }
    }
    // 如果没有找到匹配的指令集,返回0表示不支持
    return 0;
}

CG

  • Cpu-z由Cpuid提供是一个免费的系统信息软件,它收集有关计算机主要装置的信息。 它报告数据,如处理器名称和号码、代号、过程、包装、高速缓存水平、主板和芯片详情(如内存类型、大小、时序和SPD)。 这个应用可以实时测量核心内部频率和内存频率。 通过Google Playstore在Windows 系列电脑上也能使用.
  • https://github.com/pytorch/cpuinfo
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值