blob: 931509738fe7ace50bcd576b9fd5f738eeb0306e [file] [log] [blame]
jbauman@chromium.orgd07e3bd2012-03-31 16:08:53 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
hbono@chromium.org31407362011-02-24 18:20:16 +09002// 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"
jbauman@chromium.orgd07e3bd2012-03-31 16:08:53 +09006#include "build/build_config.h"
hbono@chromium.org31407362011-02-24 18:20:16 +09007
8#include "testing/gtest/include/gtest/gtest.h"
9
fbarchardddd653b2015-10-02 12:23:19 +090010#if _MSC_VER >= 1700
11// C4752: found Intel(R) Advanced Vector Extensions; consider using /arch:AVX.
12#pragma warning(disable: 4752)
13#endif
14
hbono@chromium.org31407362011-02-24 18:20:16 +090015// 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.
20TEST(CPU, RunExtendedInstructions) {
21#if defined(ARCH_CPU_X86_FAMILY)
22 // Retrieve the CPU information.
23 base::CPU cpu;
24
hbono@chromium.org31407362011-02-24 18:20:16 +090025 ASSERT_TRUE(cpu.has_mmx());
fbarchardddd653b2015-10-02 12:23:19 +090026 ASSERT_TRUE(cpu.has_sse());
27 ASSERT_TRUE(cpu.has_sse2());
hbono@chromium.org31407362011-02-24 18:20:16 +090028
fbarchardddd653b2015-10-02 12:23:19 +090029// TODO(fbarchard): consider enabling for clangcl.
30#if defined(COMPILER_GCC)
hbono@chromium.org31407362011-02-24 18:20:16 +090031 // Execute an MMX instruction.
32 __asm__ __volatile__("emms\n" : : : "mm0");
33
fbarchardddd653b2015-10-02 12:23:19 +090034 // Execute an SSE instruction.
35 __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0");
hbono@chromium.org31407362011-02-24 18:20:16 +090036
fbarchardddd653b2015-10-02 12:23:19 +090037 // Execute an SSE 2 instruction.
38 __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0");
hbono@chromium.org31407362011-02-24 18:20:16 +090039
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 }
fbarchardddd653b2015-10-02 12:23:19 +090059
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
70// TODO(jschuh): crbug.com/168866 Find a way to enable this on Win64.
71#elif defined(COMPILER_MSVC) && defined(ARCH_CPU_32_BITS)
72
73 // Execute an MMX instruction.
74 __asm emms;
75
76 // Execute an SSE instruction.
77 __asm xorps xmm0, xmm0;
78
79 // Execute an SSE 2 instruction.
80 __asm psrldq xmm0, 0;
81
82 if (cpu.has_sse3()) {
83 // Execute an SSE 3 instruction.
84 __asm addsubpd xmm0, xmm0;
85 }
86
87 if (cpu.has_ssse3()) {
88 // Execute a Supplimental SSE 3 instruction.
89 __asm psignb xmm0, xmm0;
90 }
91
92 if (cpu.has_sse41()) {
93 // Execute an SSE 4.1 instruction.
94 __asm pmuldq xmm0, xmm0;
95 }
96
97 if (cpu.has_sse42()) {
98 // Execute an SSE 4.2 instruction.
99 __asm crc32 eax, eax;
100 }
101
102// Visual C 2012 required for AVX.
103#if _MSC_VER >= 1700
104 if (cpu.has_avx()) {
105 // Execute an AVX instruction.
106 __asm vzeroupper;
107 }
108
109 if (cpu.has_avx2()) {
110 // Execute an AVX 2 instruction.
111 __asm vpunpcklbw ymm0, ymm0, ymm0
112 }
113#endif // _MSC_VER >= 1700
114#endif // defined(COMPILER_GCC)
115#endif // defined(ARCH_CPU_X86_FAMILY)
hbono@chromium.org31407362011-02-24 18:20:16 +0900116}