1 /* Test CPU feature data against __builtin_cpu_supports.
2    This file is part of the GNU C Library.
3    Copyright (C) 2020-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 #include <sys/platform/x86.h>
20 #include <stdio.h>
21 
22 int
check_supports(int supports,int active,const char * supports_name,const char * name)23 check_supports (int supports, int active, const char *supports_name,
24 		const char *name)
25 {
26   printf ("Checking %s:\n", name);
27   printf ("  %s: %d\n", name, active);
28   printf ("  __builtin_cpu_supports (%s): %d\n",
29 	  supports_name, supports);
30 
31   if ((supports != 0) != (active != 0))
32     {
33       printf (" *** failure ***\n");
34       return 1;
35     }
36 
37   return 0;
38 }
39 
40 #define CHECK_FEATURE_ACTIVE(str, name) \
41   check_supports (__builtin_cpu_supports (#str), \
42 		  CPU_FEATURE_ACTIVE (name), \
43 		  #str, "CPU_FEATURE_ACTIVE (" #name ")");
44 
45 #define CHECK_FEATURE_PRESENT(str, name) \
46   check_supports (__builtin_cpu_supports (#str), \
47 		  CPU_FEATURE_PRESENT (name), \
48 		  #str, "CPU_FEATURE_PRESENT (" #name ")");
49 
50 static int
do_test(int argc,char ** argv)51 do_test (int argc, char **argv)
52 {
53   int fails = 0;
54 
55 #if __GNUC_PREREQ (11, 0)
56   fails += CHECK_FEATURE_ACTIVE (adx, ADX);
57 #endif
58 #if __GNUC_PREREQ (6, 0)
59   fails += CHECK_FEATURE_ACTIVE (aes, AES);
60 #endif
61 #if __GNUC_PREREQ (11, 1)
62   fails += CHECK_FEATURE_ACTIVE (amx-bf16, AMX_BF16);
63   fails += CHECK_FEATURE_ACTIVE (amx-int8, AMX_INT8);
64   fails += CHECK_FEATURE_ACTIVE (amx-tile, AMX_TILE);
65 #endif
66   fails += CHECK_FEATURE_ACTIVE (avx, AVX);
67   fails += CHECK_FEATURE_ACTIVE (avx2, AVX2);
68 #if __GNUC_PREREQ (7, 0)
69   fails += CHECK_FEATURE_ACTIVE (avx5124fmaps, AVX512_4FMAPS);
70   fails += CHECK_FEATURE_ACTIVE (avx5124vnniw, AVX512_4VNNIW);
71 #endif
72 #if __GNUC_PREREQ (10, 0)
73   fails += CHECK_FEATURE_ACTIVE (avx512bf16, AVX512_BF16);
74 #endif
75 #if __GNUC_PREREQ (8, 0)
76   fails += CHECK_FEATURE_ACTIVE (avx512bitalg, AVX512_BITALG);
77 #endif
78 #if __GNUC_PREREQ (6, 0)
79   fails += CHECK_FEATURE_ACTIVE (avx512ifma, AVX512_IFMA);
80   fails += CHECK_FEATURE_ACTIVE (avx512vbmi, AVX512_VBMI);
81 #endif
82 #if __GNUC_PREREQ (8, 0)
83   fails += CHECK_FEATURE_ACTIVE (avx512vbmi2, AVX512_VBMI2);
84   fails += CHECK_FEATURE_ACTIVE (avx512vnni, AVX512_VNNI);
85 #endif
86 #if __GNUC_PREREQ (10, 0)
87   fails += CHECK_FEATURE_ACTIVE (avx512vp2intersect, AVX512_VP2INTERSECT);
88 #endif
89 #if __GNUC_PREREQ (7, 0)
90   fails += CHECK_FEATURE_ACTIVE (avx512vpopcntdq, AVX512_VPOPCNTDQ);
91 #endif
92 #if __GNUC_PREREQ (6, 0)
93   fails += CHECK_FEATURE_ACTIVE (avx512bw, AVX512BW);
94   fails += CHECK_FEATURE_ACTIVE (avx512cd, AVX512CD);
95   fails += CHECK_FEATURE_ACTIVE (avx512er, AVX512ER);
96   fails += CHECK_FEATURE_ACTIVE (avx512dq, AVX512DQ);
97 #endif
98 #if __GNUC_PREREQ (5, 0)
99   fails += CHECK_FEATURE_ACTIVE (avx512f, AVX512F);
100 #endif
101 #if __GNUC_PREREQ (6, 0)
102   fails += CHECK_FEATURE_ACTIVE (avx512pf, AVX512PF);
103   fails += CHECK_FEATURE_ACTIVE (avx512vl, AVX512VL);
104 #endif
105 #if __GNUC_PREREQ (5, 0)
106   fails += CHECK_FEATURE_ACTIVE (bmi, BMI1);
107   fails += CHECK_FEATURE_ACTIVE (bmi2, BMI2);
108 #endif
109 #if __GNUC_PREREQ (11, 0)
110   fails += CHECK_FEATURE_ACTIVE (cldemote, CLDEMOTE);
111   fails += CHECK_FEATURE_ACTIVE (clflushopt, CLFLUSHOPT);
112   fails += CHECK_FEATURE_ACTIVE (clwb, CLWB);
113 #endif
114   fails += CHECK_FEATURE_ACTIVE (cmov, CMOV);
115 #if __GNUC_PREREQ (11, 0)
116   fails += CHECK_FEATURE_ACTIVE (cmpxchg16b, CMPXCHG16B);
117   fails += CHECK_FEATURE_ACTIVE (cmpxchg8b, CX8);
118   fails += CHECK_FEATURE_ACTIVE (enqcmd, ENQCMD);
119   fails += CHECK_FEATURE_ACTIVE (f16c, F16C);
120 #endif
121 #if __GNUC_PREREQ (4, 9)
122   fails += CHECK_FEATURE_ACTIVE (fma, FMA);
123   fails += CHECK_FEATURE_ACTIVE (fma4, FMA4);
124 #endif
125 #if __GNUC_PREREQ (11, 0)
126   fails += CHECK_FEATURE_PRESENT (fsgsbase, FSGSBASE);
127   fails += CHECK_FEATURE_ACTIVE (fxsave, FXSR);
128 #endif
129 #if __GNUC_PREREQ (8, 0)
130   fails += CHECK_FEATURE_ACTIVE (gfni, GFNI);
131 #endif
132 #if __GNUC_PREREQ (11, 0)
133   fails += CHECK_FEATURE_PRESENT (hle, HLE);
134   fails += CHECK_FEATURE_PRESENT (ibt, IBT);
135   fails += CHECK_FEATURE_ACTIVE (lahf_lm, LAHF64_SAHF64);
136   fails += CHECK_FEATURE_PRESENT (lm, LM);
137   fails += CHECK_FEATURE_ACTIVE (lwp, LWP);
138   fails += CHECK_FEATURE_ACTIVE (lzcnt, LZCNT);
139 #endif
140   fails += CHECK_FEATURE_ACTIVE (mmx, MMX);
141 #if __GNUC_PREREQ (11, 0)
142   fails += CHECK_FEATURE_ACTIVE (movbe, MOVBE);
143   fails += CHECK_FEATURE_ACTIVE (movdiri, MOVDIRI);
144   fails += CHECK_FEATURE_ACTIVE (movdir64b, MOVDIR64B);
145   fails += CHECK_FEATURE_ACTIVE (osxsave, OSXSAVE);
146   fails += CHECK_FEATURE_PRESENT (pconfig, PCONFIG);
147   fails += CHECK_FEATURE_ACTIVE (pku, PKU);
148 #endif
149   fails += CHECK_FEATURE_ACTIVE (popcnt, POPCNT);
150 #if __GNUC_PREREQ (11, 0)
151   fails += CHECK_FEATURE_ACTIVE (prefetchwt1, PREFETCHWT1);
152   fails += CHECK_FEATURE_ACTIVE (ptwrite, PTWRITE);
153   fails += CHECK_FEATURE_ACTIVE (rdpid, RDPID);
154   fails += CHECK_FEATURE_ACTIVE (rdrnd, RDRAND);
155   fails += CHECK_FEATURE_ACTIVE (rdseed, RDSEED);
156   fails += CHECK_FEATURE_PRESENT (rtm, RTM);
157   fails += CHECK_FEATURE_ACTIVE (serialize, SERIALIZE);
158   fails += CHECK_FEATURE_ACTIVE (sha, SHA);
159   fails += CHECK_FEATURE_PRESENT (shstk, SHSTK);
160 #endif
161   fails += CHECK_FEATURE_ACTIVE (sse, SSE);
162   fails += CHECK_FEATURE_ACTIVE (sse2, SSE2);
163   fails += CHECK_FEATURE_ACTIVE (sse3, SSE3);
164   fails += CHECK_FEATURE_ACTIVE (sse4.1, SSE4_1);
165   fails += CHECK_FEATURE_ACTIVE (sse4.2, SSE4_2);
166 #if __GNUC_PREREQ (4, 9)
167   fails += CHECK_FEATURE_ACTIVE (sse4a, SSE4A);
168 #endif
169   fails += CHECK_FEATURE_ACTIVE (ssse3, SSSE3);
170 #if __GNUC_PREREQ (11, 0)
171   fails += CHECK_FEATURE_ACTIVE (tbm, TBM);
172   fails += CHECK_FEATURE_ACTIVE (tsxldtrk, TSXLDTRK);
173   fails += CHECK_FEATURE_ACTIVE (vaes, VAES);
174 #endif
175 #if __GNUC_PREREQ (8, 0)
176   fails += CHECK_FEATURE_ACTIVE (vpclmulqdq, VPCLMULQDQ);
177 #endif
178 #if __GNUC_PREREQ (11, 0)
179   fails += CHECK_FEATURE_ACTIVE (waitpkg, WAITPKG);
180   fails += CHECK_FEATURE_ACTIVE (wbnoinvd, WBNOINVD);
181 #endif
182 #if __GNUC_PREREQ (4, 9)
183   fails += CHECK_FEATURE_ACTIVE (xop, XOP);
184 #endif
185 #if __GNUC_PREREQ (11, 0)
186   fails += CHECK_FEATURE_ACTIVE (xsave, XSAVE);
187   fails += CHECK_FEATURE_ACTIVE (xsavec, XSAVEC);
188   fails += CHECK_FEATURE_ACTIVE (xsaveopt, XSAVEOPT);
189   fails += CHECK_FEATURE_PRESENT (xsaves, XSAVES);
190 #endif
191 
192   printf ("%d differences between __builtin_cpu_supports and glibc code.\n",
193 	  fails);
194 
195   return (fails != 0);
196 }
197 
198 #include "../../../test-skeleton.c"
199