OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/cpu.h" | 5 #include "base/cpu.h" |
6 #include "build/build_config.h" | 6 #include "build/build_config.h" |
7 | 7 |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
| 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 |
10 // Tests whether we can run extended instructions represented by the CPU | 15 // Tests whether we can run extended instructions represented by the CPU |
11 // information. This test actually executes some extended instructions (such as | 16 // 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 | 17 // 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 | 18 // "undefined instruction" exceptions. That is, this test succeeds when this |
14 // test finishes without a crash. | 19 // test finishes without a crash. |
15 TEST(CPU, RunExtendedInstructions) { | 20 TEST(CPU, RunExtendedInstructions) { |
16 #if defined(ARCH_CPU_X86_FAMILY) | 21 #if defined(ARCH_CPU_X86_FAMILY) |
17 // Retrieve the CPU information. | 22 // Retrieve the CPU information. |
18 base::CPU cpu; | 23 base::CPU cpu; |
19 | 24 |
| 25 ASSERT_TRUE(cpu.has_mmx()); |
| 26 ASSERT_TRUE(cpu.has_sse()); |
| 27 ASSERT_TRUE(cpu.has_sse2()); |
| 28 |
| 29 // TODO(fbarchard): consider enabling for clangcl. |
| 30 #if defined(COMPILER_GCC) |
| 31 // Execute an MMX instruction. |
| 32 __asm__ __volatile__("emms\n" : : : "mm0"); |
| 33 |
| 34 // Execute an SSE instruction. |
| 35 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0"); |
| 36 |
| 37 // Execute an SSE 2 instruction. |
| 38 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0"); |
| 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 } |
| 59 |
| 60 if (cpu.has_avx()) { |
| 61 // Execute an AVX instruction. |
| 62 __asm__ __volatile__("vzeroupper\n" : : : "xmm0"); |
| 63 } |
| 64 |
| 65 if (cpu.has_avx2()) { |
| 66 // Execute an AVX 2 instruction. |
| 67 __asm__ __volatile__("vpunpcklbw %%ymm0, %%ymm0, %%ymm0\n" : : : "xmm0"); |
| 68 } |
| 69 |
20 // TODO(jschuh): crbug.com/168866 Find a way to enable this on Win64. | 70 // TODO(jschuh): crbug.com/168866 Find a way to enable this on Win64. |
21 #if defined(OS_WIN) && !defined(_M_X64) | 71 #elif defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS) |
22 ASSERT_TRUE(cpu.has_mmx()); | |
23 | 72 |
24 // Execute an MMX instruction. | 73 // Execute an MMX instruction. |
25 __asm emms; | 74 __asm emms; |
26 | 75 |
27 if (cpu.has_sse()) { | 76 // Execute an SSE instruction. |
28 // Execute an SSE instruction. | 77 __asm xorps xmm0, xmm0; |
29 __asm xorps xmm0, xmm0; | |
30 } | |
31 | 78 |
32 if (cpu.has_sse2()) { | 79 // Execute an SSE 2 instruction. |
33 // Execute an SSE 2 instruction. | 80 __asm psrldq xmm0, 0; |
34 __asm psrldq xmm0, 0; | |
35 } | |
36 | 81 |
37 if (cpu.has_sse3()) { | 82 if (cpu.has_sse3()) { |
38 // Execute an SSE 3 instruction. | 83 // Execute an SSE 3 instruction. |
39 __asm addsubpd xmm0, xmm0; | 84 __asm addsubpd xmm0, xmm0; |
40 } | 85 } |
41 | 86 |
42 if (cpu.has_ssse3()) { | 87 if (cpu.has_ssse3()) { |
43 // Execute a Supplimental SSE 3 instruction. | 88 // Execute a Supplimental SSE 3 instruction. |
44 __asm psignb xmm0, xmm0; | 89 __asm psignb xmm0, xmm0; |
45 } | 90 } |
46 | 91 |
47 if (cpu.has_sse41()) { | 92 if (cpu.has_sse41()) { |
48 // Execute an SSE 4.1 instruction. | 93 // Execute an SSE 4.1 instruction. |
49 __asm pmuldq xmm0, xmm0; | 94 __asm pmuldq xmm0, xmm0; |
50 } | 95 } |
51 | 96 |
52 if (cpu.has_sse42()) { | 97 if (cpu.has_sse42()) { |
53 // Execute an SSE 4.2 instruction. | 98 // Execute an SSE 4.2 instruction. |
54 __asm crc32 eax, eax; | 99 __asm crc32 eax, eax; |
55 } | 100 } |
56 #elif defined(OS_POSIX) && defined(__x86_64__) | |
57 ASSERT_TRUE(cpu.has_mmx()); | |
58 | 101 |
59 // Execute an MMX instruction. | 102 // Visual C 2012 required for AVX. |
60 __asm__ __volatile__("emms\n" : : : "mm0"); | 103 #if _MSC_VER >= 1700 |
61 | 104 if (cpu.has_avx()) { |
62 if (cpu.has_sse()) { | 105 // Execute an AVX instruction. |
63 // Execute an SSE instruction. | 106 __asm vzeroupper; |
64 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0"); | |
65 } | 107 } |
66 | 108 |
67 if (cpu.has_sse2()) { | 109 if (cpu.has_avx2()) { |
68 // Execute an SSE 2 instruction. | 110 // Execute an AVX 2 instruction. |
69 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0"); | 111 __asm vpunpcklbw ymm0, ymm0, ymm0 |
70 } | 112 } |
71 | 113 #endif // _MSC_VER >= 1700 |
72 if (cpu.has_sse3()) { | 114 #endif // defined(COMPILER_GCC) |
73 // Execute an SSE 3 instruction. | 115 #endif // defined(ARCH_CPU_X86_FAMILY) |
74 __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0"); | |
75 } | |
76 | |
77 if (cpu.has_ssse3()) { | |
78 // Execute a Supplimental SSE 3 instruction. | |
79 __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0"); | |
80 } | |
81 | |
82 if (cpu.has_sse41()) { | |
83 // Execute an SSE 4.1 instruction. | |
84 __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0"); | |
85 } | |
86 | |
87 if (cpu.has_sse42()) { | |
88 // Execute an SSE 4.2 instruction. | |
89 __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax"); | |
90 } | |
91 #endif | |
92 #endif | |
93 } | 116 } |
OLD | NEW |