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/>. */
20 #include <cpu-features.h>
23 get_common_indeces (struct cpu_features
*cpu_features
,
24 unsigned int *family
, unsigned int *model
,
25 unsigned int *extended_model
)
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;
39 *family
+= (eax
>> 20) & 0xff;
40 *model
+= *extended_model
;
44 if (cpu_features
->max_cpuid
>= 7)
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
))
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
;
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
;
107 if (__get_cpuid_max (0, 0) == 0)
109 kind
= arch_kind_other
;
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
);
127 ecx
= cpu_features
->cpuid
[COMMON_CPUID_INDEX_1
].ecx
;
128 model
+= extended_model
;
133 /* BSF is slow on Atom. */
134 cpu_features
->feature
[index_arch_Slow_BSF
]
135 |= bit_arch_Slow_BSF
;
139 /* Knights Landing. Enable Silvermont optimizations. */
140 cpu_features
->feature
[index_arch_Prefer_No_VZEROUPPER
]
141 |= bit_arch_Prefer_No_VZEROUPPER
;
148 /* Unaligned load versions are faster than SSSE3
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
153 #if index_arch_Fast_Unaligned_Load != index_arch_Slow_SSE4_2
154 # error index_arch_Fast_Unaligned_Load != index_arch_Slow_SSE4_2
156 #if index_arch_Fast_Unaligned_Load != index_arch_Fast_Unaligned_Copy
157 # error index_arch_Fast_Unaligned_Load != index_arch_Fast_Unaligned_Copy
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
);
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)
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
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
187 #if index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Copy
188 # error index_arch_Fast_Rep_String != index_arch_Fast_Unaligned_Copy
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
);
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
;
217 __cpuid (0x80000000, eax
, ebx
, ecx
, edx
);
218 if (eax
>= 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
);
227 #if index_arch_Fast_Unaligned_Load != index_arch_Fast_Copy_Backward
228 # error index_arch_Fast_Unaligned_Load != index_arch_Fast_Copy_Backward
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
);
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
;
255 cpu_features
->family
= family
;
256 cpu_features
->model
= model
;
257 cpu_features
->kind
= kind
;