1 /* This test checks if the __builtin_cpu_is and __builtin_cpu_supports calls
2 are recognized. It also independently uses CPUID to get cpu type and
3 features supported and checks if the builtins correctly identify the
4 platform. The code to do the identification is adapted from
5 libgcc/config/i386/cpuinfo.c. */
12 /* Check if the Intel CPU model and sub-model are identified. */
14 check_intel_cpu_model (unsigned int family
, unsigned int model
,
15 unsigned int brand_id
)
17 /* Parse family and model only if brand ID is 0. */
31 assert (__builtin_cpu_is ("atom"));
39 assert (__builtin_cpu_is ("silvermont"));
42 /* Knights Landing. */
43 assert (__builtin_cpu_is ("knl"));
50 assert (__builtin_cpu_is ("corei7"));
51 assert (__builtin_cpu_is ("nehalem"));
57 assert (__builtin_cpu_is ("corei7"));
58 assert (__builtin_cpu_is ("westmere"));
63 assert (__builtin_cpu_is ("corei7"));
64 assert (__builtin_cpu_is ("sandybridge"));
69 assert (__builtin_cpu_is ("corei7"));
70 assert (__builtin_cpu_is ("ivybridge"));
77 assert (__builtin_cpu_is ("corei7"));
78 assert (__builtin_cpu_is ("haswell"));
85 assert (__builtin_cpu_is ("corei7"));
86 assert (__builtin_cpu_is ("broadwell"));
91 assert (__builtin_cpu_is ("corei7"));
92 assert (__builtin_cpu_is ("skylake"));
95 /* Skylake with AVX-512 support. */
96 assert (__builtin_cpu_is ("corei7"));
97 assert (__builtin_cpu_is ("skylake-avx512"));
104 assert (__builtin_cpu_is ("core2"));
111 /* We have no idea. */
117 /* Check if the AMD CPU model and sub-model are identified. */
119 check_amd_cpu_model (unsigned int family
, unsigned int model
)
123 /* AMD Family 10h. */
129 assert (__builtin_cpu_is ("amdfam10h"));
130 assert (__builtin_cpu_is ("barcelona"));
134 assert (__builtin_cpu_is ("amdfam10h"));
135 assert (__builtin_cpu_is ("shanghai"));
139 assert (__builtin_cpu_is ("amdfam10h"));
140 assert (__builtin_cpu_is ("istanbul"));
146 /* AMD Family 15h. */
148 assert (__builtin_cpu_is ("amdfam15h"));
149 /* Bulldozer version 1. */
151 assert (__builtin_cpu_is ("bdver1"));
152 /* Bulldozer version 2. */
153 if (model
>= 0x10 && model
<= 0x1f)
154 assert (__builtin_cpu_is ("bdver2"));
161 /* Check if the ISA features are identified. */
163 check_features (unsigned int ecx
, unsigned int edx
,
167 assert (__builtin_cpu_supports ("cmov"));
169 assert (__builtin_cpu_supports ("mmx"));
171 assert (__builtin_cpu_supports ("sse"));
173 assert (__builtin_cpu_supports ("sse2"));
174 if (ecx
& bit_POPCNT
)
175 assert (__builtin_cpu_supports ("popcnt"));
177 assert (__builtin_cpu_supports ("aes"));
178 if (ecx
& bit_PCLMUL
)
179 assert (__builtin_cpu_supports ("pclmul"));
181 assert (__builtin_cpu_supports ("sse3"));
183 assert (__builtin_cpu_supports ("ssse3"));
184 if (ecx
& bit_SSE4_1
)
185 assert (__builtin_cpu_supports ("sse4.1"));
186 if (ecx
& bit_SSE4_2
)
187 assert (__builtin_cpu_supports ("sse4.2"));
189 assert (__builtin_cpu_supports ("avx"));
191 /* Get advanced features at level 7 (eax = 7, ecx = 0). */
192 if (max_cpuid_level
>= 7)
194 unsigned int eax
, ebx
, ecx
, edx
;
195 __cpuid_count (7, 0, eax
, ebx
, ecx
, edx
);
197 assert (__builtin_cpu_supports ("avx2"));
198 if (ebx
& bit_AVX512F
)
199 assert (__builtin_cpu_supports ("avx512f"));
200 if (ebx
& bit_AVX512VL
)
201 assert (__builtin_cpu_supports ("avx512vl"));
202 if (ebx
& bit_AVX512CD
)
203 assert (__builtin_cpu_supports ("avx512cd"));
204 if (ebx
& bit_AVX512PF
)
205 assert (__builtin_cpu_supports ("avx512pf"));
206 if (ebx
& bit_AVX512ER
)
207 assert (__builtin_cpu_supports ("avx512er"));
208 if (ebx
& bit_AVX512BW
)
209 assert (__builtin_cpu_supports ("avx512bw"));
210 if (ebx
& bit_AVX512DQ
)
211 assert (__builtin_cpu_supports ("avx512dq"));
212 if (ecx
& bit_AVX512IFMA
)
213 assert (__builtin_cpu_supports ("avx512ifma"));
214 if (ecx
& bit_AVX512VBMI
)
215 assert (__builtin_cpu_supports ("avx512vbmi"));
216 if (edx
& bit_AVX5124VNNIW
)
217 assert (__builtin_cpu_supports ("avx5124vnniw"));
218 if (edx
& bit_AVX5124FMAPS
)
219 assert (__builtin_cpu_supports ("avx5124fmaps"));
223 static int __attribute__ ((noinline
))
224 __get_cpuid_output (unsigned int __level
,
225 unsigned int *__eax
, unsigned int *__ebx
,
226 unsigned int *__ecx
, unsigned int *__edx
)
228 return __get_cpuid (__level
, __eax
, __ebx
, __ecx
, __edx
);
234 unsigned int eax
, ebx
, ecx
, edx
;
238 unsigned int model
, family
, brand_id
;
239 unsigned int extended_model
, extended_family
;
241 /* Assume cpuid insn present. Run in level 0 to get vendor id. */
242 if (!__get_cpuid_output (0, &eax
, &ebx
, &ecx
, &edx
))
251 if (!__get_cpuid_output (1, &eax
, &ebx
, &ecx
, &edx
))
254 model
= (eax
>> 4) & 0x0f;
255 family
= (eax
>> 8) & 0x0f;
256 brand_id
= ebx
& 0xff;
257 extended_model
= (eax
>> 12) & 0xf0;
258 extended_family
= (eax
>> 20) & 0xff;
260 if (vendor
== signature_INTEL_ebx
)
262 assert (__builtin_cpu_is ("intel"));
263 /* Adjust family and model for Intel CPUs. */
266 family
+= extended_family
;
267 model
+= extended_model
;
269 else if (family
== 0x06)
270 model
+= extended_model
;
271 check_intel_cpu_model (family
, model
, brand_id
);
272 check_features (ecx
, edx
, max_level
);
274 else if (vendor
== signature_AMD_ebx
)
276 assert (__builtin_cpu_is ("amd"));
277 /* Adjust model and family for AMD CPUS. */
280 family
+= extended_family
;
281 model
+= (extended_model
<< 4);
283 check_amd_cpu_model (family
, model
);
284 check_features (ecx
, edx
, max_level
);
293 /* Check CPU Features. */
294 assert (__builtin_cpu_supports ("cmov") >= 0);
296 assert (__builtin_cpu_supports ("mmx") >= 0);
298 assert (__builtin_cpu_supports ("popcnt") >= 0);
300 assert (__builtin_cpu_supports ("sse") >= 0);
302 assert (__builtin_cpu_supports ("sse2") >= 0);
304 assert (__builtin_cpu_supports ("sse3") >= 0);
306 assert (__builtin_cpu_supports ("ssse3") >= 0);
308 assert (__builtin_cpu_supports ("sse4.1") >= 0);
310 assert (__builtin_cpu_supports ("sse4.2") >= 0);
312 assert (__builtin_cpu_supports ("avx") >= 0);
314 assert (__builtin_cpu_supports ("avx2") >= 0);
316 assert (__builtin_cpu_supports ("avx512f") >= 0);
318 assert (__builtin_cpu_supports ("avx5124vnniw") >= 0);
320 assert (__builtin_cpu_supports ("avx5124fmaps") >= 0);
322 /* Check CPU type. */
323 assert (__builtin_cpu_is ("amd") >= 0);
325 assert (__builtin_cpu_is ("intel") >= 0);
327 assert (__builtin_cpu_is ("atom") >= 0);
329 assert (__builtin_cpu_is ("core2") >= 0);
331 assert (__builtin_cpu_is ("corei7") >= 0);
333 assert (__builtin_cpu_is ("nehalem") >= 0);
335 assert (__builtin_cpu_is ("westmere") >= 0);
337 assert (__builtin_cpu_is ("sandybridge") >= 0);
339 assert (__builtin_cpu_is ("amdfam10h") >= 0);
341 assert (__builtin_cpu_is ("barcelona") >= 0);
343 assert (__builtin_cpu_is ("shanghai") >= 0);
345 assert (__builtin_cpu_is ("istanbul") >= 0);
347 assert (__builtin_cpu_is ("amdfam15h") >= 0);
349 assert (__builtin_cpu_is ("bdver1") >= 0);
351 assert (__builtin_cpu_is ("bdver2") >= 0);
358 __builtin_cpu_init ();