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

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: use cpuid.h 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
« base/cpu.h ('K') | « 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-2008 The Chromium Authors. All rights reserved.
fbarchard 2011/02/15 21:30:11 (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 #else
11 #include <cpuid.h>
12 #endif
13 #endif
14
15 #include <string.h>
8 16
9 namespace base { 17 namespace base {
10 18
11 CPU::CPU() 19 CPU::CPU()
12 : type_(0), 20 : type_(0),
13 family_(0), 21 family_(0),
14 model_(0), 22 model_(0),
15 stepping_(0), 23 stepping_(0),
16 ext_model_(0), 24 ext_model_(0),
17 ext_family_(0), 25 ext_family_(0),
26 has_sse2_(false),
18 cpu_vendor_("unknown") { 27 cpu_vendor_("unknown") {
19 Initialize(); 28 Initialize();
20 } 29 }
21 30
31 #if defined(ARCH_CPU_X86_FAMILY)
32 static void cpuid(int info[4], int info_type) {
33 #if defined( _MSC_VER)
34 __cpuid(info, info_type);
35 #else
36 __cpuid(info_type, info[0], info[1], info[2], info[3]);
fbarchard 2011/02/15 21:30:11 perfect :)
37 #endif
38 }
39 #endif
40
22 void CPU::Initialize() { 41 void CPU::Initialize() {
42 #if defined(ARCH_CPU_X86_FAMILY)
23 int cpu_info[4] = {-1}; 43 int cpu_info[4] = {-1};
24 char cpu_string[0x20]; 44 char cpu_string[0x20];
25 45
26 // __cpuid with an InfoType argument of 0 returns the number of 46 // __cpuid with an InfoType argument of 0 returns the number of
27 // valid Ids in CPUInfo[0] and the CPU identification string in 47 // valid Ids in CPUInfo[0] and the CPU identification string in
28 // the other three array elements. The CPU identification string is 48 // the other three array elements. The CPU identification string is
29 // not in linear order. The code below arranges the information 49 // not in linear order. The code below arranges the information
30 // in a human readable form. 50 // in a human readable form.
31 // 51 //
32 // More info can be found here: 52 // More info can be found here:
33 // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx 53 // http://msdn.microsoft.com/en-us/library/hskdteyh.aspx
34 __cpuid(cpu_info, 0); 54 cpuid(cpu_info, 0);
35 int num_ids = cpu_info[0]; 55 int num_ids = cpu_info[0];
36 memset(cpu_string, 0, sizeof(cpu_string)); 56 memset(cpu_string, 0, sizeof(cpu_string));
37 *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1]; 57 *(reinterpret_cast<int*>(cpu_string)) = cpu_info[1];
38 *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3]; 58 *(reinterpret_cast<int*>(cpu_string+4)) = cpu_info[3];
39 *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2]; 59 *(reinterpret_cast<int*>(cpu_string+8)) = cpu_info[2];
40 60
41 // Interpret CPU feature information. 61 // Interpret CPU feature information.
42 if (num_ids > 0) { 62 if (num_ids > 0) {
43 __cpuid(cpu_info, 1); 63 cpuid(cpu_info, 1);
44 stepping_ = cpu_info[0] & 0xf; 64 stepping_ = cpu_info[0] & 0xf;
45 model_ = (cpu_info[0] >> 4) & 0xf; 65 model_ = (cpu_info[0] >> 4) & 0xf;
46 family_ = (cpu_info[0] >> 8) & 0xf; 66 family_ = (cpu_info[0] >> 8) & 0xf;
47 type_ = (cpu_info[0] >> 12) & 0x3; 67 type_ = (cpu_info[0] >> 12) & 0x3;
48 ext_model_ = (cpu_info[0] >> 16) & 0xf; 68 ext_model_ = (cpu_info[0] >> 16) & 0xf;
49 ext_family_ = (cpu_info[0] >> 20) & 0xff; 69 ext_family_ = (cpu_info[0] >> 20) & 0xff;
50 cpu_vendor_ = cpu_string; 70 cpu_vendor_ = cpu_string;
71 has_sse2_ = (cpu_info[3] & (1<<26)) != 0;
51 } 72 }
73 #endif
52 } 74 }
53 75
76
77
54 } // namespace base 78 } // namespace base
OLDNEW
« base/cpu.h ('K') | « base/cpu.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698