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 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
12 | 12 |
13 #if defined(ARCH_CPU_X86_FAMILY) | 13 #if defined(ARCH_CPU_X86_FAMILY) |
14 #if defined(OS_LINUX) | |
15 #include <stdio.h> | |
16 #endif | |
17 | |
18 #if defined (OS_MACOSX) | |
19 #include <sys/param.h> | |
20 #include <sys/sysctl.h> | |
21 #endif | |
22 | |
14 #if defined(_MSC_VER) | 23 #if defined(_MSC_VER) |
15 #include <intrin.h> | 24 #include <intrin.h> |
16 #endif | 25 #endif |
17 #endif | 26 #endif |
18 | 27 |
19 namespace base { | 28 namespace base { |
20 | 29 |
21 CPU::CPU() | 30 CPU::CPU() |
22 : type_(0), | 31 : type_(0), |
23 family_(0), | 32 family_(0), |
24 model_(0), | 33 model_(0), |
25 stepping_(0), | 34 stepping_(0), |
26 ext_model_(0), | 35 ext_model_(0), |
27 ext_family_(0), | 36 ext_family_(0), |
28 has_mmx_(false), | 37 has_mmx_(false), |
29 has_sse_(false), | 38 has_sse_(false), |
30 has_sse2_(false), | 39 has_sse2_(false), |
31 has_sse3_(false), | 40 has_sse3_(false), |
32 has_ssse3_(false), | 41 has_ssse3_(false), |
33 has_sse41_(false), | 42 has_sse41_(false), |
34 has_sse42_(false), | 43 has_sse42_(false), |
44 has_avx_(false), | |
35 cpu_vendor_("unknown") { | 45 cpu_vendor_("unknown") { |
36 Initialize(); | 46 Initialize(); |
37 } | 47 } |
38 | 48 |
39 #if defined(ARCH_CPU_X86_FAMILY) | 49 #if defined(ARCH_CPU_X86_FAMILY) |
40 #ifndef _MSC_VER | 50 #ifndef _MSC_VER |
41 | 51 |
42 #if defined(__pic__) && defined(__i386__) | 52 #if defined(__pic__) && defined(__i386__) |
43 | 53 |
44 void __cpuid(int cpu_info[4], int info_type) { | 54 void __cpuid(int cpu_info[4], int info_type) { |
(...skipping 29 matching lines...) Expand all Loading... | |
74 void __cpuidex(int cpu_info[4], int info_type, int info_index) { | 84 void __cpuidex(int cpu_info[4], int info_type, int info_index) { |
75 __asm__ volatile ( | 85 __asm__ volatile ( |
76 "cpuid \n\t" | 86 "cpuid \n\t" |
77 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) | 87 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3]) |
78 : "a"(info_type), "c"(info_index) | 88 : "a"(info_type), "c"(info_index) |
79 ); | 89 ); |
80 } | 90 } |
81 | 91 |
82 #endif | 92 #endif |
83 #endif // _MSC_VER | 93 #endif // _MSC_VER |
94 | |
95 #if defined(OS_WIN) | |
96 bool OSHasAVXSupport() { | |
97 OSVERSIONINFO version_info; | |
98 | |
99 if (GetVersionEx(&version_info)) | |
apatrick_chromium
2013/03/06 02:02:28
Youu mean this?
if (!GetVersionEx(&version_info))
| |
100 return false; | |
101 | |
102 // Windows 7 SP1 added support for AVX. | |
103 return version_info.dwMajorVersion >= 6 && version_info.dwMinorVersion > 1; | |
apatrick_chromium
2013/03/06 02:02:28
This won't work for 7.0 and 7.1 if the major versi
| |
104 } | |
105 #elif defined(OS_LINUX) | |
106 bool OSHasAVXSupport() { | |
107 FILE* f = fopen("/proc/cpuinfo", "r"); | |
apatrick_chromium
2013/03/06 02:02:28
I think you can use file_util::ReadFileToString he
| |
108 if (!f) | |
109 return false; | |
110 | |
111 // Scan through /proc/cpuinfo to see if the kernel supports AVX. | |
112 char buffer[4096]; | |
113 while (fgets(buffer, (int)sizeof(buffer), f)) { | |
brettw
2013/03/06 04:53:36
Use C++ casts.
| |
114 if (strncmp(buffer, "flags", sizeof(buffer))) { | |
115 if (strstr(buffer, "avx")) { | |
apatrick_chromium
2013/03/06 02:02:28
Does /proc/cpuinfo reflect whether the OS has AVX
| |
116 fclose(f); | |
117 return true; | |
118 } | |
119 } | |
120 } | |
121 | |
122 fclose(f); | |
123 return false; | |
124 } | |
125 #elif defined(OS_MACOSX) | |
126 bool OSHasAVXSupport() { | |
127 size_t length; | |
128 char* kernelVersion; | |
apatrick_chromium
2013/03/06 02:02:28
nit: not chrome style
| |
129 static int mib[2] = { CTL_KERN, KERN_OSRELEASE }; | |
130 | |
131 if (sysctl(mib, 2, NULL, &length, NULL, 0) < 0) | |
132 return false; | |
133 | |
134 kernel_version = new char[length]; | |
apatrick_chromium
2013/03/06 02:02:28
scoped_ptr<char[]>
| |
135 if (!kernel_version) | |
136 return false; | |
137 | |
138 if (sysctl(mib, 2, kernel_version, &length, NULL, 0) < 0) { | |
139 delete [] kernel_version; | |
apatrick_chromium
2013/03/06 02:02:28
not needed for scoped_ptr
| |
140 return false; | |
141 } | |
142 | |
143 int major, minor, patch_level; | |
144 if (sscanf(kernel_version, "%d.%d.%d", &major, &minor, &patch_level) != 3) { | |
apatrick_chromium
2013/03/06 02:02:28
check base/version.h. It can deal with version num
| |
145 if (sscanf(kernel_version, "%d.%d", &major, &minor) != 2) { | |
146 delete [] kernel_version; | |
apatrick_chromium
2013/03/06 02:02:28
not needed for scoped_ptr
| |
147 return false; | |
148 } | |
149 } | |
150 free(kernelVersion); | |
apatrick_chromium
2013/03/06 02:02:28
kernelVersion is uninitialized
| |
151 | |
152 // AVX support was added in 10.6.8, which has kernel 10.8. | |
153 return major >= 10 && minor >= 8; | |
apatrick_chromium
2013/03/06 02:02:28
This won't work for 11.0 through 11.7.
| |
154 } | |
155 #else | |
156 bool OSHasAVXSupport() { | |
157 return false; | |
brettw
2013/03/06 04:53:36
indent.
| |
158 } | |
159 #endif | |
84 #endif // ARCH_CPU_X86_FAMILY | 160 #endif // ARCH_CPU_X86_FAMILY |
85 | 161 |
86 void CPU::Initialize() { | 162 void CPU::Initialize() { |
87 #if defined(ARCH_CPU_X86_FAMILY) | 163 #if defined(ARCH_CPU_X86_FAMILY) |
88 int cpu_info[4] = {-1}; | 164 int cpu_info[4] = {-1}; |
89 char cpu_string[48]; | 165 char cpu_string[48]; |
90 | 166 |
91 // __cpuid with an InfoType argument of 0 returns the number of | 167 // __cpuid with an InfoType argument of 0 returns the number of |
92 // valid Ids in CPUInfo[0] and the CPU identification string in | 168 // valid Ids in CPUInfo[0] and the CPU identification string in |
93 // the other three array elements. The CPU identification string is | 169 // the other three array elements. The CPU identification string is |
(...skipping 16 matching lines...) Expand all Loading... | |
110 type_ = (cpu_info[0] >> 12) & 0x3; | 186 type_ = (cpu_info[0] >> 12) & 0x3; |
111 ext_model_ = (cpu_info[0] >> 16) & 0xf; | 187 ext_model_ = (cpu_info[0] >> 16) & 0xf; |
112 ext_family_ = (cpu_info[0] >> 20) & 0xff; | 188 ext_family_ = (cpu_info[0] >> 20) & 0xff; |
113 has_mmx_ = (cpu_info[3] & 0x00800000) != 0; | 189 has_mmx_ = (cpu_info[3] & 0x00800000) != 0; |
114 has_sse_ = (cpu_info[3] & 0x02000000) != 0; | 190 has_sse_ = (cpu_info[3] & 0x02000000) != 0; |
115 has_sse2_ = (cpu_info[3] & 0x04000000) != 0; | 191 has_sse2_ = (cpu_info[3] & 0x04000000) != 0; |
116 has_sse3_ = (cpu_info[2] & 0x00000001) != 0; | 192 has_sse3_ = (cpu_info[2] & 0x00000001) != 0; |
117 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0; | 193 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0; |
118 has_sse41_ = (cpu_info[2] & 0x00080000) != 0; | 194 has_sse41_ = (cpu_info[2] & 0x00080000) != 0; |
119 has_sse42_ = (cpu_info[2] & 0x00100000) != 0; | 195 has_sse42_ = (cpu_info[2] & 0x00100000) != 0; |
120 has_avx_ = (cpu_info[2] & 0x10000000) != 0; | 196 has_avx_ = (cpu_info[2] & 0x10000000) != 0 && OSHasAVXSupport(); |
121 } | 197 } |
122 | 198 |
123 // Get the brand string of the cpu. | 199 // Get the brand string of the cpu. |
124 __cpuid(cpu_info, 0x80000000); | 200 __cpuid(cpu_info, 0x80000000); |
125 const int parameter_end = 0x80000004; | 201 const int parameter_end = 0x80000004; |
126 | 202 |
127 if (cpu_info[0] >= parameter_end) { | 203 if (cpu_info[0] >= parameter_end) { |
128 char* cpu_string_ptr = cpu_string; | 204 char* cpu_string_ptr = cpu_string; |
129 | 205 |
130 for (int parameter = 0x80000002; parameter <= parameter_end && | 206 for (int parameter = 0x80000002; parameter <= parameter_end && |
(...skipping 12 matching lines...) Expand all Loading... | |
143 if (has_sse42()) return SSE42; | 219 if (has_sse42()) return SSE42; |
144 if (has_sse41()) return SSE41; | 220 if (has_sse41()) return SSE41; |
145 if (has_ssse3()) return SSSE3; | 221 if (has_ssse3()) return SSSE3; |
146 if (has_sse3()) return SSE3; | 222 if (has_sse3()) return SSE3; |
147 if (has_sse2()) return SSE2; | 223 if (has_sse2()) return SSE2; |
148 if (has_sse()) return SSE; | 224 if (has_sse()) return SSE; |
149 return PENTIUM; | 225 return PENTIUM; |
150 } | 226 } |
151 | 227 |
152 } // namespace base | 228 } // namespace base |
OLD | NEW |