• Home
Name Date Size #Lines LOC

..--

cmake/03-May-2024-5235

include/03-May-2024-1,280768

ndk_compat/03-May-2024-603318

scripts/03-May-2024-317249

src/03-May-2024-2,6532,193

test/03-May-2024-1,7141,292

.clang-formatD03-May-202451 54

.gitignoreD03-May-202413 21

.travis.ymlD03-May-20242.5 KiB118104

Android.bpD03-May-20247.2 KiB326306

CMakeLists.txtD03-May-20248.1 KiB237207

CONTRIBUTING.mdD03-May-2024969 2417

LICENSED03-May-202411.1 KiB202169

METADATAD03-May-2024364 1918

MODULE_LICENSE_APACHE2D03-May-20240

NOTICED03-May-202411.1 KiB202169

OWNERSD03-May-202438 43

PREUPLOAD.cfgD03-May-202429 32

README.mdD03-May-20246.2 KiB175132

TEST_MAPPINGD03-May-2024740 4039

WORKSPACED03-May-2024194 86

appveyor.ymlD03-May-2024600 2518

README.md

1# cpu_features [![Build Status](https://travis-ci.org/google/cpu_features.svg?branch=master)](https://travis-ci.org/google/cpu_features) [![Build status](https://ci.appveyor.com/api/projects/status/46d1owsj7n8dsylq/branch/master?svg=true)](https://ci.appveyor.com/project/gchatelet/cpu-features/branch/master)
2
3A cross-platform C library to retrieve CPU features (such as available
4instructions) at runtime.
5
6## Table of Contents
7
8- [Design Rationale](#rationale)
9- [Code samples](#codesample)
10- [Running sample code](#usagesample)
11- [What's supported](#support)
12- [Android NDK's drop in replacement](#ndk)
13- [License](#license)
14- [Build with cmake](#cmake)
15
16<a name="rationale"></a>
17## Design Rationale
18
19-   **Simple to use.** See the snippets below for examples.
20-   **Extensible.** Easy to add missing features or architectures.
21-   **Compatible with old compilers** and available on many architectures so it
22    can be used widely. To ensure that cpu_features works on as many platforms
23    as possible, we implemented it in a highly portable version of C: C99.
24-   **Sandbox-compatible.** The library uses a variety of strategies to cope
25    with sandboxed environments or when `cpuid` is unavailable. This is useful
26    when running integration tests in hermetic environments.
27-   **Thread safe, no memory allocation, and raises no exceptions.**
28    cpu_features is suitable for implementing fundamental libc functions like
29    `malloc`, `memcpy`, and `memcmp`.
30-   **Unit tested.**
31
32<a name="codesample"></a>
33### Checking features at runtime
34
35Here's a simple example that executes a codepath if the CPU supports both the
36AES and the SSE4.2 instruction sets:
37
38```c
39#include "cpuinfo_x86.h"
40
41static const X86Features features = GetX86Info().features;
42
43void Compute(void) {
44  if (features.aes && features.sse4_2) {
45    // Run optimized code.
46  } else {
47    // Run standard code.
48  }
49}
50```
51
52### Caching for faster evaluation of complex checks
53
54If you wish, you can read all the features at once into a global variable, and
55then query for the specific features you care about. Below, we store all the ARM
56features and then check whether AES and NEON are supported.
57
58```c
59#include <stdbool.h>
60#include "cpuinfo_arm.h"
61
62static const ArmFeatures features = GetArmInfo().features;
63static const bool has_aes_and_neon = features.aes && features.neon;
64
65// use has_aes_and_neon.
66```
67
68This is a good approach to take if you're checking for combinations of features
69when using a compiler that is slow to extract individual bits from bit-packed
70structures.
71
72### Checking compile time flags
73
74The following code determines whether the compiler was told to use the AVX
75instruction set (e.g., `g++ -mavx`) and sets `has_avx` accordingly.
76
77```c
78#include <stdbool.h>
79#include "cpuinfo_x86.h"
80
81static const X86Features features = GetX86Info().features;
82static const bool has_avx = CPU_FEATURES_COMPILED_X86_AVX || features.avx;
83
84// use has_avx.
85```
86
87`CPU_FEATURES_COMPILED_X86_AVX` is set to 1 if the compiler was instructed to
88use AVX and 0 otherwise, combining compile time and runtime knowledge.
89
90### Rejecting poor hardware implementations based on microarchitecture
91
92On x86, the first incarnation of a feature in a microarchitecture might not be
93the most efficient (e.g. AVX on Sandy Bridge). We provide a function to retrieve
94the underlying microarchitecture so you can decide whether to use it.
95
96Below, `has_fast_avx` is set to 1 if the CPU supports the AVX instruction
97set&mdash;but only if it's not Sandy Bridge.
98
99```c
100#include <stdbool.h>
101#include "cpuinfo_x86.h"
102
103static const X86Info info = GetX86Info();
104static const X86Microarchitecture uarch = GetX86Microarchitecture(&info);
105static const bool has_fast_avx = info.features.avx && uarch != INTEL_SNB;
106
107// use has_fast_avx.
108```
109
110This feature is currently available only for x86 microarchitectures.
111
112<a name="usagesample"></a>
113### Running sample code
114
115Building `cpu_features` brings a small executable to test the library.
116
117```shell
118 % ./build/list_cpu_features
119arch            : x86
120brand           :        Intel(R) Xeon(R) CPU E5-1650 0 @ 3.20GHz
121family          :   6 (0x06)
122model           :  45 (0x2D)
123stepping        :   7 (0x07)
124uarch           : INTEL_SNB
125flags           : aes,avx,cx16,smx,sse4_1,sse4_2,ssse3
126```
127
128```shell
129% ./build/list_cpu_features --json
130{"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"]}
131```
132
133<a name="support"></a>
134## What's supported
135
136|         | x86³ |   ARM   | AArch64 |  MIPS⁴ |  POWER  |
137|---------|:----:|:-------:|:-------:|:------:|:-------:|
138| Android | yes² |   yes¹  |   yes¹  |  yes¹  |   N/A   |
139| iOS     |  N/A | not yet | not yet |   N/A  |   N/A   |
140| Linux   | yes² |   yes¹  |   yes¹  |  yes¹  |   yes¹  |
141| MacOs   | yes² |   N/A   | not yet |   N/A  |    no   |
142| Windows | yes² | not yet | not yet |   N/A  |   N/A   |
143
1441.  **Features revealed from Linux.** We gather data from several sources
145    depending on availability:
146    +   from glibc's
147        [getauxval](https://www.gnu.org/software/libc/manual/html_node/Auxiliary-Vector.html)
148    +   by parsing `/proc/self/auxv`
149    +   by parsing `/proc/cpuinfo`
1502.  **Features revealed from CPU.** features are retrieved by using the `cpuid`
151    instruction.
1523.  **Microarchitecture detection.** On x86 some features are not always
153    implemented efficiently in hardware (e.g. AVX on Sandybridge). Exposing the
154    microarchitecture allows the client to reject particular microarchitectures.
1554.  All flavors of Mips are supported, little and big endian as well as 32/64
156    bits.
157
158<a name="ndk"></a>
159## Android NDK's drop in replacement
160
161[cpu_features](https://github.com/google/cpu_features) is now officially
162supporting Android and offers a drop in replacement of for the NDK's [cpu-features.h](https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h)
163, see [ndk_compat](ndk_compat) folder for details.
164
165<a name="license"></a>
166## License
167
168The cpu_features library is licensed under the terms of the Apache license.
169See [LICENSE](LICENSE) for more information.
170
171<a name="cmake"></a>
172## Build with CMake
173
174Please check the [CMake build instructions](cmake/README.md).
175