]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/stdbit.h
Use gcc __builtin_stdc_* builtins in stdbit.h if possible
[thirdparty/glibc.git] / stdlib / stdbit.h
CommitLineData
b34b46b8
JM
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.
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#ifndef _STDBIT_H
20#define _STDBIT_H 1
21
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>
35
36#define __STDC_VERSION_STDBIT_H__ 202311L
37
38#define __STDC_ENDIAN_LITTLE__ __LITTLE_ENDIAN
39#define __STDC_ENDIAN_BIG__ __BIG_ENDIAN
40#define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER
41
42__BEGIN_DECLS
43
c8e31fbf 44/* Use __pacify_uint16 (N) instead of (uint16_t) (N) when the cast is helpful
48ef5aeb
AZ
45 only to pacify older GCC (e.g., GCC 10 -Wconversion) or non-GCC (e.g
46 clang -Wimplicit-int-conversion). */
c8e31fbf
AZ
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
54
b34b46b8
JM
55/* Count leading zeros. */
56extern unsigned int stdc_leading_zeros_uc (unsigned char __x)
57 __THROW __attribute_const__;
58extern unsigned int stdc_leading_zeros_us (unsigned short __x)
59 __THROW __attribute_const__;
60extern unsigned int stdc_leading_zeros_ui (unsigned int __x)
61 __THROW __attribute_const__;
62extern unsigned int stdc_leading_zeros_ul (unsigned long int __x)
63 __THROW __attribute_const__;
64__extension__
65extern unsigned int stdc_leading_zeros_ull (unsigned long long int __x)
66 __THROW __attribute_const__;
da894963
JJ
67#if __glibc_has_builtin (__builtin_stdc_leading_zeros)
68# define stdc_leading_zeros(x) (__builtin_stdc_leading_zeros (x))
69#else
70# define stdc_leading_zeros(x) \
b34b46b8
JM
71 (stdc_leading_zeros_ull (x) \
72 - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x))))
da894963 73#endif
b34b46b8
JM
74
75#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
76static __always_inline unsigned int
77__clz64_inline (uint64_t __x)
78{
79 return __x == 0 ? 64U : (unsigned int) __builtin_clzll (__x);
80}
81
82static __always_inline unsigned int
83__clz32_inline (uint32_t __x)
84{
85 return __x == 0 ? 32U : (unsigned int) __builtin_clz (__x);
86}
87
88static __always_inline unsigned int
89__clz16_inline (uint16_t __x)
90{
91 return __clz32_inline (__x) - 16;
92}
93
94static __always_inline unsigned int
95__clz8_inline (uint8_t __x)
96{
97 return __clz32_inline (__x) - 24;
98}
99
100# define stdc_leading_zeros_uc(x) (__clz8_inline (x))
101# define stdc_leading_zeros_us(x) (__clz16_inline (x))
102# define stdc_leading_zeros_ui(x) (__clz32_inline (x))
103# if __WORDSIZE == 64
104# define stdc_leading_zeros_ul(x) (__clz64_inline (x))
105# else
106# define stdc_leading_zeros_ul(x) (__clz32_inline (x))
107# endif
108# define stdc_leading_zeros_ull(x) (__clz64_inline (x))
109#endif
110
111/* Count leading ones. */
112extern unsigned int stdc_leading_ones_uc (unsigned char __x)
113 __THROW __attribute_const__;
114extern unsigned int stdc_leading_ones_us (unsigned short __x)
115 __THROW __attribute_const__;
116extern unsigned int stdc_leading_ones_ui (unsigned int __x)
117 __THROW __attribute_const__;
118extern unsigned int stdc_leading_ones_ul (unsigned long int __x)
119 __THROW __attribute_const__;
120__extension__
121extern unsigned int stdc_leading_ones_ull (unsigned long long int __x)
122 __THROW __attribute_const__;
da894963
JJ
123#if __glibc_has_builtin (__builtin_stdc_leading_ones)
124# define stdc_leading_ones(x) (__builtin_stdc_leading_ones (x))
125#else
126# define stdc_leading_ones(x) \
b34b46b8
JM
127 (stdc_leading_ones_ull ((unsigned long long int) (x) \
128 << 8 * (sizeof (0ULL) - sizeof (x))))
da894963 129#endif
b34b46b8
JM
130
131#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
132static __always_inline unsigned int
133__clo64_inline (uint64_t __x)
134{
135 return __clz64_inline (~__x);
136}
137
138static __always_inline unsigned int
139__clo32_inline (uint32_t __x)
140{
141 return __clz32_inline (~__x);
142}
143
144static __always_inline unsigned int
145__clo16_inline (uint16_t __x)
146{
c8e31fbf 147 return __clz16_inline (__pacify_uint16 (~__x));
b34b46b8
JM
148}
149
150static __always_inline unsigned int
151__clo8_inline (uint8_t __x)
152{
c8e31fbf 153 return __clz8_inline (__pacify_uint8 (~__x));
b34b46b8
JM
154}
155
156# define stdc_leading_ones_uc(x) (__clo8_inline (x))
157# define stdc_leading_ones_us(x) (__clo16_inline (x))
158# define stdc_leading_ones_ui(x) (__clo32_inline (x))
159# if __WORDSIZE == 64
160# define stdc_leading_ones_ul(x) (__clo64_inline (x))
161# else
162# define stdc_leading_ones_ul(x) (__clo32_inline (x))
163# endif
164# define stdc_leading_ones_ull(x) (__clo64_inline (x))
165#endif
166
167/* Count trailing zeros. */
168extern unsigned int stdc_trailing_zeros_uc (unsigned char __x)
169 __THROW __attribute_const__;
170extern unsigned int stdc_trailing_zeros_us (unsigned short __x)
171 __THROW __attribute_const__;
172extern unsigned int stdc_trailing_zeros_ui (unsigned int __x)
173 __THROW __attribute_const__;
174extern unsigned int stdc_trailing_zeros_ul (unsigned long int __x)
175 __THROW __attribute_const__;
176__extension__
177extern unsigned int stdc_trailing_zeros_ull (unsigned long long int __x)
178 __THROW __attribute_const__;
da894963
JJ
179#if __glibc_has_builtin (__builtin_stdc_trailing_zeros)
180# define stdc_trailing_zeros(x) (__builtin_stdc_trailing_zeros (x))
181#else
182# define stdc_trailing_zeros(x) \
b34b46b8
JM
183 (sizeof (x) == 8 ? stdc_trailing_zeros_ull (x) \
184 : sizeof (x) == 4 ? stdc_trailing_zeros_ui (x) \
48ef5aeb
AZ
185 : sizeof (x) == 2 ? stdc_trailing_zeros_us (__pacify_uint16 (x)) \
186 : stdc_trailing_zeros_uc (__pacify_uint8 (x)))
da894963 187#endif
b34b46b8
JM
188
189#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
190static __always_inline unsigned int
191__ctz64_inline (uint64_t __x)
192{
193 return __x == 0 ? 64U : (unsigned int) __builtin_ctzll (__x);
194}
195
196static __always_inline unsigned int
197__ctz32_inline (uint32_t __x)
198{
199 return __x == 0 ? 32U : (unsigned int) __builtin_ctz (__x);
200}
201
202static __always_inline unsigned int
203__ctz16_inline (uint16_t __x)
204{
205 return __x == 0 ? 16U : (unsigned int) __builtin_ctz (__x);
206}
207
208static __always_inline unsigned int
209__ctz8_inline (uint8_t __x)
210{
211 return __x == 0 ? 8U : (unsigned int) __builtin_ctz (__x);
212}
213
214# define stdc_trailing_zeros_uc(x) (__ctz8_inline (x))
215# define stdc_trailing_zeros_us(x) (__ctz16_inline (x))
216# define stdc_trailing_zeros_ui(x) (__ctz32_inline (x))
217# if __WORDSIZE == 64
218# define stdc_trailing_zeros_ul(x) (__ctz64_inline (x))
219# else
220# define stdc_trailing_zeros_ul(x) (__ctz32_inline (x))
221# endif
222# define stdc_trailing_zeros_ull(x) (__ctz64_inline (x))
223#endif
224
225/* Count trailing ones. */
226extern unsigned int stdc_trailing_ones_uc (unsigned char __x)
227 __THROW __attribute_const__;
228extern unsigned int stdc_trailing_ones_us (unsigned short __x)
229 __THROW __attribute_const__;
230extern unsigned int stdc_trailing_ones_ui (unsigned int __x)
231 __THROW __attribute_const__;
232extern unsigned int stdc_trailing_ones_ul (unsigned long int __x)
233 __THROW __attribute_const__;
234__extension__
235extern unsigned int stdc_trailing_ones_ull (unsigned long long int __x)
236 __THROW __attribute_const__;
da894963
JJ
237#if __glibc_has_builtin (__builtin_stdc_trailing_ones)
238# define stdc_trailing_ones(x) (__builtin_stdc_trailing_ones (x))
239#else
240# define stdc_trailing_ones(x) (stdc_trailing_ones_ull (x))
241#endif
b34b46b8
JM
242
243#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
244static __always_inline unsigned int
245__cto64_inline (uint64_t __x)
246{
247 return __ctz64_inline (~__x);
248}
249
250static __always_inline unsigned int
251__cto32_inline (uint32_t __x)
252{
253 return __ctz32_inline (~__x);
254}
255
256static __always_inline unsigned int
257__cto16_inline (uint16_t __x)
258{
c8e31fbf 259 return __ctz16_inline (__pacify_uint16 (~__x));
b34b46b8
JM
260}
261
262static __always_inline unsigned int
263__cto8_inline (uint8_t __x)
264{
c8e31fbf 265 return __ctz8_inline (__pacify_uint8 (~__x));
b34b46b8
JM
266}
267
268# define stdc_trailing_ones_uc(x) (__cto8_inline (x))
269# define stdc_trailing_ones_us(x) (__cto16_inline (x))
270# define stdc_trailing_ones_ui(x) (__cto32_inline (x))
271# if __WORDSIZE == 64
272# define stdc_trailing_ones_ul(x) (__cto64_inline (x))
273# else
274# define stdc_trailing_ones_ul(x) (__cto32_inline (x))
275# endif
276# define stdc_trailing_ones_ull(x) (__cto64_inline (x))
277#endif
278
279/* First leading zero. */
280extern unsigned int stdc_first_leading_zero_uc (unsigned char __x)
281 __THROW __attribute_const__;
282extern unsigned int stdc_first_leading_zero_us (unsigned short __x)
283 __THROW __attribute_const__;
284extern unsigned int stdc_first_leading_zero_ui (unsigned int __x)
285 __THROW __attribute_const__;
286extern unsigned int stdc_first_leading_zero_ul (unsigned long int __x)
287 __THROW __attribute_const__;
288__extension__
289extern unsigned int stdc_first_leading_zero_ull (unsigned long long int __x)
290 __THROW __attribute_const__;
da894963
JJ
291#if __glibc_has_builtin (__builtin_stdc_first_leading_zero)
292# define stdc_first_leading_zero(x) (__builtin_stdc_first_leading_zero (x))
293#else
294# define stdc_first_leading_zero(x) \
b34b46b8
JM
295 (sizeof (x) == 8 ? stdc_first_leading_zero_ull (x) \
296 : sizeof (x) == 4 ? stdc_first_leading_zero_ui (x) \
48ef5aeb
AZ
297 : sizeof (x) == 2 ? stdc_first_leading_zero_us (__pacify_uint16 (x)) \
298 : stdc_first_leading_zero_uc (__pacify_uint8 (x)))
da894963 299#endif
b34b46b8
JM
300
301#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
302static __always_inline unsigned int
303__flz64_inline (uint64_t __x)
304{
305 return __x == (uint64_t) -1 ? 0 : 1 + __clo64_inline (__x);
306}
307
308static __always_inline unsigned int
309__flz32_inline (uint32_t __x)
310{
311 return __x == (uint32_t) -1 ? 0 : 1 + __clo32_inline (__x);
312}
313
314static __always_inline unsigned int
315__flz16_inline (uint16_t __x)
316{
317 return __x == (uint16_t) -1 ? 0 : 1 + __clo16_inline (__x);
318}
319
320static __always_inline unsigned int
321__flz8_inline (uint8_t __x)
322{
323 return __x == (uint8_t) -1 ? 0 : 1 + __clo8_inline (__x);
324}
325
326# define stdc_first_leading_zero_uc(x) (__flz8_inline (x))
327# define stdc_first_leading_zero_us(x) (__flz16_inline (x))
328# define stdc_first_leading_zero_ui(x) (__flz32_inline (x))
329# if __WORDSIZE == 64
330# define stdc_first_leading_zero_ul(x) (__flz64_inline (x))
331# else
332# define stdc_first_leading_zero_ul(x) (__flz32_inline (x))
333# endif
334# define stdc_first_leading_zero_ull(x) (__flz64_inline (x))
335#endif
336
337/* First leading one. */
338extern unsigned int stdc_first_leading_one_uc (unsigned char __x)
339 __THROW __attribute_const__;
340extern unsigned int stdc_first_leading_one_us (unsigned short __x)
341 __THROW __attribute_const__;
342extern unsigned int stdc_first_leading_one_ui (unsigned int __x)
343 __THROW __attribute_const__;
344extern unsigned int stdc_first_leading_one_ul (unsigned long int __x)
345 __THROW __attribute_const__;
346__extension__
347extern unsigned int stdc_first_leading_one_ull (unsigned long long int __x)
348 __THROW __attribute_const__;
da894963
JJ
349#if __glibc_has_builtin (__builtin_stdc_first_leading_one)
350# define stdc_first_leading_one(x) (__builtin_stdc_first_leading_one (x))
351#else
352# define stdc_first_leading_one(x) \
b34b46b8
JM
353 (sizeof (x) == 8 ? stdc_first_leading_one_ull (x) \
354 : sizeof (x) == 4 ? stdc_first_leading_one_ui (x) \
48ef5aeb
AZ
355 : sizeof (x) == 2 ? stdc_first_leading_one_us (__pacify_uint16 (x)) \
356 : stdc_first_leading_one_uc (__pacify_uint8 (x)))
da894963 357#endif
b34b46b8
JM
358
359#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
360static __always_inline unsigned int
361__flo64_inline (uint64_t __x)
362{
363 return __x == 0 ? 0 : 1 + __clz64_inline (__x);
364}
365
366static __always_inline unsigned int
367__flo32_inline (uint32_t __x)
368{
369 return __x == 0 ? 0 : 1 + __clz32_inline (__x);
370}
371
372static __always_inline unsigned int
373__flo16_inline (uint16_t __x)
374{
375 return __x == 0 ? 0 : 1 + __clz16_inline (__x);
376}
377
378static __always_inline unsigned int
379__flo8_inline (uint8_t __x)
380{
381 return __x == 0 ? 0 : 1 + __clz8_inline (__x);
382}
383
384# define stdc_first_leading_one_uc(x) (__flo8_inline (x))
385# define stdc_first_leading_one_us(x) (__flo16_inline (x))
386# define stdc_first_leading_one_ui(x) (__flo32_inline (x))
387# if __WORDSIZE == 64
388# define stdc_first_leading_one_ul(x) (__flo64_inline (x))
389# else
390# define stdc_first_leading_one_ul(x) (__flo32_inline (x))
391# endif
392# define stdc_first_leading_one_ull(x) (__flo64_inline (x))
393#endif
394
395/* First trailing zero. */
396extern unsigned int stdc_first_trailing_zero_uc (unsigned char __x)
397 __THROW __attribute_const__;
398extern unsigned int stdc_first_trailing_zero_us (unsigned short __x)
399 __THROW __attribute_const__;
400extern unsigned int stdc_first_trailing_zero_ui (unsigned int __x)
401 __THROW __attribute_const__;
402extern unsigned int stdc_first_trailing_zero_ul (unsigned long int __x)
403 __THROW __attribute_const__;
404__extension__
405extern unsigned int stdc_first_trailing_zero_ull (unsigned long long int __x)
406 __THROW __attribute_const__;
da894963
JJ
407#if __glibc_has_builtin (__builtin_stdc_first_trailing_zero)
408# define stdc_first_trailing_zero(x) (__builtin_stdc_first_trailing_zero (x))
409#else
410# define stdc_first_trailing_zero(x) \
b34b46b8
JM
411 (sizeof (x) == 8 ? stdc_first_trailing_zero_ull (x) \
412 : sizeof (x) == 4 ? stdc_first_trailing_zero_ui (x) \
48ef5aeb
AZ
413 : sizeof (x) == 2 ? stdc_first_trailing_zero_us (__pacify_uint16 (x)) \
414 : stdc_first_trailing_zero_uc (__pacify_uint8 (x)))
da894963 415#endif
b34b46b8
JM
416
417#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
418static __always_inline unsigned int
419__ftz64_inline (uint64_t __x)
420{
421 return __x == (uint64_t) -1 ? 0 : 1 + __cto64_inline (__x);
422}
423
424static __always_inline unsigned int
425__ftz32_inline (uint32_t __x)
426{
427 return __x == (uint32_t) -1 ? 0 : 1 + __cto32_inline (__x);
428}
429
430static __always_inline unsigned int
431__ftz16_inline (uint16_t __x)
432{
433 return __x == (uint16_t) -1 ? 0 : 1 + __cto16_inline (__x);
434}
435
436static __always_inline unsigned int
437__ftz8_inline (uint8_t __x)
438{
439 return __x == (uint8_t) -1 ? 0 : 1 + __cto8_inline (__x);
440}
441
442# define stdc_first_trailing_zero_uc(x) (__ftz8_inline (x))
443# define stdc_first_trailing_zero_us(x) (__ftz16_inline (x))
444# define stdc_first_trailing_zero_ui(x) (__ftz32_inline (x))
445# if __WORDSIZE == 64
446# define stdc_first_trailing_zero_ul(x) (__ftz64_inline (x))
447# else
448# define stdc_first_trailing_zero_ul(x) (__ftz32_inline (x))
449# endif
450# define stdc_first_trailing_zero_ull(x) (__ftz64_inline (x))
451#endif
452
453/* First trailing one. */
454extern unsigned int stdc_first_trailing_one_uc (unsigned char __x)
455 __THROW __attribute_const__;
456extern unsigned int stdc_first_trailing_one_us (unsigned short __x)
457 __THROW __attribute_const__;
458extern unsigned int stdc_first_trailing_one_ui (unsigned int __x)
459 __THROW __attribute_const__;
460extern unsigned int stdc_first_trailing_one_ul (unsigned long int __x)
461 __THROW __attribute_const__;
462__extension__
463extern unsigned int stdc_first_trailing_one_ull (unsigned long long int __x)
464 __THROW __attribute_const__;
da894963
JJ
465#if __glibc_has_builtin (__builtin_stdc_first_trailing_one)
466# define stdc_first_trailing_one(x) (__builtin_stdc_first_trailing_one (x))
467#else
468# define stdc_first_trailing_one(x) \
b34b46b8
JM
469 (sizeof (x) == 8 ? stdc_first_trailing_one_ull (x) \
470 : sizeof (x) == 4 ? stdc_first_trailing_one_ui (x) \
48ef5aeb
AZ
471 : sizeof (x) == 2 ? stdc_first_trailing_one_us (__pacify_uint16 (x)) \
472 : stdc_first_trailing_one_uc (__pacify_uint8 (x)))
da894963 473#endif
b34b46b8
JM
474
475#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
476static __always_inline unsigned int
477__fto64_inline (uint64_t __x)
478{
479 return __x == 0 ? 0 : 1 + __ctz64_inline (__x);
480}
481
482static __always_inline unsigned int
483__fto32_inline (uint32_t __x)
484{
485 return __x == 0 ? 0 : 1 + __ctz32_inline (__x);
486}
487
488static __always_inline unsigned int
489__fto16_inline (uint16_t __x)
490{
491 return __x == 0 ? 0 : 1 + __ctz16_inline (__x);
492}
493
494static __always_inline unsigned int
495__fto8_inline (uint8_t __x)
496{
497 return __x == 0 ? 0 : 1 + __ctz8_inline (__x);
498}
499
500# define stdc_first_trailing_one_uc(x) (__fto8_inline (x))
501# define stdc_first_trailing_one_us(x) (__fto16_inline (x))
502# define stdc_first_trailing_one_ui(x) (__fto32_inline (x))
503# if __WORDSIZE == 64
504# define stdc_first_trailing_one_ul(x) (__fto64_inline (x))
505# else
506# define stdc_first_trailing_one_ul(x) (__fto32_inline (x))
507# endif
508# define stdc_first_trailing_one_ull(x) (__fto64_inline (x))
509#endif
510
511/* Count zeros. */
512extern unsigned int stdc_count_zeros_uc (unsigned char __x)
513 __THROW __attribute_const__;
514extern unsigned int stdc_count_zeros_us (unsigned short __x)
515 __THROW __attribute_const__;
516extern unsigned int stdc_count_zeros_ui (unsigned int __x)
517 __THROW __attribute_const__;
518extern unsigned int stdc_count_zeros_ul (unsigned long int __x)
519 __THROW __attribute_const__;
520__extension__
521extern unsigned int stdc_count_zeros_ull (unsigned long long int __x)
522 __THROW __attribute_const__;
da894963
JJ
523#if __glibc_has_builtin (__builtin_stdc_count_zeros)
524# define stdc_count_zeros(x) (__builtin_stdc_count_zeros (x))
525#else
526# define stdc_count_zeros(x) \
b34b46b8
JM
527 (stdc_count_zeros_ull (x) \
528 - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x))))
da894963 529#endif
b34b46b8
JM
530
531#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
532static __always_inline unsigned int
533__cz64_inline (uint64_t __x)
534{
535 return 64U - (unsigned int) __builtin_popcountll (__x);
536}
537
538static __always_inline unsigned int
539__cz32_inline (uint32_t __x)
540{
541 return 32U - (unsigned int) __builtin_popcount (__x);
542}
543
544static __always_inline unsigned int
545__cz16_inline (uint16_t __x)
546{
547 return 16U - (unsigned int) __builtin_popcount (__x);
548}
549
550static __always_inline unsigned int
551__cz8_inline (uint8_t __x)
552{
553 return 8U - (unsigned int) __builtin_popcount (__x);
554}
555
556# define stdc_count_zeros_uc(x) (__cz8_inline (x))
557# define stdc_count_zeros_us(x) (__cz16_inline (x))
558# define stdc_count_zeros_ui(x) (__cz32_inline (x))
559# if __WORDSIZE == 64
560# define stdc_count_zeros_ul(x) (__cz64_inline (x))
561# else
562# define stdc_count_zeros_ul(x) (__cz32_inline (x))
563# endif
564# define stdc_count_zeros_ull(x) (__cz64_inline (x))
565#endif
566
567/* Count ones. */
568extern unsigned int stdc_count_ones_uc (unsigned char __x)
569 __THROW __attribute_const__;
570extern unsigned int stdc_count_ones_us (unsigned short __x)
571 __THROW __attribute_const__;
572extern unsigned int stdc_count_ones_ui (unsigned int __x)
573 __THROW __attribute_const__;
574extern unsigned int stdc_count_ones_ul (unsigned long int __x)
575 __THROW __attribute_const__;
576__extension__
577extern unsigned int stdc_count_ones_ull (unsigned long long int __x)
578 __THROW __attribute_const__;
da894963
JJ
579#if __glibc_has_builtin (__builtin_stdc_count_ones)
580# define stdc_count_ones(x) (__builtin_stdc_count_ones (x))
581#else
582# define stdc_count_ones(x) (stdc_count_ones_ull (x))
583#endif
b34b46b8
JM
584
585#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
586static __always_inline unsigned int
587__co64_inline (uint64_t __x)
588{
589 return (unsigned int) __builtin_popcountll (__x);
590}
591
592static __always_inline unsigned int
593__co32_inline (uint32_t __x)
594{
595 return (unsigned int) __builtin_popcount (__x);
596}
597
598static __always_inline unsigned int
599__co16_inline (uint16_t __x)
600{
601 return (unsigned int) __builtin_popcount (__x);
602}
603
604static __always_inline unsigned int
605__co8_inline (uint8_t __x)
606{
607 return (unsigned int) __builtin_popcount (__x);
608}
609
610# define stdc_count_ones_uc(x) (__co8_inline (x))
611# define stdc_count_ones_us(x) (__co16_inline (x))
612# define stdc_count_ones_ui(x) (__co32_inline (x))
613# if __WORDSIZE == 64
614# define stdc_count_ones_ul(x) (__co64_inline (x))
615# else
616# define stdc_count_ones_ul(x) (__co32_inline (x))
617# endif
618# define stdc_count_ones_ull(x) (__co64_inline (x))
619#endif
620
621/* Single-bit check. */
622extern bool stdc_has_single_bit_uc (unsigned char __x)
623 __THROW __attribute_const__;
624extern bool stdc_has_single_bit_us (unsigned short __x)
625 __THROW __attribute_const__;
626extern bool stdc_has_single_bit_ui (unsigned int __x)
627 __THROW __attribute_const__;
628extern bool stdc_has_single_bit_ul (unsigned long int __x)
629 __THROW __attribute_const__;
630__extension__
631extern bool stdc_has_single_bit_ull (unsigned long long int __x)
632 __THROW __attribute_const__;
da894963
JJ
633#if __glibc_has_builtin (__builtin_stdc_has_single_bit)
634# define stdc_has_single_bit(x) (__builtin_stdc_has_single_bit (x))
635#else
636# define stdc_has_single_bit(x) \
b34b46b8
JM
637 ((bool) (sizeof (x) <= sizeof (unsigned int) \
638 ? stdc_has_single_bit_ui (x) \
639 : stdc_has_single_bit_ull (x)))
da894963 640#endif
b34b46b8
JM
641
642static __always_inline bool
643__hsb64_inline (uint64_t __x)
644{
645 return (__x ^ (__x - 1)) > __x - 1;
646}
647
648static __always_inline bool
649__hsb32_inline (uint32_t __x)
650{
651 return (__x ^ (__x - 1)) > __x - 1;
652}
653
654static __always_inline bool
655__hsb16_inline (uint16_t __x)
656{
657 return (__x ^ (__x - 1)) > __x - 1;
658}
659
660static __always_inline bool
661__hsb8_inline (uint8_t __x)
662{
663 return (__x ^ (__x - 1)) > __x - 1;
664}
665
666#define stdc_has_single_bit_uc(x) (__hsb8_inline (x))
667#define stdc_has_single_bit_us(x) (__hsb16_inline (x))
668#define stdc_has_single_bit_ui(x) (__hsb32_inline (x))
669#if __WORDSIZE == 64
670# define stdc_has_single_bit_ul(x) (__hsb64_inline (x))
671#else
672# define stdc_has_single_bit_ul(x) (__hsb32_inline (x))
673#endif
674#define stdc_has_single_bit_ull(x) (__hsb64_inline (x))
675
676/* Bit width. */
677extern unsigned int stdc_bit_width_uc (unsigned char __x)
678 __THROW __attribute_const__;
679extern unsigned int stdc_bit_width_us (unsigned short __x)
680 __THROW __attribute_const__;
681extern unsigned int stdc_bit_width_ui (unsigned int __x)
682 __THROW __attribute_const__;
683extern unsigned int stdc_bit_width_ul (unsigned long int __x)
684 __THROW __attribute_const__;
685__extension__
686extern unsigned int stdc_bit_width_ull (unsigned long long int __x)
687 __THROW __attribute_const__;
da894963
JJ
688#if __glibc_has_builtin (__builtin_stdc_bit_width)
689# define stdc_bit_width(x) (__builtin_stdc_bit_width (x))
690#else
691# define stdc_bit_width(x) (stdc_bit_width_ull (x))
692#endif
b34b46b8
JM
693
694#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
695static __always_inline unsigned int
696__bw64_inline (uint64_t __x)
697{
698 return 64 - __clz64_inline (__x);
699}
700
701static __always_inline unsigned int
702__bw32_inline (uint32_t __x)
703{
704 return 32 - __clz32_inline (__x);
705}
706
707static __always_inline unsigned int
708__bw16_inline (uint16_t __x)
709{
710 return 16 - __clz16_inline (__x);
711}
712
713static __always_inline unsigned int
714__bw8_inline (uint8_t __x)
715{
716 return 8 - __clz8_inline (__x);
717}
718
719# define stdc_bit_width_uc(x) (__bw8_inline (x))
720# define stdc_bit_width_us(x) (__bw16_inline (x))
721# define stdc_bit_width_ui(x) (__bw32_inline (x))
722# if __WORDSIZE == 64
723# define stdc_bit_width_ul(x) (__bw64_inline (x))
724# else
725# define stdc_bit_width_ul(x) (__bw32_inline (x))
726# endif
727# define stdc_bit_width_ull(x) (__bw64_inline (x))
728#endif
729
730/* Bit floor. */
731extern unsigned char stdc_bit_floor_uc (unsigned char __x)
732 __THROW __attribute_const__;
733extern unsigned short stdc_bit_floor_us (unsigned short __x)
734 __THROW __attribute_const__;
735extern unsigned int stdc_bit_floor_ui (unsigned int __x)
736 __THROW __attribute_const__;
737extern unsigned long int stdc_bit_floor_ul (unsigned long int __x)
738 __THROW __attribute_const__;
739__extension__
740extern unsigned long long int stdc_bit_floor_ull (unsigned long long int __x)
741 __THROW __attribute_const__;
da894963
JJ
742#if __glibc_has_builtin (__builtin_stdc_bit_floor)
743# define stdc_bit_floor(x) (__builtin_stdc_bit_floor (x))
744#else
745# define stdc_bit_floor(x) ((__typeof (x)) stdc_bit_floor_ull (x))
746#endif
b34b46b8
JM
747
748#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
749static __always_inline uint64_t
750__bf64_inline (uint64_t __x)
751{
752 return __x == 0 ? 0 : ((uint64_t) 1) << (__bw64_inline (__x) - 1);
753}
754
755static __always_inline uint32_t
756__bf32_inline (uint32_t __x)
757{
758 return __x == 0 ? 0 : ((uint32_t) 1) << (__bw32_inline (__x) - 1);
759}
760
761static __always_inline uint16_t
762__bf16_inline (uint16_t __x)
763{
c8e31fbf
AZ
764 return __pacify_uint16 (__x == 0
765 ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1));
b34b46b8
JM
766}
767
768static __always_inline uint8_t
769__bf8_inline (uint8_t __x)
770{
c8e31fbf
AZ
771 return __pacify_uint8 (__x == 0
772 ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1));
b34b46b8
JM
773}
774
775# define stdc_bit_floor_uc(x) ((unsigned char) __bf8_inline (x))
776# define stdc_bit_floor_us(x) ((unsigned short) __bf16_inline (x))
777# define stdc_bit_floor_ui(x) ((unsigned int) __bf32_inline (x))
778# if __WORDSIZE == 64
779# define stdc_bit_floor_ul(x) ((unsigned long int) __bf64_inline (x))
780# else
781# define stdc_bit_floor_ul(x) ((unsigned long int) __bf32_inline (x))
782# endif
783# define stdc_bit_floor_ull(x) ((unsigned long long int) __bf64_inline (x))
784#endif
785
786/* Bit ceiling. */
787extern unsigned char stdc_bit_ceil_uc (unsigned char __x)
788 __THROW __attribute_const__;
789extern unsigned short stdc_bit_ceil_us (unsigned short __x)
790 __THROW __attribute_const__;
791extern unsigned int stdc_bit_ceil_ui (unsigned int __x)
792 __THROW __attribute_const__;
793extern unsigned long int stdc_bit_ceil_ul (unsigned long int __x)
794 __THROW __attribute_const__;
795__extension__
796extern unsigned long long int stdc_bit_ceil_ull (unsigned long long int __x)
797 __THROW __attribute_const__;
da894963
JJ
798#if __glibc_has_builtin (__builtin_stdc_bit_ceil)
799# define stdc_bit_ceil(x) (__builtin_stdc_bit_ceil (x))
800#else
801# define stdc_bit_ceil(x) ((__typeof (x)) stdc_bit_ceil_ull (x))
802#endif
b34b46b8
JM
803
804#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
805static __always_inline uint64_t
806__bc64_inline (uint64_t __x)
807{
808 return __x <= 1 ? 1 : ((uint64_t) 2) << (__bw64_inline (__x - 1) - 1);
809}
810
811static __always_inline uint32_t
812__bc32_inline (uint32_t __x)
813{
814 return __x <= 1 ? 1 : ((uint32_t) 2) << (__bw32_inline (__x - 1) - 1);
815}
816
817static __always_inline uint16_t
818__bc16_inline (uint16_t __x)
819{
c8e31fbf
AZ
820 return __pacify_uint16 (__x <= 1
821 ? 1
822 : ((uint16_t) 2)
823 << (__bw16_inline ((uint16_t) (__x - 1)) - 1));
b34b46b8
JM
824}
825
826static __always_inline uint8_t
827__bc8_inline (uint8_t __x)
828{
c8e31fbf
AZ
829 return __pacify_uint8 (__x <= 1
830 ? 1
831 : ((uint8_t) 2)
832 << (__bw8_inline ((uint8_t) (__x - 1)) - 1));
b34b46b8
JM
833}
834
835# define stdc_bit_ceil_uc(x) ((unsigned char) __bc8_inline (x))
836# define stdc_bit_ceil_us(x) ((unsigned short) __bc16_inline (x))
837# define stdc_bit_ceil_ui(x) ((unsigned int) __bc32_inline (x))
838# if __WORDSIZE == 64
839# define stdc_bit_ceil_ul(x) ((unsigned long int) __bc64_inline (x))
840# else
841# define stdc_bit_ceil_ul(x) ((unsigned long int) __bc32_inline (x))
842# endif
843# define stdc_bit_ceil_ull(x) ((unsigned long long int) __bc64_inline (x))
844#endif
845
846__END_DECLS
847
848#endif /* _STDBIT_H */