无需组装即可检测CPU能力

发布于 2024-08-15 05:52:21 字数 254 浏览 5 评论 0原文

我一直在寻找确定 CPU 及其功能的方法(例如 SEE、SSE2 等)。

然而,我发现的所有方法都涉及使用 cpuid 指令的汇编代码。鉴于编译器甚至目标之间的 C/C++ 汇编方式不同(VC 下的 64 位目标没有内联汇编),我宁愿避免这种情况。

是否有一些简单的库或操作系统函数(适用于 Windows 和 Linux)来获取此信息?

目前我只对使用 x86 和 x86-64 CPU 的平台感兴趣,而且我肯定至少需要支持 AMD 和 Intel。

I've been looking at ways to determine the CPU, and its capabilities (e.g. SEE, SSE2, etc).

However all the ways I've found involved assembly code using the cpuid instruction. Given the different ways of doing assembly in C/C++ between compilers and even targets (no inline assembly for 64-bit targets under VC), I'd rather avoid that.

Is there some simple library around, or OS functions (for both Windows and Linux) for getting this information?

Currently I'm only interested in platforms using the x86 and x86-64 CPU's, and I definitely need to support at least AMD and Intel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玻璃人 2024-08-22 05:52:21

编辑:首先,我在谷歌上搜索“libcpuid”,记住与同行程序员的对话,谷歌搜索将我指向这个 sourceforge 页面 看起来相当过时。然后我注意到这是“libcpu”的项目页面。

实际上,确实有一个 libcpuid 可能适合您的需求。


上次我寻找这样的库时,我发现了crisscross。虽然它是C++。

还有geekinfo

但我不知道一个只做 cpuid 的跨平台库。

EDIT: At first, I googled for "libcpuid" remembering a conversation with peer programmers, and the google search pointed me to this sourceforge page which looks quite outdated. Then I noticed it was the project page of "libcpu".

Actually, there really is a libcpuid that might suit your needs.


Last time I looked for such a library, I found crisscross. It's C++ though.

There is also geekinfo.

But I don't know a cross-platform library that just does cpuid.

九八野马 2024-08-22 05:52:21

linux下查看/proc/cpuinfo

Under linux peek in /proc/cpuinfo

想念有你 2024-08-22 05:52:21

这非常依赖于硬件——这是操作系统通常会屏蔽你的一个方面——要求它是跨平台的有点困难。

拥有它会很有用,但我担心您可能只需要枚举您想要支持的 CPU 和功能的叉积,乘以操作系统的数量(此处为两个)并为所有操作系统编写测试。我不认为这已经完成了——如果我忽略了某些事情,肯定会有人纠正我。

这将是一个有用的库,祝你好运!

This so hardware-dependent --- which is an aspect the OS usually shields you from --- that requiring it to be cross-platform is a tad hard.

It would be useful to have, but I fear you may just have to enumerate the cross-product of CPU s and features you would like to support, times the number of OSs (here two) and write tests for all. I do not think it has been done -- and someone will surely correct me if I overlooked something.

That would be a useful library to have, so good luck with the endeavor!

樱娆 2024-08-22 05:52:21

您可以使用 cpuinfo 这是一个跨平台的CPU信息库

检查主机CPU是否支持x86 AVX

cpuinfo_initialize();
如果(cpuinfo_has_x86_avx()){
    avx_implementation(参数);
}

将线程固定到与当前核心(Linux 或 Android)共享二级缓存的核心

cpuinfo_initialize();
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
const struct cpuinfo_cache* current_l2 = cpuinfo_get_current_processor()->cache.l2;
for (uint32_t i = 0; i < current_l2->processor_count; i++) {
    CPU_SET(cpuinfo_get_processor(current_l2->processor_start + i)->linux_id, &cpu_set);
}
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set);

You can use cpuinfo which is a cross-platform CPU information library

Check if the host CPU supports x86 AVX

cpuinfo_initialize();
if (cpuinfo_has_x86_avx()) {
    avx_implementation(arguments);
}

Pin thread to cores sharing L2 cache with the current core (Linux or Android)

cpuinfo_initialize();
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
const struct cpuinfo_cache* current_l2 = cpuinfo_get_current_processor()->cache.l2;
for (uint32_t i = 0; i < current_l2->processor_count; i++) {
    CPU_SET(cpuinfo_get_processor(current_l2->processor_start + i)->linux_id, &cpu_set);
}
pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文