aboutsummaryrefslogtreecommitdiff
path: root/unit_test/cpu_test.cc
blob: 7264de0801624d03560076650e55445b16acaa16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 *  Copyright 2012 The LibYuv Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS. All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include <stdlib.h>
#include <string.h>

#include "../unit_test/unit_test.h"
#include "libyuv/basic_types.h"
#include "libyuv/cpu_id.h"
#include "libyuv/version.h"

namespace libyuv {

TEST_F(LibYUVBaseTest, TestCpuHas) {
  int cpu_flags = TestCpuFlag(-1);
  printf("Cpu Flags %d\n", cpu_flags);
#if defined(__arm__) || defined(__aarch64__)
  int has_arm = TestCpuFlag(kCpuHasARM);
  printf("Has ARM %d\n", has_arm);
  int has_neon = TestCpuFlag(kCpuHasNEON);
  printf("Has NEON %d\n", has_neon);
#endif
  int has_x86 = TestCpuFlag(kCpuHasX86);
  int has_sse2 = TestCpuFlag(kCpuHasSSE2);
  int has_ssse3 = TestCpuFlag(kCpuHasSSSE3);
  int has_sse41 = TestCpuFlag(kCpuHasSSE41);
  int has_sse42 = TestCpuFlag(kCpuHasSSE42);
  int has_avx = TestCpuFlag(kCpuHasAVX);
  int has_avx2 = TestCpuFlag(kCpuHasAVX2);
  int has_erms = TestCpuFlag(kCpuHasERMS);
  int has_fma3 = TestCpuFlag(kCpuHasFMA3);
  int has_f16c = TestCpuFlag(kCpuHasF16C);
  int has_gfni = TestCpuFlag(kCpuHasGFNI);
  int has_avx512bw = TestCpuFlag(kCpuHasAVX512BW);
  int has_avx512vl = TestCpuFlag(kCpuHasAVX512VL);
  int has_avx512vbmi = TestCpuFlag(kCpuHasAVX512VBMI);
  int has_avx512vbmi2 = TestCpuFlag(kCpuHasAVX512VBMI2);
  int has_avx512vbitalg = TestCpuFlag(kCpuHasAVX512VBITALG);
  int has_avx512vpopcntdq = TestCpuFlag(kCpuHasAVX512VPOPCNTDQ);
  printf("Has X86 %d\n", has_x86);
  printf("Has SSE2 %d\n", has_sse2);
  printf("Has SSSE3 %d\n", has_ssse3);
  printf("Has SSE41 %d\n", has_sse41);
  printf("Has SSE42 %d\n", has_sse42);
  printf("Has AVX %d\n", has_avx);
  printf("Has AVX2 %d\n", has_avx2);
  printf("Has ERMS %d\n", has_erms);
  printf("Has FMA3 %d\n", has_fma3);
  printf("Has F16C %d\n", has_f16c);
  printf("Has GFNI %d\n", has_gfni);
  printf("Has AVX512BW %d\n", has_avx512bw);
  printf("Has AVX512VL %d\n", has_avx512vl);
  printf("Has AVX512VBMI %d\n", has_avx512vbmi);
  printf("Has AVX512VBMI2 %d\n", has_avx512vbmi2);
  printf("Has AVX512VBITALG %d\n", has_avx512vbitalg);
  printf("Has AVX512VPOPCNTDQ %d\n", has_avx512vpopcntdq);

#if defined(__mips__)
  int has_mips = TestCpuFlag(kCpuHasMIPS);
  printf("Has MIPS %d\n", has_mips);
  int has_msa = TestCpuFlag(kCpuHasMSA);
  printf("Has MSA %d\n", has_msa);
  int has_mmi = TestCpuFlag(kCpuHasMMI);
  printf("Has MMI %d\n", has_mmi);
#endif
}

TEST_F(LibYUVBaseTest, TestCpuCompilerEnabled) {
#if defined(__aarch64__)
  printf("Arm64 build\n");
#endif
#if defined(__aarch64__) || defined(__ARM_NEON__) || defined(LIBYUV_NEON)
  printf("Neon build enabled\n");
#endif
#if defined(__x86_64__) || defined(_M_X64)
  printf("x64 build\n");
#endif
#ifdef _MSC_VER
  printf("_MSC_VER %d\n", _MSC_VER);
#endif
#if !defined(LIBYUV_DISABLE_X86) &&                      \
    (defined(GCC_HAS_AVX2) || defined(CLANG_HAS_AVX2) || \
     defined(VISUALC_HAS_AVX2))
  printf("Has AVX2 1\n");
#else
  printf("Has AVX2 0\n");
// If compiler does not support AVX2, the following function not expected:
#endif
}

#if defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || \
    defined(_M_X64)
TEST_F(LibYUVBaseTest, TestCpuId) {
  int has_x86 = TestCpuFlag(kCpuHasX86);
  if (has_x86) {
    int cpu_info[4];
    // Vendor ID:
    // AuthenticAMD AMD processor
    // CentaurHauls Centaur processor
    // CyrixInstead Cyrix processor
    // GenuineIntel Intel processor
    // GenuineTMx86 Transmeta processor
    // Geode by NSC National Semiconductor processor
    // NexGenDriven NexGen processor
    // RiseRiseRise Rise Technology processor
    // SiS SiS SiS  SiS processor
    // UMC UMC UMC  UMC processor
    CpuId(0, 0, cpu_info);
    cpu_info[0] = cpu_info[1];  // Reorder output
    cpu_info[1] = cpu_info[3];
    cpu_info[3] = 0;
    printf("Cpu Vendor: %s %x %x %x\n", reinterpret_cast<char*>(&cpu_info[0]),
           cpu_info[0], cpu_info[1], cpu_info[2]);
    EXPECT_EQ(12u, strlen(reinterpret_cast<char*>(&cpu_info[0])));

    // CPU Family and Model
    // 3:0 - Stepping
    // 7:4 - Model
    // 11:8 - Family
    // 13:12 - Processor Type
    // 19:16 - Extended Model
    // 27:20 - Extended Family
    CpuId(1, 0, cpu_info);
    int family = ((cpu_info[0] >> 8) & 0x0f) | ((cpu_info[0] >> 16) & 0xff0);
    int model = ((cpu_info[0] >> 4) & 0x0f) | ((cpu_info[0] >> 12) & 0xf0);
    printf("Cpu Family %d (0x%x), Model %d (0x%x)\n", family, family, model,
           model);
  }
}
#endif

static int FileExists(const char* file_name) {
  FILE* f = fopen(file_name, "r");
  if (!f) {
    return 0;
  }
  fclose(f);
  return 1;
}

TEST_F(LibYUVBaseTest, TestLinuxNeon) {
  if (FileExists("../../unit_test/testdata/arm_v7.txt")) {
    printf("Note: testing to load \"../../unit_test/testdata/arm_v7.txt\"\n");

    EXPECT_EQ(0, ArmCpuCaps("../../unit_test/testdata/arm_v7.txt"));
    EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/tegra3.txt"));
    EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("../../unit_test/testdata/juno.txt"));
  } else {
    printf("WARNING: unable to load \"../../unit_test/testdata/arm_v7.txt\"\n");
  }
#if defined(__linux__) && defined(__ARM_NEON__)
  EXPECT_EQ(kCpuHasNEON, ArmCpuCaps("/proc/cpuinfo"));
#endif
}

TEST_F(LibYUVBaseTest, TestLinuxMipsMsaMmi) {
  if (FileExists("../../unit_test/testdata/mips.txt")) {
    printf("Note: testing to load \"../../unit_test/testdata/mips.txt\"\n");

    EXPECT_EQ(0, MipsCpuCaps("../../unit_test/testdata/mips.txt"));
    EXPECT_EQ(kCpuHasMMI,
              MipsCpuCaps("../../unit_test/testdata/mips_loongson3.txt"));
    EXPECT_EQ(kCpuHasMMI,
              MipsCpuCaps("../../unit_test/testdata/mips_loongson_mmi.txt"));
    EXPECT_EQ(kCpuHasMSA, MipsCpuCaps("../../unit_test/testdata/mips_msa.txt"));
    EXPECT_EQ(kCpuHasMMI | kCpuHasMSA,
              MipsCpuCaps("../../unit_test/testdata/mips_loongson2k.txt"));
  } else {
    printf("WARNING: unable to load \"../../unit_test/testdata/mips.txt\"\n");
  }
}

// TODO(fbarchard): Fix clangcl test of cpuflags.
#ifdef _MSC_VER
TEST_F(LibYUVBaseTest, DISABLED_TestSetCpuFlags) {
#else
TEST_F(LibYUVBaseTest, TestSetCpuFlags) {
#endif
  // Reset any masked flags that may have been set so auto init is enabled.
  MaskCpuFlags(0);

  int original_cpu_flags = TestCpuFlag(-1);

  // Test setting different CPU configurations.
  int cpu_flags = kCpuHasARM | kCpuHasNEON | kCpuInitialized;
  SetCpuFlags(cpu_flags);
  EXPECT_EQ(cpu_flags, TestCpuFlag(-1));

  cpu_flags = kCpuHasX86 | kCpuInitialized;
  SetCpuFlags(cpu_flags);
  EXPECT_EQ(cpu_flags, TestCpuFlag(-1));

  // Test that setting 0 turns auto-init back on.
  SetCpuFlags(0);
  EXPECT_EQ(original_cpu_flags, TestCpuFlag(-1));

  // Restore the CPU flag mask.
  MaskCpuFlags(benchmark_cpu_info_);
}

}  // namespace libyuv