1 /*
2 * Copyright 2012 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 #include <stdlib.h>
12 #include <string.h>
13
14 #include "../unit_test/unit_test.h"
15 #include "libyuv/basic_types.h"
16 #include "libyuv/cpu_id.h"
17 #include "libyuv/version.h"
18
19 namespace libyuv {
20
TEST_F(LibYUVBaseTest,TestCpuHas)21 TEST_F(LibYUVBaseTest, TestCpuHas) {
22 int cpu_flags = TestCpuFlag(-1);
23 printf("Cpu Flags 0x%x\n", cpu_flags);
24 #if defined(__arm__) || defined(__aarch64__)
25 int has_arm = TestCpuFlag(kCpuHasARM);
26 printf("Has ARM 0x%x\n", has_arm);
27 int has_neon = TestCpuFlag(kCpuHasNEON);
28 printf("Has NEON 0x%x\n", has_neon);
29 #endif
30 #if defined(__riscv) && defined(__linux__)
31 int has_riscv = TestCpuFlag(kCpuHasRISCV);
32 printf("Has RISCV 0x%x\n", has_riscv);
33 int has_rvv = TestCpuFlag(kCpuHasRVV);
34 printf("Has RVV 0x%x\n", has_rvv);
35 int has_rvvzvfh = TestCpuFlag(kCpuHasRVVZVFH);
36 printf("Has RVVZVFH 0x%x\n", has_rvvzvfh);
37 #endif
38 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
39 defined(_M_X64)
40 int has_x86 = TestCpuFlag(kCpuHasX86);
41 int has_sse2 = TestCpuFlag(kCpuHasSSE2);
42 int has_ssse3 = TestCpuFlag(kCpuHasSSSE3);
43 int has_sse41 = TestCpuFlag(kCpuHasSSE41);
44 int has_sse42 = TestCpuFlag(kCpuHasSSE42);
45 int has_avx = TestCpuFlag(kCpuHasAVX);
46 int has_avx2 = TestCpuFlag(kCpuHasAVX2);
47 int has_erms = TestCpuFlag(kCpuHasERMS);
48 int has_fma3 = TestCpuFlag(kCpuHasFMA3);
49 int has_f16c = TestCpuFlag(kCpuHasF16C);
50 int has_avx512bw = TestCpuFlag(kCpuHasAVX512BW);
51 int has_avx512vl = TestCpuFlag(kCpuHasAVX512VL);
52 int has_avx512vnni = TestCpuFlag(kCpuHasAVX512VNNI);
53 int has_avx512vbmi = TestCpuFlag(kCpuHasAVX512VBMI);
54 int has_avx512vbmi2 = TestCpuFlag(kCpuHasAVX512VBMI2);
55 int has_avx512vbitalg = TestCpuFlag(kCpuHasAVX512VBITALG);
56 int has_avx10 = TestCpuFlag(kCpuHasAVX10);
57 int has_avxvnni = TestCpuFlag(kCpuHasAVXVNNI);
58 int has_avxvnniint8 = TestCpuFlag(kCpuHasAVXVNNIINT8);
59 printf("Has X86 0x%x\n", has_x86);
60 printf("Has SSE2 0x%x\n", has_sse2);
61 printf("Has SSSE3 0x%x\n", has_ssse3);
62 printf("Has SSE41 0x%x\n", has_sse41);
63 printf("Has SSE42 0x%x\n", has_sse42);
64 printf("Has AVX 0x%x\n", has_avx);
65 printf("Has AVX2 0x%x\n", has_avx2);
66 printf("Has ERMS 0x%x\n", has_erms);
67 printf("Has FMA3 0x%x\n", has_fma3);
68 printf("Has F16C 0x%x\n", has_f16c);
69 printf("Has AVX512BW 0x%x\n", has_avx512bw);
70 printf("Has AVX512VL 0x%x\n", has_avx512vl);
71 printf("Has AVX512VNNI 0x%x\n", has_avx512vnni);
72 printf("Has AVX512VBMI 0x%x\n", has_avx512vbmi);
73 printf("Has AVX512VBMI2 0x%x\n", has_avx512vbmi2);
74 printf("Has AVX512VBITALG 0x%x\n", has_avx512vbitalg);
75 printf("Has AVX10 0x%x\n", has_avx10);
76 printf("HAS AVXVNNI 0x%x\n", has_avxvnni);
77 printf("Has AVXVNNIINT8 0x%x\n", has_avxvnniint8);
78 #endif
79 #if defined(__mips__)
80 int has_mips = TestCpuFlag(kCpuHasMIPS);
81 printf("Has MIPS 0x%x\n", has_mips);
82 int has_msa = TestCpuFlag(kCpuHasMSA);
83 printf("Has MSA 0x%x\n", has_msa);
84 #endif
85 #if defined(__loongarch__)
86 int has_loongarch = TestCpuFlag(kCpuHasLOONGARCH);
87 printf("Has LOONGARCH 0x%x\n", has_loongarch);
88 int has_lsx = TestCpuFlag(kCpuHasLSX);
89 printf("Has LSX 0x%x\n", has_lsx);
90 int has_lasx = TestCpuFlag(kCpuHasLASX);
91 printf("Has LASX 0x%x\n", has_lasx);
92 #endif
93 }
94
TEST_F(LibYUVBaseTest,TestCompilerMacros)95 TEST_F(LibYUVBaseTest, TestCompilerMacros) {
96 // Tests all macros used in public headers.
97 #ifdef __ATOMIC_RELAXED
98 printf("__ATOMIC_RELAXED %d\n", __ATOMIC_RELAXED);
99 #endif
100 #ifdef __cplusplus
101 printf("__cplusplus %ld\n", __cplusplus);
102 #endif
103 #ifdef __clang_major__
104 printf("__clang_major__ %d\n", __clang_major__);
105 #endif
106 #ifdef __clang_minor__
107 printf("__clang_minor__ %d\n", __clang_minor__);
108 #endif
109 #ifdef __GNUC__
110 printf("__GNUC__ %d\n", __GNUC__);
111 #endif
112 #ifdef __GNUC_MINOR__
113 printf("__GNUC_MINOR__ %d\n", __GNUC_MINOR__);
114 #endif
115 #ifdef __i386__
116 printf("__i386__ %d\n", __i386__);
117 #endif
118 #ifdef __x86_64__
119 printf("__x86_64__ %d\n", __x86_64__);
120 #endif
121 #ifdef _M_IX86
122 printf("_M_IX86 %d\n", _M_IX86);
123 #endif
124 #ifdef _M_X64
125 printf("_M_X64 %d\n", _M_X64);
126 #endif
127 #ifdef _MSC_VER
128 printf("_MSC_VER %d\n", _MSC_VER);
129 #endif
130 #ifdef __aarch64__
131 printf("__aarch64__ %d\n", __aarch64__);
132 #endif
133 #ifdef __arm__
134 printf("__arm__ %d\n", __arm__);
135 #endif
136 #ifdef __riscv
137 printf("__riscv %d\n", __riscv);
138 #endif
139 #ifdef __riscv_vector
140 printf("__riscv_vector %d\n", __riscv_vector);
141 #endif
142 #ifdef __riscv_v_intrinsic
143 printf("__riscv_v_intrinsic %d\n", __riscv_v_intrinsic);
144 #endif
145 #ifdef __APPLE__
146 printf("__APPLE__ %d\n", __APPLE__);
147 #endif
148 #ifdef __clang__
149 printf("__clang__ %d\n", __clang__);
150 #endif
151 #ifdef __CLR_VER
152 printf("__CLR_VER %d\n", __CLR_VER);
153 #endif
154 #ifdef __CYGWIN__
155 printf("__CYGWIN__ %d\n", __CYGWIN__);
156 #endif
157 #ifdef __llvm__
158 printf("__llvm__ %d\n", __llvm__);
159 #endif
160 #ifdef __mips_msa
161 printf("__mips_msa %d\n", __mips_msa);
162 #endif
163 #ifdef __mips
164 printf("__mips %d\n", __mips);
165 #endif
166 #ifdef __mips_isa_rev
167 printf("__mips_isa_rev %d\n", __mips_isa_rev);
168 #endif
169 #ifdef _MIPS_ARCH_LOONGSON3A
170 printf("_MIPS_ARCH_LOONGSON3A %d\n", _MIPS_ARCH_LOONGSON3A);
171 #endif
172 #ifdef __loongarch__
173 printf("__loongarch__ %d\n", __loongarch__);
174 #endif
175 #ifdef _WIN32
176 printf("_WIN32 %d\n", _WIN32);
177 #endif
178 #ifdef __native_client__
179 printf("__native_client__ %d\n", __native_client__);
180 #endif
181 #ifdef __pic__
182 printf("__pic__ %d\n", __pic__);
183 #endif
184 #ifdef __pnacl__
185 printf("__pnacl__ %d\n", __pnacl__);
186 #endif
187 #ifdef GG_LONGLONG
188 printf("GG_LONGLONG %lld\n", GG_LONGLONG(1));
189 #endif
190 #ifdef INT_TYPES_DEFINED
191 printf("INT_TYPES_DEFINED\n");
192 #endif
193 #ifdef __has_feature
194 printf("__has_feature\n");
195 #if __has_feature(memory_sanitizer)
196 printf("__has_feature(memory_sanitizer) %d\n",
197 __has_feature(memory_sanitizer));
198 #endif
199 #endif
200 }
201
202 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
203 defined(_M_X64)
TEST_F(LibYUVBaseTest,TestCpuId)204 TEST_F(LibYUVBaseTest, TestCpuId) {
205 int has_x86 = TestCpuFlag(kCpuHasX86);
206 if (has_x86) {
207 int cpu_info[4];
208 // Vendor ID:
209 // AuthenticAMD AMD processor
210 // CentaurHauls Centaur processor
211 // CyrixInstead Cyrix processor
212 // GenuineIntel Intel processor
213 // GenuineTMx86 Transmeta processor
214 // Geode by NSC National Semiconductor processor
215 // NexGenDriven NexGen processor
216 // RiseRiseRise Rise Technology processor
217 // SiS SiS SiS SiS processor
218 // UMC UMC UMC UMC processor
219 CpuId(0, 0, cpu_info);
220 cpu_info[0] = cpu_info[1]; // Reorder output
221 cpu_info[1] = cpu_info[3];
222 cpu_info[3] = 0;
223 printf("Cpu Vendor: %s 0x%x 0x%x 0x%x\n",
224 reinterpret_cast<char*>(&cpu_info[0]), cpu_info[0], cpu_info[1],
225 cpu_info[2]);
226 EXPECT_EQ(12u, strlen(reinterpret_cast<char*>(&cpu_info[0])));
227
228 // CPU Family and Model
229 // 3:0 - Stepping
230 // 7:4 - Model
231 // 11:8 - Family
232 // 13:12 - Processor Type
233 // 19:16 - Extended Model
234 // 27:20 - Extended Family
235 CpuId(1, 0, cpu_info);
236 int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
237 int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
238 printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family, model,
239 model);
240 }
241 }
242 #endif
243
FileExists(const char * file_name)244 static int FileExists(const char* file_name) {
245 FILE* f = fopen(file_name, "r");
246 if (!f) {
247 return 0;
248 }
249 fclose(f);
250 return 1;
251 }
252
TEST_F(LibYUVBaseTest,TestLinuxNeon)253 TEST_F(LibYUVBaseTest, TestLinuxNeon) {
254 if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
255 printf("Note: testing to load \"../../unit_test/testdata/arm_v7.txt\"\n");
256
257 EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
258 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
259 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
260 } else {
261 printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
262 }
263 #if defined(__linux__) && defined(__ARM_NEON__)
264 if (FileExists("/proc/cpuinfo")) {
265 if (kCpuHasNEON != ArmCpuCaps("/proc/cpuinfo")) {
266 // This can happen on ARM emulator but /proc/cpuinfo is from host.
267 printf("WARNING: Neon build enabled but CPU does not have NEON\n");
268 }
269 } else {
270 printf("WARNING: unable to load \"/proc/cpuinfo\"\n");
271 }
272 #endif
273 }
274
TEST_F(LibYUVBaseTest,TestLinuxMipsMsa)275 TEST_F(LibYUVBaseTest, TestLinuxMipsMsa) {
276 if (FileExists("../../unit_test/testdata/mips.txt")) {
277 printf("Note: testing to load \"../../unit_test/testdata/mips.txt\"\n");
278
279 EXPECT_EQ(0, MipsCpuCaps("../../unit_test/testdata/mips.txt"));
280 EXPECT_EQ(kCpuHasMSA, MipsCpuCaps("../../unit_test/testdata/mips_msa.txt"));
281 EXPECT_EQ(kCpuHasMSA,
282 MipsCpuCaps("../../unit_test/testdata/mips_loongson2k.txt"));
283 } else {
284 printf("WARNING: unable to load \"../../unit_test/testdata/mips.txt\"\n");
285 }
286 }
287
TEST_F(LibYUVBaseTest,TestLinuxRVV)288 TEST_F(LibYUVBaseTest, TestLinuxRVV) {
289 if (FileExists("../../unit_test/testdata/riscv64.txt")) {
290 printf("Note: testing to load \"../../unit_test/testdata/riscv64.txt\"\n");
291
292 EXPECT_EQ(0, RiscvCpuCaps("../../unit_test/testdata/riscv64.txt"));
293 EXPECT_EQ(kCpuHasRVV,
294 RiscvCpuCaps("../../unit_test/testdata/riscv64_rvv.txt"));
295 EXPECT_EQ(kCpuHasRVV | kCpuHasRVVZVFH,
296 RiscvCpuCaps("../../unit_test/testdata/riscv64_rvv_zvfh.txt"));
297 } else {
298 printf(
299 "WARNING: unable to load "
300 "\"../../unit_test/testdata/riscv64.txt\"\n");
301 }
302 #if defined(__linux__) && defined(__riscv)
303 if (FileExists("/proc/cpuinfo")) {
304 if (!(kCpuHasRVV & RiscvCpuCaps("/proc/cpuinfo"))) {
305 // This can happen on RVV emulator but /proc/cpuinfo is from host.
306 printf("WARNING: RVV build enabled but CPU does not have RVV\n");
307 }
308 } else {
309 printf("WARNING: unable to load \"/proc/cpuinfo\"\n");
310 }
311 #endif
312 }
313
314 // TODO(fbarchard): Fix clangcl test of cpuflags.
315 #ifdef _MSC_VER
TEST_F(LibYUVBaseTest,DISABLED_TestSetCpuFlags)316 TEST_F(LibYUVBaseTest, DISABLED_TestSetCpuFlags) {
317 #else
318 TEST_F(LibYUVBaseTest, TestSetCpuFlags) {
319 #endif
320 // Reset any masked flags that may have been set so auto init is enabled.
321 MaskCpuFlags(0);
322
323 int original_cpu_flags = TestCpuFlag(-1);
324
325 // Test setting different CPU configurations.
326 int cpu_flags = kCpuHasARM | kCpuHasNEON | kCpuInitialized;
327 SetCpuFlags(cpu_flags);
328 EXPECT_EQ(cpu_flags, TestCpuFlag(-1));
329
330 cpu_flags = kCpuHasX86 | kCpuInitialized;
331 SetCpuFlags(cpu_flags);
332 EXPECT_EQ(cpu_flags, TestCpuFlag(-1));
333
334 // Test that setting 0 turns auto-init back on.
335 SetCpuFlags(0);
336 EXPECT_EQ(original_cpu_flags, TestCpuFlag(-1));
337
338 // Restore the CPU flag mask.
339 MaskCpuFlags(benchmark_cpu_info_);
340 }
341
342 } // namespace libyuv
343