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 %x\n", cpu_flags);
24 int has_arm = TestCpuFlag(kCpuHasARM);
25 printf("Has ARM %x\n", has_arm);
26 int has_neon = TestCpuFlag(kCpuHasNEON);
27 printf("Has NEON %x\n", has_neon);
28 int has_x86 = TestCpuFlag(kCpuHasX86);
29 printf("Has X86 %x\n", has_x86);
30 int has_sse2 = TestCpuFlag(kCpuHasSSE2);
31 printf("Has SSE2 %x\n", has_sse2);
32 int has_ssse3 = TestCpuFlag(kCpuHasSSSE3);
33 printf("Has SSSE3 %x\n", has_ssse3);
34 int has_sse41 = TestCpuFlag(kCpuHasSSE41);
35 printf("Has SSE4.1 %x\n", has_sse41);
36 int has_sse42 = TestCpuFlag(kCpuHasSSE42);
37 printf("Has SSE4.2 %x\n", has_sse42);
38 int has_avx = TestCpuFlag(kCpuHasAVX);
39 printf("Has AVX %x\n", has_avx);
40 int has_avx2 = TestCpuFlag(kCpuHasAVX2);
41 printf("Has AVX2 %x\n", has_avx2);
42 int has_erms = TestCpuFlag(kCpuHasERMS);
43 printf("Has ERMS %x\n", has_erms);
44 int has_fma3 = TestCpuFlag(kCpuHasFMA3);
45 printf("Has FMA3 %x\n", has_fma3);
46 int has_avx3 = TestCpuFlag(kCpuHasAVX3);
47 printf("Has AVX3 %x\n", has_avx3);
48 int has_f16c = TestCpuFlag(kCpuHasF16C);
49 printf("Has F16C %x\n", has_f16c);
50 int has_mips = TestCpuFlag(kCpuHasMIPS);
51 printf("Has MIPS %x\n", has_mips);
52 int has_dspr2 = TestCpuFlag(kCpuHasDSPR2);
53 printf("Has DSPR2 %x\n", has_dspr2);
54 int has_msa = TestCpuFlag(kCpuHasMSA);
55 printf("Has MSA %x\n", has_msa);
56 }
57
TEST_F(LibYUVBaseTest,TestCpuCompilerEnabled)58 TEST_F(LibYUVBaseTest, TestCpuCompilerEnabled) {
59 #if defined(__aarch64__)
60 printf("Arm64 build\n");
61 #endif
62 #if defined(__aarch64__) || defined(__ARM_NEON__) || defined(LIBYUV_NEON)
63 printf("Neon build enabled\n");
64 #endif
65 #if defined(__x86_64__) || defined(_M_X64)
66 printf("x64 build\n");
67 #endif
68 #ifdef _MSC_VER
69 printf("_MSC_VER %d\n", _MSC_VER);
70 #endif
71 #if !defined(LIBYUV_DISABLE_X86) && \
72 (defined(GCC_HAS_AVX2) || defined(CLANG_HAS_AVX2) || \
73 defined(VISUALC_HAS_AVX2))
74 printf("Has AVX2 1\n");
75 #else
76 printf("Has AVX2 0\n");
77 // If compiler does not support AVX2, the following function not expected:
78 #endif
79 }
80
81 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
82 defined(_M_X64)
TEST_F(LibYUVBaseTest,TestCpuId)83 TEST_F(LibYUVBaseTest, TestCpuId) {
84 int has_x86 = TestCpuFlag(kCpuHasX86);
85 if (has_x86) {
86 uint32 cpu_info[4];
87 // Vendor ID:
88 // AuthenticAMD AMD processor
89 // CentaurHauls Centaur processor
90 // CyrixInstead Cyrix processor
91 // GenuineIntel Intel processor
92 // GenuineTMx86 Transmeta processor
93 // Geode by NSC National Semiconductor processor
94 // NexGenDriven NexGen processor
95 // RiseRiseRise Rise Technology processor
96 // SiS SiS SiS SiS processor
97 // UMC UMC UMC UMC processor
98 CpuId(0, 0, cpu_info);
99 cpu_info[0] = cpu_info[1]; // Reorder output
100 cpu_info[1] = cpu_info[3];
101 cpu_info[3] = 0;
102 printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
103 cpu_info[0], cpu_info[1], cpu_info[2]);
104 EXPECT_EQ(12u, strlen(reinterpret_cast<char*>(&cpu_info[0])));
105
106 // CPU Family and Model
107 // 3:0 - Stepping
108 // 7:4 - Model
109 // 11:8 - Family
110 // 13:12 - Processor Type
111 // 19:16 - Extended Model
112 // 27:20 - Extended Family
113 CpuId(1, 0, cpu_info);
114 int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
115 int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
116 printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family, model,
117 model);
118 }
119 }
120 #endif
121
FileExists(const char * file_name)122 static int FileExists(const char* file_name) {
123 FILE* f = fopen(file_name, "r");
124 if (!f) {
125 return 0;
126 }
127 fclose(f);
128 return 1;
129 }
130
TEST_F(LibYUVBaseTest,TestLinuxNeon)131 TEST_F(LibYUVBaseTest, TestLinuxNeon) {
132 if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
133 EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
134 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
135 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
136 } else {
137 printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
138 }
139 #if defined(__linux__) && defined(__ARM_NEON__)
140 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
141 #endif
142 }
143
144 } // namespace libyuv
145