Top Related Projects
Quick Overview
Google's cpu_features is a cross-platform C library for CPU feature detection and CPU identification. It provides a simple and efficient way to access CPU information and capabilities across various architectures, including x86, ARM, AArch64, and MIPS.
Pros
- Cross-platform support for multiple CPU architectures
- Easy-to-use API for querying CPU features and capabilities
- Lightweight and efficient implementation
- Well-maintained and actively developed by Google
Cons
- Limited to CPU feature detection, not a comprehensive system information library
- May require periodic updates to support new CPU features as they are introduced
- Potential for false positives or negatives in rare edge cases
Code Examples
- Checking for specific CPU features:
#include "cpu_features_macros.h"
#include "cpuinfo_x86.h"
static const X86Features features = GetX86Info().features;
if (features.avx2) {
// Use AVX2 optimized code path
} else {
// Use fallback implementation
}
- Getting CPU vendor and model information:
#include "cpuinfo_x86.h"
const X86Info info = GetX86Info();
printf("Vendor: %s\n", GetX86VendorString(info.vendor));
printf("Brand: %s\n", info.brand_string);
- Detecting cache sizes:
#include "cpuinfo_x86.h"
const X86CacheInfo cache_info = GetX86CacheInfo();
printf("L1 Data Cache: %d KB\n", cache_info.levels[0].data_size / 1024);
printf("L2 Cache: %d KB\n", cache_info.levels[1].size / 1024);
Getting Started
-
Clone the repository:
git clone https://github.com/google/cpu_features.git
-
Build the library using CMake:
cd cpu_features mkdir build && cd build cmake .. make
-
Include the necessary headers in your C/C++ project:
#include "cpu_features_macros.h" #include "cpuinfo_x86.h" // Or other architecture-specific header
-
Link against the built library when compiling your project.
Competitor Comparisons
CPU feature identification for Go
Pros of cpuid
- Lightweight and focused specifically on x86 CPU feature detection
- Simple API with easy-to-use functions for checking specific CPU features
- Pure Go implementation, making it easily portable and embeddable
Cons of cpuid
- Limited to x86 architectures, unlike cpu_features which supports multiple platforms
- Less comprehensive feature detection compared to cpu_features
- Lacks some advanced capabilities like cache information retrieval
Code Comparison
cpuid:
if cpuid.CPU.Has(cpuid.AVX2) {
// Use AVX2 optimized code
}
cpu_features:
#include "cpu_features_macros.h"
#if defined(CPU_FEATURES_ARCH_X86)
const X86Features features = GetX86Info().features;
if (features.avx2) {
// Use AVX2 optimized code
}
#endif
Summary
cpuid is a lightweight, Go-specific library for x86 CPU feature detection, offering a simple API for basic feature checks. cpu_features provides a more comprehensive, cross-platform solution with advanced capabilities. The choice between them depends on the specific requirements of the project, such as language preference, supported architectures, and depth of CPU information needed.
CPU INFOrmation library (x86/x86-64/ARM/ARM64, Linux/Windows/Android/macOS/iOS)
Pros of cpuinfo
- More comprehensive CPU feature detection, especially for ARM processors
- Actively maintained with frequent updates and contributions
- Supports a wider range of platforms and architectures
Cons of cpuinfo
- Larger codebase, potentially more complex to integrate
- May have higher memory footprint due to extensive feature set
Code Comparison
cpuinfo:
#include <cpuinfo.h>
if (cpuinfo_initialize()) {
if (cpuinfo_has_x86_avx2()) {
// Use AVX2 optimized code
}
}
cpu_features:
#include "cpu_features_macros.h"
#include "cpuinfo_x86.h"
X86Features features = GetX86Info().features;
if (features.avx2) {
// Use AVX2 optimized code
}
Both libraries provide similar functionality for detecting CPU features, but cpuinfo offers more detailed information and supports a broader range of architectures. cpu_features has a simpler API and smaller codebase, which may be preferable for projects with limited scope or resources. The choice between the two depends on the specific requirements of your project, such as target platforms, desired feature coverage, and integration complexity.
Quantized Neural Network PACKage - mobile-optimized implementation of quantized neural network operators
Pros of QNNPACK
- Specialized for quantized neural network computations
- Optimized for mobile and embedded devices
- Integrates seamlessly with PyTorch ecosystem
Cons of QNNPACK
- More focused scope, primarily for neural network operations
- Potentially steeper learning curve for non-ML developers
Code Comparison
QNNPACK example (simplified):
qnnp_operator_t convolution;
qnnp_create_convolution2d_nhwc_q8(
padding, kernel_size, stride, dilation,
groups, input_channels, output_channels,
input_zero_point, input_scale,
kernel_zero_point, kernel_scale,
bias, output_zero_point, output_scale,
output_min, output_max,
0, &convolution);
cpu_features example:
#include "cpuinfo_x86.h"
const X86Features features = GetX86Info().features;
if (features.avx2) {
// Use AVX2 optimized code
}
QNNPACK is tailored for quantized neural network operations, making it ideal for machine learning applications on resource-constrained devices. It offers optimized performance for specific use cases but may be overkill for general CPU feature detection.
cpu_features provides a more general-purpose solution for detecting CPU capabilities across various architectures. It's simpler to use for basic feature checks but lacks the specialized optimizations that QNNPACK offers for neural network computations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
cpu_features
A cross-platform C library to retrieve CPU features (such as available instructions) at runtime.
GitHub-CI Status
Linux | FreeBSD | MacOS | Windows | |
---|---|---|---|---|
amd64 | ||||
AArch64 | ||||
ARM | ||||
MIPS | ||||
POWER | ||||
RISCV | ||||
LOONGARCH | ||||
s390x |
Table of Contents
- Design Rationale
- Code samples
- Running sample code
- What's supported
- Android NDK's drop in replacement
- License
- Build with cmake
- Community Bindings
Design Rationale
- Simple to use. See the snippets below for examples.
- Extensible. Easy to add missing features or architectures.
- Compatible with old compilers and available on many architectures so it can be used widely. To ensure that cpu_features works on as many platforms as possible, we implemented it in a highly portable version of C: C99.
- Sandbox-compatible. The library uses a variety of strategies to cope
with sandboxed environments or when
cpuid
is unavailable. This is useful when running integration tests in hermetic environments. - Thread safe, no memory allocation, and raises no exceptions.
cpu_features is suitable for implementing fundamental libc functions like
malloc
,memcpy
, andmemcmp
. - Unit tested.
Code samples
Note: For C++ code, the library functions are defined in the cpu_features
namespace.
Checking features at runtime
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"
// For C++, add `using namespace cpu_features;`
static const X86Features features = GetX86Info().features;
void Compute(void) {
if (features.aes && features.sse4_2) {
// Run optimized code.
} else {
// Run standard code.
}
}
Caching for faster evaluation of complex checks
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 <stdbool.h>
#include "cpuinfo_arm.h"
// For C++, add `using namespace cpu_features;`
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.
Checking compile time flags
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 <stdbool.h>
#include "cpuinfo_x86.h"
// For C++, add `using namespace cpu_features;`
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.
Rejecting poor hardware implementations based on microarchitecture
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 <stdbool.h>
#include "cpuinfo_x86.h"
// For C++, add `using namespace cpu_features;`
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.
Running sample code
Building cpu_features
(check quickstart below) brings a small executable to test the library.
% ./build/list_cpu_features
arch : x86
brand : Intel(R) Xeon(R) CPU E5-1650 0 @ 3.20GHz
family : 6 (0x06)
model : 45 (0x2D)
stepping : 7 (0x07)
uarch : INTEL_SNB
flags : aes,avx,cx16,smx,sse4_1,sse4_2,ssse3
% ./build/list_cpu_features --json
{"arch":"x86","brand":" Intel(R) Xeon(R) CPU E5-1650 0 @ 3.20GHz","family":6,"model":45,"stepping":7,"uarch":"INTEL_SNB","flags":["aes","avx","cx16","smx","sse4_1","sse4_2","ssse3"]}
What's supported
x86³ | AArch64 | ARM | MIPSⴠ| POWER | RISCV | Loongarch | s390x | |
---|---|---|---|---|---|---|---|---|
Linux | yes² | yes¹ | yes¹ | yes¹ | yes¹ | yes¹ | yes¹ | yes¹ |
FreeBSD | yes² | not yet | not yet | not yet | not yet | N/A | not yet | not yet |
MacOs | yes² | yesⵠ| N/A | N/A | N/A | N/A | N/A | N/A |
Windows | yes² | not yet | not yet | N/A | N/A | N/A | N/A | N/A |
Android | yes² | yes¹ | yes¹ | yes¹ | N/A | N/A | N/A | N/A |
iOS | N/A | not yet | not yet | N/A | N/A | N/A | N/A | N/A |
- Features revealed from Linux. We gather data from several sources
depending on availability:
- from glibc's getauxval
- by parsing
/proc/self/auxv
- by parsing
/proc/cpuinfo
- Features revealed from CPU. features are retrieved by using the
cpuid
instruction. - Microarchitecture detection. On x86 some features are not always implemented efficiently in hardware (e.g. AVX on Sandybridge). Exposing the microarchitecture allows the client to reject particular microarchitectures.
- All flavors of Mips are supported, little and big endian as well as 32/64 bits.
- Features revealed from sysctl. features are retrieved by the
sysctl
instruction.
Android NDK's drop in replacement
cpu_features is now officially supporting Android and offers a drop in replacement of for the NDK's cpu-features.h , see ndk_compat folder for details.
License
The cpu_features library is licensed under the terms of the Apache license. See LICENSE for more information.
Build with CMake
Please check the CMake build instructions.
Quickstart
-
Run
list_cpu_features
cmake -S. -Bbuild -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release cmake --build build --config Release -j ./build/list_cpu_features --json
Note: Use
--target ALL_BUILD
on the second line forVisual Studio
andXCode
. -
run tests
cmake -S. -Bbuild -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Debug cmake --build build --config Debug -j cmake --build build --config Debug --target test
Note: Use
--target RUN_TESTS
on the last line forVisual Studio
and--target RUN_TEST
forXCode
. -
install
cpu_features
cmake --build build --config Release --target install -v
Note: Use
--target INSTALL
forVisual Studio
.Note: When using
Makefile
orXCode
generator, you can useDESTDIR
to install on a local repository.
e.g.cmake --build build --config Release --target install -v -- DESTDIR=install
Community bindings
Links provided here are not affiliated with Google but are kindly provided by the OSS Community.
- .Net
- Python
- Java
Send PR to showcase your wrapper here
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot