[M85 Dev][EFL] Fix crashes at webview launch
[platform/framework/web/chromium-efl.git] / base / cpu_unittest.cc
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.
4
5 #include "base/cpu.h"
6 #include "base/stl_util.h"
7 #include "build/build_config.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 // Tests whether we can run extended instructions represented by the CPU
11 // 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
13 // "undefined instruction" exceptions. That is, this test succeeds when this
14 // test finishes without a crash.
15 TEST(CPU, RunExtendedInstructions) {
16 #if defined(ARCH_CPU_X86_FAMILY)
17   // Retrieve the CPU information.
18   base::CPU cpu;
19
20   ASSERT_TRUE(cpu.has_mmx());
21   ASSERT_TRUE(cpu.has_sse());
22   ASSERT_TRUE(cpu.has_sse2());
23
24 // GCC and clang instruction test.
25 #if defined(COMPILER_GCC)
26   // Execute an MMX instruction.
27   __asm__ __volatile__("emms\n" : : : "mm0");
28
29   // Execute an SSE instruction.
30   __asm__ __volatile__("xorps %%xmm0, %%xmm0\n" : : : "xmm0");
31
32   // Execute an SSE 2 instruction.
33   __asm__ __volatile__("psrldq $0, %%xmm0\n" : : : "xmm0");
34
35   if (cpu.has_sse3()) {
36     // Execute an SSE 3 instruction.
37     __asm__ __volatile__("addsubpd %%xmm0, %%xmm0\n" : : : "xmm0");
38   }
39
40   if (cpu.has_ssse3()) {
41     // Execute a Supplimental SSE 3 instruction.
42     __asm__ __volatile__("psignb %%xmm0, %%xmm0\n" : : : "xmm0");
43   }
44
45   if (cpu.has_sse41()) {
46     // Execute an SSE 4.1 instruction.
47     __asm__ __volatile__("pmuldq %%xmm0, %%xmm0\n" : : : "xmm0");
48   }
49
50   if (cpu.has_sse42()) {
51     // Execute an SSE 4.2 instruction.
52     __asm__ __volatile__("crc32 %%eax, %%eax\n" : : : "eax");
53   }
54
55   if (cpu.has_popcnt()) {
56     // Execute a POPCNT instruction.
57     __asm__ __volatile__("popcnt %%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
70 // Visual C 32 bit and ClangCL 32/64 bit test.
71 #elif defined(COMPILER_MSVC) && (defined(ARCH_CPU_32_BITS) || \
72       (defined(ARCH_CPU_64_BITS) && defined(__clang__)))
73
74   // Execute an MMX instruction.
75   __asm emms;
76
77   // Execute an SSE instruction.
78   __asm xorps xmm0, xmm0;
79
80   // Execute an SSE 2 instruction.
81   __asm psrldq xmm0, 0;
82
83   if (cpu.has_sse3()) {
84     // Execute an SSE 3 instruction.
85     __asm addsubpd xmm0, xmm0;
86   }
87
88   if (cpu.has_ssse3()) {
89     // Execute a Supplimental SSE 3 instruction.
90     __asm psignb xmm0, xmm0;
91   }
92
93   if (cpu.has_sse41()) {
94     // Execute an SSE 4.1 instruction.
95     __asm pmuldq xmm0, xmm0;
96   }
97
98   if (cpu.has_sse42()) {
99     // Execute an SSE 4.2 instruction.
100     __asm crc32 eax, eax;
101   }
102
103   if (cpu.has_popcnt()) {
104     // Execute a POPCNT instruction.
105     __asm popcnt eax, eax;
106   }
107
108   if (cpu.has_avx()) {
109     // Execute an AVX instruction.
110     __asm vzeroupper;
111   }
112
113   if (cpu.has_avx2()) {
114     // Execute an AVX 2 instruction.
115     __asm vpunpcklbw ymm0, ymm0, ymm0
116   }
117 #endif  // defined(COMPILER_GCC)
118 #endif  // defined(ARCH_CPU_X86_FAMILY)
119 }
120
121 // For https://crbug.com/249713
122 TEST(CPU, BrandAndVendorContainsNoNUL) {
123   base::CPU cpu;
124   EXPECT_FALSE(base::Contains(cpu.cpu_brand(), '\0'));
125   EXPECT_FALSE(base::Contains(cpu.vendor_name(), '\0'));
126 }
127
128 #if defined(ARCH_CPU_X86_FAMILY)
129 // Tests that we compute the correct CPU family and model based on the vendor
130 // and CPUID signature.
131 TEST(CPU, X86FamilyAndModel) {
132   int family;
133   int model;
134   int ext_family;
135   int ext_model;
136
137   // Check with an Intel Skylake signature.
138   std::tie(family, model, ext_family, ext_model) =
139       base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406e3);
140   EXPECT_EQ(family, 6);
141   EXPECT_EQ(model, 78);
142   EXPECT_EQ(ext_family, 0);
143   EXPECT_EQ(ext_model, 4);
144
145   // Check with an Intel Airmont signature.
146   std::tie(family, model, ext_family, ext_model) =
147       base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x000406c2);
148   EXPECT_EQ(family, 6);
149   EXPECT_EQ(model, 76);
150   EXPECT_EQ(ext_family, 0);
151   EXPECT_EQ(ext_model, 4);
152
153   // Check with an Intel Prescott signature.
154   std::tie(family, model, ext_family, ext_model) =
155       base::internal::ComputeX86FamilyAndModel("GenuineIntel", 0x00000f31);
156   EXPECT_EQ(family, 15);
157   EXPECT_EQ(model, 3);
158   EXPECT_EQ(ext_family, 0);
159   EXPECT_EQ(ext_model, 0);
160
161   // Check with an AMD Excavator signature.
162   std::tie(family, model, ext_family, ext_model) =
163       base::internal::ComputeX86FamilyAndModel("AuthenticAMD", 0x00670f00);
164   EXPECT_EQ(family, 21);
165   EXPECT_EQ(model, 112);
166   EXPECT_EQ(ext_family, 6);
167   EXPECT_EQ(ext_model, 7);
168 }
169 #endif  // defined(ARCH_CPU_X86_FAMILY)