commit | 44d56a0a28ea6fee0cdeedb6823cca50d39e5209 | [log] [tgz] |
---|---|---|
author | Guillaume Chatelet <gchatelet@google.com> | Fri Dec 22 12:48:26 2017 +0100 |
committer | Guillaume Chatelet <gchatelet@google.com> | Wed Jan 31 15:17:13 2018 +0100 |
tree | dae6fb7a5de4fcbff235b4ed06d1b6086e3c2b44 |
Initial commit.
A cross platform C89 library to get cpu features at runtime.
#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. } }
Features are bit packed and some compilers generate poor code when combining them. A simple solution is to read them once and store the result in a global variable.
#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.
#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 is instructed to use AVX, 0 otherwise. This allows combining compile time and runtime knowledge.
#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.
On x86, the first incarnation of a feature in a microarchitecture may not be very efficient (e.g. AVX on Sandybridge). We provide a function to retrieve the underlying microarchitecture so clients can decide whether they want to use it or not.
| x86 | ARM | aarch64 | mips | POWER
---------------------------- | :-: | :-: | :-----: | :----: | :-----: Features From cpu | yes | no* | no* | no yet | not yet Features From Linux | no | yes | yes | yes | not yet Micro Architecture Detection | yes | no | no | no | not yet Windows support | yes | no | no | no | not yet
/proc/self/auxv
/proc/cpuinfo