1/* Test <stdbit.h> type-generic macros with compiler __builtin_stdc_* support.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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 <stdbit.h>
20#include <limits.h>
21#include <support/check.h>
22
23#if __glibc_has_builtin (__builtin_stdc_leading_zeros) \
24 && __glibc_has_builtin (__builtin_stdc_leading_ones) \
25 && __glibc_has_builtin (__builtin_stdc_trailing_zeros) \
26 && __glibc_has_builtin (__builtin_stdc_trailing_ones) \
27 && __glibc_has_builtin (__builtin_stdc_first_leading_zero) \
28 && __glibc_has_builtin (__builtin_stdc_first_leading_one) \
29 && __glibc_has_builtin (__builtin_stdc_first_trailing_zero) \
30 && __glibc_has_builtin (__builtin_stdc_first_trailing_one) \
31 && __glibc_has_builtin (__builtin_stdc_count_zeros) \
32 && __glibc_has_builtin (__builtin_stdc_count_ones) \
33 && __glibc_has_builtin (__builtin_stdc_has_single_bit) \
34 && __glibc_has_builtin (__builtin_stdc_bit_width) \
35 && __glibc_has_builtin (__builtin_stdc_bit_floor) \
36 && __glibc_has_builtin (__builtin_stdc_bit_ceil)
37
38# if !defined (BITINT_MAXWIDTH) && defined (__BITINT_MAXWIDTH__)
39# define BITINT_MAXWIDTH __BITINT_MAXWIDTH__
40# endif
41
42typedef unsigned char uc;
43typedef unsigned short us;
44typedef unsigned int ui;
45typedef unsigned long int ul;
46typedef unsigned long long int ull;
47
48# define expr_has_type(e, t) _Generic (e, default : 0, t : 1)
49
50static int
51do_test (void)
52{
53 TEST_COMPARE (stdc_leading_zeros ((uc) 0), CHAR_BIT);
54 TEST_COMPARE (expr_has_type (stdc_leading_zeros ((uc) 0), ui), 1);
55 TEST_COMPARE (stdc_leading_zeros ((us) 0), sizeof (short) * CHAR_BIT);
56 TEST_COMPARE (expr_has_type (stdc_leading_zeros ((us) 0), ui), 1);
57 TEST_COMPARE (stdc_leading_zeros (0U), sizeof (int) * CHAR_BIT);
58 TEST_COMPARE (expr_has_type (stdc_leading_zeros (0U), ui), 1);
59 TEST_COMPARE (stdc_leading_zeros (0UL), sizeof (long int) * CHAR_BIT);
60 TEST_COMPARE (expr_has_type (stdc_leading_zeros (0UL), ui), 1);
61 TEST_COMPARE (stdc_leading_zeros (0ULL), sizeof (long long int) * CHAR_BIT);
62 TEST_COMPARE (expr_has_type (stdc_leading_zeros (0ULL), ui), 1);
63 TEST_COMPARE (stdc_leading_zeros ((uc) ~0U), 0);
64 TEST_COMPARE (stdc_leading_zeros ((us) ~0U), 0);
65 TEST_COMPARE (stdc_leading_zeros (~0U), 0);
66 TEST_COMPARE (stdc_leading_zeros (~0UL), 0);
67 TEST_COMPARE (stdc_leading_zeros (~0ULL), 0);
68 TEST_COMPARE (stdc_leading_zeros ((uc) 3), CHAR_BIT - 2);
69 TEST_COMPARE (stdc_leading_zeros ((us) 9), sizeof (short) * CHAR_BIT - 4);
70 TEST_COMPARE (stdc_leading_zeros (34U), sizeof (int) * CHAR_BIT - 6);
71 TEST_COMPARE (stdc_leading_zeros (130UL), sizeof (long int) * CHAR_BIT - 8);
72 TEST_COMPARE (stdc_leading_zeros (512ULL),
73 sizeof (long long int) * CHAR_BIT - 10);
74 TEST_COMPARE (stdc_leading_ones ((uc) 0), 0);
75 TEST_COMPARE (expr_has_type (stdc_leading_ones ((uc) 0), ui), 1);
76 TEST_COMPARE (stdc_leading_ones ((us) 0), 0);
77 TEST_COMPARE (expr_has_type (stdc_leading_ones ((us) 0), ui), 1);
78 TEST_COMPARE (stdc_leading_ones (0U), 0);
79 TEST_COMPARE (expr_has_type (stdc_leading_ones (0U), ui), 1);
80 TEST_COMPARE (stdc_leading_ones (0UL), 0);
81 TEST_COMPARE (expr_has_type (stdc_leading_ones (0UL), ui), 1);
82 TEST_COMPARE (stdc_leading_ones (0ULL), 0);
83 TEST_COMPARE (expr_has_type (stdc_leading_ones (0ULL), ui), 1);
84 TEST_COMPARE (stdc_leading_ones ((uc) ~0U), CHAR_BIT);
85 TEST_COMPARE (stdc_leading_ones ((us) ~0U), sizeof (short) * CHAR_BIT);
86 TEST_COMPARE (stdc_leading_ones (~0U), sizeof (int) * CHAR_BIT);
87 TEST_COMPARE (stdc_leading_ones (~0UL), sizeof (long int) * CHAR_BIT);
88 TEST_COMPARE (stdc_leading_ones (~0ULL), sizeof (long long int) * CHAR_BIT);
89 TEST_COMPARE (stdc_leading_ones ((uc) ~3), CHAR_BIT - 2);
90 TEST_COMPARE (stdc_leading_ones ((us) ~9), sizeof (short) * CHAR_BIT - 4);
91 TEST_COMPARE (stdc_leading_ones (~34U), sizeof (int) * CHAR_BIT - 6);
92 TEST_COMPARE (stdc_leading_ones (~130UL), sizeof (long int) * CHAR_BIT - 8);
93 TEST_COMPARE (stdc_leading_ones (~512ULL),
94 sizeof (long long int) * CHAR_BIT - 10);
95 TEST_COMPARE (stdc_trailing_zeros ((uc) 0), CHAR_BIT);
96 TEST_COMPARE (expr_has_type (stdc_trailing_zeros ((uc) 0), ui), 1);
97 TEST_COMPARE (stdc_trailing_zeros ((us) 0), sizeof (short) * CHAR_BIT);
98 TEST_COMPARE (expr_has_type (stdc_trailing_zeros ((us) 0), ui), 1);
99 TEST_COMPARE (stdc_trailing_zeros (0U), sizeof (int) * CHAR_BIT);
100 TEST_COMPARE (expr_has_type (stdc_trailing_zeros (0U), ui), 1);
101 TEST_COMPARE (stdc_trailing_zeros (0UL), sizeof (long int) * CHAR_BIT);
102 TEST_COMPARE (expr_has_type (stdc_trailing_zeros (0UL), ui), 1);
103 TEST_COMPARE (stdc_trailing_zeros (0ULL), sizeof (long long int) * CHAR_BIT);
104 TEST_COMPARE (expr_has_type (stdc_trailing_zeros (0ULL), ui), 1);
105 TEST_COMPARE (stdc_trailing_zeros ((uc) ~0U), 0);
106 TEST_COMPARE (stdc_trailing_zeros ((us) ~0U), 0);
107 TEST_COMPARE (stdc_trailing_zeros (~0U), 0);
108 TEST_COMPARE (stdc_trailing_zeros (~0UL), 0);
109 TEST_COMPARE (stdc_trailing_zeros (~0ULL), 0);
110 TEST_COMPARE (stdc_trailing_zeros ((uc) 2), 1);
111 TEST_COMPARE (stdc_trailing_zeros ((us) 24), 3);
112 TEST_COMPARE (stdc_trailing_zeros (32U), 5);
113 TEST_COMPARE (stdc_trailing_zeros (128UL), 7);
114 TEST_COMPARE (stdc_trailing_zeros (512ULL), 9);
115 TEST_COMPARE (stdc_trailing_ones ((uc) 0), 0);
116 TEST_COMPARE (expr_has_type (stdc_trailing_ones ((uc) 0), ui), 1);
117 TEST_COMPARE (stdc_trailing_ones ((us) 0), 0);
118 TEST_COMPARE (expr_has_type (stdc_trailing_ones ((us) 0), ui), 1);
119 TEST_COMPARE (stdc_trailing_ones (0U), 0);
120 TEST_COMPARE (expr_has_type (stdc_trailing_ones (0U), ui), 1);
121 TEST_COMPARE (stdc_trailing_ones (0UL), 0);
122 TEST_COMPARE (expr_has_type (stdc_trailing_ones (0UL), ui), 1);
123 TEST_COMPARE (stdc_trailing_ones (0ULL), 0);
124 TEST_COMPARE (expr_has_type (stdc_trailing_ones (0ULL), ui), 1);
125 TEST_COMPARE (stdc_trailing_ones ((uc) ~0U), CHAR_BIT);
126 TEST_COMPARE (stdc_trailing_ones ((us) ~0U), sizeof (short) * CHAR_BIT);
127 TEST_COMPARE (stdc_trailing_ones (~0U), sizeof (int) * CHAR_BIT);
128 TEST_COMPARE (stdc_trailing_ones (~0UL), sizeof (long int) * CHAR_BIT);
129 TEST_COMPARE (stdc_trailing_ones (~0ULL), sizeof (long long int) * CHAR_BIT);
130 TEST_COMPARE (stdc_trailing_ones ((uc) 5), 1);
131 TEST_COMPARE (stdc_trailing_ones ((us) 15), 4);
132 TEST_COMPARE (stdc_trailing_ones (127U), 7);
133 TEST_COMPARE (stdc_trailing_ones (511UL), 9);
134 TEST_COMPARE (stdc_trailing_ones (~0ULL >> 2),
135 sizeof (long long int) * CHAR_BIT - 2);
136 TEST_COMPARE (stdc_first_leading_zero ((uc) 0), 1);
137 TEST_COMPARE (expr_has_type (stdc_first_leading_zero ((uc) 0), ui), 1);
138 TEST_COMPARE (stdc_first_leading_zero ((us) 0), 1);
139 TEST_COMPARE (expr_has_type (stdc_first_leading_zero ((us) 0), ui), 1);
140 TEST_COMPARE (stdc_first_leading_zero (0U), 1);
141 TEST_COMPARE (expr_has_type (stdc_first_leading_zero (0U), ui), 1);
142 TEST_COMPARE (stdc_first_leading_zero (0UL), 1);
143 TEST_COMPARE (expr_has_type (stdc_first_leading_zero (0UL), ui), 1);
144 TEST_COMPARE (stdc_first_leading_zero (0ULL), 1);
145 TEST_COMPARE (expr_has_type (stdc_first_leading_zero (0ULL), ui), 1);
146 TEST_COMPARE (stdc_first_leading_zero ((uc) ~0U), 0);
147 TEST_COMPARE (stdc_first_leading_zero ((us) ~0U), 0);
148 TEST_COMPARE (stdc_first_leading_zero (~0U), 0);
149 TEST_COMPARE (stdc_first_leading_zero (~0UL), 0);
150 TEST_COMPARE (stdc_first_leading_zero (~0ULL), 0);
151 TEST_COMPARE (stdc_first_leading_zero ((uc) ~3U), CHAR_BIT - 1);
152 TEST_COMPARE (stdc_first_leading_zero ((us) ~15U),
153 sizeof (short) * CHAR_BIT - 3);
154 TEST_COMPARE (stdc_first_leading_zero (~63U), sizeof (int) * CHAR_BIT - 5);
155 TEST_COMPARE (stdc_first_leading_zero (~255UL),
156 sizeof (long int) * CHAR_BIT - 7);
157 TEST_COMPARE (stdc_first_leading_zero (~1023ULL),
158 sizeof (long long int) * CHAR_BIT - 9);
159 TEST_COMPARE (stdc_first_leading_one ((uc) 0), 0);
160 TEST_COMPARE (expr_has_type (stdc_first_leading_one ((uc) 0), ui), 1);
161 TEST_COMPARE (stdc_first_leading_one ((us) 0), 0);
162 TEST_COMPARE (expr_has_type (stdc_first_leading_one ((us) 0), ui), 1);
163 TEST_COMPARE (stdc_first_leading_one (0U), 0);
164 TEST_COMPARE (expr_has_type (stdc_first_leading_one (0U), ui), 1);
165 TEST_COMPARE (stdc_first_leading_one (0UL), 0);
166 TEST_COMPARE (expr_has_type (stdc_first_leading_one (0UL), ui), 1);
167 TEST_COMPARE (stdc_first_leading_one (0ULL), 0);
168 TEST_COMPARE (expr_has_type (stdc_first_leading_one (0ULL), ui), 1);
169 TEST_COMPARE (stdc_first_leading_one ((uc) ~0U), 1);
170 TEST_COMPARE (stdc_first_leading_one ((us) ~0U), 1);
171 TEST_COMPARE (stdc_first_leading_one (~0U), 1);
172 TEST_COMPARE (stdc_first_leading_one (~0UL), 1);
173 TEST_COMPARE (stdc_first_leading_one (~0ULL), 1);
174 TEST_COMPARE (stdc_first_leading_one ((uc) 3), CHAR_BIT - 1);
175 TEST_COMPARE (stdc_first_leading_one ((us) 9),
176 sizeof (short) * CHAR_BIT - 3);
177 TEST_COMPARE (stdc_first_leading_one (34U), sizeof (int) * CHAR_BIT - 5);
178 TEST_COMPARE (stdc_first_leading_one (130UL),
179 sizeof (long int) * CHAR_BIT - 7);
180 TEST_COMPARE (stdc_first_leading_one (512ULL),
181 sizeof (long long int) * CHAR_BIT - 9);
182 TEST_COMPARE (stdc_first_trailing_zero ((uc) 0), 1);
183 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero ((uc) 0), ui), 1);
184 TEST_COMPARE (stdc_first_trailing_zero ((us) 0), 1);
185 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero ((us) 0), ui), 1);
186 TEST_COMPARE (stdc_first_trailing_zero (0U), 1);
187 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero (0U), ui), 1);
188 TEST_COMPARE (stdc_first_trailing_zero (0UL), 1);
189 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero (0UL), ui), 1);
190 TEST_COMPARE (stdc_first_trailing_zero (0ULL), 1);
191 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero (0ULL), ui), 1);
192 TEST_COMPARE (stdc_first_trailing_zero ((uc) ~0U), 0);
193 TEST_COMPARE (stdc_first_trailing_zero ((us) ~0U), 0);
194 TEST_COMPARE (stdc_first_trailing_zero (~0U), 0);
195 TEST_COMPARE (stdc_first_trailing_zero (~0UL), 0);
196 TEST_COMPARE (stdc_first_trailing_zero (~0ULL), 0);
197 TEST_COMPARE (stdc_first_trailing_zero ((uc) 2), 1);
198 TEST_COMPARE (stdc_first_trailing_zero ((us) 15), 5);
199 TEST_COMPARE (stdc_first_trailing_zero (63U), 7);
200 TEST_COMPARE (stdc_first_trailing_zero (128UL), 1);
201 TEST_COMPARE (stdc_first_trailing_zero (511ULL), 10);
202 TEST_COMPARE (stdc_first_trailing_one ((uc) 0), 0);
203 TEST_COMPARE (expr_has_type (stdc_first_trailing_one ((uc) 0), ui), 1);
204 TEST_COMPARE (stdc_first_trailing_one ((us) 0), 0);
205 TEST_COMPARE (expr_has_type (stdc_first_trailing_one ((us) 0), ui), 1);
206 TEST_COMPARE (stdc_first_trailing_one (0U), 0);
207 TEST_COMPARE (expr_has_type (stdc_first_trailing_one (0U), ui), 1);
208 TEST_COMPARE (stdc_first_trailing_one (0UL), 0);
209 TEST_COMPARE (expr_has_type (stdc_first_trailing_one (0UL), ui), 1);
210 TEST_COMPARE (stdc_first_trailing_one (0ULL), 0);
211 TEST_COMPARE (expr_has_type (stdc_first_trailing_one (0ULL), ui), 1);
212 TEST_COMPARE (stdc_first_trailing_one ((uc) ~0U), 1);
213 TEST_COMPARE (stdc_first_trailing_one ((us) ~0U), 1);
214 TEST_COMPARE (stdc_first_trailing_one (~0U), 1);
215 TEST_COMPARE (stdc_first_trailing_one (~0UL), 1);
216 TEST_COMPARE (stdc_first_trailing_one (~0ULL), 1);
217 TEST_COMPARE (stdc_first_trailing_one ((uc) 4), 3);
218 TEST_COMPARE (stdc_first_trailing_one ((us) 96), 6);
219 TEST_COMPARE (stdc_first_trailing_one (127U), 1);
220 TEST_COMPARE (stdc_first_trailing_one (511UL), 1);
221 TEST_COMPARE (stdc_first_trailing_one (~0ULL << 12), 13);
222 TEST_COMPARE (stdc_count_zeros ((uc) 0), CHAR_BIT);
223 TEST_COMPARE (expr_has_type (stdc_count_zeros ((uc) 0), ui), 1);
224 TEST_COMPARE (stdc_count_zeros ((us) 0), sizeof (short) * CHAR_BIT);
225 TEST_COMPARE (expr_has_type (stdc_count_zeros ((us) 0), ui), 1);
226 TEST_COMPARE (stdc_count_zeros (0U), sizeof (int) * CHAR_BIT);
227 TEST_COMPARE (expr_has_type (stdc_count_zeros (0U), ui), 1);
228 TEST_COMPARE (stdc_count_zeros (0UL), sizeof (long int) * CHAR_BIT);
229 TEST_COMPARE (expr_has_type (stdc_count_zeros (0UL), ui), 1);
230 TEST_COMPARE (stdc_count_zeros (0ULL), sizeof (long long int) * CHAR_BIT);
231 TEST_COMPARE (expr_has_type (stdc_count_zeros (0ULL), ui), 1);
232 TEST_COMPARE (stdc_count_zeros ((uc) ~0U), 0);
233 TEST_COMPARE (stdc_count_zeros ((us) ~0U), 0);
234 TEST_COMPARE (stdc_count_zeros (~0U), 0);
235 TEST_COMPARE (stdc_count_zeros (~0UL), 0);
236 TEST_COMPARE (stdc_count_zeros (~0ULL), 0);
237 TEST_COMPARE (stdc_count_zeros ((uc) 1U), CHAR_BIT - 1);
238 TEST_COMPARE (stdc_count_zeros ((us) 42), sizeof (short) * CHAR_BIT - 3);
239 TEST_COMPARE (stdc_count_zeros (291U), sizeof (int) * CHAR_BIT - 4);
240 TEST_COMPARE (stdc_count_zeros (~1315UL), 5);
241 TEST_COMPARE (stdc_count_zeros (3363ULL),
242 sizeof (long long int) * CHAR_BIT - 6);
243 TEST_COMPARE (stdc_count_ones ((uc) 0), 0);
244 TEST_COMPARE (expr_has_type (stdc_count_ones ((uc) 0), ui), 1);
245 TEST_COMPARE (stdc_count_ones ((us) 0), 0);
246 TEST_COMPARE (expr_has_type (stdc_count_ones ((us) 0), ui), 1);
247 TEST_COMPARE (stdc_count_ones (0U), 0);
248 TEST_COMPARE (expr_has_type (stdc_count_ones (0U), ui), 1);
249 TEST_COMPARE (stdc_count_ones (0UL), 0);
250 TEST_COMPARE (expr_has_type (stdc_count_ones (0UL), ui), 1);
251 TEST_COMPARE (stdc_count_ones (0ULL), 0);
252 TEST_COMPARE (expr_has_type (stdc_count_ones (0ULL), ui), 1);
253 TEST_COMPARE (stdc_count_ones ((uc) ~0U), CHAR_BIT);
254 TEST_COMPARE (stdc_count_ones ((us) ~0U), sizeof (short) * CHAR_BIT);
255 TEST_COMPARE (stdc_count_ones (~0U), sizeof (int) * CHAR_BIT);
256 TEST_COMPARE (stdc_count_ones (~0UL), sizeof (long int) * CHAR_BIT);
257 TEST_COMPARE (stdc_count_ones (~0ULL), sizeof (long long int) * CHAR_BIT);
258 TEST_COMPARE (stdc_count_ones ((uc) ~1U), CHAR_BIT - 1);
259 TEST_COMPARE (stdc_count_ones ((us) ~42), sizeof (short) * CHAR_BIT - 3);
260 TEST_COMPARE (stdc_count_ones (~291U), sizeof (int) * CHAR_BIT - 4);
261 TEST_COMPARE (stdc_count_ones (1315UL), 5);
262 TEST_COMPARE (stdc_count_ones (~3363ULL),
263 sizeof (long long int) * CHAR_BIT - 6);
264 TEST_COMPARE (stdc_has_single_bit ((uc) 0), 0);
265 TEST_COMPARE (expr_has_type (stdc_has_single_bit ((uc) 0), _Bool), 1);
266 TEST_COMPARE (stdc_has_single_bit ((us) 0), 0);
267 TEST_COMPARE (expr_has_type (stdc_has_single_bit ((us) 0), _Bool), 1);
268 TEST_COMPARE (stdc_has_single_bit (0U), 0);
269 TEST_COMPARE (expr_has_type (stdc_has_single_bit (0U), _Bool), 1);
270 TEST_COMPARE (stdc_has_single_bit (0UL), 0);
271 TEST_COMPARE (expr_has_type (stdc_has_single_bit (0UL), _Bool), 1);
272 TEST_COMPARE (stdc_has_single_bit (0ULL), 0);
273 TEST_COMPARE (expr_has_type (stdc_has_single_bit (0ULL), _Bool), 1);
274 TEST_COMPARE (stdc_has_single_bit ((uc) 2), 1);
275 TEST_COMPARE (stdc_has_single_bit ((us) 8), 1);
276 TEST_COMPARE (stdc_has_single_bit (32U), 1);
277 TEST_COMPARE (stdc_has_single_bit (128UL), 1);
278 TEST_COMPARE (stdc_has_single_bit (512ULL), 1);
279 TEST_COMPARE (stdc_has_single_bit ((uc) 7), 0);
280 TEST_COMPARE (stdc_has_single_bit ((us) 96), 0);
281 TEST_COMPARE (stdc_has_single_bit (513U), 0);
282 TEST_COMPARE (stdc_has_single_bit (1022UL), 0);
283 TEST_COMPARE (stdc_has_single_bit (12ULL), 0);
284 TEST_COMPARE (stdc_bit_width ((uc) 0), 0);
285 TEST_COMPARE (expr_has_type (stdc_bit_width ((uc) 0), ui), 1);
286 TEST_COMPARE (stdc_bit_width ((us) 0), 0);
287 TEST_COMPARE (expr_has_type (stdc_bit_width ((us) 0), ui), 1);
288 TEST_COMPARE (stdc_bit_width (0U), 0);
289 TEST_COMPARE (expr_has_type (stdc_bit_width (0U), ui), 1);
290 TEST_COMPARE (stdc_bit_width (0UL), 0);
291 TEST_COMPARE (expr_has_type (stdc_bit_width (0UL), ui), 1);
292 TEST_COMPARE (stdc_bit_width (0ULL), 0);
293 TEST_COMPARE (expr_has_type (stdc_bit_width (0ULL), ui), 1);
294 TEST_COMPARE (stdc_bit_width ((uc) ~0U), CHAR_BIT);
295 TEST_COMPARE (stdc_bit_width ((us) ~0U), sizeof (short) * CHAR_BIT);
296 TEST_COMPARE (stdc_bit_width (~0U), sizeof (int) * CHAR_BIT);
297 TEST_COMPARE (stdc_bit_width (~0UL), sizeof (long int) * CHAR_BIT);
298 TEST_COMPARE (stdc_bit_width (~0ULL), sizeof (long long int) * CHAR_BIT);
299 TEST_COMPARE (stdc_bit_width ((uc) ((uc) ~0U >> 1)), CHAR_BIT - 1);
300 TEST_COMPARE (stdc_bit_width ((uc) 6), 3);
301 TEST_COMPARE (stdc_bit_width ((us) 12U), 4);
302 TEST_COMPARE (stdc_bit_width ((us) ((us) ~0U >> 5)),
303 sizeof (short) * CHAR_BIT - 5);
304 TEST_COMPARE (stdc_bit_width (137U), 8);
305 TEST_COMPARE (stdc_bit_width (269U), 9);
306 TEST_COMPARE (stdc_bit_width (39UL), 6);
307 TEST_COMPARE (stdc_bit_width (~0UL >> 2), sizeof (long int) * CHAR_BIT - 2);
308 TEST_COMPARE (stdc_bit_width (1023ULL), 10);
309 TEST_COMPARE (stdc_bit_width (1024ULL), 11);
310 TEST_COMPARE (stdc_bit_floor ((uc) 0), 0);
311 TEST_COMPARE (expr_has_type (stdc_bit_floor ((uc) 0), uc), 1);
312 TEST_COMPARE (stdc_bit_floor ((us) 0), 0);
313 TEST_COMPARE (expr_has_type (stdc_bit_floor ((us) 0), us), 1);
314 TEST_COMPARE (stdc_bit_floor (0U), 0U);
315 TEST_COMPARE (expr_has_type (stdc_bit_floor (0U), ui), 1);
316 TEST_COMPARE (stdc_bit_floor (0UL), 0UL);
317 TEST_COMPARE (expr_has_type (stdc_bit_floor (0UL), ul), 1);
318 TEST_COMPARE (stdc_bit_floor (0ULL), 0ULL);
319 TEST_COMPARE (expr_has_type (stdc_bit_floor (0ULL), ull), 1);
320 TEST_COMPARE (stdc_bit_floor ((uc) ~0U), (1U << (CHAR_BIT - 1)));
321 TEST_COMPARE (stdc_bit_floor ((us) ~0U),
322 (1U << (sizeof (short) * CHAR_BIT - 1)));
323 TEST_COMPARE (stdc_bit_floor (~0U), (1U << (sizeof (int) * CHAR_BIT - 1)));
324 TEST_COMPARE (stdc_bit_floor (~0UL),
325 (1UL << (sizeof (long int) * CHAR_BIT - 1)));
326 TEST_COMPARE (stdc_bit_floor (~0ULL),
327 (1ULL << (sizeof (long long int) * CHAR_BIT - 1)));
328 TEST_COMPARE (stdc_bit_floor ((uc) 4), 4);
329 TEST_COMPARE (stdc_bit_floor ((uc) 7), 4);
330 TEST_COMPARE (stdc_bit_floor ((us) 8U), 8);
331 TEST_COMPARE (stdc_bit_floor ((us) 31U), 16);
332 TEST_COMPARE (stdc_bit_floor (137U), 128U);
333 TEST_COMPARE (stdc_bit_floor (269U), 256U);
334 TEST_COMPARE (stdc_bit_floor (511UL), 256UL);
335 TEST_COMPARE (stdc_bit_floor (512UL), 512UL);
336 TEST_COMPARE (stdc_bit_floor (513UL), 512ULL);
337 TEST_COMPARE (stdc_bit_floor (1024ULL), 1024ULL);
338 TEST_COMPARE (stdc_bit_ceil ((uc) 0), 1);
339 TEST_COMPARE (expr_has_type (stdc_bit_ceil ((uc) 0), uc), 1);
340 TEST_COMPARE (stdc_bit_ceil ((us) 0), 1);
341 TEST_COMPARE (expr_has_type (stdc_bit_ceil ((us) 0), us), 1);
342 TEST_COMPARE (stdc_bit_ceil (0U), 1U);
343 TEST_COMPARE (expr_has_type (stdc_bit_ceil (0U), ui), 1);
344 TEST_COMPARE (stdc_bit_ceil (0UL), 1UL);
345 TEST_COMPARE (expr_has_type (stdc_bit_ceil (0UL), ul), 1);
346 TEST_COMPARE (stdc_bit_ceil (0ULL), 1ULL);
347 TEST_COMPARE (expr_has_type (stdc_bit_ceil (0ULL), ull), 1);
348 TEST_COMPARE (stdc_bit_ceil ((uc) ~0U), 0);
349 TEST_COMPARE (stdc_bit_ceil ((us) ~0U), 0);
350 TEST_COMPARE (stdc_bit_ceil (~0U), 0U);
351 TEST_COMPARE (stdc_bit_ceil (~0UL), 0UL);
352 TEST_COMPARE (stdc_bit_ceil (~0ULL), 0ULL);
353 TEST_COMPARE (stdc_bit_ceil ((uc) ((uc) ~0U >> 1)), (1U << (CHAR_BIT - 1)));
354 TEST_COMPARE (stdc_bit_ceil ((uc) ((uc) ~0U >> 1)), (1U << (CHAR_BIT - 1)));
355 TEST_COMPARE (stdc_bit_ceil ((us) ((us) ~0U >> 1)),
356 (1U << (sizeof (short) * CHAR_BIT - 1)));
357 TEST_COMPARE (stdc_bit_ceil ((us) ((us) ~0U >> 1)),
358 (1U << (sizeof (short) * CHAR_BIT - 1)));
359 TEST_COMPARE (stdc_bit_ceil (~0U >> 1),
360 (1U << (sizeof (int) * CHAR_BIT - 1)));
361 TEST_COMPARE (stdc_bit_ceil (1U << (sizeof (int) * CHAR_BIT - 1)),
362 (1U << (sizeof (int) * CHAR_BIT - 1)));
363 TEST_COMPARE (stdc_bit_ceil (~0UL >> 1),
364 (1UL << (sizeof (long int) * CHAR_BIT - 1)));
365 TEST_COMPARE (stdc_bit_ceil (~0UL >> 1),
366 (1UL << (sizeof (long int) * CHAR_BIT - 1)));
367 TEST_COMPARE (stdc_bit_ceil (1ULL
368 << (sizeof (long long int) * CHAR_BIT - 1)),
369 (1ULL << (sizeof (long long int) * CHAR_BIT - 1)));
370 TEST_COMPARE (stdc_bit_ceil (~0ULL >> 1),
371 (1ULL << (sizeof (long long int) * CHAR_BIT - 1)));
372 TEST_COMPARE (stdc_bit_ceil ((uc) 1), 1);
373 TEST_COMPARE (stdc_bit_ceil ((uc) 2), 2);
374 TEST_COMPARE (stdc_bit_ceil ((us) 3U), 4);
375 TEST_COMPARE (stdc_bit_ceil ((us) 4U), 4);
376 TEST_COMPARE (stdc_bit_ceil (5U), 8U);
377 TEST_COMPARE (stdc_bit_ceil (269U), 512U);
378 TEST_COMPARE (stdc_bit_ceil (511UL), 512UL);
379 TEST_COMPARE (stdc_bit_ceil (512UL), 512UL);
380 TEST_COMPARE (stdc_bit_ceil (513ULL), 1024ULL);
381 TEST_COMPARE (stdc_bit_ceil (1025ULL), 2048ULL);
382# ifdef __SIZEOF_INT128__
383 TEST_COMPARE (stdc_leading_zeros ((unsigned __int128) 0),
384 sizeof (__int128) * CHAR_BIT);
385 TEST_COMPARE (expr_has_type (stdc_leading_zeros ((unsigned __int128) 0), ui),
386 1);
387 TEST_COMPARE (stdc_leading_zeros (~(unsigned __int128) 0), 0);
388 TEST_COMPARE (stdc_leading_ones ((unsigned __int128) 0), 0);
389 TEST_COMPARE (expr_has_type (stdc_leading_ones ((unsigned __int128) 0), ui),
390 1);
391 TEST_COMPARE (stdc_leading_ones (~(unsigned __int128) 0),
392 sizeof (__int128) * CHAR_BIT);
393 TEST_COMPARE (stdc_trailing_zeros ((unsigned __int128) 0),
394 sizeof (__int128) * CHAR_BIT);
395 TEST_COMPARE (expr_has_type (stdc_trailing_zeros ((unsigned __int128) 0),
396 ui), 1);
397 TEST_COMPARE (stdc_trailing_zeros (~(unsigned __int128) 0), 0);
398 TEST_COMPARE (stdc_trailing_ones ((unsigned __int128) 0), 0);
399 TEST_COMPARE (expr_has_type (stdc_trailing_ones ((unsigned __int128) 0), ui),
400 1);
401 TEST_COMPARE (stdc_trailing_ones (~(unsigned __int128) 0),
402 sizeof (__int128) * CHAR_BIT);
403 TEST_COMPARE (stdc_first_leading_zero ((unsigned __int128) 0), 1);
404 TEST_COMPARE (expr_has_type (stdc_first_leading_zero ((unsigned __int128) 0),
405 ui), 1);
406 TEST_COMPARE (stdc_first_leading_zero (~(unsigned __int128) 0), 0);
407 TEST_COMPARE (stdc_first_leading_one ((unsigned __int128) 0), 0);
408 TEST_COMPARE (expr_has_type (stdc_first_leading_one ((unsigned __int128) 0),
409 ui), 1);
410 TEST_COMPARE (stdc_first_leading_one (~(unsigned __int128) 0), 1);
411 TEST_COMPARE (stdc_first_trailing_zero ((unsigned __int128) 0), 1);
412 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero ((unsigned __int128)
413 0), ui), 1);
414 TEST_COMPARE (stdc_first_trailing_zero (~(unsigned __int128) 0), 0);
415 TEST_COMPARE (stdc_first_trailing_one ((unsigned __int128) 0), 0);
416 TEST_COMPARE (expr_has_type (stdc_first_trailing_one ((unsigned __int128) 0),
417 ui), 1);
418 TEST_COMPARE (stdc_first_trailing_one (~(unsigned __int128) 0), 1);
419 TEST_COMPARE (stdc_count_zeros ((unsigned __int128) 0),
420 sizeof (__int128) * CHAR_BIT);
421 TEST_COMPARE (expr_has_type (stdc_count_zeros ((unsigned __int128) 0), ui),
422 1);
423 TEST_COMPARE (stdc_count_zeros (~(unsigned __int128) 0), 0);
424 TEST_COMPARE (stdc_count_ones ((unsigned __int128) 0), 0);
425 TEST_COMPARE (expr_has_type (stdc_count_ones ((unsigned __int128) 0), ui),
426 1);
427 TEST_COMPARE (stdc_count_ones (~(unsigned __int128) 0),
428 sizeof (__int128) * CHAR_BIT);
429 TEST_COMPARE (stdc_has_single_bit ((unsigned __int128) 0), 0);
430 TEST_COMPARE (expr_has_type (stdc_has_single_bit ((unsigned __int128) 0),
431 _Bool), 1);
432 TEST_COMPARE (stdc_has_single_bit (~(unsigned __int128) 0), 0);
433 TEST_COMPARE (stdc_bit_width ((unsigned __int128) 0), 0);
434 TEST_COMPARE (expr_has_type (stdc_bit_width ((unsigned __int128) 0), ui), 1);
435 TEST_COMPARE (stdc_bit_width (~(unsigned __int128) 0),
436 sizeof (__int128) * CHAR_BIT);
437 TEST_COMPARE (stdc_bit_floor ((unsigned __int128) 0) != 0, 0);
438 TEST_COMPARE (expr_has_type (stdc_bit_floor ((unsigned __int128) 0),
439 unsigned __int128), 1);
440 TEST_COMPARE (stdc_bit_floor (~(unsigned __int128) 0)
441 != ((unsigned __int128) 1) << (sizeof (__int128)
442 * CHAR_BIT - 1), 0);
443 TEST_COMPARE (stdc_bit_ceil ((unsigned __int128) 0) != 1, 0);
444 TEST_COMPARE (expr_has_type (stdc_bit_ceil ((unsigned __int128) 0),
445 unsigned __int128), 1);
446 TEST_COMPARE (stdc_bit_ceil ((unsigned __int128) 1) != 1, 0);
447 TEST_COMPARE (stdc_bit_ceil ((~(unsigned __int128) 0) >> 1)
448 != ((unsigned __int128) 1) << (sizeof (__int128)
449 * CHAR_BIT - 1), 0);
450 TEST_COMPARE (stdc_bit_ceil (~(unsigned __int128) 0) != 0, 0);
451# endif
452 uc a = 0;
453 TEST_COMPARE (stdc_bit_width (a++), 0);
454 TEST_COMPARE (a, 1);
455 ull b = 0;
456 TEST_COMPARE (stdc_bit_width (b++), 0);
457 TEST_COMPARE (b, 1);
458 TEST_COMPARE (stdc_bit_floor (a++), 1);
459 TEST_COMPARE (a, 2);
460 TEST_COMPARE (stdc_bit_floor (b++), 1);
461 TEST_COMPARE (b, 2);
462 TEST_COMPARE (stdc_bit_ceil (a++), 2);
463 TEST_COMPARE (a, 3);
464 TEST_COMPARE (stdc_bit_ceil (b++), 2);
465 TEST_COMPARE (b, 3);
466 TEST_COMPARE (stdc_leading_zeros (a++), CHAR_BIT - 2);
467 TEST_COMPARE (a, 4);
468 TEST_COMPARE (stdc_leading_zeros (b++),
469 sizeof (long long int) * CHAR_BIT - 2);
470 TEST_COMPARE (b, 4);
471 TEST_COMPARE (stdc_leading_ones (a++), 0);
472 TEST_COMPARE (a, 5);
473 TEST_COMPARE (stdc_leading_ones (b++), 0);
474 TEST_COMPARE (b, 5);
475 TEST_COMPARE (stdc_trailing_zeros (a++), 0);
476 TEST_COMPARE (a, 6);
477 TEST_COMPARE (stdc_trailing_zeros (b++), 0);
478 TEST_COMPARE (b, 6);
479 TEST_COMPARE (stdc_trailing_ones (a++), 0);
480 TEST_COMPARE (a, 7);
481 TEST_COMPARE (stdc_trailing_ones (b++), 0);
482 TEST_COMPARE (b, 7);
483 TEST_COMPARE (stdc_first_leading_zero (a++), 1);
484 TEST_COMPARE (a, 8);
485 TEST_COMPARE (stdc_first_leading_zero (b++), 1);
486 TEST_COMPARE (b, 8);
487 TEST_COMPARE (stdc_first_leading_one (a++), CHAR_BIT - 3);
488 TEST_COMPARE (a, 9);
489 TEST_COMPARE (stdc_first_leading_one (b++),
490 sizeof (long long int) * CHAR_BIT - 3);
491 TEST_COMPARE (b, 9);
492 TEST_COMPARE (stdc_first_trailing_zero (a++), 2);
493 TEST_COMPARE (a, 10);
494 TEST_COMPARE (stdc_first_trailing_zero (b++), 2);
495 TEST_COMPARE (b, 10);
496 TEST_COMPARE (stdc_first_trailing_one (a++), 2);
497 TEST_COMPARE (a, 11);
498 TEST_COMPARE (stdc_first_trailing_one (b++), 2);
499 TEST_COMPARE (b, 11);
500 TEST_COMPARE (stdc_count_zeros (a++), CHAR_BIT - 3);
501 TEST_COMPARE (a, 12);
502 TEST_COMPARE (stdc_count_zeros (b++),
503 sizeof (long long int) * CHAR_BIT - 3);
504 TEST_COMPARE (b, 12);
505 TEST_COMPARE (stdc_count_ones (a++), 2);
506 TEST_COMPARE (a, 13);
507 TEST_COMPARE (stdc_count_ones (b++), 2);
508 TEST_COMPARE (b, 13);
509 TEST_COMPARE (stdc_has_single_bit (a++), 0);
510 TEST_COMPARE (a, 14);
511 TEST_COMPARE (stdc_has_single_bit (b++), 0);
512 TEST_COMPARE (b, 14);
513# ifdef BITINT_MAXWIDTH
514# if BITINT_MAXWIDTH >= 64
515 TEST_COMPARE (stdc_leading_zeros (0uwb), 1);
516 TEST_COMPARE (expr_has_type (stdc_leading_zeros (0uwb), ui), 1);
517 TEST_COMPARE (stdc_leading_zeros (1uwb), 0);
518 TEST_COMPARE (expr_has_type (stdc_leading_zeros (1uwb), ui), 1);
519 TEST_COMPARE (stdc_leading_ones (0uwb), 0);
520 TEST_COMPARE (expr_has_type (stdc_leading_ones (0uwb), ui), 1);
521 TEST_COMPARE (stdc_leading_ones (1uwb), 1);
522 TEST_COMPARE (expr_has_type (stdc_leading_ones (1uwb), ui), 1);
523 TEST_COMPARE (stdc_trailing_zeros (0uwb), 1);
524 TEST_COMPARE (expr_has_type (stdc_trailing_zeros (0uwb), ui), 1);
525 TEST_COMPARE (stdc_trailing_zeros (1uwb), 0);
526 TEST_COMPARE (expr_has_type (stdc_trailing_zeros (1uwb), ui), 1);
527 TEST_COMPARE (stdc_trailing_ones (0uwb), 0);
528 TEST_COMPARE (expr_has_type (stdc_trailing_ones (0uwb), ui), 1);
529 TEST_COMPARE (stdc_trailing_ones (1uwb), 1);
530 TEST_COMPARE (expr_has_type (stdc_trailing_ones (1uwb), ui), 1);
531 TEST_COMPARE (stdc_first_leading_zero (0uwb), 1);
532 TEST_COMPARE (expr_has_type (stdc_first_leading_zero (0uwb), ui), 1);
533 TEST_COMPARE (stdc_first_leading_zero (1uwb), 0);
534 TEST_COMPARE (expr_has_type (stdc_first_leading_zero (1uwb), ui), 1);
535 TEST_COMPARE (stdc_first_leading_one (0uwb), 0);
536 TEST_COMPARE (expr_has_type (stdc_first_leading_one (0uwb), ui), 1);
537 TEST_COMPARE (stdc_first_leading_one (1uwb), 1);
538 TEST_COMPARE (expr_has_type (stdc_first_leading_one (1uwb), ui), 1);
539 TEST_COMPARE (stdc_first_trailing_zero (0uwb), 1);
540 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero (0uwb), ui), 1);
541 TEST_COMPARE (stdc_first_trailing_zero (1uwb), 0);
542 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero (1uwb), ui), 1);
543 TEST_COMPARE (stdc_first_trailing_one (0uwb), 0);
544 TEST_COMPARE (expr_has_type (stdc_first_trailing_one (0uwb), ui), 1);
545 TEST_COMPARE (stdc_first_trailing_one (1uwb), 1);
546 TEST_COMPARE (expr_has_type (stdc_first_trailing_one (1uwb), ui), 1);
547 TEST_COMPARE (stdc_count_zeros (0uwb), 1);
548 TEST_COMPARE (expr_has_type (stdc_count_zeros (0uwb), ui), 1);
549 TEST_COMPARE (stdc_count_zeros (1uwb), 0);
550 TEST_COMPARE (expr_has_type (stdc_count_zeros (1uwb), ui), 1);
551 TEST_COMPARE (stdc_count_ones (0uwb), 0);
552 TEST_COMPARE (expr_has_type (stdc_count_ones (0uwb), ui), 1);
553 TEST_COMPARE (stdc_count_ones (1uwb), 1);
554 TEST_COMPARE (expr_has_type (stdc_count_ones (1uwb), ui), 1);
555 TEST_COMPARE (stdc_has_single_bit (0uwb), 0);
556 TEST_COMPARE (expr_has_type (stdc_has_single_bit (0uwb), _Bool), 1);
557 TEST_COMPARE (stdc_has_single_bit (1uwb), 1);
558 TEST_COMPARE (expr_has_type (stdc_has_single_bit (1uwb), _Bool), 1);
559 TEST_COMPARE (stdc_bit_width (0uwb), 0);
560 TEST_COMPARE (expr_has_type (stdc_bit_width (0uwb), ui), 1);
561 TEST_COMPARE (stdc_bit_width (1uwb), 1);
562 TEST_COMPARE (expr_has_type (stdc_bit_width (1uwb), ui), 1);
563 TEST_COMPARE (stdc_bit_floor (0uwb), 0);
564 TEST_COMPARE (expr_has_type (stdc_bit_floor (0uwb), unsigned _BitInt(1)), 1);
565 TEST_COMPARE (stdc_bit_floor (1uwb), 1);
566 TEST_COMPARE (expr_has_type (stdc_bit_floor (1uwb), unsigned _BitInt(1)), 1);
567 TEST_COMPARE (stdc_bit_ceil (0uwb), 1);
568 TEST_COMPARE (expr_has_type (stdc_bit_ceil (0uwb), unsigned _BitInt(1)), 1);
569 TEST_COMPARE (stdc_bit_ceil (1uwb), 1);
570 TEST_COMPARE (expr_has_type (stdc_bit_ceil (1uwb), unsigned _BitInt(1)), 1);
571 unsigned _BitInt(1) c = 0;
572 TEST_COMPARE (stdc_bit_floor (c++), 0);
573 TEST_COMPARE (c, 1);
574 TEST_COMPARE (stdc_bit_floor (c++), 1);
575 TEST_COMPARE (c, 0);
576 TEST_COMPARE (stdc_bit_ceil (c++), 1);
577 TEST_COMPARE (c, 1);
578 TEST_COMPARE (stdc_bit_ceil (c++), 1);
579 TEST_COMPARE (c, 0);
580# endif
581# if BITINT_MAXWIDTH >= 512
582 TEST_COMPARE (stdc_leading_zeros ((unsigned _BitInt(512)) 0), 512);
583 TEST_COMPARE (expr_has_type (stdc_leading_zeros ((unsigned _BitInt(512)) 0),
584 ui), 1);
585 TEST_COMPARE (stdc_leading_zeros ((unsigned _BitInt(373)) 0), 373);
586 TEST_COMPARE (expr_has_type (stdc_leading_zeros ((unsigned _BitInt(373)) 0),
587 ui), 1);
588 TEST_COMPARE (stdc_leading_zeros (~(unsigned _BitInt(512)) 0), 0);
589 TEST_COMPARE (stdc_leading_zeros (~(unsigned _BitInt(373)) 0), 0);
590 TEST_COMPARE (stdc_leading_zeros ((unsigned _BitInt(512)) 275), 512 - 9);
591 TEST_COMPARE (stdc_leading_zeros ((unsigned _BitInt(373)) 512), 373 - 10);
592 TEST_COMPARE (stdc_leading_ones ((unsigned _BitInt(512)) 0), 0);
593 TEST_COMPARE (expr_has_type (stdc_leading_ones ((unsigned _BitInt(512)) 0),
594 ui), 1);
595 TEST_COMPARE (stdc_leading_ones ((unsigned _BitInt(373)) 0), 0);
596 TEST_COMPARE (expr_has_type (stdc_leading_ones ((unsigned _BitInt(373)) 0),
597 ui), 1);
598 TEST_COMPARE (stdc_leading_ones (~(unsigned _BitInt(512)) 0), 512);
599 TEST_COMPARE (stdc_leading_ones (~(unsigned _BitInt(373)) 0), 373);
600 TEST_COMPARE (stdc_leading_ones (~(unsigned _BitInt(512)) 275), 512 - 9);
601 TEST_COMPARE (stdc_leading_ones (~(unsigned _BitInt(373)) 512), 373 - 10);
602 TEST_COMPARE (stdc_trailing_zeros ((unsigned _BitInt(512)) 0), 512);
603 TEST_COMPARE (expr_has_type (stdc_trailing_zeros ((unsigned _BitInt(512)) 0),
604 ui), 1);
605 TEST_COMPARE (stdc_trailing_zeros ((unsigned _BitInt(373)) 0), 373);
606 TEST_COMPARE (expr_has_type (stdc_trailing_zeros ((unsigned _BitInt(373)) 0),
607 ui), 1);
608 TEST_COMPARE (stdc_trailing_zeros (~(unsigned _BitInt(512)) 0), 0);
609 TEST_COMPARE (stdc_trailing_zeros (~(unsigned _BitInt(373)) 0), 0);
610 TEST_COMPARE (stdc_trailing_zeros ((unsigned _BitInt(512)) 256), 8);
611 TEST_COMPARE (stdc_trailing_zeros ((unsigned _BitInt(373)) 512), 9);
612 TEST_COMPARE (stdc_trailing_ones ((unsigned _BitInt(512)) 0), 0);
613 TEST_COMPARE (expr_has_type (stdc_trailing_ones ((unsigned _BitInt(512)) 0),
614 ui), 1);
615 TEST_COMPARE (stdc_trailing_ones ((unsigned _BitInt(373)) 0), 0);
616 TEST_COMPARE (expr_has_type (stdc_trailing_ones ((unsigned _BitInt(373)) 0),
617 ui), 1);
618 TEST_COMPARE (stdc_trailing_ones (~(unsigned _BitInt(512)) 0), 512);
619 TEST_COMPARE (stdc_trailing_ones (~(unsigned _BitInt(373)) 0), 373);
620 TEST_COMPARE (stdc_trailing_ones ((unsigned _BitInt(512)) 255), 8);
621 TEST_COMPARE (stdc_trailing_ones ((~(unsigned _BitInt(373)) 0) >> 2),
622 373 - 2);
623 TEST_COMPARE (stdc_first_leading_zero ((unsigned _BitInt(512)) 0), 1);
624 TEST_COMPARE (expr_has_type (stdc_first_leading_zero ((unsigned _BitInt(512))
625 0), ui), 1);
626 TEST_COMPARE (stdc_first_leading_zero ((unsigned _BitInt(373)) 0), 1);
627 TEST_COMPARE (expr_has_type (stdc_first_leading_zero ((unsigned _BitInt(373))
628 0), ui), 1);
629 TEST_COMPARE (stdc_first_leading_zero (~(unsigned _BitInt(512)) 0), 0);
630 TEST_COMPARE (stdc_first_leading_zero (~(unsigned _BitInt(373)) 0), 0);
631 TEST_COMPARE (stdc_first_leading_zero (~(unsigned _BitInt(512)) 511),
632 512 - 8);
633 TEST_COMPARE (stdc_first_leading_zero (~(unsigned _BitInt(373)) 1023),
634 373 - 9);
635 TEST_COMPARE (stdc_first_leading_one ((unsigned _BitInt(512)) 0), 0);
636 TEST_COMPARE (expr_has_type (stdc_first_leading_one ((unsigned _BitInt(512))
637 0), ui), 1);
638 TEST_COMPARE (stdc_first_leading_one ((unsigned _BitInt(373)) 0), 0);
639 TEST_COMPARE (expr_has_type (stdc_first_leading_one ((unsigned _BitInt(373))
640 0), ui), 1);
641 TEST_COMPARE (stdc_first_leading_one (~(unsigned _BitInt(512)) 0), 1);
642 TEST_COMPARE (stdc_first_leading_one (~(unsigned _BitInt(373)) 0), 1);
643 TEST_COMPARE (stdc_first_leading_one ((unsigned _BitInt(512)) 275), 512 - 8);
644 TEST_COMPARE (stdc_first_leading_one ((unsigned _BitInt(373)) 512), 373 - 9);
645 TEST_COMPARE (stdc_first_trailing_zero ((unsigned _BitInt(512)) 0), 1);
646 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero ((unsigned
647 _BitInt(512)) 0),
648 ui), 1);
649 TEST_COMPARE (stdc_first_trailing_zero ((unsigned _BitInt(373)) 0), 1);
650 TEST_COMPARE (expr_has_type (stdc_first_trailing_zero ((unsigned
651 _BitInt(373)) 0),
652 ui), 1);
653 TEST_COMPARE (stdc_first_trailing_zero (~(unsigned _BitInt(512)) 0), 0);
654 TEST_COMPARE (stdc_first_trailing_zero (~(unsigned _BitInt(373)) 0), 0);
655 TEST_COMPARE (stdc_first_trailing_zero ((unsigned _BitInt(512)) 255), 9);
656 TEST_COMPARE (stdc_first_trailing_zero ((unsigned _BitInt(373)) 511), 10);
657 TEST_COMPARE (stdc_first_trailing_one ((unsigned _BitInt(512)) 0), 0);
658 TEST_COMPARE (expr_has_type (stdc_first_trailing_one ((unsigned _BitInt(512))
659 0), ui), 1);
660 TEST_COMPARE (stdc_first_trailing_one ((unsigned _BitInt(373)) 0), 0);
661 TEST_COMPARE (expr_has_type (stdc_first_trailing_one ((unsigned _BitInt(373))
662 0), ui), 1);
663 TEST_COMPARE (stdc_first_trailing_one (~(unsigned _BitInt(512)) 0), 1);
664 TEST_COMPARE (stdc_first_trailing_one (~(unsigned _BitInt(373)) 0), 1);
665 TEST_COMPARE (stdc_first_trailing_one (((unsigned _BitInt(512)) 255) << 175),
666 176);
667 TEST_COMPARE (stdc_first_trailing_one ((~(unsigned _BitInt(373)) 0) << 311),
668 312);
669 TEST_COMPARE (stdc_count_zeros ((unsigned _BitInt(512)) 0), 512);
670 TEST_COMPARE (expr_has_type (stdc_count_zeros ((unsigned _BitInt(512)) 0),
671 ui), 1);
672 TEST_COMPARE (stdc_count_zeros ((unsigned _BitInt(373)) 0), 373);
673 TEST_COMPARE (expr_has_type (stdc_count_zeros ((unsigned _BitInt(373)) 0),
674 ui), 1);
675 TEST_COMPARE (stdc_count_zeros (~(unsigned _BitInt(512)) 0), 0);
676 TEST_COMPARE (stdc_count_zeros (~(unsigned _BitInt(373)) 0), 0);
677 TEST_COMPARE (stdc_count_zeros ((unsigned _BitInt(512)) 1315), 512 - 5);
678 TEST_COMPARE (stdc_count_zeros ((unsigned _BitInt(373)) 3363), 373 - 6);
679 TEST_COMPARE (stdc_count_ones ((unsigned _BitInt(512)) 0), 0);
680 TEST_COMPARE (expr_has_type (stdc_count_ones ((unsigned _BitInt(512)) 0),
681 ui), 1);
682 TEST_COMPARE (stdc_count_ones ((unsigned _BitInt(373)) 0), 0);
683 TEST_COMPARE (expr_has_type (stdc_count_ones ((unsigned _BitInt(373)) 0),
684 ui), 1);
685 TEST_COMPARE (stdc_count_ones (~(unsigned _BitInt(512)) 0), 512);
686 TEST_COMPARE (stdc_count_ones (~(unsigned _BitInt(373)) 0), 373);
687 TEST_COMPARE (stdc_count_ones (~(unsigned _BitInt(512)) 1315), 512 - 5);
688 TEST_COMPARE (stdc_count_ones (~(unsigned _BitInt(373)) 3363), 373 - 6);
689 TEST_COMPARE (stdc_has_single_bit ((unsigned _BitInt(512)) 0), 0);
690 TEST_COMPARE (expr_has_type (stdc_has_single_bit ((unsigned _BitInt(512)) 0),
691 _Bool), 1);
692 TEST_COMPARE (stdc_has_single_bit ((unsigned _BitInt(373)) 0), 0);
693 TEST_COMPARE (expr_has_type (stdc_has_single_bit ((unsigned _BitInt(373)) 0),
694 _Bool), 1);
695 TEST_COMPARE (stdc_has_single_bit (~(unsigned _BitInt(512)) 0), 0);
696 TEST_COMPARE (stdc_has_single_bit (~(unsigned _BitInt(373)) 0), 0);
697 TEST_COMPARE (stdc_has_single_bit (((unsigned _BitInt(512)) 1022) << 279),
698 0);
699 TEST_COMPARE (stdc_has_single_bit (((unsigned _BitInt(373)) 12) << 305), 0);
700 TEST_COMPARE (stdc_bit_width ((unsigned _BitInt(512)) 0), 0);
701 TEST_COMPARE (expr_has_type (stdc_bit_width ((unsigned _BitInt(512)) 0),
702 ui), 1);
703 TEST_COMPARE (stdc_bit_width ((unsigned _BitInt(373)) 0), 0);
704 TEST_COMPARE (expr_has_type (stdc_bit_width ((unsigned _BitInt(373)) 0),
705 ui), 1);
706 TEST_COMPARE (stdc_bit_width (~(unsigned _BitInt(512)) 0), 512);
707 TEST_COMPARE (stdc_bit_width (~(unsigned _BitInt(373)) 0), 373);
708 TEST_COMPARE (stdc_bit_width (((unsigned _BitInt(512)) 1023) << 405),
709 405 + 10);
710 TEST_COMPARE (stdc_bit_width (((unsigned _BitInt(373)) 1024) << 242),
711 242 + 11);
712 TEST_COMPARE (stdc_bit_floor ((unsigned _BitInt(512)) 0) != 0, 0);
713 TEST_COMPARE (expr_has_type (stdc_bit_floor ((unsigned _BitInt(512)) 0),
714 unsigned _BitInt(512)), 1);
715 TEST_COMPARE (stdc_bit_floor ((unsigned _BitInt(373)) 0) != 0, 0);
716 TEST_COMPARE (expr_has_type (stdc_bit_floor ((unsigned _BitInt(373)) 0),
717 unsigned _BitInt(373)), 1);
718 TEST_COMPARE (stdc_bit_floor (~(unsigned _BitInt(512)) 0)
719 != ((unsigned _BitInt(512)) 1) << (512 - 1), 0);
720 TEST_COMPARE (stdc_bit_floor (~(unsigned _BitInt(373)) 0)
721 != ((unsigned _BitInt(373)) 1) << (373 - 1), 0);
722 TEST_COMPARE (stdc_bit_floor (((unsigned _BitInt(512)) 511) << 405)
723 != (((unsigned _BitInt(512)) 256) << 405), 0);
724 TEST_COMPARE (stdc_bit_floor (((unsigned _BitInt(373)) 512) << 242)
725 != (((unsigned _BitInt(512)) 512) << 242), 0);
726 TEST_COMPARE (stdc_bit_ceil ((unsigned _BitInt(512)) 0) != 1, 0);
727 TEST_COMPARE (expr_has_type (stdc_bit_ceil ((unsigned _BitInt(512)) 0),
728 unsigned _BitInt(512)), 1);
729 TEST_COMPARE (stdc_bit_ceil ((unsigned _BitInt(373)) 0) != 1, 0);
730 TEST_COMPARE (expr_has_type (stdc_bit_ceil ((unsigned _BitInt(373)) 0),
731 unsigned _BitInt(373)), 1);
732 TEST_COMPARE (stdc_bit_ceil (~(unsigned _BitInt(512)) 0) != 0, 0);
733 TEST_COMPARE (stdc_bit_ceil (~(unsigned _BitInt(373)) 0) != 0, 0);
734 TEST_COMPARE (stdc_bit_ceil (((unsigned _BitInt(512)) 1) << (512 - 1))
735 != ((unsigned _BitInt(512)) 1) << (512 - 1), 0);
736 TEST_COMPARE (stdc_bit_ceil ((~(unsigned _BitInt(373)) 0) >> 1)
737 != ((unsigned _BitInt(373)) 1) << (373 - 1), 0);
738 TEST_COMPARE (stdc_bit_ceil (((unsigned _BitInt(512)) 512) << 405)
739 != (((unsigned _BitInt(512)) 512) << 405), 0);
740 TEST_COMPARE (stdc_bit_ceil (((unsigned _BitInt(373)) 513) << 242)
741 != (((unsigned _BitInt(512)) 1024) << 242), 0);
742 TEST_COMPARE (stdc_bit_floor ((unsigned _BitInt(BITINT_MAXWIDTH)) 0) != 0,
743 0);
744 TEST_COMPARE (stdc_bit_floor (~(unsigned _BitInt(BITINT_MAXWIDTH)) 0)
745 != ((unsigned _BitInt(BITINT_MAXWIDTH)) 1) << (BITINT_MAXWIDTH
746 - 1), 0);
747 TEST_COMPARE (stdc_bit_floor (((unsigned _BitInt(BITINT_MAXWIDTH)) 511)
748 << 405)
749 != (((unsigned _BitInt(BITINT_MAXWIDTH)) 256) << 405), 0);
750 TEST_COMPARE (stdc_bit_floor (((unsigned _BitInt(BITINT_MAXWIDTH)) 512)
751 << 405)
752 != (((unsigned _BitInt(BITINT_MAXWIDTH)) 512) << 405), 0);
753 TEST_COMPARE (stdc_bit_ceil ((unsigned _BitInt(BITINT_MAXWIDTH)) 0) != 1, 0);
754 TEST_COMPARE (stdc_bit_ceil (~(unsigned _BitInt(BITINT_MAXWIDTH)) 0) != 0,
755 0);
756 TEST_COMPARE (stdc_bit_ceil (((unsigned _BitInt(BITINT_MAXWIDTH)) 1)
757 << (BITINT_MAXWIDTH - 1))
758 != ((unsigned _BitInt(BITINT_MAXWIDTH)) 1) << (BITINT_MAXWIDTH
759 - 1), 0);
760 TEST_COMPARE (stdc_bit_ceil (((unsigned _BitInt(BITINT_MAXWIDTH)) 512)
761 << 405)
762 != (((unsigned _BitInt(BITINT_MAXWIDTH)) 512) << 405), 0);
763 TEST_COMPARE (stdc_bit_ceil (((unsigned _BitInt(BITINT_MAXWIDTH)) 513)
764 << 405)
765 != (((unsigned _BitInt(BITINT_MAXWIDTH)) 1024) << 405), 0);
766# endif
767# endif
768 return 0;
769}
770#else
771static int
772do_test (void)
773{
774 return 0;
775}
776#endif
777
778#include <support/test-driver.c>
779

source code of glibc/stdlib/tst-stdbit-builtins.c