Make __get_cpu_features hidden
[glibc.git] / sysdeps / x86_64 / multiarch / init-arch.h
blobcba10cf3f1ed5fe03c28ff2c908b036deb280324
1 /* This file is part of the GNU C Library.
2 Copyright (C) 2008-2013 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #define bit_Fast_Rep_String (1 << 0)
19 #define bit_Fast_Copy_Backward (1 << 1)
20 #define bit_Slow_BSF (1 << 2)
21 #define bit_Prefer_SSE_for_memop (1 << 3)
22 #define bit_Fast_Unaligned_Load (1 << 4)
23 #define bit_Prefer_PMINUB_for_stringop (1 << 5)
24 #define bit_AVX_Usable (1 << 6)
25 #define bit_FMA_Usable (1 << 7)
26 #define bit_FMA4_Usable (1 << 8)
28 /* CPUID Feature flags. */
30 /* COMMON_CPUID_INDEX_1. */
31 #define bit_SSE2 (1 << 26)
32 #define bit_SSSE3 (1 << 9)
33 #define bit_SSE4_1 (1 << 19)
34 #define bit_SSE4_2 (1 << 20)
35 #define bit_OSXSAVE (1 << 27)
36 #define bit_AVX (1 << 28)
37 #define bit_POPCOUNT (1 << 23)
38 #define bit_FMA (1 << 12)
39 #define bit_FMA4 (1 << 16)
41 /* COMMON_CPUID_INDEX_7. */
42 #define bit_RTM (1 << 11)
44 /* XCR0 Feature flags. */
45 #define bit_XMM_state (1 << 1)
46 #define bit_YMM_state (2 << 1)
48 #ifdef __ASSEMBLER__
50 # include <ifunc-defines.h>
52 # define index_SSE2 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_EDX_OFFSET
53 # define index_SSSE3 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
54 # define index_SSE4_1 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
55 # define index_SSE4_2 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
56 # define index_AVX COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
58 # define index_Fast_Rep_String FEATURE_INDEX_1*FEATURE_SIZE
59 # define index_Fast_Copy_Backward FEATURE_INDEX_1*FEATURE_SIZE
60 # define index_Slow_BSF FEATURE_INDEX_1*FEATURE_SIZE
61 # define index_Prefer_SSE_for_memop FEATURE_INDEX_1*FEATURE_SIZE
62 # define index_Fast_Unaligned_Load FEATURE_INDEX_1*FEATURE_SIZE
63 # define index_Prefer_PMINUB_for_stringop FEATURE_INDEX_1*FEATURE_SIZE
64 # define index_AVX_Usable FEATURE_INDEX_1*FEATURE_SIZE
65 # define index_FMA_Usable FEATURE_INDEX_1*FEATURE_SIZE
66 # define index_FMA4_Usable FEATURE_INDEX_1*FEATURE_SIZE
68 #else /* __ASSEMBLER__ */
70 # include <sys/param.h>
72 enum
74 COMMON_CPUID_INDEX_1 = 0,
75 COMMON_CPUID_INDEX_7,
76 COMMON_CPUID_INDEX_80000001, /* for AMD */
77 /* Keep the following line at the end. */
78 COMMON_CPUID_INDEX_MAX
81 enum
83 FEATURE_INDEX_1 = 0,
84 /* Keep the following line at the end. */
85 FEATURE_INDEX_MAX
88 extern struct cpu_features
90 enum cpu_features_kind
92 arch_kind_unknown = 0,
93 arch_kind_intel,
94 arch_kind_amd,
95 arch_kind_other
96 } kind;
97 int max_cpuid;
98 struct cpuid_registers
100 unsigned int eax;
101 unsigned int ebx;
102 unsigned int ecx;
103 unsigned int edx;
104 } cpuid[COMMON_CPUID_INDEX_MAX];
105 unsigned int family;
106 unsigned int model;
107 unsigned int feature[FEATURE_INDEX_MAX];
108 } __cpu_features attribute_hidden;
111 extern void __init_cpu_features (void) attribute_hidden;
113 /* Implicitly call __init_cpu_features before accessing the CPU features
114 structure. */
116 extern __always_inline attribute_hidden
117 const struct cpu_features *
118 __get_cpu_features (void)
120 if (__cpu_features.kind == arch_kind_unknown)
121 __init_cpu_features ();
122 return &__cpu_features;
125 # define HAS_CPU_FEATURE(idx, reg, bit) \
126 ((__get_cpu_features ()->cpuid[idx].reg & (bit)) != 0)
128 /* Following are the feature tests used throughout libc. */
130 /* CPUID_* evaluates to true if the feature flag is enabled.
131 We always use &__cpu_features because the HAS_CPUID_* macros
132 are called only within __init_cpu_features, where we can't
133 call __get_cpu_features without infinite recursion. */
134 # define HAS_CPUID_FLAG(idx, reg, bit) \
135 (((&__cpu_features)->cpuid[idx].reg & (bit)) != 0)
137 # define CPUID_OSXSAVE \
138 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_OSXSAVE)
139 # define CPUID_AVX \
140 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_AVX)
141 # define CPUID_FMA \
142 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_FMA)
143 # define CPUID_FMA4 \
144 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_80000001, ecx, bit_FMA4)
145 # define CPUID_RTM \
146 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_7, ebx, bit_RTM)
148 /* HAS_* evaluates to true if we may use the feature at runtime. */
149 # define HAS_SSE2 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, edx, bit_SSE2)
150 # define HAS_POPCOUNT HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_POPCOUNT)
151 # define HAS_SSSE3 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSSE3)
152 # define HAS_SSE4_1 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSE4_1)
153 # define HAS_SSE4_2 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSE4_2)
154 # define HAS_RTM HAS_CPU_FEATURE (COMMON_CPUID_INDEX_7, ebx, bit_RTM)
156 # define index_Fast_Rep_String FEATURE_INDEX_1
157 # define index_Fast_Copy_Backward FEATURE_INDEX_1
158 # define index_Slow_BSF FEATURE_INDEX_1
159 # define index_Prefer_SSE_for_memop FEATURE_INDEX_1
160 # define index_Fast_Unaligned_Load FEATURE_INDEX_1
161 # define index_AVX_Usable FEATURE_INDEX_1
162 # define index_FMA_Usable FEATURE_INDEX_1
163 # define index_FMA4_Usable FEATURE_INDEX_1
165 # define HAS_ARCH_FEATURE(name) \
166 ((__get_cpu_features ()->feature[index_##name] & (bit_##name)) != 0)
168 # define HAS_FAST_REP_STRING HAS_ARCH_FEATURE (Fast_Rep_String)
169 # define HAS_FAST_COPY_BACKWARD HAS_ARCH_FEATURE (Fast_Copy_Backward)
170 # define HAS_SLOW_BSF HAS_ARCH_FEATURE (Slow_BSF)
171 # define HAS_PREFER_SSE_FOR_MEMOP HAS_ARCH_FEATURE (Prefer_SSE_for_memop)
172 # define HAS_FAST_UNALIGNED_LOAD HAS_ARCH_FEATURE (Fast_Unaligned_Load)
173 # define HAS_AVX HAS_ARCH_FEATURE (AVX_Usable)
174 # define HAS_FMA HAS_ARCH_FEATURE (FMA_Usable)
175 # define HAS_FMA4 HAS_ARCH_FEATURE (FMA4_Usable)
177 #endif /* __ASSEMBLER__ */