1 /*
2 * Copyright 2011 The LibYuv Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #ifndef INCLUDE_LIBYUV_CPU_ID_H_
12 #define INCLUDE_LIBYUV_CPU_ID_H_
13
14 #include "libyuv/basic_types.h"
15
16 #ifdef __cplusplus
17 namespace libyuv {
18 extern "C" {
19 #endif
20
21 // Internal flag to indicate cpuid requires initialization.
22 static const int kCpuInitialized = 0x1;
23
24 // These flags are only valid on ARM processors.
25 static const int kCpuHasARM = 0x2;
26 static const int kCpuHasNEON = 0x4;
27 // 0x8 reserved for future ARM flag.
28
29 // These flags are only valid on x86 processors.
30 static const int kCpuHasX86 = 0x10;
31 static const int kCpuHasSSE2 = 0x20;
32 static const int kCpuHasSSSE3 = 0x40;
33 static const int kCpuHasSSE41 = 0x80;
34 static const int kCpuHasSSE42 = 0x100;
35 static const int kCpuHasAVX = 0x200;
36 static const int kCpuHasAVX2 = 0x400;
37 static const int kCpuHasERMS = 0x800;
38 static const int kCpuHasFMA3 = 0x1000;
39 static const int kCpuHasF16C = 0x2000;
40 static const int kCpuHasAVX512BW = 0x4000;
41 static const int kCpuHasAVX512VL = 0x8000;
42 static const int kCpuHasAVX512VNNI = 0x10000;
43 static const int kCpuHasAVX512VBMI = 0x20000;
44 static const int kCpuHasAVX512VBMI2 = 0x40000;
45 static const int kCpuHasAVX512VBITALG = 0x80000;
46 static const int kCpuHasAVX10 = 0x100000;
47 static const int kCpuHasAVXVNNI = 0x200000;
48 static const int kCpuHasAVXVNNIINT8 = 0x400000;
49
50 // These flags are only valid on MIPS processors.
51 static const int kCpuHasMIPS = 0x800000;
52 static const int kCpuHasMSA = 0x1000000;
53
54 // These flags are only valid on LOONGARCH processors.
55 static const int kCpuHasLOONGARCH = 0x2000000;
56 static const int kCpuHasLSX = 0x4000000;
57 static const int kCpuHasLASX = 0x8000000;
58
59 // These flags are only valid on RISCV processors.
60 static const int kCpuHasRISCV = 0x10000000;
61 static const int kCpuHasRVV = 0x20000000;
62 static const int kCpuHasRVVZVFH = 0x40000000;
63
64 // Optional init function. TestCpuFlag does an auto-init.
65 // Returns cpu_info flags.
66 LIBYUV_API
67 int InitCpuFlags(void);
68
69 // Detect CPU has SSE2 etc.
70 // Test_flag parameter should be one of kCpuHas constants above.
71 // Returns non-zero if instruction set is detected
TestCpuFlag(int test_flag)72 static __inline int TestCpuFlag(int test_flag) {
73 LIBYUV_API extern int cpu_info_;
74 #ifdef __ATOMIC_RELAXED
75 int cpu_info = __atomic_load_n(&cpu_info_, __ATOMIC_RELAXED);
76 #else
77 int cpu_info = cpu_info_;
78 #endif
79 return (!cpu_info ? InitCpuFlags() : cpu_info) & test_flag;
80 }
81
82 // Internal function for parsing /proc/cpuinfo.
83 LIBYUV_API
84 int ArmCpuCaps(const char* cpuinfo_name);
85 LIBYUV_API
86 int MipsCpuCaps(const char* cpuinfo_name);
87 LIBYUV_API
88 int RiscvCpuCaps(const char* cpuinfo_name);
89
90 // For testing, allow CPU flags to be disabled.
91 // ie MaskCpuFlags(~kCpuHasSSSE3) to disable SSSE3.
92 // MaskCpuFlags(-1) to enable all cpu specific optimizations.
93 // MaskCpuFlags(1) to disable all cpu specific optimizations.
94 // MaskCpuFlags(0) to reset state so next call will auto init.
95 // Returns cpu_info flags.
96 LIBYUV_API
97 int MaskCpuFlags(int enable_flags);
98
99 // Sets the CPU flags to |cpu_flags|, bypassing the detection code. |cpu_flags|
100 // should be a valid combination of the kCpuHas constants above and include
101 // kCpuInitialized. Use this method when running in a sandboxed process where
102 // the detection code might fail (as it might access /proc/cpuinfo). In such
103 // cases the cpu_info can be obtained from a non sandboxed process by calling
104 // InitCpuFlags() and passed to the sandboxed process (via command line
105 // parameters, IPC...) which can then call this method to initialize the CPU
106 // flags.
107 // Notes:
108 // - when specifying 0 for |cpu_flags|, the auto initialization is enabled
109 // again.
110 // - enabling CPU features that are not supported by the CPU will result in
111 // undefined behavior.
112 // TODO(fbarchard): consider writing a helper function that translates from
113 // other library CPU info to libyuv CPU info and add a .md doc that explains
114 // CPU detection.
SetCpuFlags(int cpu_flags)115 static __inline void SetCpuFlags(int cpu_flags) {
116 LIBYUV_API extern int cpu_info_;
117 #ifdef __ATOMIC_RELAXED
118 __atomic_store_n(&cpu_info_, cpu_flags, __ATOMIC_RELAXED);
119 #else
120 cpu_info_ = cpu_flags;
121 #endif
122 }
123
124 // Low level cpuid for X86. Returns zeros on other CPUs.
125 // eax is the info type that you want.
126 // ecx is typically the cpu number, and should normally be zero.
127 LIBYUV_API
128 void CpuId(int info_eax, int info_ecx, int* cpu_info);
129
130 #ifdef __cplusplus
131 } // extern "C"
132 } // namespace libyuv
133 #endif
134
135 #endif // INCLUDE_LIBYUV_CPU_ID_H_
136