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

Side by Side Diff: runtime/vm/cpuinfo_win.cc

Issue 120723003: Refactors CPU feature detection. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Fixed cpu service field names. Created 6 years, 11 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
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 #include "vm/globals.h"
6 #if defined(TARGET_OS_WINDOWS)
7
8 #include "vm/cpuinfo.h"
9
10 // __cpuid()
11 #include <intrin.h> // NOLINT
12 #include <string.h> // NOLINT
13
14 #include "platform/assert.h"
15
16 namespace dart {
17
18 struct CpuIdData {
19 static bool sse2;
20 static bool sse41;
21 static char* id_string;
22 static char* brand_string;
23 };
24
25
26 char* CpuInfo::data_ = NULL;
27 intptr_t CpuInfo::datalen_ = 0;
28 bool CpuIdData::sse2 = false;
29 bool CpuIdData::sse41 = false;
30 char* CpuIdData::id_string = NULL;
31 char* CpuIdData::brand_string = NULL;
32
33
34 // The documentation for __cpuid is at:
35 // http://msdn.microsoft.com/en-us/library/hskdteyh(v=vs.90).aspx
36 // InitOnce reads and caches __cpuid results for use by CpuInfo.
37 void CpuInfo::InitOnce() {
38 int32_t info[4] = {-1};
39
40 __cpuid(info, 0);
41 char *id_string = new char[3 * sizeof(int32_t)];
42 // Yes, these are supposed to be out of order.
43 *reinterpret_cast<int32_t*>(id_string) = info[1];
44 *reinterpret_cast<int32_t*>(id_string + 4) = info[3];
45 *reinterpret_cast<int32_t*>(id_string + 8) = info[2];
46 CpuIdData::id_string = id_string;
47
48 __cpuid(info, 1);
49 CpuIdData::sse41 = (info[2] & (1 << 19)) != 0;
50 CpuIdData::sse2 = (info[3] & (1 << 26)) != 0;
51
52 char* brand_string = new char[3 * 4 * sizeof(int32_t)];
53 for (intptr_t i = 0x80000002; i <= 0x80000004; i++) {
54 intptr_t off = (i - 0x80000002) * 4 * sizeof(int32_t);
55 __cpuid(info, i);
56 *reinterpret_cast<int32_t*>(brand_string + off) = info[0];
57 *reinterpret_cast<int32_t*>(brand_string + off + 4) = info[1];
58 *reinterpret_cast<int32_t*>(brand_string + off + 8) = info[2];
59 *reinterpret_cast<int32_t*>(brand_string + off + 12) = info[3];
60 }
61 CpuIdData::brand_string = brand_string;
62 }
63
64
65 bool CpuInfo::FieldContains(const char* field, const char* search_string) {
66 if (strcmp(field, fields_[kCpuInfoProcessor]) == 0) {
67 return strstr(CpuIdData::id_string, search_string);
68 } else if (strcmp(field, fields_[kCpuInfoModel]) == 0) {
69 return strstr(CpuIdData::brand_string, search_string);
70 } else if (strcmp(field, fields_[kCpuInfoFeatures]) == 0) {
71 if (strcmp(search_string, "sse2") == 0) {
72 return CpuIdData::sse2;
73 } else if (strcmp(search_string, "sse4.1") == 0) {
74 return CpuIdData::sse41;
75 } else {
76 return false;
77 }
78 } else {
79 UNIMPLEMENTED();
80 }
81 return false;
82 }
83
84
85 char* CpuInfo::ExtractField(const char* field) {
86 if (strcmp(field, fields_[kCpuInfoProcessor]) == 0) {
87 return CpuIdData::id_string;
88 } else if (strcmp(field, fields_[kCpuInfoModel]) == 0) {
89 return CpuIdData::brand_string;
90 } else {
91 UNIMPLEMENTED();
92 }
93 return NULL;
94 }
95
96
97 bool CpuInfo::HasField(const char* field) {
98 return (strcmp(field, fields_[kCpuInfoProcessor]) == 0) ||
99 (strcmp(field, fields_[kCpuInfoModel]) == 0) ||
100 (strcmp(field, fields_[kCpuInfoFeatures]) == 0);
101 }
102
103
104 const char* CpuInfo::fields_[] = {
105 "Processor",
106 "Hardware",
107 "Features",
108 };
109
110 } // namespace dart
111
112 #endif // defined(TARGET_OS_WINDOWS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698