Remove Fast_Copy_Backward from Intel Core processors
[glibc.git] / sysdeps / x86 / cpu-features.c
blob963b84591687e08bea6917077c6bd99954d344a0
1 /* Initialize CPU feature data.
2 This file is part of the GNU C Library.
3 Copyright (C) 2008-2016 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/>. */
19 #include <cpuid.h>
20 #include <cpu-features.h>
22 static void
23 get_common_indeces (struct cpu_features *cpu_features,
24 unsigned int *family, unsigned int *model,
25 unsigned int *extended_model)
27 if (family)
29 unsigned int eax;
30 __cpuid (1, eax, cpu_features->cpuid[COMMON_CPUID_INDEX_1].ebx,
31 cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx,
32 cpu_features->cpuid[COMMON_CPUID_INDEX_1].edx);
33 cpu_features->cpuid[COMMON_CPUID_INDEX_1].eax = eax;
34 *family = (eax >> 8) & 0x0f;
35 *model = (eax >> 4) & 0x0f;
36 *extended_model = (eax >> 12) & 0xf0;
37 if (*family == 0x0f)
39 *family += (eax >> 20) & 0xff;
40 *model += *extended_model;
44 if (cpu_features->max_cpuid >= 7)
45 __cpuid_count (7, 0,
46 cpu_features->cpuid[COMMON_CPUID_INDEX_7].eax,
47 cpu_features->cpuid[COMMON_CPUID_INDEX_7].ebx,
48 cpu_features->cpuid[COMMON_CPUID_INDEX_7].ecx,
49 cpu_features->cpuid[COMMON_CPUID_INDEX_7].edx);
51 /* Can we call xgetbv? */
52 if (CPU_FEATURES_CPU_P (cpu_features, OSXSAVE))
54 unsigned int xcrlow;
55 unsigned int xcrhigh;
56 asm ("xgetbv" : "=a" (xcrlow), "=d" (xcrhigh) : "c" (0));
57 /* Is YMM and XMM state usable? */
58 if ((xcrlow & (bit_YMM_state | bit_XMM_state)) ==
59 (bit_YMM_state | bit_XMM_state))
61 /* Determine if AVX is usable. */
62 if (CPU_FEATURES_CPU_P (cpu_features, AVX))
63 cpu_features->feature[index_arch_AVX_Usable]
64 |= bit_arch_AVX_Usable;
65 /* Determine if AVX2 is usable. */
66 if (CPU_FEATURES_CPU_P (cpu_features, AVX2))
67 cpu_features->feature[index_arch_AVX2_Usable]
68 |= bit_arch_AVX2_Usable;
69 /* Check if OPMASK state, upper 256-bit of ZMM0-ZMM15 and
70 ZMM16-ZMM31 state are enabled. */
71 if ((xcrlow & (bit_Opmask_state | bit_ZMM0_15_state
72 | bit_ZMM16_31_state)) ==
73 (bit_Opmask_state | bit_ZMM0_15_state | bit_ZMM16_31_state))
75 /* Determine if AVX512F is usable. */
76 if (CPU_FEATURES_CPU_P (cpu_features, AVX512F))
78 cpu_features->feature[index_arch_AVX512F_Usable]
79 |= bit_arch_AVX512F_Usable;
80 /* Determine if AVX512DQ is usable. */
81 if (CPU_FEATURES_CPU_P (cpu_features, AVX512DQ))
82 cpu_features->feature[index_arch_AVX512DQ_Usable]
83 |= bit_arch_AVX512DQ_Usable;
86 /* Determine if FMA is usable. */
87 if (CPU_FEATURES_CPU_P (cpu_features, FMA))
88 cpu_features->feature[index_arch_FMA_Usable]
89 |= bit_arch_FMA_Usable;
90 /* Determine if FMA4 is usable. */
91 if (CPU_FEATURES_CPU_P (cpu_features, FMA4))
92 cpu_features->feature[index_arch_FMA4_Usable]
93 |= bit_arch_FMA4_Usable;
98 static inline void
99 init_cpu_features (struct cpu_features *cpu_features)
101 unsigned int ebx, ecx, edx;
102 unsigned int family = 0;
103 unsigned int model = 0;
104 enum cpu_features_kind kind;
106 #if !HAS_CPUID
107 if (__get_cpuid_max (0, 0) == 0)
109 kind = arch_kind_other;
110 goto no_cpuid;
112 #endif
114 __cpuid (0, cpu_features->max_cpuid, ebx, ecx, edx);
116 /* This spells out "GenuineIntel". */
117 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
119 unsigned int extended_model;
121 kind = arch_kind_intel;
123 get_common_indeces (cpu_features, &family, &model, &extended_model);
125 if (family == 0x06)
127 ecx = cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx;
128 model += extended_model;
129 switch (model)
131 case 0x1c:
132 case 0x26:
133 /* BSF is slow on Atom. */
134 cpu_features->feature[index_arch_Slow_BSF]
135 |= bit_arch_Slow_BSF;
136 break;
138 case 0x57:
139 /* Knights Landing. Enable Silvermont optimizations. */
140 cpu_features->feature[index_arch_Prefer_No_VZEROUPPER]
141 |= bit_arch_Prefer_No_VZEROUPPER;
143 case 0x37:
144 case 0x4a:
145 case 0x4d:
146 case 0x5a:
147 case 0x5d:
148 /* Unaligned load versions are faster than SSSE3
149 on Silvermont. */
150 #if index_arch_Fast_Unaligned_Load != index_arch_Prefer_PMINUB_for_stringop
151 # error index_arch_Fast_Unaligned_Load != index_arch_Prefer_PMINUB_for_stringop
152 #endif
153 #if index_arch_Fast_Unaligned_Load != index_arch_Slow_SSE4_2
154 # error index_arch_Fast_Unaligned_Load != index_arch_Slow_SSE4_2
155 #endif
156 #if index_arch_Fast_Unaligned_Load != index_arch_Fast_Unaligned_Copy
157 # error index_arch_Fast_Unaligned_Load != index_arch_Fast_Unaligned_Copy
158 #endif
159 cpu_features->feature[index_arch_Fast_Unaligned_Load]
160 |= (bit_arch_Fast_Unaligned_Load
161 | bit_arch_Fast_Unaligned_Copy
162 | bit_arch_Prefer_PMINUB_for_stringop
163 | bit_arch_Slow_SSE4_2);
164 break;
166 default:
167 /* Unknown family 0x06 processors. Assuming this is one
168 of Core i3/i5/i7 processors if AVX is available. */
169 if ((ecx & bit_cpu_AVX) == 0)
170 break;
172 case 0x1a:
173 case 0x1e:
174 case 0x1f:
175 case 0x25:
176 case 0x2c:
177 case 0x2e:
178 case 0x2f:
179 /* Rep string instructions, unaligned load, unaligned copy,
180 and pminub are fast on Intel Core i3, i5 and i7. */
181 #if index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Load
182 # error index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Load
183 #endif
184 #if index_arch_Fast_Rep_String != index_arch_Prefer_PMINUB_for_stringop
185 # error index_arch_Fast_Rep_String != index_arch_Prefer_PMINUB_for_stringop
186 #endif
187 #if index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Copy
188 # error index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Copy
189 #endif
190 cpu_features->feature[index_arch_Fast_Rep_String]
191 |= (bit_arch_Fast_Rep_String
192 | bit_arch_Fast_Unaligned_Load
193 | bit_arch_Fast_Unaligned_Copy
194 | bit_arch_Prefer_PMINUB_for_stringop);
195 break;
199 /* Unaligned load with 256-bit AVX registers are faster on
200 Intel processors with AVX2. */
201 if (CPU_FEATURES_ARCH_P (cpu_features, AVX2_Usable))
202 cpu_features->feature[index_arch_AVX_Fast_Unaligned_Load]
203 |= bit_arch_AVX_Fast_Unaligned_Load;
205 /* This spells out "AuthenticAMD". */
206 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
208 unsigned int extended_model;
210 kind = arch_kind_amd;
212 get_common_indeces (cpu_features, &family, &model, &extended_model);
214 ecx = cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx;
216 unsigned int eax;
217 __cpuid (0x80000000, eax, ebx, ecx, edx);
218 if (eax >= 0x80000001)
219 __cpuid (0x80000001,
220 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].eax,
221 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].ebx,
222 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].ecx,
223 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].edx);
225 if (family == 0x15)
227 #if index_arch_Fast_Unaligned_Load != index_arch_Fast_Copy_Backward
228 # error index_arch_Fast_Unaligned_Load != index_arch_Fast_Copy_Backward
229 #endif
230 /* "Excavator" */
231 if (model >= 0x60 && model <= 0x7f)
232 cpu_features->feature[index_arch_Fast_Unaligned_Load]
233 |= (bit_arch_Fast_Unaligned_Load
234 | bit_arch_Fast_Copy_Backward);
237 else
239 kind = arch_kind_other;
240 get_common_indeces (cpu_features, NULL, NULL, NULL);
243 /* Support i586 if CX8 is available. */
244 if (CPU_FEATURES_CPU_P (cpu_features, CX8))
245 cpu_features->feature[index_arch_I586] |= bit_arch_I586;
247 /* Support i686 if CMOV is available. */
248 if (CPU_FEATURES_CPU_P (cpu_features, CMOV))
249 cpu_features->feature[index_arch_I686] |= bit_arch_I686;
251 #if !HAS_CPUID
252 no_cpuid:
253 #endif
255 cpu_features->family = family;
256 cpu_features->model = model;
257 cpu_features->kind = kind;