commit | dcefd46d08077e780f71c7ca176ea17d14c150c6 | [log] [tgz] |
---|---|---|
author | Guillaume Chatelet <gchatelet@google.com> | Thu Feb 01 22:12:57 2018 +0100 |
committer | Guillaume Chatelet <gchatelet@google.com> | Thu Feb 01 22:12:57 2018 +0100 |
tree | 010dfb9b355353b99927a3cade0a82a0ddb59a56 | |
parent | b50b7b5e08337308753fd2cf5bc8f5e00f336558 [diff] |
Update README.md
A cross-platform C library to retrieve CPU features (such as available instructions) at runtime.
cpuid
is unavailable. This is useful when running integration tests in hermetic environments.malloc
, memcpy
, and memcmp
.Here's a simple example that executes a codepath if the CPU supports both the AES and the SSE4.2 instruction sets:
#include "cpuinfo_x86.h" static const X86Features features = GetX86Info().features; void Compute(void) { if(features.aes && features.sse4_2) { // Run optimized code. } else { // Run standard code. } }
If you wish, you can read all the features at once into a global variable, and then query for the specific features you care about. Below, we store all the ARM features and then check whether AES and NEON are supported.
#include "cpuinfo_arm.h" static const ArmFeatures features = GetArmInfo().features; static const bool has_aes_and_neon = features.aes && features.neon; // use has_aes_and_neon.
This is a good approach to take if you're checking for combinations of features when using a compiler that is slow to extract individual bits from bit-packed structures.
The following code determines whether the compiler was told to use the AVX instruction set (e.g., g++ -mavx
) and sets has_avx
accordingly.
#include "cpuinfo_x86.h" static const X86Features features = GetX86Info().features; static const bool has_avx = CPU_FEATURES_COMPILED_X86_AVX || features.avx; // use has_avx.
CPU_FEATURES_COMPILED_X86_AVX
is set to 1 if the compiler was instructed to use AVX and 0 otherwise, combining compile time and runtime knowledge.
On x86, the first incarnation of a feature in a microarchitecture might not be the most efficient (e.g., AVX on Sandy Bridge). We provide a function to retrieve the underlying microarchitecture so you can decide whether to use it.
Below, has_fast_avx
is set to 1 if the CPU supports the AVX instruction set—but only if it's not Sandy Bridge.
#include "cpuinfo_x86.h" static const X86Info info = GetX86Info(); static const X86Microarchitecture uarch = GetX86Microarchitecture(&info); static const bool has_fast_avx = info.features.avx && uarch != INTEL_SNB; // use has_fast_avx.
This feature is currently available only for x86 microarchitectures.
x86 | ARM | AArch64 | MIPS | POWER | |
---|---|---|---|---|---|
Features revealed from CPU | yes | no* | no* | not yet | not yet |
Features revealed from Linux | no | yes | yes | yes | not yet |
Microarchitecture detection | yes | no | no | no | not yet |
Windows support | yes | no | no | no | not yet |
cpuid
instruction. *Unfortunately this instruction is privileged for some architectures, in which case we fall back to Linux./proc/self/auxv
/proc/cpuinfo