[email protected] | d1811bc | 2012-03-31 07:08:53 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/cpu.h" |
Lei Zhang | 49c4b2a | 2017-11-03 21:34:23 | [diff] [blame] | 6 | #include "base/stl_util.h" |
[email protected] | d1811bc | 2012-03-31 07:08:53 | [diff] [blame] | 7 | #include "build/build_config.h" |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 10 | #if _MSC_VER >= 1700 |
| 11 | // C4752: found Intel(R) Advanced Vector Extensions; consider using /arch:AVX. |
| 12 | #pragma warning(disable: 4752) |
| 13 | #endif |
| 14 | |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 15 | // Tests whether we can run extended instructions represented by the CPU |
| 16 | // information. This test actually executes some extended instructions (such as |
| 17 | // MMX, SSE, etc.) supported by the CPU and sees we can run them without |
| 18 | // "undefined instruction" exceptions. That is, this test succeeds when this |
| 19 | // test finishes without a crash. |
| 20 | TEST(CPU, RunExtendedInstructions) { |
| 21 | #if defined(ARCH_CPU_X86_FAMILY) |
| 22 | // Retrieve the CPU information. |
| 23 | base::CPU cpu; |
| 24 | |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 25 | ASSERT_TRUE(cpu.has_mmx()); |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 26 | ASSERT_TRUE(cpu.has_sse()); |
| 27 | ASSERT_TRUE(cpu.has_sse2()); |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 28 | |
fbarchard | 20028e6 | 2015-10-06 17:26:26 | [diff] [blame] | 29 | // GCC and clang instruction test. |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 30 | #if defined(COMPILER_GCC) |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 31 | // Execute an MMX instruction. |
| 32 | __asm__ __volatile__("emms\n" : : : "mm0"); |
| 33 | |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 34 | // Execute an SSE instruction. |
| 35 | __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0"); |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 36 | |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 37 | // Execute an SSE 2 instruction. |
| 38 | __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0"); |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 39 | |
| 40 | if (cpu.has_sse3()) { |
| 41 | // Execute an SSE 3 instruction. |
| 42 | __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0"); |
| 43 | } |
| 44 | |
| 45 | if (cpu.has_ssse3()) { |
| 46 | // Execute a Supplimental SSE 3 instruction. |
| 47 | __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0"); |
| 48 | } |
| 49 | |
| 50 | if (cpu.has_sse41()) { |
| 51 | // Execute an SSE 4.1 instruction. |
| 52 | __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0"); |
| 53 | } |
| 54 | |
| 55 | if (cpu.has_sse42()) { |
| 56 | // Execute an SSE 4.2 instruction. |
| 57 | __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax"); |
| 58 | } |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 59 | |
ilevy | b7d2f408 | 2016-10-30 20:46:57 | [diff] [blame] | 60 | if (cpu.has_popcnt()) { |
| 61 | // Execute a POPCNT instruction. |
| 62 | __asm__ __volatile__("popcnt %%eax, %%eax\n" : : : "eax"); |
| 63 | } |
| 64 | |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 65 | if (cpu.has_avx()) { |
| 66 | // Execute an AVX instruction. |
| 67 | __asm__ __volatile__("vzeroupper\n" : : : "xmm0"); |
| 68 | } |
| 69 | |
| 70 | if (cpu.has_avx2()) { |
| 71 | // Execute an AVX 2 instruction. |
| 72 | __asm__ __volatile__("vpunpcklbw %%ymm0, %%ymm0, %%ymm0\n" : : : "xmm0"); |
| 73 | } |
| 74 | |
fbarchard | 20028e6 | 2015-10-06 17:26:26 | [diff] [blame] | 75 | // Visual C 32 bit and ClangCL 32/64 bit test. |
| 76 | #elif defined(COMPILER_MSVC) && (defined(ARCH_CPU_32_BITS) || \ |
| 77 | (defined(ARCH_CPU_64_BITS) && defined(__clang__))) |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 78 | |
| 79 | // Execute an MMX instruction. |
| 80 | __asm emms; |
| 81 | |
| 82 | // Execute an SSE instruction. |
| 83 | __asm xorps xmm0, xmm0; |
| 84 | |
| 85 | // Execute an SSE 2 instruction. |
| 86 | __asm psrldq xmm0, 0; |
| 87 | |
| 88 | if (cpu.has_sse3()) { |
| 89 | // Execute an SSE 3 instruction. |
| 90 | __asm addsubpd xmm0, xmm0; |
| 91 | } |
| 92 | |
| 93 | if (cpu.has_ssse3()) { |
| 94 | // Execute a Supplimental SSE 3 instruction. |
| 95 | __asm psignb xmm0, xmm0; |
| 96 | } |
| 97 | |
| 98 | if (cpu.has_sse41()) { |
| 99 | // Execute an SSE 4.1 instruction. |
| 100 | __asm pmuldq xmm0, xmm0; |
| 101 | } |
| 102 | |
| 103 | if (cpu.has_sse42()) { |
| 104 | // Execute an SSE 4.2 instruction. |
| 105 | __asm crc32 eax, eax; |
| 106 | } |
| 107 | |
ilevy | b7d2f408 | 2016-10-30 20:46:57 | [diff] [blame] | 108 | if (cpu.has_popcnt()) { |
| 109 | // Execute a POPCNT instruction. |
| 110 | __asm popcnt eax, eax; |
| 111 | } |
| 112 | |
fbarchard | 0ce41ae | 2015-10-02 03:23:19 | [diff] [blame] | 113 | // Visual C 2012 required for AVX. |
| 114 | #if _MSC_VER >= 1700 |
| 115 | if (cpu.has_avx()) { |
| 116 | // Execute an AVX instruction. |
| 117 | __asm vzeroupper; |
| 118 | } |
| 119 | |
| 120 | if (cpu.has_avx2()) { |
| 121 | // Execute an AVX 2 instruction. |
| 122 | __asm vpunpcklbw ymm0, ymm0, ymm0 |
| 123 | } |
| 124 | #endif // _MSC_VER >= 1700 |
| 125 | #endif // defined(COMPILER_GCC) |
| 126 | #endif // defined(ARCH_CPU_X86_FAMILY) |
[email protected] | 14cd2e6 | 2011-02-24 09:20:16 | [diff] [blame] | 127 | } |
Lei Zhang | 49c4b2a | 2017-11-03 21:34:23 | [diff] [blame] | 128 | |
| 129 | // For https://crbug.com/249713 |
| 130 | TEST(CPU, BrandAndVendorContainsNoNUL) { |
| 131 | base::CPU cpu; |
Jan Wilken Dörrie | f61e74c | 2019-06-07 08:20:02 | [diff] [blame] | 132 | EXPECT_FALSE(base::Contains(cpu.cpu_brand(), '\0')); |
| 133 | EXPECT_FALSE(base::Contains(cpu.vendor_name(), '\0')); |
Lei Zhang | 49c4b2a | 2017-11-03 21:34:23 | [diff] [blame] | 134 | } |
Gabriel Marin | 8fdd777 | 2019-08-17 00:28:09 | [diff] [blame^] | 135 | |
| 136 | #if defined(ARCH_CPU_X86_FAMILY) |
| 137 | // Tests that we compute the correct CPU family and model based on the vendor |
| 138 | // and CPUID signature. |
| 139 | TEST(CPU, X86FamilyAndModel) { |
| 140 | int family; |
| 141 | int model; |
| 142 | int ext_family; |
| 143 | int ext_model; |
| 144 | |
| 145 | // Check with an Intel Skylake signature. |
| 146 | std::tie(family, model, ext_family, ext_model) = |
| 147 | base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406e3); |
| 148 | EXPECT_EQ(family, 6); |
| 149 | EXPECT_EQ(model, 78); |
| 150 | EXPECT_EQ(ext_family, 0); |
| 151 | EXPECT_EQ(ext_model, 4); |
| 152 | |
| 153 | // Check with an Intel Airmont signature. |
| 154 | std::tie(family, model, ext_family, ext_model) = |
| 155 | base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406c2); |
| 156 | EXPECT_EQ(family, 6); |
| 157 | EXPECT_EQ(model, 76); |
| 158 | EXPECT_EQ(ext_family, 0); |
| 159 | EXPECT_EQ(ext_model, 4); |
| 160 | |
| 161 | // Check with an Intel Prescott signature. |
| 162 | std::tie(family, model, ext_family, ext_model) = |
| 163 | base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x00000f31); |
| 164 | EXPECT_EQ(family, 15); |
| 165 | EXPECT_EQ(model, 3); |
| 166 | EXPECT_EQ(ext_family, 0); |
| 167 | EXPECT_EQ(ext_model, 0); |
| 168 | |
| 169 | // Check with an AMD Excavator signature. |
| 170 | std::tie(family, model, ext_family, ext_model) = |
| 171 | base::internal::ComputeX86FamilyAndModel("AuthenticAMD", 0x00670f00); |
| 172 | EXPECT_EQ(family, 21); |
| 173 | EXPECT_EQ(model, 112); |
| 174 | EXPECT_EQ(ext_family, 6); |
| 175 | EXPECT_EQ(ext_model, 7); |
| 176 | } |
| 177 | #endif // defined(ARCH_CPU_X86_FAMILY) |