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/>. */
20 #include <cpu-features.h>
23 get_common_indeces (struct cpu_features
*cpu_features
,
24 unsigned int *family
, unsigned int *model
)
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;
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
;
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;
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
;
76 /* BSF is slow on Atom. */
77 cpu_features
->feature
[index_Slow_BSF
] |= bit_Slow_BSF
;
85 /* Unaligned load versions are faster than SSSE3
87 #if index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
88 # error index_Fast_Unaligned_Load != index_Prefer_PMINUB_for_stringop
90 #if index_Fast_Unaligned_Load != index_Slow_SSE4_2
91 # error index_Fast_Unaligned_Load != index_Slow_SSE4_2
93 cpu_features
->feature
[index_Fast_Unaligned_Load
]
94 |= (bit_Fast_Unaligned_Load
95 | bit_Prefer_PMINUB_for_stringop
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)
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
117 #if index_Fast_Rep_String != index_Fast_Unaligned_Load
118 # error index_Fast_Rep_String != index_Fast_Unaligned_Load
120 #if index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
121 # error index_Fast_Rep_String != index_Prefer_PMINUB_for_stringop
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
);
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
;
142 __cpuid (0x80000000, eax
, ebx
, ecx
, edx
);
143 if (eax
>= 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
);
151 kind
= arch_kind_other
;
153 if (cpu_features
->max_cpuid
>= 7)
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
))
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
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__
211 cpu_features
->family
= family
;
212 cpu_features
->model
= model
;
213 cpu_features
->kind
= kind
;