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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/*
* 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 <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include "arch/x86_64/instruction_set_features_x86_64.h"
#include <cpu_features_macros.h>
#ifdef CPU_FEATURES_ARCH_X86
// This header can only be included on x86 targets,
// as determined by cpu_features own define.
#include <cpuinfo_x86.h>
#endif
namespace art {
using android::base::StringPrintf;
// Feature-support arrays.
static constexpr const char* x86_known_variants[] = {
"atom",
"sandybridge",
"silvermont",
"kabylake",
};
static constexpr const char* x86_variants_with_ssse3[] = {
"atom",
"sandybridge",
"silvermont",
"kabylake",
};
static constexpr const char* x86_variants_with_sse4_1[] = {
"sandybridge",
"silvermont",
"kabylake",
};
static constexpr const char* x86_variants_with_sse4_2[] = {
"sandybridge",
"silvermont",
"kabylake",
};
static constexpr const char* x86_variants_with_popcnt[] = {
"sandybridge",
"silvermont",
"kabylake",
};
static constexpr const char* x86_variants_with_avx[] = {
"kabylake",
};
static constexpr const char* x86_variants_with_avx2[] = {
"kabylake",
};
X86FeaturesUniquePtr X86InstructionSetFeatures::Create(bool x86_64,
bool has_SSSE3,
bool has_SSE4_1,
bool has_SSE4_2,
bool has_AVX,
bool has_AVX2,
bool has_POPCNT) {
if (x86_64) {
return X86FeaturesUniquePtr(new X86_64InstructionSetFeatures(has_SSSE3,
has_SSE4_1,
has_SSE4_2,
has_AVX,
has_AVX2,
has_POPCNT));
} else {
return X86FeaturesUniquePtr(new X86InstructionSetFeatures(has_SSSE3,
has_SSE4_1,
has_SSE4_2,
has_AVX,
has_AVX2,
has_POPCNT));
}
}
X86FeaturesUniquePtr X86InstructionSetFeatures::FromVariant(
const std::string& variant, std::string* error_msg ATTRIBUTE_UNUSED,
bool x86_64) {
const bool is_runtime_isa =
kRuntimeISA == (x86_64 ? InstructionSet::kX86_64 : InstructionSet::kX86);
if (is_runtime_isa && variant == "default") {
return FromCppDefines(x86_64);
}
bool has_SSSE3 = FindVariantInArray(x86_variants_with_ssse3, arraysize(x86_variants_with_ssse3),
variant);
bool has_SSE4_1 = FindVariantInArray(x86_variants_with_sse4_1,
arraysize(x86_variants_with_sse4_1),
variant);
bool has_SSE4_2 = FindVariantInArray(x86_variants_with_sse4_2,
arraysize(x86_variants_with_sse4_2),
variant);
bool has_AVX = FindVariantInArray(x86_variants_with_avx,
arraysize(x86_variants_with_avx),
variant);
bool has_AVX2 = FindVariantInArray(x86_variants_with_avx2,
arraysize(x86_variants_with_avx2),
variant);
bool has_POPCNT = FindVariantInArray(x86_variants_with_popcnt,
arraysize(x86_variants_with_popcnt),
variant);
// Verify that variant is known.
bool known_variant = FindVariantInArray(x86_known_variants, arraysize(x86_known_variants),
variant);
if (!known_variant && variant != "default") {
LOG(WARNING) << "Unexpected CPU variant for X86 using defaults: " << variant;
}
return Create(x86_64, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2, has_POPCNT);
}
X86FeaturesUniquePtr X86InstructionSetFeatures::FromBitmap(uint32_t bitmap, bool x86_64) {
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;
bool has_POPCNT = (bitmap & kPopCntBitfield) != 0;
return Create(x86_64, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2, has_POPCNT);
}
X86FeaturesUniquePtr X86InstructionSetFeatures::FromCppDefines(bool x86_64) {
#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
#ifndef __POPCNT__
const bool has_POPCNT = false;
#else
const bool has_POPCNT = true;
#endif
return Create(x86_64, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2, has_POPCNT);
}
X86FeaturesUniquePtr 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 has_SSSE3 = false;
bool has_SSE4_1 = false;
bool has_SSE4_2 = false;
bool has_AVX = false;
bool has_AVX2 = false;
bool has_POPCNT = 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;
}
if (line.find("popcnt") != std::string::npos) {
has_POPCNT = true;
}
}
}
}
in.close();
} else {
LOG(ERROR) << "Failed to open /proc/cpuinfo";
}
return Create(x86_64, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2, has_POPCNT);
}
X86FeaturesUniquePtr X86InstructionSetFeatures::FromHwcap(bool x86_64) {
UNIMPLEMENTED(WARNING);
return FromCppDefines(x86_64);
}
X86FeaturesUniquePtr X86InstructionSetFeatures::FromAssembly(bool x86_64) {
UNIMPLEMENTED(WARNING);
return FromCppDefines(x86_64);
}
X86FeaturesUniquePtr X86InstructionSetFeatures::FromCpuFeatures(bool x86_64) {
#ifdef CPU_FEATURES_ARCH_X86
cpu_features::X86Features features = cpu_features::GetX86Info().features;
return Create(x86_64,
features.ssse3,
features.sse4_1,
features.sse4_2,
features.avx,
features.avx2,
features.popcnt);
#else
UNIMPLEMENTED(WARNING);
return FromCppDefines(x86_64);
#endif
}
bool X86InstructionSetFeatures::Equals(const InstructionSetFeatures* other) const {
if (GetInstructionSet() != other->GetInstructionSet()) {
return false;
}
const X86InstructionSetFeatures* other_as_x86 = other->AsX86InstructionSetFeatures();
return (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_) &&
(has_POPCNT_ == other_as_x86->has_POPCNT_);
}
bool X86InstructionSetFeatures::HasAtLeast(const InstructionSetFeatures* other) const {
if (GetInstructionSet() != other->GetInstructionSet()) {
return false;
}
const X86InstructionSetFeatures* other_as_x86 = other->AsX86InstructionSetFeatures();
return (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_) &&
(has_POPCNT_ || !other_as_x86->has_POPCNT_);
}
uint32_t X86InstructionSetFeatures::AsBitmap() const {
return (has_SSSE3_ ? kSsse3Bitfield : 0) |
(has_SSE4_1_ ? kSse4_1Bitfield : 0) |
(has_SSE4_2_ ? kSse4_2Bitfield : 0) |
(has_AVX_ ? kAvxBitfield : 0) |
(has_AVX2_ ? kAvx2Bitfield : 0) |
(has_POPCNT_ ? kPopCntBitfield : 0);
}
std::string X86InstructionSetFeatures::GetFeatureString() const {
std::string result;
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";
}
if (has_POPCNT_) {
result += ",popcnt";
} else {
result += ",-popcnt";
}
return result;
}
std::unique_ptr<const InstructionSetFeatures> X86InstructionSetFeatures::AddFeaturesFromSplitString(
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_;
bool has_POPCNT = has_POPCNT_;
for (const std::string& feature : features) {
DCHECK_EQ(android::base::Trim(feature), feature)
<< "Feature name is not trimmed: '" << feature << "'";
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 if (feature == "popcnt") {
has_POPCNT = true;
} else if (feature == "-popcnt") {
has_POPCNT = false;
} else {
*error_msg = StringPrintf("Unknown instruction set feature: '%s'", feature.c_str());
return nullptr;
}
}
return Create(x86_64, has_SSSE3, has_SSE4_1, has_SSE4_2, has_AVX, has_AVX2, has_POPCNT);
}
} // namespace art
|