Reputation: 30145
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.
Upvotes: 4
Views: 1179
Reputation: 41962
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);
Upvotes: 0
Reputation: 70234
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.
Upvotes: 4
Reputation: 368499
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!
Upvotes: 0