blob: 8d40f71128d3f3a617dcb9f285213e8824676259 [file] [log] [blame]
Guillaume Chatelet3cc8f312020-10-12 08:55:20 +00001// Copyright 2017 Google LLC
Jeff Hammonde6983272020-09-21 00:54:58 -07002// Copyright 2020 Intel Corporation
Guillaume Chatelet439d3712018-02-01 10:03:09 +01003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
Guillaume Chatelet8e58ef02018-02-01 10:38:48 +010016#ifndef CPU_FEATURES_INCLUDE_CPUINFO_X86_H_
17#define CPU_FEATURES_INCLUDE_CPUINFO_X86_H_
Guillaume Chatelet439d3712018-02-01 10:03:09 +010018
Artem Alekseev653d5812019-07-02 17:52:25 +030019#include "cpu_features_cache_info.h"
Guillaume Chatelet439d3712018-02-01 10:03:09 +010020#include "cpu_features_macros.h"
21
Guillaume Chatelete4195732018-02-12 16:15:15 +010022CPU_FEATURES_START_CPP_NAMESPACE
Guillaume Chatelet439d3712018-02-01 10:03:09 +010023
24// See https://en.wikipedia.org/wiki/CPUID for a list of x86 cpu features.
Guillaume Chateletd395dfa2019-01-22 13:19:42 +010025// The field names are based on the short name provided in the wikipedia tables.
Guillaume Chatelet439d3712018-02-01 10:03:09 +010026typedef struct {
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +020027 int fpu : 1;
28 int tsc : 1;
29 int cx8 : 1;
30 int clfsh : 1;
31 int mmx : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010032 int aes : 1;
33 int erms : 1;
34 int f16c : 1;
gadoofou873262a552020-03-11 18:44:49 +050035 int fma4 : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010036 int fma3 : 1;
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +020037 int vaes : 1;
Guillaume Chatelet11e3e202018-02-09 08:55:11 +010038 int vpclmulqdq : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010039 int bmi1 : 1;
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +020040 int hle : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010041 int bmi2 : 1;
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +020042 int rtm : 1;
43 int rdseed : 1;
44 int clflushopt : 1;
45 int clwb : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010046
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +020047 int sse : 1;
48 int sse2 : 1;
49 int sse3 : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010050 int ssse3 : 1;
51 int sse4_1 : 1;
52 int sse4_2 : 1;
gadoofou873262a552020-03-11 18:44:49 +050053 int sse4a : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010054
55 int avx : 1;
56 int avx2 : 1;
57
58 int avx512f : 1;
59 int avx512cd : 1;
60 int avx512er : 1;
61 int avx512pf : 1;
62 int avx512bw : 1;
63 int avx512dq : 1;
64 int avx512vl : 1;
65 int avx512ifma : 1;
66 int avx512vbmi : 1;
67 int avx512vbmi2 : 1;
68 int avx512vnni : 1;
69 int avx512bitalg : 1;
70 int avx512vpopcntdq : 1;
71 int avx512_4vnniw : 1;
Jeff Hammond17ffb652020-09-22 00:29:46 -070072 int avx512_4vbmi2 : 1;
73 int avx512_second_fma : 1;
Jeff Hammond33bd72c2020-09-21 00:56:26 -070074 int avx512_4fmaps : 1;
75 int avx512_bf16 : 1;
76 int avx512_vp2intersect : 1;
Jeff Hammond33bd72c2020-09-21 00:56:26 -070077 int amx_bf16 : 1;
78 int amx_tile : 1;
79 int amx_int8 : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010080
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +020081 int pclmulqdq : 1;
Patrik Fiedler3ee0d622018-02-13 11:14:32 +010082 int smx : 1;
83 int sgx : 1;
Guillaume Chatelet9b872ce2018-03-13 10:58:42 +010084 int cx16 : 1; // aka. CMPXCHG16B
Guillaume Chateletd395dfa2019-01-22 13:19:42 +010085 int sha : 1;
86 int popcnt : 1;
87 int movbe : 1;
88 int rdrnd : 1;
Patrik Fiedler3ee0d622018-02-13 11:14:32 +010089
Artem Alekseev3ee4a9e2019-06-19 16:06:05 +030090 int dca : 1;
91 int ss : 1;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010092 // Make sure to update X86FeaturesEnum below if you add a field here.
93} X86Features;
94
95typedef struct {
96 X86Features features;
97 int family;
98 int model;
99 int stepping;
100 char vendor[13]; // 0 terminated string
101} X86Info;
102
103// Calls cpuid and returns an initialized X86info.
104// This function is guaranteed to be malloc, memset and memcpy free.
105X86Info GetX86Info(void);
106
Artem Alekseev653d5812019-07-02 17:52:25 +0300107// Returns cache hierarchy informations.
108// Can call cpuid multiple times.
109// Only works on Intel CPU at the moment.
110// This function is guaranteed to be malloc, memset and memcpy free.
111CacheInfo GetX86CacheInfo(void);
112
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100113typedef enum {
114 X86_UNKNOWN,
115 INTEL_CORE, // CORE
116 INTEL_PNR, // PENRYN
117 INTEL_NHM, // NEHALEM
118 INTEL_ATOM_BNL, // BONNELL
119 INTEL_WSM, // WESTMERE
120 INTEL_SNB, // SANDYBRIDGE
121 INTEL_IVB, // IVYBRIDGE
122 INTEL_ATOM_SMT, // SILVERMONT
123 INTEL_HSW, // HASWELL
124 INTEL_BDW, // BROADWELL
125 INTEL_SKL, // SKYLAKE
126 INTEL_ATOM_GMT, // GOLDMONT
127 INTEL_KBL, // KABY LAKE
128 INTEL_CFL, // COFFEE LAKE
Moxeja73a121b2020-01-05 13:15:12 +0000129 INTEL_WHL, // WHISKEY LAKE
Brandon Surmanskiefcc49a2018-02-07 11:07:00 -0800130 INTEL_CNL, // CANNON LAKE
Moxeja73a121b2020-01-05 13:15:12 +0000131 INTEL_ICL, // ICE LAKE
Jeff Hammonde6983272020-09-21 00:54:58 -0700132 INTEL_TGL, // TIGER LAKE
133 INTEL_SPR, // SAPPHIRE RAPIDS
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100134 AMD_HAMMER, // K8
135 AMD_K10, // K10
136 AMD_BOBCAT, // K14
137 AMD_BULLDOZER, // K15
138 AMD_JAGUAR, // K16
139 AMD_ZEN, // K17
140} X86Microarchitecture;
141
142// Returns the underlying microarchitecture by looking at X86Info's vendor,
143// family and model.
144X86Microarchitecture GetX86Microarchitecture(const X86Info* info);
145
146// Calls cpuid and fills the brand_string.
147// - brand_string *must* be of size 49 (beware of array decaying).
148// - brand_string will be zero terminated.
149// - This function calls memcpy.
150void FillX86BrandString(char brand_string[49]);
151
152////////////////////////////////////////////////////////////////////////////////
153// Introspection functions
154
155typedef enum {
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +0200156 X86_FPU,
157 X86_TSC,
158 X86_CX8,
159 X86_CLFSH,
160 X86_MMX,
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100161 X86_AES,
162 X86_ERMS,
163 X86_F16C,
gadoofou873262a552020-03-11 18:44:49 +0500164 X86_FMA4,
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100165 X86_FMA3,
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +0200166 X86_VAES,
Guillaume Chatelet11e3e202018-02-09 08:55:11 +0100167 X86_VPCLMULQDQ,
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100168 X86_BMI1,
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +0200169 X86_HLE,
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100170 X86_BMI2,
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +0200171 X86_RTM,
172 X86_RDSEED,
173 X86_CLFLUSHOPT,
174 X86_CLWB,
175 X86_SSE,
176 X86_SSE2,
177 X86_SSE3,
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100178 X86_SSSE3,
179 X86_SSE4_1,
180 X86_SSE4_2,
gadoofou873262a552020-03-11 18:44:49 +0500181 X86_SSE4A,
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100182 X86_AVX,
183 X86_AVX2,
184 X86_AVX512F,
185 X86_AVX512CD,
186 X86_AVX512ER,
187 X86_AVX512PF,
188 X86_AVX512BW,
189 X86_AVX512DQ,
190 X86_AVX512VL,
191 X86_AVX512IFMA,
192 X86_AVX512VBMI,
193 X86_AVX512VBMI2,
194 X86_AVX512VNNI,
195 X86_AVX512BITALG,
196 X86_AVX512VPOPCNTDQ,
197 X86_AVX512_4VNNIW,
Jeff Hammond17ffb652020-09-22 00:29:46 -0700198 X86_AVX512_4VBMI2,
199 X86_AVX512_SECOND_FMA,
Jeff Hammond33bd72c2020-09-21 00:56:26 -0700200 X86_AVX512_4FMAPS,
201 X86_AVX512_BF16,
202 X86_AVX512_VP2INTERSECT,
203 X86_AMX_BF16,
204 X86_AMX_TILE,
205 X86_AMX_INT8,
Dr.-Ing. Patrick Siegl367bc422019-06-13 11:53:39 +0200206 X86_PCLMULQDQ,
Patrik Fiedler3ee0d622018-02-13 11:14:32 +0100207 X86_SMX,
208 X86_SGX,
Guillaume Chatelet9b872ce2018-03-13 10:58:42 +0100209 X86_CX16,
Guillaume Chateletd395dfa2019-01-22 13:19:42 +0100210 X86_SHA,
211 X86_POPCNT,
212 X86_MOVBE,
213 X86_RDRND,
Artem Alekseev3ee4a9e2019-06-19 16:06:05 +0300214 X86_DCA,
215 X86_SS,
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100216 X86_LAST_,
217} X86FeaturesEnum;
218
219int GetX86FeaturesEnumValue(const X86Features* features, X86FeaturesEnum value);
220
221const char* GetX86FeaturesEnumName(X86FeaturesEnum);
222
223const char* GetX86MicroarchitectureName(X86Microarchitecture);
224
Guillaume Chatelete4195732018-02-12 16:15:15 +0100225CPU_FEATURES_END_CPP_NAMESPACE
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100226
Guillaume Chatelet4155ee72019-01-18 13:38:22 +0100227#if !defined(CPU_FEATURES_ARCH_X86)
228#error "Including cpuinfo_x86.h from a non-x86 target."
229#endif
230
Guillaume Chatelet8e58ef02018-02-01 10:38:48 +0100231#endif // CPU_FEATURES_INCLUDE_CPUINFO_X86_H_