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"
11 // C4752: found Intel(R) Advanced Vector Extensions; consider using /arch:AVX.
12 #pragma warning(disable: 4752)
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.
25 ASSERT_TRUE(cpu.has_mmx());
26 ASSERT_TRUE(cpu.has_sse());
27 ASSERT_TRUE(cpu.has_sse2());
29 // GCC and clang instruction test.
30 #if defined(COMPILER_GCC)
31 // Execute an MMX instruction.
32 __asm__ __volatile__("emms\n" : : : "mm0");
34 // Execute an SSE instruction.
35 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0");
37 // Execute an SSE 2 instruction.
38 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0");
41 // Execute an SSE 3 instruction.
42 __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0");
45 if (cpu.has_ssse3()) {
46 // Execute a Supplimental SSE 3 instruction.
47 __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0");
50 if (cpu.has_sse41()) {
51 // Execute an SSE 4.1 instruction.
52 __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0");
55 if (cpu.has_sse42()) {
56 // Execute an SSE 4.2 instruction.
57 __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax");
60 if (cpu.has_popcnt()) {
61 // Execute a POPCNT instruction.
62 __asm__ __volatile__("popcnt %%eax, %%eax\n" : : : "eax");
66 // Execute an AVX instruction.
67 __asm__ __volatile__("vzeroupper\n" : : : "xmm0");
71 // Execute an AVX 2 instruction.
72 __asm__ __volatile__("vpunpcklbw %%ymm0, %%ymm0, %%ymm0\n" : : : "xmm0");
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__)))
79 // Execute an MMX instruction.
82 // Execute an SSE instruction.
83 __asm xorps xmm0, xmm0;
85 // Execute an SSE 2 instruction.
89 // Execute an SSE 3 instruction.
90 __asm addsubpd xmm0, xmm0;
93 if (cpu.has_ssse3()) {
94 // Execute a Supplimental SSE 3 instruction.
95 __asm psignb xmm0, xmm0;
98 if (cpu.has_sse41()) {
99 // Execute an SSE 4.1 instruction.
100 __asm pmuldq xmm0, xmm0;
103 if (cpu.has_sse42()) {
104 // Execute an SSE 4.2 instruction.
105 __asm crc32 eax, eax;
108 if (cpu.has_popcnt()) {
109 // Execute a POPCNT instruction.
110 __asm popcnt eax, eax;
113 // Visual C 2012 required for AVX.
116 // Execute an AVX instruction.
120 if (cpu.has_avx2()) {
121 // Execute an AVX 2 instruction.
122 __asm vpunpcklbw ymm0, ymm0, ymm0
124 #endif // _MSC_VER >= 1700
125 #endif // defined(COMPILER_GCC)
126 #endif // defined(ARCH_CPU_X86_FAMILY)
129 // For https://crbug.com/249713
130 TEST(CPU, BrandAndVendorContainsNoNUL) {
132 EXPECT_FALSE(base::ContainsValue(cpu.cpu_brand(), '\0'));
133 EXPECT_FALSE(base::ContainsValue(cpu.vendor_name(), '\0'));