1e41f4b71Sopenharmony_ci# CPU Features
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciCPU 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.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci
6e41f4b71Sopenharmony_ciDuring native library development in your OpenHarmony project, you can follow this topic to use code related to the CPU features in your application.
7e41f4b71Sopenharmony_ci
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciCurrently, 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.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci## How to Use
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci1. 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:
15e41f4b71Sopenharmony_ci   - The directory structure after the decompression is as follows:
16e41f4b71Sopenharmony_ci      ```
17e41f4b71Sopenharmony_ci      MyApplication
18e41f4b71Sopenharmony_ci        |-- entry
19e41f4b71Sopenharmony_ci         |-- src
20e41f4b71Sopenharmony_ci              |-- main
21e41f4b71Sopenharmony_ci                   |-- cpp
22e41f4b71Sopenharmony_ci                       |--cpu_features
23e41f4b71Sopenharmony_ci                       |--CMakeLists.txt
24e41f4b71Sopenharmony_ci                       |--hello.cpp
25e41f4b71Sopenharmony_ci      ```
26e41f4b71Sopenharmony_ci   - Add the path to the cpu_features library to the **CMakeLists.txt** file.
27e41f4b71Sopenharmony_ci      ```makefile
28e41f4b71Sopenharmony_ci      add_subdirectory(cpu_features) # Add a subdirectory to the project.
29e41f4b71Sopenharmony_ci      target_link_libraries(entry PUBLIC CpuFeature::cpu_features) # Add the library files that need to be linked.
30e41f4b71Sopenharmony_ci      ```
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci2. 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:
33e41f4b71Sopenharmony_ci   ```c++
34e41f4b71Sopenharmony_ci   ...
35e41f4b71Sopenharmony_ci   // Include the header file for CPU architecture target detection.
36e41f4b71Sopenharmony_ci   #include "cpu_features_macros.h"
37e41f4b71Sopenharmony_ci   // In the ARM architecture, this macro is automatically defined in the preceding header file based on the target.
38e41f4b71Sopenharmony_ci   #if defined (CPU_FEATURES_ARCH_ARM)
39e41f4b71Sopenharmony_ci   #include "cpuinfo_arm.h" // Include the cpuinfo header file of the ARM architecture.
40e41f4b71Sopenharmony_ci   #elif defined (CPU_FEATURES_ARCH_AARCH64)
41e41f4b71Sopenharmony_ci   #include "cpuinfo_aarch64.h" // Include the cpuinfo header file of the AArch64 architecture.
42e41f4b71Sopenharmony_ci   #endif
43e41f4b71Sopenharmony_ci   // Add `using namespace cpu_features;` in the C++ code.
44e41f4b71Sopenharmony_ci   // Obtain CPU feature information based on the architecture in use.
45e41f4b71Sopenharmony_ci   #if defined (CPU_FEATURES_ARCH_ARM)
46e41f4b71Sopenharmony_ci   static const ArmFeatures features = GetArmInfo().features;
47e41f4b71Sopenharmony_ci   #elif defined (CPU_FEATURES_ARCH_AARCH64)
48e41f4b71Sopenharmony_ci   static const Aarch64Features features = GetAarch64Info().features;
49e41f4b71Sopenharmony_ci   #endif
50e41f4b71Sopenharmony_ci   void Compute(void) {
51e41f4b71Sopenharmony_ci     // Determine whether the CPU features are supported based on the features field.
52e41f4b71Sopenharmony_ci     if (features.aes) {
53e41f4b71Sopenharmony_ci       // ...
54e41f4b71Sopenharmony_ci     } else {
55e41f4b71Sopenharmony_ci       // ...
56e41f4b71Sopenharmony_ci     }
57e41f4b71Sopenharmony_ci   }
58e41f4b71Sopenharmony_ci   ```
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci   > **NOTE**
61e41f4b71Sopenharmony_ci   > 
62e41f4b71Sopenharmony_ci   > 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