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 #if defined(__x86_64__) || defined(__i386__) | |
Mark Mentovai
2015/10/01 17:54:14
Can you use the build/build_config.h macros for th
fbarchard
2015/10/01 18:42:06
Done.
Caveat The intent here is clangcl support fo
| |
30 // Execute an MMX instruction. | |
31 __asm__ __volatile__("emms\n" : : : "mm0"); | |
32 | |
33 // Execute an SSE instruction. | |
34 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0"); | |
35 | |
36 // Execute an SSE 2 instruction. | |
37 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0"); | |
38 | |
39 if (cpu.has_sse3()) { | |
40 // Execute an SSE 3 instruction. | |
41 __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0"); | |
42 } | |
43 | |
44 if (cpu.has_ssse3()) { | |
45 // Execute a Supplimental SSE 3 instruction. | |
46 __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0"); | |
47 } | |
48 | |
49 if (cpu.has_sse41()) { | |
50 // Execute an SSE 4.1 instruction. | |
51 __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0"); | |
52 } | |
53 | |
54 if (cpu.has_sse42()) { | |
55 // Execute an SSE 4.2 instruction. | |
56 __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax"); | |
57 } | |
58 | |
59 if (cpu.has_avx()) { | |
60 // Execute an AVX instruction. | |
61 __asm__ __volatile__("vzeroupper\n" : : : "xmm0"); | |
Mark Mentovai
2015/10/01 17:54:15
This actually clobbers ymmh0 (but not the low xmm0
fbarchard
2015/10/01 18:42:06
clang/gcc do not let you say clobber ymm0.
| |
62 } | |
63 | |
64 if (cpu.has_avx2()) { | |
65 // Execute an AVX 2 instruction. | |
66 __asm__ __volatile__("vpunpcklbw %%ymm0, %%ymm0, %%ymm0\n" : : : "xmm0"); | |
Mark Mentovai
2015/10/01 17:54:14
Same here, this clobbers all of ymm0.
fbarchard
2015/10/01 18:42:06
Acknowledged.
| |
67 } | |
68 | |
20 // TODO(jschuh): crbug.com/168866 Find a way to enable this on Win64. | 69 // TODO(jschuh): crbug.com/168866 Find a way to enable this on Win64. |
21 #if defined(OS_WIN) && !defined(_M_X64) | 70 #elif defined(_M_IX86) |
Mark Mentovai
2015/10/01 17:54:15
defined(COPMILER_MSVC) && defined(ARCH_CPU_X86)
fbarchard
2015/10/01 18:42:06
Done.
I'd prefer not make it specific to Microsoft
| |
22 ASSERT_TRUE(cpu.has_mmx()); | |
23 | 71 |
24 // Execute an MMX instruction. | 72 // Execute an MMX instruction. |
25 __asm emms; | 73 __asm emms; |
Mark Mentovai
2015/10/01 17:54:15
Are there intrinsics that you can use so that this
fbarchard
2015/10/01 18:42:06
Acknowledged.
Its possible, but not trivial.
purpo
| |
26 | 74 |
27 if (cpu.has_sse()) { | 75 // Execute an SSE instruction. |
28 // Execute an SSE instruction. | 76 __asm xorps xmm0, xmm0; |
29 __asm xorps xmm0, xmm0; | |
30 } | |
31 | 77 |
32 if (cpu.has_sse2()) { | 78 // Execute an SSE 2 instruction. |
33 // Execute an SSE 2 instruction. | 79 __asm psrldq xmm0, 0; |
34 __asm psrldq xmm0, 0; | |
35 } | |
36 | 80 |
37 if (cpu.has_sse3()) { | 81 if (cpu.has_sse3()) { |
38 // Execute an SSE 3 instruction. | 82 // Execute an SSE 3 instruction. |
39 __asm addsubpd xmm0, xmm0; | 83 __asm addsubpd xmm0, xmm0; |
40 } | 84 } |
41 | 85 |
42 if (cpu.has_ssse3()) { | 86 if (cpu.has_ssse3()) { |
43 // Execute a Supplimental SSE 3 instruction. | 87 // Execute a Supplimental SSE 3 instruction. |
44 __asm psignb xmm0, xmm0; | 88 __asm psignb xmm0, xmm0; |
45 } | 89 } |
46 | 90 |
47 if (cpu.has_sse41()) { | 91 if (cpu.has_sse41()) { |
48 // Execute an SSE 4.1 instruction. | 92 // Execute an SSE 4.1 instruction. |
49 __asm pmuldq xmm0, xmm0; | 93 __asm pmuldq xmm0, xmm0; |
50 } | 94 } |
51 | 95 |
52 if (cpu.has_sse42()) { | 96 if (cpu.has_sse42()) { |
53 // Execute an SSE 4.2 instruction. | 97 // Execute an SSE 4.2 instruction. |
54 __asm crc32 eax, eax; | 98 __asm crc32 eax, eax; |
55 } | 99 } |
56 #elif defined(OS_POSIX) && defined(__x86_64__) | |
57 ASSERT_TRUE(cpu.has_mmx()); | |
58 | 100 |
59 // Execute an MMX instruction. | 101 // Visual C 2012 required for AVX. |
60 __asm__ __volatile__("emms\n" : : : "mm0"); | 102 #if _MSC_VER >= 1700 |
61 | 103 if (cpu.has_avx()) { |
62 if (cpu.has_sse()) { | 104 // Execute an AVX instruction. |
63 // Execute an SSE instruction. | 105 __asm vzeroupper; |
64 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0"); | |
65 } | 106 } |
66 | 107 |
67 if (cpu.has_sse2()) { | 108 if (cpu.has_avx2()) { |
68 // Execute an SSE 2 instruction. | 109 // Execute an AVX 2 instruction. |
69 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0"); | 110 __asm vpunpcklbw ymm0, ymm0, ymm0 |
70 } | 111 } |
71 | 112 #endif // _MSC_VER >= 1700 |
72 if (cpu.has_sse3()) { | 113 #endif // defined(__x86_64__) || defined(__i386__) |
73 // Execute an SSE 3 instruction. | 114 #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 } | 115 } |
OLD | NEW |