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 #if defined(__arm__) || defined(__aarch64__)
25   int has_arm = TestCpuFlag(kCpuHasARM);
26   printf("Has ARM %x\n", has_arm);
27   int has_neon = TestCpuFlag(kCpuHasNEON);
28   printf("Has NEON %x\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 %x\n", has_x86);
48   printf("Has SSE2 %x\n", has_sse2);
49   printf("Has SSSE3 %x\n", has_ssse3);
50   printf("Has SSE4.1 %x\n", has_sse41);
51   printf("Has SSE4.2 %x\n", has_sse42);
52   printf("Has AVX %x\n", has_avx);
53   printf("Has AVX2 %x\n", has_avx2);
54   printf("Has ERMS %x\n", has_erms);
55   printf("Has FMA3 %x\n", has_fma3);
56   printf("Has F16C %x\n", has_f16c);
57   printf("Has GFNI %x\n", has_gfni);
58   printf("Has AVX512BW %x\n", has_avx512bw);
59   printf("Has AVX512VL %x\n", has_avx512vl);
60   printf("Has AVX512VBMI %x\n", has_avx512vbmi);
61   printf("Has AVX512VBMI2 %x\n", has_avx512vbmi2);
62   printf("Has AVX512VBITALG %x\n", has_avx512vbitalg);
63   printf("Has AVX512VPOPCNTDQ %x\n", has_avx512vpopcntdq);
64 
65 #if defined(__mips__)
66   int has_mips = TestCpuFlag(kCpuHasMIPS);
67   printf("Has MIPS %x\n", has_mips);
68   int has_msa = TestCpuFlag(kCpuHasMSA);
69   printf("Has MSA %x\n", has_msa);
70 #endif
71 }
72 
TEST_F(LibYUVBaseTest,TestCpuCompilerEnabled)73 TEST_F(LibYUVBaseTest, TestCpuCompilerEnabled) {
74 #if defined(__aarch64__)
75   printf("Arm64 build\n");
76 #endif
77 #if defined(__aarch64__) || defined(__ARM_NEON__) || defined(LIBYUV_NEON)
78   printf("Neon build enabled\n");
79 #endif
80 #if defined(__x86_64__) || defined(_M_X64)
81   printf("x64 build\n");
82 #endif
83 #ifdef _MSC_VER
84   printf("_MSC_VER %d\n", _MSC_VER);
85 #endif
86 #if !defined(LIBYUV_DISABLE_X86) &&                      \
87     (defined(GCC_HAS_AVX2) || defined(CLANG_HAS_AVX2) || \
88      defined(VISUALC_HAS_AVX2))
89   printf("Has AVX2 1\n");
90 #else
91   printf("Has AVX2 0\n");
92 // If compiler does not support AVX2, the following function not expected:
93 #endif
94 }
95 
96 #if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
97     defined(_M_X64)
TEST_F(LibYUVBaseTest,TestCpuId)98 TEST_F(LibYUVBaseTest, TestCpuId) {
99   int has_x86 = TestCpuFlag(kCpuHasX86);
100   if (has_x86) {
101     int cpu_info[4];
102     // Vendor ID:
103     // AuthenticAMD AMD processor
104     // CentaurHauls Centaur processor
105     // CyrixInstead Cyrix processor
106     // GenuineIntel Intel processor
107     // GenuineTMx86 Transmeta processor
108     // Geode by NSC National Semiconductor processor
109     // NexGenDriven NexGen processor
110     // RiseRiseRise Rise Technology processor
111     // SiS SiS SiS  SiS processor
112     // UMC UMC UMC  UMC processor
113     CpuId(0, 0, cpu_info);
114     cpu_info[0] = cpu_info[1];  // Reorder output
115     cpu_info[1] = cpu_info[3];
116     cpu_info[3] = 0;
117     printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
118            cpu_info[0], cpu_info[1], cpu_info[2]);
119     EXPECT_EQ(12u, strlen(reinterpret_cast<char*>(&cpu_info[0])));
120 
121     // CPU Family and Model
122     // 3:0 - Stepping
123     // 7:4 - Model
124     // 11:8 - Family
125     // 13:12 - Processor Type
126     // 19:16 - Extended Model
127     // 27:20 - Extended Family
128     CpuId(1, 0, cpu_info);
129     int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
130     int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
131     printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family, model,
132            model);
133   }
134 }
135 #endif
136 
FileExists(const char * file_name)137 static int FileExists(const char* file_name) {
138   FILE* f = fopen(file_name, "r");
139   if (!f) {
140     return 0;
141   }
142   fclose(f);
143   return 1;
144 }
145 
TEST_F(LibYUVBaseTest,TestLinuxNeon)146 TEST_F(LibYUVBaseTest, TestLinuxNeon) {
147   if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
148     printf("Note: testing to load \"../../unit_test/testdata/arm_v7.txt\"\n");
149 
150     EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
151     EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
152     EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
153   } else {
154     printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
155   }
156 #if defined(__linux__) && defined(__ARM_NEON__)
157   EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
158 #endif
159 }
160 
161 }  // namespace libyuv
162