1# CPU Features
2
3CPU features are hardware extensions provided by CPUs. You can call instructions and set special registers to use the CPU features, such as VFP-v32d32, Neon, IDIV, and AES on the ARMv7-A architecture. Many CPU features are optional, and different CPU vendors provide different CPU features.
4
5
6During native library development in your OpenHarmony project, you can follow this topic to use code related to the CPU features in your application.
7
8
9Currently, OpenHarmony does not provide APIs for obtaining CPU features. You can import library functions of the existing open source community as described below. Alternatively, you can directly read **/proc/cpuinfo** or call **libc getauxval(AT_HWCAP)** to obtain the CPU features of a device.
10
11
12## How to Use
13
141. Import the open source library to your OpenHarmony C++ project. Specifically, download the [cpu_features library](https://github.com/google/cpu_features), and decompress it to the **cpp** directory of the project. The following uses the C++ template in DevEco Studio as an example:
15   - The directory structure after the decompression is as follows:
16      ```
17      MyApplication
18        |-- entry
19         |-- src
20              |-- main
21                   |-- cpp
22                       |--cpu_features
23                       |--CMakeLists.txt
24                       |--hello.cpp
25      ```
26   - Add the path to the cpu_features library to the **CMakeLists.txt** file.
27      ```makefile
28      add_subdirectory(cpu_features) # Add a subdirectory to the project.
29      target_link_libraries(entry PUBLIC CpuFeature::cpu_features) # Add the library files that need to be linked.
30      ```
31
322. Add the statement for determining the support for the CPU features to the code. The following uses the ARM and AArch64 architectures as an example:
33   ```c++
34   ...
35   // Include the header file for CPU architecture target detection.
36   #include "cpu_features_macros.h"
37   // In the ARM architecture, this macro is automatically defined in the preceding header file based on the target.
38   #if defined (CPU_FEATURES_ARCH_ARM)
39   #include "cpuinfo_arm.h" // Include the cpuinfo header file of the ARM architecture.
40   #elif defined (CPU_FEATURES_ARCH_AARCH64)
41   #include "cpuinfo_aarch64.h" // Include the cpuinfo header file of the AArch64 architecture.
42   #endif
43   // Add `using namespace cpu_features;` in the C++ code.
44   // Obtain CPU feature information based on the architecture in use.
45   #if defined (CPU_FEATURES_ARCH_ARM)
46   static const ArmFeatures features = GetArmInfo().features;
47   #elif defined (CPU_FEATURES_ARCH_AARCH64)
48   static const Aarch64Features features = GetAarch64Info().features;
49   #endif
50   void Compute(void) {
51     // Determine whether the CPU features are supported based on the features field.
52     if (features.aes) {
53       // ...
54     } else {
55       // ...
56     }
57   }
58   ```
59
60   > **NOTE**
61   >
62   > The cpu_features library reads the corresponding CPU feature information from the kernel through **/proc/cpuinfo** or **getauxval**. The information may fail to be read due to the use of the application sandbox. Pay attention to error handling.
63