OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
fbarchard
2011/02/15 01:42:56
(C) 2011
| |
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 | |
7 #if defined(ARCH_CPU_X86_FAMILY) | |
8 #if defined(_MSC_VER) | |
6 #include <intrin.h> | 9 #include <intrin.h> |
7 #include <string> | 10 #endif |
11 #endif | |
12 | |
13 #include <string.h> | |
8 | 14 |
9 namespace base { | 15 namespace base { |
10 | 16 |
11 CPU::CPU() | 17 CPU::CPU() |
12 : type_(0), | 18 : type_(0), |
13 family_(0), | 19 family_(0), |
14 model_(0), | 20 model_(0), |
15 stepping_(0), | 21 stepping_(0), |
16 ext_model_(0), | 22 ext_model_(0), |
17 ext_family_(0), | 23 ext_family_(0), |
24 has_sse2_(false), | |
18 cpu_vendor_("unknown") { | 25 cpu_vendor_("unknown") { |
19 Initialize(); | 26 Initialize(); |
20 } | 27 } |
21 | 28 |
29 #if defined(ARCH_CPU_X86_FAMILY) | |
30 static void cpuid(int info[4], int info_type) { | |
31 #if defined( _MSC_VER) | |
32 __cpuid(info, info_type); | |
33 #else | |
34 asm volatile ( | |
35 "cpuid \n\t" | |
36 "movl %%ebx, %1 \n\t" | |
37 : "=a"(info[0]), "=r"(info[1]), "=c"(info[2]), "=d"(info[3]) | |
38 : "a"(info_type) | |
39 ); | |
40 #endif | |
41 } | |
42 #endif | |
fbarchard
2011/02/15 01:42:56
Your ebx usage seems -m32 -fpic unsafe?
I tried it
| |
43 | |
22 void CPU::Initialize() { | 44 void CPU::Initialize() { |
45 #if defined(ARCH_CPU_X86_FAMILY) | |
23 int cpu_info[4] = {-1}; | 46 int cpu_info[4] = {-1}; |
24 char cpu_string[0x20]; | 47 char cpu_string[0x20]; |
25 | 48 |
26 // __cpuid with an InfoType argument of 0 returns the number of | 49 // __cpuid with an InfoType argument of 0 returns the number of |
27 // valid Ids in CPUInfo[0] and the CPU identification string in | 50 // valid Ids in CPUInfo[0] and the CPU identification string in |
28 // the other three array elements. The CPU identification string is | 51 // the other three array elements. The CPU identification string is |
29 // not in linear order. The code below arranges the information | 52 // not in linear order. The code below arranges the information |
30 // in a human readable form. | 53 // in a human readable form. |
31 // | 54 // |
32 // More info can be found here: | 55 // More info can be found here: |
33 // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx | 56 // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx |
34 __cpuid(cpu_info, 0); | 57 cpuid(cpu_info, 0); |
35 int num_ids = cpu_info[0]; | 58 int num_ids = cpu_info[0]; |
36 memset(cpu_string, 0, sizeof(cpu_string)); | 59 memset(cpu_string, 0, sizeof(cpu_string)); |
37 *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1]; | 60 *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1]; |
38 *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3]; | 61 *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3]; |
39 *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2]; | 62 *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2]; |
40 | 63 |
41 // Interpret CPU feature information. | 64 // Interpret CPU feature information. |
42 if (num_ids > 0) { | 65 if (num_ids > 0) { |
43 __cpuid(cpu_info, 1); | 66 cpuid(cpu_info, 1); |
44 stepping_ = cpu_info[0] & 0xf; | 67 stepping_ = cpu_info[0] & 0xf; |
45 model_ = (cpu_info[0] >> 4) & 0xf; | 68 model_ = (cpu_info[0] >> 4) & 0xf; |
46 family_ = (cpu_info[0] >> 8) & 0xf; | 69 family_ = (cpu_info[0] >> 8) & 0xf; |
47 type_ = (cpu_info[0] >> 12) & 0x3; | 70 type_ = (cpu_info[0] >> 12) & 0x3; |
48 ext_model_ = (cpu_info[0] >> 16) & 0xf; | 71 ext_model_ = (cpu_info[0] >> 16) & 0xf; |
49 ext_family_ = (cpu_info[0] >> 20) & 0xff; | 72 ext_family_ = (cpu_info[0] >> 20) & 0xff; |
50 cpu_vendor_ = cpu_string; | 73 cpu_vendor_ = cpu_string; |
74 has_sse2_ = (cpu_info[3] & (1<<26)) != 0; | |
fbarchard
2011/02/15 01:42:56
add has_ssse3_ while you're at it? Or a todo
| |
51 } | 75 } |
76 #endif | |
52 } | 77 } |
53 | 78 |
79 | |
80 | |
54 } // namespace base | 81 } // namespace base |
OLD | NEW |