forked from youtube/cobalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_features.h
330 lines (292 loc) · 12.1 KB
/
cpu_features.h
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
// Copyright 2019 The Cobalt Authors. All Rights Reserved.
//
// 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.
// Module Overview: Starboard CPU features API
// The Starboard API SbCPUFeaturesGet() queries the features of the underlying
// CPU.
//
// The features are returned in a structure SbCPUFeatures. It contains processor
// information like architecture, model and family, and capability flags that
// indicate whether certain features are supported, like NEON and VFP.
// The purpose of this API is to enable hardware optimizations if the
// architecture supports them.
#ifndef STARBOARD_CPU_FEATURES_H_
#define STARBOARD_CPU_FEATURES_H_
#include "starboard/export.h"
#include "starboard/types.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum SbCPUFeaturesArchitecture {
kSbCPUFeaturesArchitectureArm,
kSbCPUFeaturesArchitectureArm64,
kSbCPUFeaturesArchitectureMips,
kSbCPUFeaturesArchitectureMips64,
kSbCPUFeaturesArchitecturePpc,
kSbCPUFeaturesArchitecturePpc64,
kSbCPUFeaturesArchitectureX86,
kSbCPUFeaturesArchitectureX86_64,
kSbCPUFeaturesArchitectureUnknown,
} SbCPUFeaturesArchitecture;
typedef struct SbCPUFeaturesARM {
// -------------------------------------------------------------------
// Processor version information, only valid on Arm and Arm64.
//
// Stored in processor ID registers. See the reference manual of
// Cortex-M0 for instance: http://infocenter.arm.com/help/
// index.jsp?topic=/com.arm.doc.ddi0432c/Bhccjgga.html, and
// Cortex-A8 for instance: http://infocenter.arm.com/help/
// index.jsp?topic=/com.arm.doc.ddi0344k/Babififh.html.
// About how to retrieve the information, see Arm CPU Feature
// Registers documentation: https://www.kernel.org/doc/Documentation/
// arm64/cpu-feature-registers.txt, or retrieve from /proc/cpuinfo
// if available.
// -------------------------------------------------------------------
// Processor implementer code. ARM is 0x41, NVIDIA is 0x4e, etc.
int16_t implementer;
// Processor variant number, indicating the major revision number.
int16_t variant;
// Processor revision number, indicating the minor revision number.
int16_t revision;
// Processor architecture generation number, indicating the generations
// (ARMv6-M, ARMv7, etc) within an architecture family. This field is
// called "Architecture" or "Constant" in the processor ID register.
int16_t architecture_generation;
// Processor part number, indicating Cortex-M0, Cortex-A8, etc.
int16_t part;
// ------------------------------------------------------------------------
// Processor feature flags valid on Arm and Arm64
//
// See ARM compiler armasm user guide:
// https://developer.arm.com/docs/100069/0604
// and Android NDK cpu features guide:
// https://developer.android.com/ndk/guides/cpu-features
// ------------------------------------------------------------------------
// ---------------------------------------------------------------------
// Arm 32 feature flags
// ---------------------------------------------------------------------
// ARM Advanced SIMD (NEON) vector instruction set extension.
bool has_neon;
// Thumb-2 mode.
bool has_thumb2;
// VFP (SIMD vector floating point instructions).
bool has_vfp;
// VFP version 3
bool has_vfp3;
// VFP version 4
bool has_vfp4;
// VFP version 3 with 32 D-registers.
bool has_vfp3_d32;
// SDIV and UDIV hardware division in ARM mode.
bool has_idiva;
// ---------------------------------------------------------------------
// Arm 64 feature flags
// ---------------------------------------------------------------------
// AES instructions.
bool has_aes;
// CRC32 instructions.
bool has_crc32;
// SHA-1 instructions.
bool has_sha1;
// SHA-256 instructions.
bool has_sha2;
// 64-bit PMULL and PMULL2 instructions.
bool has_pmull;
} SbCPUFeaturesARM;
typedef struct SbCPUFeaturesMIPS {
// ----------------------------------------------------------------------
// Processor feature flags valid only on Mips and Mips64
//
// See MIPS SIMD documentation:
// https://www.mips.com/products/architectures/ase/simd/
// ----------------------------------------------------------------------
// MIPS SIMD Architecture (MSA).
bool has_msa;
} SbCPUFeaturesMIPS;
typedef struct SbCPUFeaturesX86 {
// -------------------------------------------------------------------
// Processor version information, valid only on x86 and x86_64
//
// See Intel Advanced Vector Extensions Programming Reference:
// https://software.intel.com/file/36945.
// Version information is returned in EAX when CPUID executes with
// EAX = 0x00000001.
// -------------------------------------------------------------------
// Processor vendor ID string, e.g. "GenuineIntel", "AuthenticAMD", etc
const char* vendor;
// Processor family ID
int16_t family;
// Processor extended family ID, needs to be examined only when the
// family ID is 0FH.
int16_t ext_family;
// Processor model ID
int16_t model;
// Processor extended model ID, needs to be examined only when the
// family ID is 06H or 0FH.
int16_t ext_model;
// Processor stepping ID, a product revision number
int16_t stepping;
// Processor type ID
int16_t type;
// A raw form of collection of processor stepping, model, and
// family information
int32_t signature;
// ---------------------------------------------------------------------
// Processor feature flags
//
// These flags denote whether a feature is supported by the processor.
// They can be used easily to check one specific feature. The set of
// valid flags depends of specific architecture. Below are processor
// feature flags valid on x86 and x86_64
//
// See kernel source:
// https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/
// tree/arch/x86/include/asm/cpufeatures.h
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Intel-defined CPU features, CPUID level 0x00000001(EDX), word 0
// ---------------------------------------------------------------------
// Conditional Move Instructions (plus FCMOVcc, FCOMI with FPU).
bool has_cmov;
// Multimedia extensions.
bool has_mmx;
// SSE (Streaming SIMD Extensions).
bool has_sse;
// SSE2 extensions.
bool has_sse2;
// Time Stamp Counter.
bool has_tsc;
// ---------------------------------------------------------------------
// Intel-defined CPU features, CPUID level 0x00000001(ECX), word 4
// ---------------------------------------------------------------------
// SSE3 extensions.
bool has_sse3;
// PCLMULQDQ instruction.
bool has_pclmulqdq;
// Supplemental SSE3 extensions.
bool has_ssse3;
// SSE-4.1 extensions.
bool has_sse41;
// SSE-4.2 extensions.
bool has_sse42;
// MOVBE instruction.
bool has_movbe;
// POPCNT instruction.
bool has_popcnt;
// XSAVE/XRSTOR/XGETBV/XSETBV instruction enabled in this OS.
bool has_osxsave;
// AVX (Advanced Vector Extensions).
bool has_avx;
// 16-bit FP conversions.
bool has_f16c;
// Fused multiply-add.
bool has_fma3;
// AES new instructions (AES-NI).
bool has_aesni;
// ---------------------------------------------------------------------
// Intel-defined CPU features, CPUID level 0x00000007:0(EBX), word 9
// ---------------------------------------------------------------------
// AVX2 Extensions.
bool has_avx2;
// AVX-512 Foundation.
bool has_avx512f;
// AVX-512 DQ (Double/Quad granular) Instructions.
bool has_avx512dq;
// AVX-512 Integer Fused Multiply-Add instructions.
bool has_avx512ifma;
// AVX-512 Prefetch.
bool has_avx512pf;
// AVX-512 Exponential and Reciprocal.
bool has_avx512er;
// AVX-512 Conflict Detection.
bool has_avx512cd;
// AVX-512 BW (Byte/Word granular) Instructions.
bool has_avx512bw;
// AVX-512 VL (128/256 Vector Length) Extensions.
bool has_avx512vl;
// First group of bit manipulation extensions.
bool has_bmi1;
// Second group of bit manipulation extensions.
bool has_bmi2;
// ---------------------------------------------------------------------
// Intel-defined CPU features, CPUID level 0x80000001:0(ECX)
// ---------------------------------------------------------------------
// Bit manipulation instruction LZCNT.
bool has_lzcnt;
// ---------------------------------------------------------------------
// AMD-defined CPU features: CPUID level 0x80000001(ECX), word 6
// ---------------------------------------------------------------------
// SAHF in long mode.
bool has_sahf;
} SbCPUFeaturesX86;
typedef struct SbCPUFeatures {
// -----------------------------------------------------------------
// General processor information, valid on all architectures.
// -----------------------------------------------------------------
// Architecture of the processor.
SbCPUFeaturesArchitecture architecture;
// Processor brand string retrievable by CPUID or from /proc/cpuinfo
// under the key "model name" or "Processor".
const char* brand;
// Processor cache line size in bytes of Level 1 instruction cache and data
// cache. Queried by sysconf(_SC_LEVEL1_ICACHE_LINESIZE) and
// sysconf(_SC_LEVEL1_DCACHE_LINESIZE), or from files /proc/cpuinfo,
// /proc/self/auxv, or CPUID with CLFLUSH instruction.
int32_t icache_line_size;
int32_t dcache_line_size;
// Processor has floating-point unit on-chip.
bool has_fpu;
// ------------------------------------------------------------------
// Processor feature flags bitmask, valid on the architectures where
// libc’s getauxval() is supported.
//
// These are compact bitmasks containing information of the feature
// flags. The bitmasks hold different flags depending on specific
// architectures. See third_party/musl/arch/<arch_name>/bits/hwcap.h
// for details. The flags contained by the bitmasks may be broader
// than the features flags contained by the SbCPUFeatures. The
// recommendation of usage is to never use these bitmasks as long as
// the information is available as a field of SbCPUFeatures, and
// only rely on bitmasks if the information is not natively supported
// by SbCPUFeatures.
// ------------------------------------------------------------------
// Bit-mask containing processor features flags. Queried by
// getauxval(AT_HWCAP) if it is supported.
uint32_t hwcap;
// Similar to hwcap. Queried by getauxval(AT_HWCAP2) if
// it is supported.
uint32_t hwcap2;
// Processor features specific to each architecture. Set the appropriate
// |SbCPUFeatures<ARCH_NAME>| for the underlying architecture. Set the
// other |SbCPUFeatures<OTHER_ARCH>| to be invalid, meaning:
// - '-1' for signed integer fields
// - 'false' for feature flag fields
// - '0' for feature flag bitmasks
// - empty string for string fields
SbCPUFeaturesARM arm;
// The reason that the "_arch" suffix exists for mips is because on some
// platforms that use MIPS as the underlying architecture, "mips" is
// already defined as a macro.
SbCPUFeaturesMIPS mips_arch;
SbCPUFeaturesX86 x86;
} SbCPUFeatures;
// Retrieve the underlying CPU features and place it in |features|, which must
// not be NULL.
//
// If this function returns false, it means the CPU architecture is unknown and
// all fields in |features| are invalid.
SB_EXPORT bool SbCPUFeaturesGet(SbCPUFeatures* features);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // STARBOARD_CPU_FEATURES_H_