Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: base/cpu.cc

Issue 6526005: detect cpu feature for all x64 and x86 platforms (no longer windows only). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: inline implementation Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/cpu.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2011 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
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_mmx_(false),
25 has_sse_(false),
26 has_sse2_(false),
27 has_sse3_(false),
28 has_ssse3_(false),
29 has_sse4_(false),
18 cpu_vendor_("unknown") { 30 cpu_vendor_("unknown") {
19 Initialize(); 31 Initialize();
20 } 32 }
21 33
34 #if defined(ARCH_CPU_X86_FAMILY)
35 #ifndef _MSC_VER
36
37 #if defined(__pic__) && defined(__i386__)
38
39 void __cpuid(int cpu_info[4], int info_type) {
40 __asm__ volatile (
41 "mov %%ebx, %%edi\n"
42 "cpuid\n"
43 "xchg %%edi, %%ebx\n"
44 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
45 : "a"(info_type)
46 );
47 }
48
49 void __cpuidex(int cpu_info[4], int info_type, int info_index) {
50 __asm__ volatile (
51 "mov %%ebx, %%edi\n"
52 "cpuid\n"
53 "xchg %%edi, %%ebx\n"
54 : "=a"(cpu_info[0]), "=D"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
55 : "a"(info_type), "c"(info_index)
56 );
57 }
58
59 #else
60
61 void __cpuid(int cpu_info[4], int info_type) {
62 __asm__ volatile (
63 "cpuid \n\t"
64 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
65 : "a"(info_type)
66 );
67 }
68
69 void __cpuidex(int cpu_info[4], int info_type, int info_index) {
70 __asm__ volatile (
71 "cpuid \n\t"
72 : "=a"(cpu_info[0]), "=b"(cpu_info[1]), "=c"(cpu_info[2]), "=d"(cpu_info[3])
73 : "a"(info_type), "c"(info_index)
74 );
75 }
76
77 #endif
78 #endif // _MSC_VER
79 #endif // ARCH_CPU_X86_FAMILY
80
22 void CPU::Initialize() { 81 void CPU::Initialize() {
82 #if defined(ARCH_CPU_X86_FAMILY)
23 int cpu_info[4] = {-1}; 83 int cpu_info[4] = {-1};
24 char cpu_string[0x20]; 84 char cpu_string[0x20];
25 85
26 // __cpuid with an InfoType argument of 0 returns the number of 86 // __cpuid with an InfoType argument of 0 returns the number of
27 // valid Ids in CPUInfo[0] and the CPU identification string in 87 // valid Ids in CPUInfo[0] and the CPU identification string in
28 // the other three array elements. The CPU identification string is 88 // the other three array elements. The CPU identification string is
29 // not in linear order. The code below arranges the information 89 // not in linear order. The code below arranges the information
30 // in a human readable form. 90 // in a human readable form.
31 // 91 //
32 // More info can be found here: 92 // More info can be found here:
33 // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx 93 // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
34 __cpuid(cpu_info, 0); 94 __cpuid(cpu_info, 0);
35 int num_ids = cpu_info[0]; 95 int num_ids = cpu_info[0];
36 memset(cpu_string, 0, sizeof(cpu_string)); 96 memset(cpu_string, 0, sizeof(cpu_string));
37 *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1]; 97 *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1];
38 *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3]; 98 *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3];
39 *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2]; 99 *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2];
40 100
41 // Interpret CPU feature information. 101 // Interpret CPU feature information.
42 if (num_ids > 0) { 102 if (num_ids > 0) {
43 __cpuid(cpu_info, 1); 103 __cpuid(cpu_info, 1);
44 stepping_ = cpu_info[0] & 0xf; 104 stepping_ = cpu_info[0] & 0xf;
45 model_ = (cpu_info[0] >> 4) & 0xf; 105 model_ = (cpu_info[0] >> 4) & 0xf;
46 family_ = (cpu_info[0] >> 8) & 0xf; 106 family_ = (cpu_info[0] >> 8) & 0xf;
47 type_ = (cpu_info[0] >> 12) & 0x3; 107 type_ = (cpu_info[0] >> 12) & 0x3;
48 ext_model_ = (cpu_info[0] >> 16) & 0xf; 108 ext_model_ = (cpu_info[0] >> 16) & 0xf;
49 ext_family_ = (cpu_info[0] >> 20) & 0xff; 109 ext_family_ = (cpu_info[0] >> 20) & 0xff;
50 cpu_vendor_ = cpu_string; 110 cpu_vendor_ = cpu_string;
111 has_mmx_ = (cpu_info[3] & 0x00800000) != 0;
112 has_sse_ = (cpu_info[3] & 0x02000000) != 0;
113 has_sse2_ = (cpu_info[3] & 0x04000000) != 0;
114 has_sse3_ = (cpu_info[2] & 0x00000001) != 0;
115 has_ssse3_ = (cpu_info[2] & 0x00000200) != 0;
116 has_sse4_ = (cpu_info[2] & 0x00080000) != 0;
fbarchard 2011/02/16 00:30:15 I think you mean sse41 ? should probably be expli
51 } 117 }
118 #endif
52 } 119 }
53 120
121
122
54 } // namespace base 123 } // namespace base
OLDNEW
« no previous file with comments | « base/cpu.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698