• Home
Name Date Size #Lines LOC

..--

.github/workflows/03-May-2024-248205

bazel/03-May-2024-126

ci/03-May-2024-916746

cmake/03-May-2024-5437

include/03-May-2024-1,5321,013

ndk_compat/03-May-2024-606319

scripts/03-May-2024-517397

src/03-May-2024-3,8263,234

test/03-May-2024-2,6802,145

.clang-formatD03-May-202451 54

.dockerignoreD03-May-2024335 3229

.gitignoreD03-May-2024199 2016

Android.bpD03-May-20249 KiB382361

BUILD.bazelD03-May-20248.3 KiB331307

CMakeLists.txtD03-May-20248.1 KiB250216

CONTRIBUTING.mdD03-May-2024969 2417

LICENSED03-May-202412.6 KiB231195

METADATAD03-May-2024386 2019

MODULE_LICENSE_APACHE2D03-May-20240

OWNERSD03-May-202438 43

PREUPLOAD.cfgD03-May-202429 32

README.mdD03-May-20247.9 KiB229173

TEST_MAPPINGD03-May-2024525 3130

WORKSPACED03-May-2024890 2418

README.md

1# cpu_features
2[![Linux Status][linux_svg]][linux_link]
3[![Macos Status][macos_svg]][macos_link]
4[![Windows Status][windows_svg]][windows_link]
5
6[linux_svg]: https://github.com/google/cpu_features/actions/workflows/amd64_linux.yml/badge.svg?branch=main
7[linux_link]: https://github.com/google/cpu_features/actions/workflows/amd64_linux.yml
8[macos_svg]: https://github.com/google/cpu_features/actions/workflows/amd64_macos.yml/badge.svg?branch=main
9[macos_link]: https://github.com/google/cpu_features/actions/workflows/amd64_macos.yml
10[windows_svg]: https://github.com/google/cpu_features/actions/workflows/amd64_windows.yml/badge.svg?branch=main
11[windows_link]: https://github.com/google/cpu_features/actions/workflows/amd64_windows.yml
12
13A cross-platform C library to retrieve CPU features (such as available
14instructions) at runtime.
15
16## Table of Contents
17
18- [Design Rationale](#rationale)
19- [Code samples](#codesample)
20- [Running sample code](#usagesample)
21- [What's supported](#support)
22- [Android NDK's drop in replacement](#ndk)
23- [License](#license)
24- [Build with cmake](#cmake)
25- [Community Bindings](#bindings)
26
27<a name="rationale"></a>
28## Design Rationale
29
30-   **Simple to use.** See the snippets below for examples.
31-   **Extensible.** Easy to add missing features or architectures.
32-   **Compatible with old compilers** and available on many architectures so it
33    can be used widely. To ensure that cpu_features works on as many platforms
34    as possible, we implemented it in a highly portable version of C: C99.
35-   **Sandbox-compatible.** The library uses a variety of strategies to cope
36    with sandboxed environments or when `cpuid` is unavailable. This is useful
37    when running integration tests in hermetic environments.
38-   **Thread safe, no memory allocation, and raises no exceptions.**
39    cpu_features is suitable for implementing fundamental libc functions like
40    `malloc`, `memcpy`, and `memcmp`.
41-   **Unit tested.**
42
43<a name="codesample"></a>
44## Code samples
45
46**Note:** For C++ code, the library functions are defined in the `cpu_features` namespace.
47
48### Checking features at runtime
49
50Here's a simple example that executes a codepath if the CPU supports both the
51AES and the SSE4.2 instruction sets:
52
53```c
54#include "cpuinfo_x86.h"
55
56// For C++, add `using namespace cpu_features;`
57static const X86Features features = GetX86Info().features;
58
59void Compute(void) {
60  if (features.aes && features.sse4_2) {
61    // Run optimized code.
62  } else {
63    // Run standard code.
64  }
65}
66```
67
68### Caching for faster evaluation of complex checks
69
70If you wish, you can read all the features at once into a global variable, and
71then query for the specific features you care about. Below, we store all the ARM
72features and then check whether AES and NEON are supported.
73
74```c
75#include <stdbool.h>
76#include "cpuinfo_arm.h"
77
78// For C++, add `using namespace cpu_features;`
79static const ArmFeatures features = GetArmInfo().features;
80static const bool has_aes_and_neon = features.aes && features.neon;
81
82// use has_aes_and_neon.
83```
84
85This is a good approach to take if you're checking for combinations of features
86when using a compiler that is slow to extract individual bits from bit-packed
87structures.
88
89### Checking compile time flags
90
91The following code determines whether the compiler was told to use the AVX
92instruction set (e.g., `g++ -mavx`) and sets `has_avx` accordingly.
93
94```c
95#include <stdbool.h>
96#include "cpuinfo_x86.h"
97
98// For C++, add `using namespace cpu_features;`
99static const X86Features features = GetX86Info().features;
100static const bool has_avx = CPU_FEATURES_COMPILED_X86_AVX || features.avx;
101
102// use has_avx.
103```
104
105`CPU_FEATURES_COMPILED_X86_AVX` is set to 1 if the compiler was instructed to
106use AVX and 0 otherwise, combining compile time and runtime knowledge.
107
108### Rejecting poor hardware implementations based on microarchitecture
109
110On x86, the first incarnation of a feature in a microarchitecture might not be
111the most efficient (e.g. AVX on Sandy Bridge). We provide a function to retrieve
112the underlying microarchitecture so you can decide whether to use it.
113
114Below, `has_fast_avx` is set to 1 if the CPU supports the AVX instruction
115set&mdash;but only if it's not Sandy Bridge.
116
117```c
118#include <stdbool.h>
119#include "cpuinfo_x86.h"
120
121// For C++, add `using namespace cpu_features;`
122static const X86Info info = GetX86Info();
123static const X86Microarchitecture uarch = GetX86Microarchitecture(&info);
124static const bool has_fast_avx = info.features.avx && uarch != INTEL_SNB;
125
126// use has_fast_avx.
127```
128
129This feature is currently available only for x86 microarchitectures.
130
131<a name="usagesample"></a>
132### Running sample code
133
134Building `cpu_features` (check [quickstart](#quickstart) below) brings a small executable to test the library.
135
136```shell
137 % ./build/list_cpu_features
138arch            : x86
139brand           :        Intel(R) Xeon(R) CPU E5-1650 0 @ 3.20GHz
140family          :   6 (0x06)
141model           :  45 (0x2D)
142stepping        :   7 (0x07)
143uarch           : INTEL_SNB
144flags           : aes,avx,cx16,smx,sse4_1,sse4_2,ssse3
145```
146
147```shell
148% ./build/list_cpu_features --json
149{"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"]}
150```
151
152<a name="support"></a>
153## What's supported
154
155|         | x86³ | ARM     | AArch64 | MIPS⁴   | POWER   |
156|---------|:----:|:-------:|:-------:|:-------:|:-------:|
157| Android | yes² | yes¹    | yes¹    | yes¹    | N/A     |
158| iOS     | N/A  | not yet | not yet | N/A     | N/A     |
159| Linux   | yes² | yes¹    | yes¹    | yes¹    | yes¹    |
160| MacOs   | yes² | N/A     | not yet | N/A     | no      |
161| Windows | yes² | not yet | not yet | N/A     | N/A     |
162| FreeBSD | yes² | not yet | not yet | not yet | not yet |
163
1641.  **Features revealed from Linux.** We gather data from several sources
165    depending on availability:
166    +   from glibc's
167        [getauxval](https://www.gnu.org/software/libc/manual/html_node/Auxiliary-Vector.html)
168    +   by parsing `/proc/self/auxv`
169    +   by parsing `/proc/cpuinfo`
1702.  **Features revealed from CPU.** features are retrieved by using the `cpuid`
171    instruction.
1723.  **Microarchitecture detection.** On x86 some features are not always
173    implemented efficiently in hardware (e.g. AVX on Sandybridge). Exposing the
174    microarchitecture allows the client to reject particular microarchitectures.
1754.  All flavors of Mips are supported, little and big endian as well as 32/64
176    bits.
177
178<a name="ndk"></a>
179## Android NDK's drop in replacement
180
181[cpu_features](https://github.com/google/cpu_features) is now officially
182supporting Android and offers a drop in replacement of for the NDK's [cpu-features.h](https://android.googlesource.com/platform/ndk/+/main/sources/android/cpufeatures/cpu-features.h)
183, see [ndk_compat](ndk_compat) folder for details.
184
185<a name="license"></a>
186## License
187
188The cpu_features library is licensed under the terms of the Apache license.
189See [LICENSE](LICENSE) for more information.
190
191<a name="cmake"></a>
192## Build with CMake
193
194Please check the [CMake build instructions](cmake/README.md).
195
196<a name="quickstart"></a>
197### Quickstart
198
199 - Run `list_cpu_features`
200```sh
201cmake -S. -Bbuild -DBUILD_TESTING=OFF -DCMAKE_BUILD_TYPE=Release
202cmake --build build --config Release -j
203./build/list_cpu_features --json
204```
205
206_Note_: Use `--target ALL_BUILD` on the second line for `Visual Studio` and `XCode`.
207
208 - run tests
209```sh
210cmake -S. -Bbuild -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Debug
211cmake --build build --config Debug -j
212cmake --build build --config Debug --target test
213```
214
215_Note_: Use `--target RUN_TESTS` on the last line for `Visual Studio` and `--target RUN_TEST` for `XCode`.
216
217<a name="bindings"></a>
218## Community bindings
219
220Links provided here are not affiliated with Google but are kindly provided by the OSS Community.
221
222 - .Net
223   - https://github.com/toor1245/cpu_features.NET
224 - Python
225   - https://github.com/Narasimha1997/py_cpu
226
227
228_Send PR to showcase your wrapper here_
229