1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/stl_util.h"
7 #include "build/build_config.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 // Tests whether we can run extended instructions represented by the CPU
11 // information. This test actually executes some extended instructions (such as
12 // MMX, SSE, etc.) supported by the CPU and sees we can run them without
13 // "undefined instruction" exceptions. That is, this test succeeds when this
14 // test finishes without a crash.
15 TEST(CPU, RunExtendedInstructions) {
16 #if defined(ARCH_CPU_X86_FAMILY)
17 // Retrieve the CPU information.
20 ASSERT_TRUE(cpu.has_mmx());
21 ASSERT_TRUE(cpu.has_sse());
22 ASSERT_TRUE(cpu.has_sse2());
24 // GCC and clang instruction test.
25 #if defined(COMPILER_GCC)
26 // Execute an MMX instruction.
27 __asm__ __volatile__("emms\n" : : : "mm0");
29 // Execute an SSE instruction.
30 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0");
32 // Execute an SSE 2 instruction.
33 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0");
36 // Execute an SSE 3 instruction.
37 __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0");
40 if (cpu.has_ssse3()) {
41 // Execute a Supplimental SSE 3 instruction.
42 __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0");
45 if (cpu.has_sse41()) {
46 // Execute an SSE 4.1 instruction.
47 __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0");
50 if (cpu.has_sse42()) {
51 // Execute an SSE 4.2 instruction.
52 __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax");
55 if (cpu.has_popcnt()) {
56 // Execute a POPCNT instruction.
57 __asm__ __volatile__("popcnt %%eax, %%eax\n" : : : "eax");
61 // Execute an AVX instruction.
62 __asm__ __volatile__("vzeroupper\n" : : : "xmm0");
66 // Execute an AVX 2 instruction.
67 __asm__ __volatile__("vpunpcklbw %%ymm0, %%ymm0, %%ymm0\n" : : : "xmm0");
70 // Visual C 32 bit and ClangCL 32/64 bit test.
71 #elif defined(COMPILER_MSVC) && (defined(ARCH_CPU_32_BITS) || \
72 (defined(ARCH_CPU_64_BITS) && defined(__clang__)))
74 // Execute an MMX instruction.
77 // Execute an SSE instruction.
78 __asm xorps xmm0, xmm0;
80 // Execute an SSE 2 instruction.
84 // Execute an SSE 3 instruction.
85 __asm addsubpd xmm0, xmm0;
88 if (cpu.has_ssse3()) {
89 // Execute a Supplimental SSE 3 instruction.
90 __asm psignb xmm0, xmm0;
93 if (cpu.has_sse41()) {
94 // Execute an SSE 4.1 instruction.
95 __asm pmuldq xmm0, xmm0;
98 if (cpu.has_sse42()) {
99 // Execute an SSE 4.2 instruction.
100 __asm crc32 eax, eax;
103 if (cpu.has_popcnt()) {
104 // Execute a POPCNT instruction.
105 __asm popcnt eax, eax;
109 // Execute an AVX instruction.
113 if (cpu.has_avx2()) {
114 // Execute an AVX 2 instruction.
115 __asm vpunpcklbw ymm0, ymm0, ymm0
117 #endif // defined(COMPILER_GCC)
118 #endif // defined(ARCH_CPU_X86_FAMILY)
121 // For https://crbug.com/249713
122 TEST(CPU, BrandAndVendorContainsNoNUL) {
124 EXPECT_FALSE(base::Contains(cpu.cpu_brand(), '\0'));
125 EXPECT_FALSE(base::Contains(cpu.vendor_name(), '\0'));
128 #if defined(ARCH_CPU_X86_FAMILY)
129 // Tests that we compute the correct CPU family and model based on the vendor
130 // and CPUID signature.
131 TEST(CPU, X86FamilyAndModel) {
137 // Check with an Intel Skylake signature.
138 std::tie(family, model, ext_family, ext_model) =
139 base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406e3);
140 EXPECT_EQ(family, 6);
141 EXPECT_EQ(model, 78);
142 EXPECT_EQ(ext_family, 0);
143 EXPECT_EQ(ext_model, 4);
145 // Check with an Intel Airmont signature.
146 std::tie(family, model, ext_family, ext_model) =
147 base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406c2);
148 EXPECT_EQ(family, 6);
149 EXPECT_EQ(model, 76);
150 EXPECT_EQ(ext_family, 0);
151 EXPECT_EQ(ext_model, 4);
153 // Check with an Intel Prescott signature.
154 std::tie(family, model, ext_family, ext_model) =
155 base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x00000f31);
156 EXPECT_EQ(family, 15);
158 EXPECT_EQ(ext_family, 0);
159 EXPECT_EQ(ext_model, 0);
161 // Check with an AMD Excavator signature.
162 std::tie(family, model, ext_family, ext_model) =
163 base::internal::ComputeX86FamilyAndModel("AuthenticAMD", 0x00670f00);
164 EXPECT_EQ(family, 21);
165 EXPECT_EQ(model, 112);
166 EXPECT_EQ(ext_family, 6);
167 EXPECT_EQ(ext_model, 7);
169 #endif // defined(ARCH_CPU_X86_FAMILY)