1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "instruction_set_features_x86.h"
#include <fstream>
#include <sstream>
#include "arch/x86_64/instruction_set_features_x86_64.h"
#include "base/stringprintf.h"
#include "utils.h" // For Trim.
namespace art {
const X86InstructionSetFeatures* X86InstructionSetFeatures::FromVariant(
const std::string& variant ATTRIBUTE_UNUSED, std::string* error_msg ATTRIBUTE_UNUSED,
bool x86_64) {
bool known_variant = false;
bool smp = true; // Conservative default.
static const char* x86_variants_with_ssse3[] = {
"atom"
};
bool has_SSSE3 = FindVariantInArray(x86_variants_with_ssse3, arraysize(x86_variants_with_ssse3),
variant);
bool has_SSE4_1 = false;
bool has_SSE4_2 = false;
bool has_AVX = false;
bool has_AVX2 = false;
if (!known_variant && variant != "default") {
std::ostringstream os;
LOG(WARNING) << "Unexpected CPU variant for X86 using defaults: " << variant;
}
if (x86_64) {
return new X86_64InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
has_AVX2);
} else {
return new X86InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
has_AVX2);
}
}
const X86InstructionSetFeatures* X86InstructionSetFeatures::FromBitmap(uint32_t bitmap,
bool x86_64) {
bool smp = (bitmap & kSmpBitfield) != 0;
bool has_SSSE3 = (bitmap & kSsse3Bitfield) != 0;
bool has_SSE4_1 = (bitmap & kSse4_1Bitfield) != 0;
bool has_SSE4_2 = (bitmap & kSse4_2Bitfield) != 0;
bool has_AVX = (bitmap & kAvxBitfield) != 0;
bool has_AVX2 = (bitmap & kAvxBitfield) != 0;
if (x86_64) {
return new X86_64InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2);
} else {
return new X86InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
has_AVX2);
}
}
const X86InstructionSetFeatures* X86InstructionSetFeatures::FromCppDefines(bool x86_64) {
const bool smp = true;
#ifndef __SSSE3__
const bool has_SSSE3 = false;
#else
const bool has_SSSE3 = true;
#endif
#ifndef __SSE4_1__
const bool has_SSE4_1 = false;
#else
const bool has_SSE4_1 = true;
#endif
#ifndef __SSE4_2__
const bool has_SSE4_2 = false;
#else
const bool has_SSE4_2 = true;
#endif
#ifndef __AVX__
const bool has_AVX = false;
#else
const bool has_AVX = true;
#endif
#ifndef __AVX2__
const bool has_AVX2 = false;
#else
const bool has_AVX2 = true;
#endif
if (x86_64) {
return new X86_64InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2);
} else {
return new X86InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
has_AVX2);
}
}
const X86InstructionSetFeatures* X86InstructionSetFeatures::FromCpuInfo(bool x86_64) {
// Look in /proc/cpuinfo for features we need. Only use this when we can guarantee that
// the kernel puts the appropriate feature flags in here. Sometimes it doesn't.
bool smp = false;
bool has_SSSE3 = false;
bool has_SSE4_1 = false;
bool has_SSE4_2 = false;
bool has_AVX = false;
bool has_AVX2 = false;
std::ifstream in("/proc/cpuinfo");
if (!in.fail()) {
while (!in.eof()) {
std::string line;
std::getline(in, line);
if (!in.eof()) {
LOG(INFO) << "cpuinfo line: " << line;
if (line.find("flags") != std::string::npos) {
LOG(INFO) << "found flags";
if (line.find("ssse3") != std::string::npos) {
has_SSSE3 = true;
}
if (line.find("sse4_1") != std::string::npos) {
has_SSE4_1 = true;
}
if (line.find("sse4_2") != std::string::npos) {
has_SSE4_2 = true;
}
if (line.find("avx") != std::string::npos) {
has_AVX = true;
}
if (line.find("avx2") != std::string::npos) {
has_AVX2 = true;
}
} else if (line.find("processor") != std::string::npos &&
line.find(": 1") != std::string::npos) {
smp = true;
}
}
}
in.close();
} else {
LOG(ERROR) << "Failed to open /proc/cpuinfo";
}
if (x86_64) {
return new X86_64InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2);
} else {
return new X86InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
has_AVX2);
}
}
const X86InstructionSetFeatures* X86InstructionSetFeatures::FromHwcap(bool x86_64) {
UNIMPLEMENTED(WARNING);
return FromCppDefines(x86_64);
}
const X86InstructionSetFeatures* X86InstructionSetFeatures::FromAssembly(bool x86_64) {
UNIMPLEMENTED(WARNING);
return FromCppDefines(x86_64);
}
bool X86InstructionSetFeatures::Equals(const InstructionSetFeatures* other) const {
if (GetInstructionSet() != other->GetInstructionSet()) {
return false;
}
const X86InstructionSetFeatures* other_as_x86 = other->AsX86InstructionSetFeatures();
return (IsSmp() == other->IsSmp()) &&
(has_SSSE3_ == other_as_x86->has_SSSE3_) &&
(has_SSE4_1_ == other_as_x86->has_SSE4_1_) &&
(has_SSE4_2_ == other_as_x86->has_SSE4_2_) &&
(has_AVX_ == other_as_x86->has_AVX_) &&
(has_AVX2_ == other_as_x86->has_AVX2_);
}
uint32_t X86InstructionSetFeatures::AsBitmap() const {
return (IsSmp() ? kSmpBitfield : 0) |
(has_SSSE3_ ? kSsse3Bitfield : 0) |
(has_SSE4_1_ ? kSse4_1Bitfield : 0) |
(has_SSE4_2_ ? kSse4_2Bitfield : 0) |
(has_AVX_ ? kAvxBitfield : 0) |
(has_AVX2_ ? kAvx2Bitfield : 0);
}
std::string X86InstructionSetFeatures::GetFeatureString() const {
std::string result;
if (IsSmp()) {
result += "smp";
} else {
result += "-smp";
}
if (has_SSSE3_) {
result += ",ssse3";
} else {
result += ",-ssse3";
}
if (has_SSE4_1_) {
result += ",sse4.1";
} else {
result += ",-sse4.1";
}
if (has_SSE4_2_) {
result += ",sse4.2";
} else {
result += ",-sse4.2";
}
if (has_AVX_) {
result += ",avx";
} else {
result += ",-avx";
}
if (has_AVX2_) {
result += ",avx2";
} else {
result += ",-avx2";
}
return result;
}
const InstructionSetFeatures* X86InstructionSetFeatures::AddFeaturesFromSplitString(
const bool smp, const std::vector<std::string>& features, bool x86_64,
std::string* error_msg) const {
bool has_SSSE3 = has_SSSE3_;
bool has_SSE4_1 = has_SSE4_1_;
bool has_SSE4_2 = has_SSE4_2_;
bool has_AVX = has_AVX_;
bool has_AVX2 = has_AVX2_;
for (auto i = features.begin(); i != features.end(); i++) {
std::string feature = Trim(*i);
if (feature == "ssse3") {
has_SSSE3 = true;
} else if (feature == "-ssse3") {
has_SSSE3 = false;
} else if (feature == "sse4.1") {
has_SSE4_1 = true;
} else if (feature == "-sse4.1") {
has_SSE4_1 = false;
} else if (feature == "sse4.2") {
has_SSE4_2 = true;
} else if (feature == "-sse4.2") {
has_SSE4_2 = false;
} else if (feature == "avx") {
has_AVX = true;
} else if (feature == "-avx") {
has_AVX = false;
} else if (feature == "avx2") {
has_AVX2 = true;
} else if (feature == "-avx2") {
has_AVX2 = false;
} else {
*error_msg = StringPrintf("Unknown instruction set feature: '%s'", feature.c_str());
return nullptr;
}
}
if (x86_64) {
return new X86_64InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
has_AVX2);
} else {
return new X86InstructionSetFeatures(smp, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX,
has_AVX2);
}
}
} // namespace art
|