1 /* Initialize CPU feature data.
2 This file is part of the GNU C Library.
3 Copyright (C) 2008-2017 Free Software Foundation, Inc.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
20 #include <cpu-features.h>
24 get_common_indeces (struct cpu_features
*cpu_features
,
25 unsigned int *family
, unsigned int *model
,
26 unsigned int *extended_model
, unsigned int *stepping
)
31 __cpuid (1, eax
, cpu_features
->cpuid
[COMMON_CPUID_INDEX_1
].ebx
,
32 cpu_features
->cpuid
[COMMON_CPUID_INDEX_1
].ecx
,
33 cpu_features
->cpuid
[COMMON_CPUID_INDEX_1
].edx
);
34 cpu_features
->cpuid
[COMMON_CPUID_INDEX_1
].eax
= eax
;
35 *family
= (eax
>> 8) & 0x0f;
36 *model
= (eax
>> 4) & 0x0f;
37 *extended_model
= (eax
>> 12) & 0xf0;
38 *stepping
= eax
& 0x0f;
41 *family
+= (eax
>> 20) & 0xff;
42 *model
+= *extended_model
;
46 if (cpu_features
->max_cpuid
>= 7)
48 cpu_features
->cpuid
[COMMON_CPUID_INDEX_7
].eax
,
49 cpu_features
->cpuid
[COMMON_CPUID_INDEX_7
].ebx
,
50 cpu_features
->cpuid
[COMMON_CPUID_INDEX_7
].ecx
,
51 cpu_features
->cpuid
[COMMON_CPUID_INDEX_7
].edx
);
53 /* Can we call xgetbv? */
54 if (CPU_FEATURES_CPU_P (cpu_features
, OSXSAVE
))
58 asm ("xgetbv" : "=a" (xcrlow
), "=d" (xcrhigh
) : "c" (0));
59 /* Is YMM and XMM state usable? */
60 if ((xcrlow
& (bit_YMM_state
| bit_XMM_state
)) ==
61 (bit_YMM_state
| bit_XMM_state
))
63 /* Determine if AVX is usable. */
64 if (CPU_FEATURES_CPU_P (cpu_features
, AVX
))
66 cpu_features
->feature
[index_arch_AVX_Usable
]
67 |= bit_arch_AVX_Usable
;
68 /* The following features depend on AVX being usable. */
69 /* Determine if AVX2 is usable. */
70 if (CPU_FEATURES_CPU_P (cpu_features
, AVX2
))
71 cpu_features
->feature
[index_arch_AVX2_Usable
]
72 |= bit_arch_AVX2_Usable
;
73 /* Determine if FMA is usable. */
74 if (CPU_FEATURES_CPU_P (cpu_features
, FMA
))
75 cpu_features
->feature
[index_arch_FMA_Usable
]
76 |= bit_arch_FMA_Usable
;
79 /* Check if OPMASK state, upper 256-bit of ZMM0-ZMM15 and
80 ZMM16-ZMM31 state are enabled. */
81 if ((xcrlow
& (bit_Opmask_state
| bit_ZMM0_15_state
82 | bit_ZMM16_31_state
)) ==
83 (bit_Opmask_state
| bit_ZMM0_15_state
| bit_ZMM16_31_state
))
85 /* Determine if AVX512F is usable. */
86 if (CPU_FEATURES_CPU_P (cpu_features
, AVX512F
))
88 cpu_features
->feature
[index_arch_AVX512F_Usable
]
89 |= bit_arch_AVX512F_Usable
;
90 /* Determine if AVX512DQ is usable. */
91 if (CPU_FEATURES_CPU_P (cpu_features
, AVX512DQ
))
92 cpu_features
->feature
[index_arch_AVX512DQ_Usable
]
93 |= bit_arch_AVX512DQ_Usable
;
101 init_cpu_features (struct cpu_features
*cpu_features
)
103 unsigned int ebx
, ecx
, edx
;
104 unsigned int family
= 0;
105 unsigned int model
= 0;
106 enum cpu_features_kind kind
;
109 if (__get_cpuid_max (0, 0) == 0)
111 kind
= arch_kind_other
;
116 __cpuid (0, cpu_features
->max_cpuid
, ebx
, ecx
, edx
);
118 /* This spells out "GenuineIntel". */
119 if (ebx
== 0x756e6547 && ecx
== 0x6c65746e && edx
== 0x49656e69)
121 unsigned int extended_model
, stepping
;
123 kind
= arch_kind_intel
;
125 get_common_indeces (cpu_features
, &family
, &model
, &extended_model
,
130 model
+= extended_model
;
135 /* BSF is slow on Atom. */
136 cpu_features
->feature
[index_arch_Slow_BSF
]
137 |= bit_arch_Slow_BSF
;
141 /* Knights Landing. Enable Silvermont optimizations. */
145 /* Unaligned load versions are faster than SSSE3
149 /* Airmont is a die shrink of Silvermont. */
156 /* Unaligned load versions are faster than SSSE3
158 #if index_arch_Fast_Unaligned_Load != index_arch_Prefer_PMINUB_for_stringop
159 # error index_arch_Fast_Unaligned_Load != index_arch_Prefer_PMINUB_for_stringop
161 #if index_arch_Fast_Unaligned_Load != index_arch_Slow_SSE4_2
162 # error index_arch_Fast_Unaligned_Load != index_arch_Slow_SSE4_2
164 #if index_arch_Fast_Unaligned_Load != index_arch_Fast_Unaligned_Copy
165 # error index_arch_Fast_Unaligned_Load != index_arch_Fast_Unaligned_Copy
167 cpu_features
->feature
[index_arch_Fast_Unaligned_Load
]
168 |= (bit_arch_Fast_Unaligned_Load
169 | bit_arch_Fast_Unaligned_Copy
170 | bit_arch_Prefer_PMINUB_for_stringop
171 | bit_arch_Slow_SSE4_2
);
175 /* Unknown family 0x06 processors. Assuming this is one
176 of Core i3/i5/i7 processors if AVX is available. */
177 if (!CPU_FEATURES_CPU_P (cpu_features
, AVX
))
187 /* Rep string instructions, unaligned load, unaligned copy,
188 and pminub are fast on Intel Core i3, i5 and i7. */
189 #if index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Load
190 # error index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Load
192 #if index_arch_Fast_Rep_String != index_arch_Prefer_PMINUB_for_stringop
193 # error index_arch_Fast_Rep_String != index_arch_Prefer_PMINUB_for_stringop
195 #if index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Copy
196 # error index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Copy
198 cpu_features
->feature
[index_arch_Fast_Rep_String
]
199 |= (bit_arch_Fast_Rep_String
200 | bit_arch_Fast_Unaligned_Load
201 | bit_arch_Fast_Unaligned_Copy
202 | bit_arch_Prefer_PMINUB_for_stringop
);
206 /* Xeon E7 v3 with stepping >= 4 has working TSX. */
212 /* Disable Intel TSX on Haswell processors (except Xeon E7 v3
213 with stepping >= 4) to avoid TSX on kernels that weren't
214 updated with the latest microcode package (which disables
215 broken feature by default). */
216 cpu_features
->cpuid
[index_cpu_RTM
].reg_RTM
&= ~bit_cpu_RTM
;
221 /* Unaligned load with 256-bit AVX registers are faster on
222 Intel processors with AVX2. */
223 if (CPU_FEATURES_ARCH_P (cpu_features
, AVX2_Usable
))
224 cpu_features
->feature
[index_arch_AVX_Fast_Unaligned_Load
]
225 |= bit_arch_AVX_Fast_Unaligned_Load
;
227 /* Since AVX512ER is unique to Xeon Phi, set Prefer_No_VZEROUPPER
228 if AVX512ER is available. Don't use AVX512 to avoid lower CPU
229 frequency if AVX512ER isn't available. */
230 if (CPU_FEATURES_CPU_P (cpu_features
, AVX512ER
))
231 cpu_features
->feature
[index_arch_Prefer_No_VZEROUPPER
]
232 |= bit_arch_Prefer_No_VZEROUPPER
;
234 cpu_features
->feature
[index_arch_Prefer_No_AVX512
]
235 |= bit_arch_Prefer_No_AVX512
;
237 /* To avoid SSE transition penalty, use _dl_runtime_resolve_slow.
238 If XGETBV suports ECX == 1, use _dl_runtime_resolve_opt. */
239 cpu_features
->feature
[index_arch_Use_dl_runtime_resolve_slow
]
240 |= bit_arch_Use_dl_runtime_resolve_slow
;
241 if (cpu_features
->max_cpuid
>= 0xd)
245 __cpuid_count (0xd, 1, eax
, ebx
, ecx
, edx
);
246 if ((eax
& (1 << 2)) != 0)
247 cpu_features
->feature
[index_arch_Use_dl_runtime_resolve_opt
]
248 |= bit_arch_Use_dl_runtime_resolve_opt
;
251 /* This spells out "AuthenticAMD". */
252 else if (ebx
== 0x68747541 && ecx
== 0x444d4163 && edx
== 0x69746e65)
254 unsigned int extended_model
, stepping
;
256 kind
= arch_kind_amd
;
258 get_common_indeces (cpu_features
, &family
, &model
, &extended_model
,
261 ecx
= cpu_features
->cpuid
[COMMON_CPUID_INDEX_1
].ecx
;
264 __cpuid (0x80000000, eax
, ebx
, ecx
, edx
);
265 if (eax
>= 0x80000001)
267 cpu_features
->cpuid
[COMMON_CPUID_INDEX_80000001
].eax
,
268 cpu_features
->cpuid
[COMMON_CPUID_INDEX_80000001
].ebx
,
269 cpu_features
->cpuid
[COMMON_CPUID_INDEX_80000001
].ecx
,
270 cpu_features
->cpuid
[COMMON_CPUID_INDEX_80000001
].edx
);
272 if (HAS_ARCH_FEATURE (AVX_Usable
))
274 /* Since the FMA4 bit is in COMMON_CPUID_INDEX_80000001 and
275 FMA4 requires AVX, determine if FMA4 is usable here. */
276 if (CPU_FEATURES_CPU_P (cpu_features
, FMA4
))
277 cpu_features
->feature
[index_arch_FMA4_Usable
]
278 |= bit_arch_FMA4_Usable
;
283 #if index_arch_Fast_Unaligned_Load != index_arch_Fast_Copy_Backward
284 # error index_arch_Fast_Unaligned_Load != index_arch_Fast_Copy_Backward
287 if (model
>= 0x60 && model
<= 0x7f)
288 cpu_features
->feature
[index_arch_Fast_Unaligned_Load
]
289 |= (bit_arch_Fast_Unaligned_Load
290 | bit_arch_Fast_Copy_Backward
);
295 kind
= arch_kind_other
;
296 get_common_indeces (cpu_features
, NULL
, NULL
, NULL
, NULL
);
299 /* Support i586 if CX8 is available. */
300 if (CPU_FEATURES_CPU_P (cpu_features
, CX8
))
301 cpu_features
->feature
[index_arch_I586
] |= bit_arch_I586
;
303 /* Support i686 if CMOV is available. */
304 if (CPU_FEATURES_CPU_P (cpu_features
, CMOV
))
305 cpu_features
->feature
[index_arch_I686
] |= bit_arch_I686
;
311 cpu_features
->family
= family
;
312 cpu_features
->model
= model
;
313 cpu_features
->kind
= kind
;
315 /* Reuse dl_platform, dl_hwcap and dl_hwcap_mask for x86. */
316 GLRO(dl_platform
) = NULL
;
318 #if !HAVE_TUNABLES && defined SHARED
319 /* The glibc.tune.hwcap_mask tunable is initialized already, so no need to do
321 GLRO(dl_hwcap_mask
) = HWCAP_IMPORTANT
;
325 if (cpu_features
->kind
== arch_kind_intel
)
327 if (CPU_FEATURES_ARCH_P (cpu_features
, AVX512F_Usable
)
328 && CPU_FEATURES_CPU_P (cpu_features
, AVX512CD
))
330 if (CPU_FEATURES_CPU_P (cpu_features
, AVX512ER
))
332 if (CPU_FEATURES_CPU_P (cpu_features
, AVX512PF
))
333 GLRO(dl_platform
) = "xeon_phi";
337 if (CPU_FEATURES_CPU_P (cpu_features
, AVX512BW
)
338 && CPU_FEATURES_CPU_P (cpu_features
, AVX512DQ
)
339 && CPU_FEATURES_CPU_P (cpu_features
, AVX512VL
))
340 GLRO(dl_hwcap
) |= HWCAP_X86_AVX512_1
;
344 if (GLRO(dl_platform
) == NULL
345 && CPU_FEATURES_ARCH_P (cpu_features
, AVX2_Usable
)
346 && CPU_FEATURES_ARCH_P (cpu_features
, FMA_Usable
)
347 && CPU_FEATURES_CPU_P (cpu_features
, BMI1
)
348 && CPU_FEATURES_CPU_P (cpu_features
, BMI2
)
349 && CPU_FEATURES_CPU_P (cpu_features
, LZCNT
)
350 && CPU_FEATURES_CPU_P (cpu_features
, MOVBE
)
351 && CPU_FEATURES_CPU_P (cpu_features
, POPCNT
))
352 GLRO(dl_platform
) = "haswell";
355 if (CPU_FEATURES_CPU_P (cpu_features
, SSE2
))
356 GLRO(dl_hwcap
) |= HWCAP_X86_SSE2
;
358 if (CPU_FEATURES_ARCH_P (cpu_features
, I686
))
359 GLRO(dl_platform
) = "i686";
360 else if (CPU_FEATURES_ARCH_P (cpu_features
, I586
))
361 GLRO(dl_platform
) = "i586";