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