1 /* Initialize CPU feature data.
2 This file is part of the GNU C Library.
3 Copyright (C) 2008-2015 Free Software Foundation, Inc.
4 Contributed by Ulrich Drepper <drepper@redhat.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
22 #include "init-arch.h"
25 struct cpu_features __cpu_features attribute_hidden;
29 get_common_indeces (unsigned int *family, unsigned int *model)
31 __cpuid (1, __cpu_features.cpuid[COMMON_CPUID_INDEX_1].eax,
32 __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ebx,
33 __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ecx,
34 __cpu_features.cpuid[COMMON_CPUID_INDEX_1].edx);
36 unsigned int eax = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].eax;
37 *family = (eax >> 8) & 0x0f;
38 *model = (eax >> 4) & 0x0f;
43 __init_cpu_features (void)
48 unsigned int family = 0;
49 unsigned int model = 0;
50 enum cpu_features_kind kind;
52 __cpuid (0, __cpu_features.max_cpuid, ebx, ecx, edx);
54 /* This spells out "GenuineIntel". */
55 if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
57 kind = arch_kind_intel;
59 get_common_indeces (&family, &model);
61 unsigned int eax = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].eax;
62 unsigned int extended_family = (eax >> 20) & 0xff;
63 unsigned int extended_model = (eax >> 12) & 0xf0;
66 family += extended_family;
67 model += extended_model;
69 else if (family == 0x06)
71 ecx = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ecx;
72 model += extended_model;
77 /* BSF is slow on Atom. */
78 __cpu_features.feature[index_Slow_BSF] |= bit_Slow_BSF;
86 /* Unaligned load versions are faster than SSSE3
88 #if index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
89 # error index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
91 #if index_Fast_Unaligned_Load != index_Slow_SSE4_2
92 # error index_Fast_Unaligned_Load != index_Slow_SSE4_2
94 __cpu_features.feature[index_Fast_Unaligned_Load]
95 |= (bit_Fast_Unaligned_Load
96 | bit_Prefer_PMINUB_for_stringop
101 /* Unknown family 0x06 processors. Assuming this is one
102 of Core i3/i5/i7 processors if AVX is available. */
103 if ((ecx & bit_AVX) == 0)
113 /* Rep string instructions, copy backward, unaligned loads
114 and pminub are fast on Intel Core i3, i5 and i7. */
115 #if index_Fast_Rep_String != index_Fast_Copy_Backward
116 # error index_Fast_Rep_String != index_Fast_Copy_Backward
118 #if index_Fast_Rep_String != index_Fast_Unaligned_Load
119 # error index_Fast_Rep_String != index_Fast_Unaligned_Load
121 #if index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
122 # error index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
124 __cpu_features.feature[index_Fast_Rep_String]
125 |= (bit_Fast_Rep_String
126 | bit_Fast_Copy_Backward
127 | bit_Fast_Unaligned_Load
128 | bit_Prefer_PMINUB_for_stringop);
133 /* This spells out "AuthenticAMD". */
134 else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
136 kind = arch_kind_amd;
138 get_common_indeces (&family, &model);
140 ecx = __cpu_features.cpuid[COMMON_CPUID_INDEX_1].ecx;
143 __cpuid (0x80000000, eax, ebx, ecx, edx);
144 if (eax >= 0x80000001)
146 __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].eax,
147 __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].ebx,
148 __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].ecx,
149 __cpu_features.cpuid[COMMON_CPUID_INDEX_80000001].edx);
152 kind = arch_kind_other;
154 if (__cpu_features.max_cpuid >= 7)
156 __cpu_features.cpuid[COMMON_CPUID_INDEX_7].eax,
157 __cpu_features.cpuid[COMMON_CPUID_INDEX_7].ebx,
158 __cpu_features.cpuid[COMMON_CPUID_INDEX_7].ecx,
159 __cpu_features.cpuid[COMMON_CPUID_INDEX_7].edx);
161 /* Can we call xgetbv? */
165 unsigned int xcrhigh;
166 asm ("xgetbv" : "=a" (xcrlow), "=d" (xcrhigh) : "c" (0));
167 /* Is YMM and XMM state usable? */
168 if ((xcrlow & (bit_YMM_state | bit_XMM_state)) ==
169 (bit_YMM_state | bit_XMM_state))
171 /* Determine if AVX is usable. */
173 __cpu_features.feature[index_AVX_Usable] |= bit_AVX_Usable;
174 /* Determine if AVX2 is usable. */
176 __cpu_features.feature[index_AVX2_Usable] |= bit_AVX2_Usable;
177 /* Determine if FMA is usable. */
179 __cpu_features.feature[index_FMA_Usable] |= bit_FMA_Usable;
180 /* Determine if FMA4 is usable. */
182 __cpu_features.feature[index_FMA4_Usable] |= bit_FMA4_Usable;
186 __cpu_features.family = family;
187 __cpu_features.model = model;
188 atomic_write_barrier ();
189 __cpu_features.kind = kind;
192 #undef __get_cpu_features
194 const struct cpu_features *
195 __get_cpu_features (void)
197 if (__cpu_features.kind == arch_kind_unknown)
198 __init_cpu_features ();
200 return &__cpu_features;