stdlib: Fix stdbit.h with -Wconversion for clang
[glibc.git] / stdlib / stdbit.h
blobf334eb174d209c82382ca3577b8a8270176d3825
1 /* ISO C23 Standard: 7.18 - Bit and byte utilities <stdbit.h>.
2 Copyright (C) 2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
19 #ifndef _STDBIT_H
20 #define _STDBIT_H 1
22 #include <features.h>
23 #include <bits/endian.h>
24 #include <bits/stdint-intn.h>
25 #include <bits/stdint-uintn.h>
26 #include <bits/stdint-least.h>
27 /* In C23, <stdbool.h> defines only an implementation-namespace macro,
28 so is OK to include here. Before C23, including <stdbool.h> allows
29 the header to use bool rather than _Bool unconditionally, and so to
30 compile as C++ (although the type-generic macros are not a good
31 form of type-generic interface for C++). */
32 #include <stdbool.h>
33 #define __need_size_t
34 #include <stddef.h>
36 #define __STDC_VERSION_STDBIT_H__ 202311L
38 #define __STDC_ENDIAN_LITTLE__ __LITTLE_ENDIAN
39 #define __STDC_ENDIAN_BIG__ __BIG_ENDIAN
40 #define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER
42 __BEGIN_DECLS
44 /* Use __pacify_uint16 (N) instead of (uint16_t) (N) when the cast is helpful
45 only to pacify older GCC (e.g., GCC 10 -Wconversion) or non-GCC (e.g
46 clang -Wimplicit-int-conversion). */
47 #if __GNUC_PREREQ (11, 0)
48 # define __pacify_uint8(n) (n)
49 # define __pacify_uint16(n) (n)
50 #else
51 # define __pacify_uint8(n) ((uint8_t) (n))
52 # define __pacify_uint16(n) ((uint16_t) (n))
53 #endif
55 /* Count leading zeros. */
56 extern unsigned int stdc_leading_zeros_uc (unsigned char __x)
57 __THROW __attribute_const__;
58 extern unsigned int stdc_leading_zeros_us (unsigned short __x)
59 __THROW __attribute_const__;
60 extern unsigned int stdc_leading_zeros_ui (unsigned int __x)
61 __THROW __attribute_const__;
62 extern unsigned int stdc_leading_zeros_ul (unsigned long int __x)
63 __THROW __attribute_const__;
64 __extension__
65 extern unsigned int stdc_leading_zeros_ull (unsigned long long int __x)
66 __THROW __attribute_const__;
67 #define stdc_leading_zeros(x) \
68 (stdc_leading_zeros_ull (x) \
69 - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x))))
71 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
72 static __always_inline unsigned int
73 __clz64_inline (uint64_t __x)
75 return __x == 0 ? 64U : (unsigned int) __builtin_clzll (__x);
78 static __always_inline unsigned int
79 __clz32_inline (uint32_t __x)
81 return __x == 0 ? 32U : (unsigned int) __builtin_clz (__x);
84 static __always_inline unsigned int
85 __clz16_inline (uint16_t __x)
87 return __clz32_inline (__x) - 16;
90 static __always_inline unsigned int
91 __clz8_inline (uint8_t __x)
93 return __clz32_inline (__x) - 24;
96 # define stdc_leading_zeros_uc(x) (__clz8_inline (x))
97 # define stdc_leading_zeros_us(x) (__clz16_inline (x))
98 # define stdc_leading_zeros_ui(x) (__clz32_inline (x))
99 # if __WORDSIZE == 64
100 # define stdc_leading_zeros_ul(x) (__clz64_inline (x))
101 # else
102 # define stdc_leading_zeros_ul(x) (__clz32_inline (x))
103 # endif
104 # define stdc_leading_zeros_ull(x) (__clz64_inline (x))
105 #endif
107 /* Count leading ones. */
108 extern unsigned int stdc_leading_ones_uc (unsigned char __x)
109 __THROW __attribute_const__;
110 extern unsigned int stdc_leading_ones_us (unsigned short __x)
111 __THROW __attribute_const__;
112 extern unsigned int stdc_leading_ones_ui (unsigned int __x)
113 __THROW __attribute_const__;
114 extern unsigned int stdc_leading_ones_ul (unsigned long int __x)
115 __THROW __attribute_const__;
116 __extension__
117 extern unsigned int stdc_leading_ones_ull (unsigned long long int __x)
118 __THROW __attribute_const__;
119 #define stdc_leading_ones(x) \
120 (stdc_leading_ones_ull ((unsigned long long int) (x) \
121 << 8 * (sizeof (0ULL) - sizeof (x))))
123 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
124 static __always_inline unsigned int
125 __clo64_inline (uint64_t __x)
127 return __clz64_inline (~__x);
130 static __always_inline unsigned int
131 __clo32_inline (uint32_t __x)
133 return __clz32_inline (~__x);
136 static __always_inline unsigned int
137 __clo16_inline (uint16_t __x)
139 return __clz16_inline (__pacify_uint16 (~__x));
142 static __always_inline unsigned int
143 __clo8_inline (uint8_t __x)
145 return __clz8_inline (__pacify_uint8 (~__x));
148 # define stdc_leading_ones_uc(x) (__clo8_inline (x))
149 # define stdc_leading_ones_us(x) (__clo16_inline (x))
150 # define stdc_leading_ones_ui(x) (__clo32_inline (x))
151 # if __WORDSIZE == 64
152 # define stdc_leading_ones_ul(x) (__clo64_inline (x))
153 # else
154 # define stdc_leading_ones_ul(x) (__clo32_inline (x))
155 # endif
156 # define stdc_leading_ones_ull(x) (__clo64_inline (x))
157 #endif
159 /* Count trailing zeros. */
160 extern unsigned int stdc_trailing_zeros_uc (unsigned char __x)
161 __THROW __attribute_const__;
162 extern unsigned int stdc_trailing_zeros_us (unsigned short __x)
163 __THROW __attribute_const__;
164 extern unsigned int stdc_trailing_zeros_ui (unsigned int __x)
165 __THROW __attribute_const__;
166 extern unsigned int stdc_trailing_zeros_ul (unsigned long int __x)
167 __THROW __attribute_const__;
168 __extension__
169 extern unsigned int stdc_trailing_zeros_ull (unsigned long long int __x)
170 __THROW __attribute_const__;
171 #define stdc_trailing_zeros(x) \
172 (sizeof (x) == 8 ? stdc_trailing_zeros_ull (x) \
173 : sizeof (x) == 4 ? stdc_trailing_zeros_ui (x) \
174 : sizeof (x) == 2 ? stdc_trailing_zeros_us (__pacify_uint16 (x)) \
175 : stdc_trailing_zeros_uc (__pacify_uint8 (x)))
177 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
178 static __always_inline unsigned int
179 __ctz64_inline (uint64_t __x)
181 return __x == 0 ? 64U : (unsigned int) __builtin_ctzll (__x);
184 static __always_inline unsigned int
185 __ctz32_inline (uint32_t __x)
187 return __x == 0 ? 32U : (unsigned int) __builtin_ctz (__x);
190 static __always_inline unsigned int
191 __ctz16_inline (uint16_t __x)
193 return __x == 0 ? 16U : (unsigned int) __builtin_ctz (__x);
196 static __always_inline unsigned int
197 __ctz8_inline (uint8_t __x)
199 return __x == 0 ? 8U : (unsigned int) __builtin_ctz (__x);
202 # define stdc_trailing_zeros_uc(x) (__ctz8_inline (x))
203 # define stdc_trailing_zeros_us(x) (__ctz16_inline (x))
204 # define stdc_trailing_zeros_ui(x) (__ctz32_inline (x))
205 # if __WORDSIZE == 64
206 # define stdc_trailing_zeros_ul(x) (__ctz64_inline (x))
207 # else
208 # define stdc_trailing_zeros_ul(x) (__ctz32_inline (x))
209 # endif
210 # define stdc_trailing_zeros_ull(x) (__ctz64_inline (x))
211 #endif
213 /* Count trailing ones. */
214 extern unsigned int stdc_trailing_ones_uc (unsigned char __x)
215 __THROW __attribute_const__;
216 extern unsigned int stdc_trailing_ones_us (unsigned short __x)
217 __THROW __attribute_const__;
218 extern unsigned int stdc_trailing_ones_ui (unsigned int __x)
219 __THROW __attribute_const__;
220 extern unsigned int stdc_trailing_ones_ul (unsigned long int __x)
221 __THROW __attribute_const__;
222 __extension__
223 extern unsigned int stdc_trailing_ones_ull (unsigned long long int __x)
224 __THROW __attribute_const__;
225 #define stdc_trailing_ones(x) (stdc_trailing_ones_ull (x))
227 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
228 static __always_inline unsigned int
229 __cto64_inline (uint64_t __x)
231 return __ctz64_inline (~__x);
234 static __always_inline unsigned int
235 __cto32_inline (uint32_t __x)
237 return __ctz32_inline (~__x);
240 static __always_inline unsigned int
241 __cto16_inline (uint16_t __x)
243 return __ctz16_inline (__pacify_uint16 (~__x));
246 static __always_inline unsigned int
247 __cto8_inline (uint8_t __x)
249 return __ctz8_inline (__pacify_uint8 (~__x));
252 # define stdc_trailing_ones_uc(x) (__cto8_inline (x))
253 # define stdc_trailing_ones_us(x) (__cto16_inline (x))
254 # define stdc_trailing_ones_ui(x) (__cto32_inline (x))
255 # if __WORDSIZE == 64
256 # define stdc_trailing_ones_ul(x) (__cto64_inline (x))
257 # else
258 # define stdc_trailing_ones_ul(x) (__cto32_inline (x))
259 # endif
260 # define stdc_trailing_ones_ull(x) (__cto64_inline (x))
261 #endif
263 /* First leading zero. */
264 extern unsigned int stdc_first_leading_zero_uc (unsigned char __x)
265 __THROW __attribute_const__;
266 extern unsigned int stdc_first_leading_zero_us (unsigned short __x)
267 __THROW __attribute_const__;
268 extern unsigned int stdc_first_leading_zero_ui (unsigned int __x)
269 __THROW __attribute_const__;
270 extern unsigned int stdc_first_leading_zero_ul (unsigned long int __x)
271 __THROW __attribute_const__;
272 __extension__
273 extern unsigned int stdc_first_leading_zero_ull (unsigned long long int __x)
274 __THROW __attribute_const__;
275 #define stdc_first_leading_zero(x) \
276 (sizeof (x) == 8 ? stdc_first_leading_zero_ull (x) \
277 : sizeof (x) == 4 ? stdc_first_leading_zero_ui (x) \
278 : sizeof (x) == 2 ? stdc_first_leading_zero_us (__pacify_uint16 (x)) \
279 : stdc_first_leading_zero_uc (__pacify_uint8 (x)))
281 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
282 static __always_inline unsigned int
283 __flz64_inline (uint64_t __x)
285 return __x == (uint64_t) -1 ? 0 : 1 + __clo64_inline (__x);
288 static __always_inline unsigned int
289 __flz32_inline (uint32_t __x)
291 return __x == (uint32_t) -1 ? 0 : 1 + __clo32_inline (__x);
294 static __always_inline unsigned int
295 __flz16_inline (uint16_t __x)
297 return __x == (uint16_t) -1 ? 0 : 1 + __clo16_inline (__x);
300 static __always_inline unsigned int
301 __flz8_inline (uint8_t __x)
303 return __x == (uint8_t) -1 ? 0 : 1 + __clo8_inline (__x);
306 # define stdc_first_leading_zero_uc(x) (__flz8_inline (x))
307 # define stdc_first_leading_zero_us(x) (__flz16_inline (x))
308 # define stdc_first_leading_zero_ui(x) (__flz32_inline (x))
309 # if __WORDSIZE == 64
310 # define stdc_first_leading_zero_ul(x) (__flz64_inline (x))
311 # else
312 # define stdc_first_leading_zero_ul(x) (__flz32_inline (x))
313 # endif
314 # define stdc_first_leading_zero_ull(x) (__flz64_inline (x))
315 #endif
317 /* First leading one. */
318 extern unsigned int stdc_first_leading_one_uc (unsigned char __x)
319 __THROW __attribute_const__;
320 extern unsigned int stdc_first_leading_one_us (unsigned short __x)
321 __THROW __attribute_const__;
322 extern unsigned int stdc_first_leading_one_ui (unsigned int __x)
323 __THROW __attribute_const__;
324 extern unsigned int stdc_first_leading_one_ul (unsigned long int __x)
325 __THROW __attribute_const__;
326 __extension__
327 extern unsigned int stdc_first_leading_one_ull (unsigned long long int __x)
328 __THROW __attribute_const__;
329 #define stdc_first_leading_one(x) \
330 (sizeof (x) == 8 ? stdc_first_leading_one_ull (x) \
331 : sizeof (x) == 4 ? stdc_first_leading_one_ui (x) \
332 : sizeof (x) == 2 ? stdc_first_leading_one_us (__pacify_uint16 (x)) \
333 : stdc_first_leading_one_uc (__pacify_uint8 (x)))
335 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
336 static __always_inline unsigned int
337 __flo64_inline (uint64_t __x)
339 return __x == 0 ? 0 : 1 + __clz64_inline (__x);
342 static __always_inline unsigned int
343 __flo32_inline (uint32_t __x)
345 return __x == 0 ? 0 : 1 + __clz32_inline (__x);
348 static __always_inline unsigned int
349 __flo16_inline (uint16_t __x)
351 return __x == 0 ? 0 : 1 + __clz16_inline (__x);
354 static __always_inline unsigned int
355 __flo8_inline (uint8_t __x)
357 return __x == 0 ? 0 : 1 + __clz8_inline (__x);
360 # define stdc_first_leading_one_uc(x) (__flo8_inline (x))
361 # define stdc_first_leading_one_us(x) (__flo16_inline (x))
362 # define stdc_first_leading_one_ui(x) (__flo32_inline (x))
363 # if __WORDSIZE == 64
364 # define stdc_first_leading_one_ul(x) (__flo64_inline (x))
365 # else
366 # define stdc_first_leading_one_ul(x) (__flo32_inline (x))
367 # endif
368 # define stdc_first_leading_one_ull(x) (__flo64_inline (x))
369 #endif
371 /* First trailing zero. */
372 extern unsigned int stdc_first_trailing_zero_uc (unsigned char __x)
373 __THROW __attribute_const__;
374 extern unsigned int stdc_first_trailing_zero_us (unsigned short __x)
375 __THROW __attribute_const__;
376 extern unsigned int stdc_first_trailing_zero_ui (unsigned int __x)
377 __THROW __attribute_const__;
378 extern unsigned int stdc_first_trailing_zero_ul (unsigned long int __x)
379 __THROW __attribute_const__;
380 __extension__
381 extern unsigned int stdc_first_trailing_zero_ull (unsigned long long int __x)
382 __THROW __attribute_const__;
383 #define stdc_first_trailing_zero(x) \
384 (sizeof (x) == 8 ? stdc_first_trailing_zero_ull (x) \
385 : sizeof (x) == 4 ? stdc_first_trailing_zero_ui (x) \
386 : sizeof (x) == 2 ? stdc_first_trailing_zero_us (__pacify_uint16 (x)) \
387 : stdc_first_trailing_zero_uc (__pacify_uint8 (x)))
389 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
390 static __always_inline unsigned int
391 __ftz64_inline (uint64_t __x)
393 return __x == (uint64_t) -1 ? 0 : 1 + __cto64_inline (__x);
396 static __always_inline unsigned int
397 __ftz32_inline (uint32_t __x)
399 return __x == (uint32_t) -1 ? 0 : 1 + __cto32_inline (__x);
402 static __always_inline unsigned int
403 __ftz16_inline (uint16_t __x)
405 return __x == (uint16_t) -1 ? 0 : 1 + __cto16_inline (__x);
408 static __always_inline unsigned int
409 __ftz8_inline (uint8_t __x)
411 return __x == (uint8_t) -1 ? 0 : 1 + __cto8_inline (__x);
414 # define stdc_first_trailing_zero_uc(x) (__ftz8_inline (x))
415 # define stdc_first_trailing_zero_us(x) (__ftz16_inline (x))
416 # define stdc_first_trailing_zero_ui(x) (__ftz32_inline (x))
417 # if __WORDSIZE == 64
418 # define stdc_first_trailing_zero_ul(x) (__ftz64_inline (x))
419 # else
420 # define stdc_first_trailing_zero_ul(x) (__ftz32_inline (x))
421 # endif
422 # define stdc_first_trailing_zero_ull(x) (__ftz64_inline (x))
423 #endif
425 /* First trailing one. */
426 extern unsigned int stdc_first_trailing_one_uc (unsigned char __x)
427 __THROW __attribute_const__;
428 extern unsigned int stdc_first_trailing_one_us (unsigned short __x)
429 __THROW __attribute_const__;
430 extern unsigned int stdc_first_trailing_one_ui (unsigned int __x)
431 __THROW __attribute_const__;
432 extern unsigned int stdc_first_trailing_one_ul (unsigned long int __x)
433 __THROW __attribute_const__;
434 __extension__
435 extern unsigned int stdc_first_trailing_one_ull (unsigned long long int __x)
436 __THROW __attribute_const__;
437 #define stdc_first_trailing_one(x) \
438 (sizeof (x) == 8 ? stdc_first_trailing_one_ull (x) \
439 : sizeof (x) == 4 ? stdc_first_trailing_one_ui (x) \
440 : sizeof (x) == 2 ? stdc_first_trailing_one_us (__pacify_uint16 (x)) \
441 : stdc_first_trailing_one_uc (__pacify_uint8 (x)))
443 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
444 static __always_inline unsigned int
445 __fto64_inline (uint64_t __x)
447 return __x == 0 ? 0 : 1 + __ctz64_inline (__x);
450 static __always_inline unsigned int
451 __fto32_inline (uint32_t __x)
453 return __x == 0 ? 0 : 1 + __ctz32_inline (__x);
456 static __always_inline unsigned int
457 __fto16_inline (uint16_t __x)
459 return __x == 0 ? 0 : 1 + __ctz16_inline (__x);
462 static __always_inline unsigned int
463 __fto8_inline (uint8_t __x)
465 return __x == 0 ? 0 : 1 + __ctz8_inline (__x);
468 # define stdc_first_trailing_one_uc(x) (__fto8_inline (x))
469 # define stdc_first_trailing_one_us(x) (__fto16_inline (x))
470 # define stdc_first_trailing_one_ui(x) (__fto32_inline (x))
471 # if __WORDSIZE == 64
472 # define stdc_first_trailing_one_ul(x) (__fto64_inline (x))
473 # else
474 # define stdc_first_trailing_one_ul(x) (__fto32_inline (x))
475 # endif
476 # define stdc_first_trailing_one_ull(x) (__fto64_inline (x))
477 #endif
479 /* Count zeros. */
480 extern unsigned int stdc_count_zeros_uc (unsigned char __x)
481 __THROW __attribute_const__;
482 extern unsigned int stdc_count_zeros_us (unsigned short __x)
483 __THROW __attribute_const__;
484 extern unsigned int stdc_count_zeros_ui (unsigned int __x)
485 __THROW __attribute_const__;
486 extern unsigned int stdc_count_zeros_ul (unsigned long int __x)
487 __THROW __attribute_const__;
488 __extension__
489 extern unsigned int stdc_count_zeros_ull (unsigned long long int __x)
490 __THROW __attribute_const__;
491 #define stdc_count_zeros(x) \
492 (stdc_count_zeros_ull (x) \
493 - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x))))
495 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
496 static __always_inline unsigned int
497 __cz64_inline (uint64_t __x)
499 return 64U - (unsigned int) __builtin_popcountll (__x);
502 static __always_inline unsigned int
503 __cz32_inline (uint32_t __x)
505 return 32U - (unsigned int) __builtin_popcount (__x);
508 static __always_inline unsigned int
509 __cz16_inline (uint16_t __x)
511 return 16U - (unsigned int) __builtin_popcount (__x);
514 static __always_inline unsigned int
515 __cz8_inline (uint8_t __x)
517 return 8U - (unsigned int) __builtin_popcount (__x);
520 # define stdc_count_zeros_uc(x) (__cz8_inline (x))
521 # define stdc_count_zeros_us(x) (__cz16_inline (x))
522 # define stdc_count_zeros_ui(x) (__cz32_inline (x))
523 # if __WORDSIZE == 64
524 # define stdc_count_zeros_ul(x) (__cz64_inline (x))
525 # else
526 # define stdc_count_zeros_ul(x) (__cz32_inline (x))
527 # endif
528 # define stdc_count_zeros_ull(x) (__cz64_inline (x))
529 #endif
531 /* Count ones. */
532 extern unsigned int stdc_count_ones_uc (unsigned char __x)
533 __THROW __attribute_const__;
534 extern unsigned int stdc_count_ones_us (unsigned short __x)
535 __THROW __attribute_const__;
536 extern unsigned int stdc_count_ones_ui (unsigned int __x)
537 __THROW __attribute_const__;
538 extern unsigned int stdc_count_ones_ul (unsigned long int __x)
539 __THROW __attribute_const__;
540 __extension__
541 extern unsigned int stdc_count_ones_ull (unsigned long long int __x)
542 __THROW __attribute_const__;
543 #define stdc_count_ones(x) (stdc_count_ones_ull (x))
545 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
546 static __always_inline unsigned int
547 __co64_inline (uint64_t __x)
549 return (unsigned int) __builtin_popcountll (__x);
552 static __always_inline unsigned int
553 __co32_inline (uint32_t __x)
555 return (unsigned int) __builtin_popcount (__x);
558 static __always_inline unsigned int
559 __co16_inline (uint16_t __x)
561 return (unsigned int) __builtin_popcount (__x);
564 static __always_inline unsigned int
565 __co8_inline (uint8_t __x)
567 return (unsigned int) __builtin_popcount (__x);
570 # define stdc_count_ones_uc(x) (__co8_inline (x))
571 # define stdc_count_ones_us(x) (__co16_inline (x))
572 # define stdc_count_ones_ui(x) (__co32_inline (x))
573 # if __WORDSIZE == 64
574 # define stdc_count_ones_ul(x) (__co64_inline (x))
575 # else
576 # define stdc_count_ones_ul(x) (__co32_inline (x))
577 # endif
578 # define stdc_count_ones_ull(x) (__co64_inline (x))
579 #endif
581 /* Single-bit check. */
582 extern bool stdc_has_single_bit_uc (unsigned char __x)
583 __THROW __attribute_const__;
584 extern bool stdc_has_single_bit_us (unsigned short __x)
585 __THROW __attribute_const__;
586 extern bool stdc_has_single_bit_ui (unsigned int __x)
587 __THROW __attribute_const__;
588 extern bool stdc_has_single_bit_ul (unsigned long int __x)
589 __THROW __attribute_const__;
590 __extension__
591 extern bool stdc_has_single_bit_ull (unsigned long long int __x)
592 __THROW __attribute_const__;
593 #define stdc_has_single_bit(x) \
594 ((bool) (sizeof (x) <= sizeof (unsigned int) \
595 ? stdc_has_single_bit_ui (x) \
596 : stdc_has_single_bit_ull (x)))
598 static __always_inline bool
599 __hsb64_inline (uint64_t __x)
601 return (__x ^ (__x - 1)) > __x - 1;
604 static __always_inline bool
605 __hsb32_inline (uint32_t __x)
607 return (__x ^ (__x - 1)) > __x - 1;
610 static __always_inline bool
611 __hsb16_inline (uint16_t __x)
613 return (__x ^ (__x - 1)) > __x - 1;
616 static __always_inline bool
617 __hsb8_inline (uint8_t __x)
619 return (__x ^ (__x - 1)) > __x - 1;
622 #define stdc_has_single_bit_uc(x) (__hsb8_inline (x))
623 #define stdc_has_single_bit_us(x) (__hsb16_inline (x))
624 #define stdc_has_single_bit_ui(x) (__hsb32_inline (x))
625 #if __WORDSIZE == 64
626 # define stdc_has_single_bit_ul(x) (__hsb64_inline (x))
627 #else
628 # define stdc_has_single_bit_ul(x) (__hsb32_inline (x))
629 #endif
630 #define stdc_has_single_bit_ull(x) (__hsb64_inline (x))
632 /* Bit width. */
633 extern unsigned int stdc_bit_width_uc (unsigned char __x)
634 __THROW __attribute_const__;
635 extern unsigned int stdc_bit_width_us (unsigned short __x)
636 __THROW __attribute_const__;
637 extern unsigned int stdc_bit_width_ui (unsigned int __x)
638 __THROW __attribute_const__;
639 extern unsigned int stdc_bit_width_ul (unsigned long int __x)
640 __THROW __attribute_const__;
641 __extension__
642 extern unsigned int stdc_bit_width_ull (unsigned long long int __x)
643 __THROW __attribute_const__;
644 #define stdc_bit_width(x) (stdc_bit_width_ull (x))
646 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
647 static __always_inline unsigned int
648 __bw64_inline (uint64_t __x)
650 return 64 - __clz64_inline (__x);
653 static __always_inline unsigned int
654 __bw32_inline (uint32_t __x)
656 return 32 - __clz32_inline (__x);
659 static __always_inline unsigned int
660 __bw16_inline (uint16_t __x)
662 return 16 - __clz16_inline (__x);
665 static __always_inline unsigned int
666 __bw8_inline (uint8_t __x)
668 return 8 - __clz8_inline (__x);
671 # define stdc_bit_width_uc(x) (__bw8_inline (x))
672 # define stdc_bit_width_us(x) (__bw16_inline (x))
673 # define stdc_bit_width_ui(x) (__bw32_inline (x))
674 # if __WORDSIZE == 64
675 # define stdc_bit_width_ul(x) (__bw64_inline (x))
676 # else
677 # define stdc_bit_width_ul(x) (__bw32_inline (x))
678 # endif
679 # define stdc_bit_width_ull(x) (__bw64_inline (x))
680 #endif
682 /* Bit floor. */
683 extern unsigned char stdc_bit_floor_uc (unsigned char __x)
684 __THROW __attribute_const__;
685 extern unsigned short stdc_bit_floor_us (unsigned short __x)
686 __THROW __attribute_const__;
687 extern unsigned int stdc_bit_floor_ui (unsigned int __x)
688 __THROW __attribute_const__;
689 extern unsigned long int stdc_bit_floor_ul (unsigned long int __x)
690 __THROW __attribute_const__;
691 __extension__
692 extern unsigned long long int stdc_bit_floor_ull (unsigned long long int __x)
693 __THROW __attribute_const__;
694 #define stdc_bit_floor(x) ((__typeof (x)) stdc_bit_floor_ull (x))
696 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
697 static __always_inline uint64_t
698 __bf64_inline (uint64_t __x)
700 return __x == 0 ? 0 : ((uint64_t) 1) << (__bw64_inline (__x) - 1);
703 static __always_inline uint32_t
704 __bf32_inline (uint32_t __x)
706 return __x == 0 ? 0 : ((uint32_t) 1) << (__bw32_inline (__x) - 1);
709 static __always_inline uint16_t
710 __bf16_inline (uint16_t __x)
712 return __pacify_uint16 (__x == 0
713 ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1));
716 static __always_inline uint8_t
717 __bf8_inline (uint8_t __x)
719 return __pacify_uint8 (__x == 0
720 ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1));
723 # define stdc_bit_floor_uc(x) ((unsigned char) __bf8_inline (x))
724 # define stdc_bit_floor_us(x) ((unsigned short) __bf16_inline (x))
725 # define stdc_bit_floor_ui(x) ((unsigned int) __bf32_inline (x))
726 # if __WORDSIZE == 64
727 # define stdc_bit_floor_ul(x) ((unsigned long int) __bf64_inline (x))
728 # else
729 # define stdc_bit_floor_ul(x) ((unsigned long int) __bf32_inline (x))
730 # endif
731 # define stdc_bit_floor_ull(x) ((unsigned long long int) __bf64_inline (x))
732 #endif
734 /* Bit ceiling. */
735 extern unsigned char stdc_bit_ceil_uc (unsigned char __x)
736 __THROW __attribute_const__;
737 extern unsigned short stdc_bit_ceil_us (unsigned short __x)
738 __THROW __attribute_const__;
739 extern unsigned int stdc_bit_ceil_ui (unsigned int __x)
740 __THROW __attribute_const__;
741 extern unsigned long int stdc_bit_ceil_ul (unsigned long int __x)
742 __THROW __attribute_const__;
743 __extension__
744 extern unsigned long long int stdc_bit_ceil_ull (unsigned long long int __x)
745 __THROW __attribute_const__;
746 #define stdc_bit_ceil(x) ((__typeof (x)) stdc_bit_ceil_ull (x))
748 #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
749 static __always_inline uint64_t
750 __bc64_inline (uint64_t __x)
752 return __x <= 1 ? 1 : ((uint64_t) 2) << (__bw64_inline (__x - 1) - 1);
755 static __always_inline uint32_t
756 __bc32_inline (uint32_t __x)
758 return __x <= 1 ? 1 : ((uint32_t) 2) << (__bw32_inline (__x - 1) - 1);
761 static __always_inline uint16_t
762 __bc16_inline (uint16_t __x)
764 return __pacify_uint16 (__x <= 1
766 : ((uint16_t) 2)
767 << (__bw16_inline ((uint16_t) (__x - 1)) - 1));
770 static __always_inline uint8_t
771 __bc8_inline (uint8_t __x)
773 return __pacify_uint8 (__x <= 1
775 : ((uint8_t) 2)
776 << (__bw8_inline ((uint8_t) (__x - 1)) - 1));
779 # define stdc_bit_ceil_uc(x) ((unsigned char) __bc8_inline (x))
780 # define stdc_bit_ceil_us(x) ((unsigned short) __bc16_inline (x))
781 # define stdc_bit_ceil_ui(x) ((unsigned int) __bc32_inline (x))
782 # if __WORDSIZE == 64
783 # define stdc_bit_ceil_ul(x) ((unsigned long int) __bc64_inline (x))
784 # else
785 # define stdc_bit_ceil_ul(x) ((unsigned long int) __bc32_inline (x))
786 # endif
787 # define stdc_bit_ceil_ull(x) ((unsigned long long int) __bc64_inline (x))
788 #endif
790 __END_DECLS
792 #endif /* _STDBIT_H */