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 "libyuv/basic_types.h"
15 #include "libyuv/cpu_id.h"
16 #include "libyuv/version.h"
17 #include "../unit_test/unit_test.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_mips = TestCpuFlag(kCpuHasMIPS);
49 printf("Has MIPS %x\n", has_mips);
50 int has_dspr2 = TestCpuFlag(kCpuHasDSPR2);
51 printf("Has DSPR2 %x\n", has_dspr2);
52 }
53
TEST_F(LibYUVBaseTest,TestCpuCompilerEnabled)54 TEST_F(LibYUVBaseTest, TestCpuCompilerEnabled) {
55 #if defined(__aarch64__)
56 printf("Arm64 build\n");
57 #endif
58 #if defined(__aarch64__) || defined(__ARM_NEON__) || defined(LIBYUV_NEON)
59 printf("Neon build enabled\n");
60 #endif
61 #if defined(__x86_64__) || defined(_M_X64)
62 printf("x64 build\n");
63 #endif
64 #ifdef _MSC_VER
65 printf("_MSC_VER %d\n", _MSC_VER);
66 #endif
67 #if !defined(LIBYUV_DISABLE_X86) && (defined(GCC_HAS_AVX2) || \
68 defined(CLANG_HAS_AVX2) || defined(VISUALC_HAS_AVX2))
69 printf("Has AVX2 1\n");
70 #else
71 printf("Has AVX2 0\n");
72 // If compiler does not support AVX2, the following function not expected:
73 #endif
74 }
75
76 #if defined(__i386__) || defined(__x86_64__) || \
77 defined(_M_IX86) || defined(_M_X64)
TEST_F(LibYUVBaseTest,TestCpuId)78 TEST_F(LibYUVBaseTest, TestCpuId) {
79 int has_x86 = TestCpuFlag(kCpuHasX86);
80 if (has_x86) {
81 uint32 cpu_info[4];
82 // Vendor ID:
83 // AuthenticAMD AMD processor
84 // CentaurHauls Centaur processor
85 // CyrixInstead Cyrix processor
86 // GenuineIntel Intel processor
87 // GenuineTMx86 Transmeta processor
88 // Geode by NSC National Semiconductor processor
89 // NexGenDriven NexGen processor
90 // RiseRiseRise Rise Technology processor
91 // SiS SiS SiS SiS processor
92 // UMC UMC UMC UMC processor
93 CpuId(0, 0, cpu_info);
94 cpu_info[0] = cpu_info[1]; // Reorder output
95 cpu_info[1] = cpu_info[3];
96 cpu_info[3] = 0;
97 printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
98 cpu_info[0], cpu_info[1], cpu_info[2]);
99 EXPECT_EQ(12, strlen(reinterpret_cast<char*>(&cpu_info[0])));
100
101 // CPU Family and Model
102 // 3:0 - Stepping
103 // 7:4 - Model
104 // 11:8 - Family
105 // 13:12 - Processor Type
106 // 19:16 - Extended Model
107 // 27:20 - Extended Family
108 CpuId(1, 0, cpu_info);
109 int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
110 int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
111 printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family,
112 model, model);
113 }
114 }
115 #endif
116
FileExists(const char * file_name)117 static int FileExists(const char* file_name) {
118 FILE* f = fopen(file_name, "r");
119 if (!f) {
120 return 0;
121 }
122 fclose(f);
123 return 1;
124 }
125
TEST_F(LibYUVBaseTest,TestLinuxNeon)126 TEST_F(LibYUVBaseTest, TestLinuxNeon) {
127 if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
128 EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
129 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
130 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
131 } else {
132 printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
133 }
134 #if defined(__linux__) && defined(__ARM_NEON__)
135 EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
136 #endif
137 }
138
139 } // namespace libyuv
140