Check if cpuid is available in init_cpu_features
[glibc.git] / sysdeps / x86 / cpu-features.c
blob582a8f5f7980edbf9a31b0d04a6194e46e6b63af
1 /* Initialize CPU feature data.
2 This file is part of the GNU C Library.
3 Copyright (C) 2008-2015 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 inline void
23 get_common_indeces (struct cpu_features *cpu_features,
24 unsigned int *family, unsigned int *model)
26 unsigned int eax;
27 __cpuid (1, eax, cpu_features->cpuid[COMMON_CPUID_INDEX_1].ebx,
28 cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx,
29 cpu_features->cpuid[COMMON_CPUID_INDEX_1].edx);
30 GLRO(dl_x86_cpu_features).cpuid[COMMON_CPUID_INDEX_1].eax = eax;
31 *family = (eax >> 8) & 0x0f;
32 *model = (eax >> 4) & 0x0f;
35 static inline void
36 init_cpu_features (struct cpu_features *cpu_features)
38 unsigned int ebx, ecx, edx;
39 unsigned int family = 0;
40 unsigned int model = 0;
41 enum cpu_features_kind kind;
43 #if !defined __i586__ && !defined __i686__ && !defined __x86_64__
44 if (__get_cpuid_max (0, 0) == 0)
46 kind = arch_kind_other;
47 goto no_cpuid;
49 #endif
51 __cpuid (0, cpu_features->max_cpuid, ebx, ecx, edx);
53 /* This spells out "GenuineIntel". */
54 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
56 kind = arch_kind_intel;
58 get_common_indeces (cpu_features, &family, &model);
60 unsigned int eax = cpu_features->cpuid[COMMON_CPUID_INDEX_1].eax;
61 unsigned int extended_family = (eax >> 20) & 0xff;
62 unsigned int extended_model = (eax >> 12) & 0xf0;
63 if (family == 0x0f)
65 family += extended_family;
66 model += extended_model;
68 else if (family == 0x06)
70 ecx = cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx;
71 model += extended_model;
72 switch (model)
74 case 0x1c:
75 case 0x26:
76 /* BSF is slow on Atom. */
77 cpu_features->feature[index_Slow_BSF] |= bit_Slow_BSF;
78 break;
80 case 0x37:
81 case 0x4a:
82 case 0x4d:
83 case 0x5a:
84 case 0x5d:
85 /* Unaligned load versions are faster than SSSE3
86 on Silvermont. */
87 #if index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
88 # error index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
89 #endif
90 #if index_Fast_Unaligned_Load != index_Slow_SSE4_2
91 # error index_Fast_Unaligned_Load != index_Slow_SSE4_2
92 #endif
93 cpu_features->feature[index_Fast_Unaligned_Load]
94 |= (bit_Fast_Unaligned_Load
95 | bit_Prefer_PMINUB_for_stringop
96 | bit_Slow_SSE4_2);
97 break;
99 default:
100 /* Unknown family 0x06 processors. Assuming this is one
101 of Core i3/i5/i7 processors if AVX is available. */
102 if ((ecx & bit_AVX) == 0)
103 break;
105 case 0x1a:
106 case 0x1e:
107 case 0x1f:
108 case 0x25:
109 case 0x2c:
110 case 0x2e:
111 case 0x2f:
112 /* Rep string instructions, copy backward, unaligned loads
113 and pminub are fast on Intel Core i3, i5 and i7. */
114 #if index_Fast_Rep_String != index_Fast_Copy_Backward
115 # error index_Fast_Rep_String != index_Fast_Copy_Backward
116 #endif
117 #if index_Fast_Rep_String != index_Fast_Unaligned_Load
118 # error index_Fast_Rep_String != index_Fast_Unaligned_Load
119 #endif
120 #if index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
121 # error index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
122 #endif
123 cpu_features->feature[index_Fast_Rep_String]
124 |= (bit_Fast_Rep_String
125 | bit_Fast_Copy_Backward
126 | bit_Fast_Unaligned_Load
127 | bit_Prefer_PMINUB_for_stringop);
128 break;
132 /* This spells out "AuthenticAMD". */
133 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
135 kind = arch_kind_amd;
137 get_common_indeces (cpu_features, &family, &model);
139 ecx = cpu_features->cpuid[COMMON_CPUID_INDEX_1].ecx;
141 unsigned int eax;
142 __cpuid (0x80000000, eax, ebx, ecx, edx);
143 if (eax >= 0x80000001)
144 __cpuid (0x80000001,
145 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].eax,
146 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].ebx,
147 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].ecx,
148 cpu_features->cpuid[COMMON_CPUID_INDEX_80000001].edx);
150 else
151 kind = arch_kind_other;
153 if (cpu_features->max_cpuid >= 7)
154 __cpuid_count (7, 0,
155 cpu_features->cpuid[COMMON_CPUID_INDEX_7].eax,
156 cpu_features->cpuid[COMMON_CPUID_INDEX_7].ebx,
157 cpu_features->cpuid[COMMON_CPUID_INDEX_7].ecx,
158 cpu_features->cpuid[COMMON_CPUID_INDEX_7].edx);
160 /* Can we call xgetbv? */
161 if (HAS_CPU_FEATURE (OSXSAVE))
163 unsigned int xcrlow;
164 unsigned int xcrhigh;
165 asm ("xgetbv" : "=a" (xcrlow), "=d" (xcrhigh) : "c" (0));
166 /* Is YMM and XMM state usable? */
167 if ((xcrlow & (bit_YMM_state | bit_XMM_state)) ==
168 (bit_YMM_state | bit_XMM_state))
170 /* Determine if AVX is usable. */
171 if (HAS_CPU_FEATURE (AVX))
172 cpu_features->feature[index_AVX_Usable] |= bit_AVX_Usable;
173 #if index_AVX2_Usable != index_AVX_Fast_Unaligned_Load
174 # error index_AVX2_Usable != index_AVX_Fast_Unaligned_Load
175 #endif
176 /* Determine if AVX2 is usable. Unaligned load with 256-bit
177 AVX registers are faster on processors with AVX2. */
178 if (HAS_CPU_FEATURE (AVX2))
179 cpu_features->feature[index_AVX2_Usable]
180 |= bit_AVX2_Usable | bit_AVX_Fast_Unaligned_Load;
181 /* Check if OPMASK state, upper 256-bit of ZMM0-ZMM15 and
182 ZMM16-ZMM31 state are enabled. */
183 if ((xcrlow & (bit_Opmask_state | bit_ZMM0_15_state
184 | bit_ZMM16_31_state)) ==
185 (bit_Opmask_state | bit_ZMM0_15_state | bit_ZMM16_31_state))
187 /* Determine if AVX512F is usable. */
188 if (HAS_CPU_FEATURE (AVX512F))
190 cpu_features->feature[index_AVX512F_Usable]
191 |= bit_AVX512F_Usable;
192 /* Determine if AVX512DQ is usable. */
193 if (HAS_CPU_FEATURE (AVX512DQ))
194 cpu_features->feature[index_AVX512DQ_Usable]
195 |= bit_AVX512DQ_Usable;
198 /* Determine if FMA is usable. */
199 if (HAS_CPU_FEATURE (FMA))
200 cpu_features->feature[index_FMA_Usable] |= bit_FMA_Usable;
201 /* Determine if FMA4 is usable. */
202 if (HAS_CPU_FEATURE (FMA4))
203 cpu_features->feature[index_FMA4_Usable] |= bit_FMA4_Usable;
207 #if !defined __i586__ && !defined __i686__ && !defined __x86_64__
208 no_cpuid:
209 #endif
211 cpu_features->family = family;
212 cpu_features->model = model;
213 cpu_features->kind = kind;