1 /* Data structure for x86 CPU features.
2    This file is part of the GNU C Library.
3    Copyright (C) 2008-2022 Free Software Foundation, Inc.
4 
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.
9 
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.
14 
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    <https://www.gnu.org/licenses/>.  */
18 
19 #ifndef _SYS_PLATFORM_X86_H
20 #define _SYS_PLATFORM_X86_H
21 
22 #include <features.h>
23 #include <stdbool.h>
24 #include <bits/platform/x86.h>
25 
26 __BEGIN_DECLS
27 
28 /* Get a pointer to the CPU feature structure.  */
29 extern const struct cpuid_feature *__x86_get_cpuid_feature_leaf (unsigned int)
30      __attribute__ ((pure));
31 
32 static __inline__ _Bool
x86_cpu_present(unsigned int __index)33 x86_cpu_present (unsigned int __index)
34 {
35   const struct cpuid_feature *__ptr = __x86_get_cpuid_feature_leaf
36     (__index / (8 * sizeof (unsigned int) * 4));
37   unsigned int __reg
38      = __index & (8 * sizeof (unsigned int) * 4 - 1);
39   unsigned int __bit = __reg & (8 * sizeof (unsigned int) - 1);
40   __reg /= 8 * sizeof (unsigned int);
41 
42   return __ptr->cpuid_array[__reg] & (1 << __bit);
43 }
44 
45 static __inline__ _Bool
x86_cpu_active(unsigned int __index)46 x86_cpu_active (unsigned int __index)
47 {
48   const struct cpuid_feature *__ptr = __x86_get_cpuid_feature_leaf
49     (__index / (8 * sizeof (unsigned int) * 4));
50   unsigned int __reg
51      = __index & (8 * sizeof (unsigned int) * 4 - 1);
52   unsigned int __bit = __reg & (8 * sizeof (unsigned int) - 1);
53   __reg /= 8 * sizeof (unsigned int);
54 
55   return __ptr->active_array[__reg] & (1 << __bit);
56 }
57 
58 /* CPU_FEATURE_PRESENT evaluates to true if CPU supports the feature.  */
59 #define CPU_FEATURE_PRESENT(name) x86_cpu_present (x86_cpu_##name)
60 /* CPU_FEATURE_ACTIVE evaluates to true if the feature is active.  */
61 #define CPU_FEATURE_ACTIVE(name) x86_cpu_active (x86_cpu_##name)
62 
63 __END_DECLS
64 
65 #endif  /* _SYS_PLATFORM_X86_H */
66