2016-12-21 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / testsuite / gcc.target / i386 / builtin_target.c
blobc620a7415ac65ff7672dcd058b520adf1796ad0f
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. */
7 /* { dg-do run } */
9 #include <assert.h>
10 #include "cpuid.h"
12 /* Check if the Intel CPU model and sub-model are identified. */
13 static void
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. */
18 if (brand_id == 0)
20 switch (family)
22 case 0x5:
23 /* Pentium. */
24 break;
25 case 0x6:
26 switch (model)
28 case 0x1c:
29 case 0x26:
30 /* Atom. */
31 assert (__builtin_cpu_is ("atom"));
32 break;
33 case 0x37:
34 case 0x4a:
35 case 0x4d:
36 case 0x5a:
37 case 0x5d:
38 /* Silvermont. */
39 assert (__builtin_cpu_is ("silvermont"));
40 break;
41 case 0x57:
42 /* Knights Landing. */
43 assert (__builtin_cpu_is ("knl"));
44 break;
45 case 0x1a:
46 case 0x1e:
47 case 0x1f:
48 case 0x2e:
49 /* Nehalem. */
50 assert (__builtin_cpu_is ("corei7"));
51 assert (__builtin_cpu_is ("nehalem"));
52 break;
53 case 0x25:
54 case 0x2c:
55 case 0x2f:
56 /* Westmere. */
57 assert (__builtin_cpu_is ("corei7"));
58 assert (__builtin_cpu_is ("westmere"));
59 break;
60 case 0x2a:
61 case 0x2d:
62 /* Sandy Bridge. */
63 assert (__builtin_cpu_is ("corei7"));
64 assert (__builtin_cpu_is ("sandybridge"));
65 break;
66 case 0x3a:
67 case 0x3e:
68 /* Ivy Bridge. */
69 assert (__builtin_cpu_is ("corei7"));
70 assert (__builtin_cpu_is ("ivybridge"));
71 break;
72 case 0x3c:
73 case 0x3f:
74 case 0x45:
75 case 0x46:
76 /* Haswell. */
77 assert (__builtin_cpu_is ("corei7"));
78 assert (__builtin_cpu_is ("haswell"));
79 break;
80 case 0x3d:
81 case 0x47:
82 case 0x4f:
83 case 0x56:
84 /* Broadwell. */
85 assert (__builtin_cpu_is ("corei7"));
86 assert (__builtin_cpu_is ("broadwell"));
87 break;
88 case 0x4e:
89 case 0x5e:
90 /* Skylake. */
91 assert (__builtin_cpu_is ("corei7"));
92 assert (__builtin_cpu_is ("skylake"));
93 break;
94 case 0x55:
95 /* Skylake with AVX-512 support. */
96 assert (__builtin_cpu_is ("corei7"));
97 assert (__builtin_cpu_is ("skylake-avx512"));
98 break;
99 case 0x17:
100 case 0x1d:
101 /* Penryn. */
102 case 0x0f:
103 /* Merom. */
104 assert (__builtin_cpu_is ("core2"));
105 break;
106 default:
107 break;
109 break;
110 default:
111 /* We have no idea. */
112 break;
117 /* Check if the AMD CPU model and sub-model are identified. */
118 static void
119 check_amd_cpu_model (unsigned int family, unsigned int model)
121 switch (family)
123 /* AMD Family 10h. */
124 case 0x10:
125 switch (model)
127 case 0x2:
128 /* Barcelona. */
129 assert (__builtin_cpu_is ("amdfam10h"));
130 assert (__builtin_cpu_is ("barcelona"));
131 break;
132 case 0x4:
133 /* Shanghai. */
134 assert (__builtin_cpu_is ("amdfam10h"));
135 assert (__builtin_cpu_is ("shanghai"));
136 break;
137 case 0x8:
138 /* Istanbul. */
139 assert (__builtin_cpu_is ("amdfam10h"));
140 assert (__builtin_cpu_is ("istanbul"));
141 break;
142 default:
143 break;
145 break;
146 /* AMD Family 15h. */
147 case 0x15:
148 assert (__builtin_cpu_is ("amdfam15h"));
149 /* Bulldozer version 1. */
150 if ( model <= 0xf)
151 assert (__builtin_cpu_is ("bdver1"));
152 /* Bulldozer version 2. */
153 if (model >= 0x10 && model <= 0x1f)
154 assert (__builtin_cpu_is ("bdver2"));
155 break;
156 default:
157 break;
161 /* Check if the ISA features are identified. */
162 static void
163 check_features (unsigned int ecx, unsigned int edx,
164 int max_cpuid_level)
166 if (edx & bit_CMOV)
167 assert (__builtin_cpu_supports ("cmov"));
168 if (edx & bit_MMX)
169 assert (__builtin_cpu_supports ("mmx"));
170 if (edx & bit_SSE)
171 assert (__builtin_cpu_supports ("sse"));
172 if (edx & bit_SSE2)
173 assert (__builtin_cpu_supports ("sse2"));
174 if (ecx & bit_POPCNT)
175 assert (__builtin_cpu_supports ("popcnt"));
176 if (ecx & bit_AES)
177 assert (__builtin_cpu_supports ("aes"));
178 if (ecx & bit_PCLMUL)
179 assert (__builtin_cpu_supports ("pclmul"));
180 if (ecx & bit_SSE3)
181 assert (__builtin_cpu_supports ("sse3"));
182 if (ecx & bit_SSSE3)
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"));
188 if (ecx & bit_AVX)
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);
196 if (ebx & bit_AVX2)
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);
231 static int
232 check_detailed ()
234 unsigned int eax, ebx, ecx, edx;
236 int max_level;
237 unsigned int vendor;
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))
243 return 0;
245 vendor = ebx;
246 max_level = eax;
248 if (max_level < 1)
249 return 0;
251 if (!__get_cpuid_output (1, &eax, &ebx, &ecx, &edx))
252 return 0;
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. */
264 if (family == 0x0f)
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. */
278 if (family == 0x0f)
280 family += extended_family;
281 model += (extended_model << 4);
283 check_amd_cpu_model (family, model);
284 check_features (ecx, edx, max_level);
287 return 0;
290 static int
291 quick_check ()
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);
353 return 0;
356 int main ()
358 __builtin_cpu_init ();
359 quick_check ();
360 check_detailed ();
361 return 0;