Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / x86_64 / multiarch / init-arch.h
blob55f1c5b34cb973796cb5376cb00036fc3d9ae731
1 /* This file is part of the GNU C Library.
2 Copyright (C) 2008-2015 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_Fast_Unaligned_Load (1 << 4)
22 #define bit_Prefer_PMINUB_for_stringop (1 << 5)
23 #define bit_AVX_Usable (1 << 6)
24 #define bit_FMA_Usable (1 << 7)
25 #define bit_FMA4_Usable (1 << 8)
26 #define bit_Slow_SSE4_2 (1 << 9)
27 #define bit_AVX2_Usable (1 << 10)
29 /* CPUID Feature flags. */
31 /* COMMON_CPUID_INDEX_1. */
32 #define bit_SSE2 (1 << 26)
33 #define bit_SSSE3 (1 << 9)
34 #define bit_SSE4_1 (1 << 19)
35 #define bit_SSE4_2 (1 << 20)
36 #define bit_OSXSAVE (1 << 27)
37 #define bit_AVX (1 << 28)
38 #define bit_POPCOUNT (1 << 23)
39 #define bit_FMA (1 << 12)
40 #define bit_FMA4 (1 << 16)
42 /* COMMON_CPUID_INDEX_7. */
43 #define bit_RTM (1 << 11)
44 #define bit_AVX2 (1 << 5)
46 /* XCR0 Feature flags. */
47 #define bit_XMM_state (1 << 1)
48 #define bit_YMM_state (2 << 1)
50 /* The integer bit array index for the first set of internal feature bits. */
51 # define FEATURE_INDEX_1 0
53 /* The current maximum size of the feature integer bit array. */
54 # define FEATURE_INDEX_MAX 1
56 #ifdef __ASSEMBLER__
58 # include <ifunc-defines.h>
60 # define index_SSE2 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_EDX_OFFSET
61 # define index_SSSE3 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
62 # define index_SSE4_1 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
63 # define index_SSE4_2 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
64 # define index_AVX COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
65 # define index_AVX2 COMMON_CPUID_INDEX_7*CPUID_SIZE+CPUID_EBX_OFFSET
67 # define index_Fast_Rep_String FEATURE_INDEX_1*FEATURE_SIZE
68 # define index_Fast_Copy_Backward FEATURE_INDEX_1*FEATURE_SIZE
69 # define index_Slow_BSF FEATURE_INDEX_1*FEATURE_SIZE
70 # define index_Fast_Unaligned_Load FEATURE_INDEX_1*FEATURE_SIZE
71 # define index_Prefer_PMINUB_for_stringop FEATURE_INDEX_1*FEATURE_SIZE
72 # define index_AVX_Usable FEATURE_INDEX_1*FEATURE_SIZE
73 # define index_FMA_Usable FEATURE_INDEX_1*FEATURE_SIZE
74 # define index_FMA4_Usable FEATURE_INDEX_1*FEATURE_SIZE
75 # define index_Slow_SSE4_2 FEATURE_INDEX_1*FEATURE_SIZE
76 # define index_AVX2_Usable FEATURE_INDEX_1*FEATURE_SIZE
78 #else /* __ASSEMBLER__ */
80 # include <sys/param.h>
82 enum
84 COMMON_CPUID_INDEX_1 = 0,
85 COMMON_CPUID_INDEX_7,
86 COMMON_CPUID_INDEX_80000001, /* for AMD */
87 /* Keep the following line at the end. */
88 COMMON_CPUID_INDEX_MAX
91 extern struct cpu_features
93 enum cpu_features_kind
95 arch_kind_unknown = 0,
96 arch_kind_intel,
97 arch_kind_amd,
98 arch_kind_other
99 } kind;
100 int max_cpuid;
101 struct cpuid_registers
103 unsigned int eax;
104 unsigned int ebx;
105 unsigned int ecx;
106 unsigned int edx;
107 } cpuid[COMMON_CPUID_INDEX_MAX];
108 unsigned int family;
109 unsigned int model;
110 unsigned int feature[FEATURE_INDEX_MAX];
111 } __cpu_features attribute_hidden;
114 extern void __init_cpu_features (void) attribute_hidden;
115 # define INIT_ARCH() \
116 do \
117 if (__cpu_features.kind == arch_kind_unknown) \
118 __init_cpu_features (); \
119 while (0)
121 /* Used from outside libc.so to get access to the CPU features structure. */
122 extern const struct cpu_features *__get_cpu_features (void)
123 __attribute__ ((const));
125 # if IS_IN (libc)
126 # define __get_cpu_features() (&__cpu_features)
127 # endif
129 # define HAS_CPU_FEATURE(idx, reg, bit) \
130 ((__get_cpu_features ()->cpuid[idx].reg & (bit)) != 0)
132 /* Following are the feature tests used throughout libc. */
134 /* CPUID_* evaluates to true if the feature flag is enabled.
135 We always use &__cpu_features because the HAS_CPUID_* macros
136 are called only within __init_cpu_features, where we can't
137 call __get_cpu_features without infinite recursion. */
138 # define HAS_CPUID_FLAG(idx, reg, bit) \
139 (((&__cpu_features)->cpuid[idx].reg & (bit)) != 0)
141 # define CPUID_OSXSAVE \
142 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_OSXSAVE)
143 # define CPUID_AVX \
144 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_AVX)
145 # define CPUID_FMA \
146 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_FMA)
147 # define CPUID_FMA4 \
148 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_80000001, ecx, bit_FMA4)
149 # define CPUID_RTM \
150 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_7, ebx, bit_RTM)
151 # define CPUID_AVX2 \
152 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_7, ebx, bit_AVX2)
154 /* HAS_* evaluates to true if we may use the feature at runtime. */
155 # define HAS_SSE2 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, edx, bit_SSE2)
156 # define HAS_POPCOUNT HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_POPCOUNT)
157 # define HAS_SSSE3 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSSE3)
158 # define HAS_SSE4_1 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSE4_1)
159 # define HAS_SSE4_2 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSE4_2)
160 # define HAS_RTM HAS_CPU_FEATURE (COMMON_CPUID_INDEX_7, ebx, bit_RTM)
162 # define index_Fast_Rep_String FEATURE_INDEX_1
163 # define index_Fast_Copy_Backward FEATURE_INDEX_1
164 # define index_Slow_BSF FEATURE_INDEX_1
165 # define index_Fast_Unaligned_Load FEATURE_INDEX_1
166 # define index_Prefer_PMINUB_for_stringop FEATURE_INDEX_1
167 # define index_AVX_Usable FEATURE_INDEX_1
168 # define index_FMA_Usable FEATURE_INDEX_1
169 # define index_FMA4_Usable FEATURE_INDEX_1
170 # define index_Slow_SSE4_2 FEATURE_INDEX_1
171 # define index_AVX2_Usable FEATURE_INDEX_1
173 # define HAS_ARCH_FEATURE(name) \
174 ((__get_cpu_features ()->feature[index_##name] & (bit_##name)) != 0)
176 # define HAS_FAST_REP_STRING HAS_ARCH_FEATURE (Fast_Rep_String)
177 # define HAS_FAST_COPY_BACKWARD HAS_ARCH_FEATURE (Fast_Copy_Backward)
178 # define HAS_SLOW_BSF HAS_ARCH_FEATURE (Slow_BSF)
179 # define HAS_FAST_UNALIGNED_LOAD HAS_ARCH_FEATURE (Fast_Unaligned_Load)
180 # define HAS_AVX HAS_ARCH_FEATURE (AVX_Usable)
181 # define HAS_AVX2 HAS_ARCH_FEATURE (AVX2_Usable)
182 # define HAS_FMA HAS_ARCH_FEATURE (FMA_Usable)
183 # define HAS_FMA4 HAS_ARCH_FEATURE (FMA4_Usable)
185 #endif /* __ASSEMBLER__ */