]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/x86_64/multiarch/init-arch.h
Update copyright notices with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / x86_64 / multiarch / init-arch.h
1 /* This file is part of the GNU C Library.
2 Copyright (C) 2008-2013 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
17
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)
27
28 /* CPUID Feature flags. */
29 #define bit_SSE2 (1 << 26)
30 #define bit_SSSE3 (1 << 9)
31 #define bit_SSE4_1 (1 << 19)
32 #define bit_SSE4_2 (1 << 20)
33 #define bit_OSXSAVE (1 << 27)
34 #define bit_AVX (1 << 28)
35 #define bit_POPCOUNT (1 << 23)
36 #define bit_FMA (1 << 12)
37 #define bit_FMA4 (1 << 16)
38
39 /* XCR0 Feature flags. */
40 #define bit_XMM_state (1 << 1)
41 #define bit_YMM_state (2 << 1)
42
43 #ifdef __ASSEMBLER__
44
45 # include <ifunc-defines.h>
46
47 # define index_SSE2 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_EDX_OFFSET
48 # define index_SSSE3 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
49 # define index_SSE4_1 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
50 # define index_SSE4_2 COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
51 # define index_AVX COMMON_CPUID_INDEX_1*CPUID_SIZE+CPUID_ECX_OFFSET
52
53 # define index_Fast_Rep_String FEATURE_INDEX_1*FEATURE_SIZE
54 # define index_Fast_Copy_Backward FEATURE_INDEX_1*FEATURE_SIZE
55 # define index_Slow_BSF FEATURE_INDEX_1*FEATURE_SIZE
56 # define index_Prefer_SSE_for_memop FEATURE_INDEX_1*FEATURE_SIZE
57 # define index_Fast_Unaligned_Load FEATURE_INDEX_1*FEATURE_SIZE
58 # define index_Prefer_PMINUB_for_stringop FEATURE_INDEX_1*FEATURE_SIZE
59 # define index_AVX_Usable FEATURE_INDEX_1*FEATURE_SIZE
60 # define index_FMA_Usable FEATURE_INDEX_1*FEATURE_SIZE
61 # define index_FMA4_Usable FEATURE_INDEX_1*FEATURE_SIZE
62
63 #else /* __ASSEMBLER__ */
64
65 # include <sys/param.h>
66
67 enum
68 {
69 COMMON_CPUID_INDEX_1 = 0,
70 COMMON_CPUID_INDEX_80000001, /* for AMD */
71 /* Keep the following line at the end. */
72 COMMON_CPUID_INDEX_MAX
73 };
74
75 enum
76 {
77 FEATURE_INDEX_1 = 0,
78 /* Keep the following line at the end. */
79 FEATURE_INDEX_MAX
80 };
81
82 extern struct cpu_features
83 {
84 enum cpu_features_kind
85 {
86 arch_kind_unknown = 0,
87 arch_kind_intel,
88 arch_kind_amd,
89 arch_kind_other
90 } kind;
91 int max_cpuid;
92 struct cpuid_registers
93 {
94 unsigned int eax;
95 unsigned int ebx;
96 unsigned int ecx;
97 unsigned int edx;
98 } cpuid[COMMON_CPUID_INDEX_MAX];
99 unsigned int family;
100 unsigned int model;
101 unsigned int feature[FEATURE_INDEX_MAX];
102 } __cpu_features attribute_hidden;
103
104
105 extern void __init_cpu_features (void) attribute_hidden;
106 # define INIT_ARCH() \
107 do \
108 if (__cpu_features.kind == arch_kind_unknown) \
109 __init_cpu_features (); \
110 while (0)
111
112 /* Used from outside libc.so to get access to the CPU features structure. */
113 extern const struct cpu_features *__get_cpu_features (void)
114 __attribute__ ((const));
115
116 # ifndef NOT_IN_libc
117 # define __get_cpu_features() (&__cpu_features)
118 # endif
119
120 # define HAS_CPU_FEATURE(idx, reg, bit) \
121 ((__get_cpu_features ()->cpuid[idx].reg & (bit)) != 0)
122
123 /* Following are the feature tests used throughout libc. */
124
125 /* CPUID_* evaluates to true if the feature flag is enabled.
126 We always use &__cpu_features because the HAS_CPUID_* macros
127 are called only within __init_cpu_features, where we can't
128 call __get_cpu_features without infinite recursion. */
129 # define HAS_CPUID_FLAG(idx, reg, bit) \
130 (((&__cpu_features)->cpuid[idx].reg & (bit)) != 0)
131
132 # define CPUID_OSXSAVE \
133 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_OSXSAVE)
134 # define CPUID_AVX \
135 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_AVX)
136 # define CPUID_FMA \
137 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_1, ecx, bit_FMA)
138 # define CPUID_FMA4 \
139 HAS_CPUID_FLAG (COMMON_CPUID_INDEX_80000001, ecx, bit_FMA4)
140
141 /* HAS_* evaluates to true if we may use the feature at runtime. */
142 # define HAS_SSE2 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, edx, bit_SSE2)
143 # define HAS_POPCOUNT HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_POPCOUNT)
144 # define HAS_SSSE3 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSSE3)
145 # define HAS_SSE4_1 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSE4_1)
146 # define HAS_SSE4_2 HAS_CPU_FEATURE (COMMON_CPUID_INDEX_1, ecx, bit_SSE4_2)
147
148 # define index_Fast_Rep_String FEATURE_INDEX_1
149 # define index_Fast_Copy_Backward FEATURE_INDEX_1
150 # define index_Slow_BSF FEATURE_INDEX_1
151 # define index_Prefer_SSE_for_memop FEATURE_INDEX_1
152 # define index_Fast_Unaligned_Load FEATURE_INDEX_1
153 # define index_AVX_Usable FEATURE_INDEX_1
154 # define index_FMA_Usable FEATURE_INDEX_1
155 # define index_FMA4_Usable FEATURE_INDEX_1
156
157 # define HAS_ARCH_FEATURE(name) \
158 ((__get_cpu_features ()->feature[index_##name] & (bit_##name)) != 0)
159
160 # define HAS_FAST_REP_STRING HAS_ARCH_FEATURE (Fast_Rep_String)
161 # define HAS_FAST_COPY_BACKWARD HAS_ARCH_FEATURE (Fast_Copy_Backward)
162 # define HAS_SLOW_BSF HAS_ARCH_FEATURE (Slow_BSF)
163 # define HAS_PREFER_SSE_FOR_MEMOP HAS_ARCH_FEATURE (Prefer_SSE_for_memop)
164 # define HAS_FAST_UNALIGNED_LOAD HAS_ARCH_FEATURE (Fast_Unaligned_Load)
165 # define HAS_AVX HAS_ARCH_FEATURE (AVX_Usable)
166 # define HAS_FMA HAS_ARCH_FEATURE (FMA_Usable)
167 # define HAS_FMA4 HAS_ARCH_FEATURE (FMA4_Usable)
168
169 #endif /* __ASSEMBLER__ */