• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,TestCpuCompilerEnabled)75 TEST_F(LibYUVBaseTest, TestCpuCompilerEnabled) {
76 #if defined(__aarch64__)
77   printf("Arm64 build\n");
78 #endif
79 #if defined(__aarch64__) || defined(__ARM_NEON__) || defined(LIBYUV_NEON)
80   printf("Neon build enabled\n");
81 #endif
82 #if defined(__x86_64__) || defined(_M_X64)
83   printf("x64 build\n");
84 #endif
85 #ifdef _MSC_VER
86   printf("_MSC_VER %d\n", _MSC_VER);
87 #endif
88 #if !defined(LIBYUV_DISABLE_X86) &&                      \
89     (defined(GCC_HAS_AVX2) || defined(CLANG_HAS_AVX2) || \
90      defined(VISUALC_HAS_AVX2))
91   printf("Has AVX2 1\n");
92 #else
93   printf("Has AVX2 0\n");
94 // If compiler does not support AVX2, the following function not expected:
95 #endif
96 }
97 
98 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
99     defined(_M_X64)
TEST_F(LibYUVBaseTest,TestCpuId)100 TEST_F(LibYUVBaseTest, TestCpuId) {
101   int has_x86 = TestCpuFlag(kCpuHasX86);
102   if (has_x86) {
103     int cpu_info[4];
104     // Vendor ID:
105     // AuthenticAMD AMD processor
106     // CentaurHauls Centaur processor
107     // CyrixInstead Cyrix processor
108     // GenuineIntel Intel processor
109     // GenuineTMx86 Transmeta processor
110     // Geode by NSC National Semiconductor processor
111     // NexGenDriven NexGen processor
112     // RiseRiseRise Rise Technology processor
113     // SiS SiS SiS  SiS processor
114     // UMC UMC UMC  UMC processor
115     CpuId(0, 0, cpu_info);
116     cpu_info[0] = cpu_info[1];  // Reorder output
117     cpu_info[1] = cpu_info[3];
118     cpu_info[3] = 0;
119     printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
120            cpu_info[0], cpu_info[1], cpu_info[2]);
121     EXPECT_EQ(12u, strlen(reinterpret_cast<char*>(&cpu_info[0])));
122 
123     // CPU Family and Model
124     // 3:0 - Stepping
125     // 7:4 - Model
126     // 11:8 - Family
127     // 13:12 - Processor Type
128     // 19:16 - Extended Model
129     // 27:20 - Extended Family
130     CpuId(1, 0, cpu_info);
131     int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
132     int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
133     printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family, model,
134            model);
135   }
136 }
137 #endif
138 
FileExists(const char * file_name)139 static int FileExists(const char* file_name) {
140   FILE* f = fopen(file_name, "r");
141   if (!f) {
142     return 0;
143   }
144   fclose(f);
145   return 1;
146 }
147 
TEST_F(LibYUVBaseTest,TestLinuxNeon)148 TEST_F(LibYUVBaseTest, TestLinuxNeon) {
149   if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
150     printf("Note: testing to load \"../../unit_test/testdata/arm_v7.txt\"\n");
151 
152     EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
153     EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
154     EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
155   } else {
156     printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
157   }
158 #if defined(__linux__) && defined(__ARM_NEON__)
159   EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
160 #endif
161 }
162 
TEST_F(LibYUVBaseTest,TestSetCpuFlags)163 TEST_F(LibYUVBaseTest, TestSetCpuFlags) {
164   // Reset any masked flags that may have been set so auto init is enabled.
165   MaskCpuFlags(0);
166 
167   int original_cpu_flags = TestCpuFlag(-1);
168 
169   // Test setting different CPU configurations.
170   int cpu_flags = kCpuHasARM | kCpuHasNEON | kCpuInitialized;
171   SetCpuFlags(cpu_flags);
172   EXPECT_EQ(cpu_flags, TestCpuFlag(-1));
173 
174   cpu_flags = kCpuHasX86 | kCpuInitialized;
175   SetCpuFlags(cpu_flags);
176   EXPECT_EQ(cpu_flags, TestCpuFlag(-1));
177 
178   // Test that setting 0 turns auto-init back on.
179   SetCpuFlags(0);
180   EXPECT_EQ(original_cpu_flags, TestCpuFlag(-1));
181 
182   // Restore the CPU flag mask.
183   MaskCpuFlags(benchmark_cpu_info_);
184 }
185 
186 }  // namespace libyuv
187