]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/stdbit.h
Open master branch for glibc 2.40 development
[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__;
67#define stdc_leading_zeros(x) \
68 (stdc_leading_zeros_ull (x) \
69 - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x))))
70
71#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
72static __always_inline unsigned int
73__clz64_inline (uint64_t __x)
74{
75 return __x == 0 ? 64U : (unsigned int) __builtin_clzll (__x);
76}
77
78static __always_inline unsigned int
79__clz32_inline (uint32_t __x)
80{
81 return __x == 0 ? 32U : (unsigned int) __builtin_clz (__x);
82}
83
84static __always_inline unsigned int
85__clz16_inline (uint16_t __x)
86{
87 return __clz32_inline (__x) - 16;
88}
89
90static __always_inline unsigned int
91__clz8_inline (uint8_t __x)
92{
93 return __clz32_inline (__x) - 24;
94}
95
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
106
107/* Count leading ones. */
108extern unsigned int stdc_leading_ones_uc (unsigned char __x)
109 __THROW __attribute_const__;
110extern unsigned int stdc_leading_ones_us (unsigned short __x)
111 __THROW __attribute_const__;
112extern unsigned int stdc_leading_ones_ui (unsigned int __x)
113 __THROW __attribute_const__;
114extern unsigned int stdc_leading_ones_ul (unsigned long int __x)
115 __THROW __attribute_const__;
116__extension__
117extern 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))))
122
123#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
124static __always_inline unsigned int
125__clo64_inline (uint64_t __x)
126{
127 return __clz64_inline (~__x);
128}
129
130static __always_inline unsigned int
131__clo32_inline (uint32_t __x)
132{
133 return __clz32_inline (~__x);
134}
135
136static __always_inline unsigned int
137__clo16_inline (uint16_t __x)
138{
c8e31fbf 139 return __clz16_inline (__pacify_uint16 (~__x));
b34b46b8
JM
140}
141
142static __always_inline unsigned int
143__clo8_inline (uint8_t __x)
144{
c8e31fbf 145 return __clz8_inline (__pacify_uint8 (~__x));
b34b46b8
JM
146}
147
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
158
159/* Count trailing zeros. */
160extern unsigned int stdc_trailing_zeros_uc (unsigned char __x)
161 __THROW __attribute_const__;
162extern unsigned int stdc_trailing_zeros_us (unsigned short __x)
163 __THROW __attribute_const__;
164extern unsigned int stdc_trailing_zeros_ui (unsigned int __x)
165 __THROW __attribute_const__;
166extern unsigned int stdc_trailing_zeros_ul (unsigned long int __x)
167 __THROW __attribute_const__;
168__extension__
169extern 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) \
48ef5aeb
AZ
174 : sizeof (x) == 2 ? stdc_trailing_zeros_us (__pacify_uint16 (x)) \
175 : stdc_trailing_zeros_uc (__pacify_uint8 (x)))
b34b46b8
JM
176
177#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
178static __always_inline unsigned int
179__ctz64_inline (uint64_t __x)
180{
181 return __x == 0 ? 64U : (unsigned int) __builtin_ctzll (__x);
182}
183
184static __always_inline unsigned int
185__ctz32_inline (uint32_t __x)
186{
187 return __x == 0 ? 32U : (unsigned int) __builtin_ctz (__x);
188}
189
190static __always_inline unsigned int
191__ctz16_inline (uint16_t __x)
192{
193 return __x == 0 ? 16U : (unsigned int) __builtin_ctz (__x);
194}
195
196static __always_inline unsigned int
197__ctz8_inline (uint8_t __x)
198{
199 return __x == 0 ? 8U : (unsigned int) __builtin_ctz (__x);
200}
201
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
212
213/* Count trailing ones. */
214extern unsigned int stdc_trailing_ones_uc (unsigned char __x)
215 __THROW __attribute_const__;
216extern unsigned int stdc_trailing_ones_us (unsigned short __x)
217 __THROW __attribute_const__;
218extern unsigned int stdc_trailing_ones_ui (unsigned int __x)
219 __THROW __attribute_const__;
220extern unsigned int stdc_trailing_ones_ul (unsigned long int __x)
221 __THROW __attribute_const__;
222__extension__
223extern 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))
226
227#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
228static __always_inline unsigned int
229__cto64_inline (uint64_t __x)
230{
231 return __ctz64_inline (~__x);
232}
233
234static __always_inline unsigned int
235__cto32_inline (uint32_t __x)
236{
237 return __ctz32_inline (~__x);
238}
239
240static __always_inline unsigned int
241__cto16_inline (uint16_t __x)
242{
c8e31fbf 243 return __ctz16_inline (__pacify_uint16 (~__x));
b34b46b8
JM
244}
245
246static __always_inline unsigned int
247__cto8_inline (uint8_t __x)
248{
c8e31fbf 249 return __ctz8_inline (__pacify_uint8 (~__x));
b34b46b8
JM
250}
251
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
262
263/* First leading zero. */
264extern unsigned int stdc_first_leading_zero_uc (unsigned char __x)
265 __THROW __attribute_const__;
266extern unsigned int stdc_first_leading_zero_us (unsigned short __x)
267 __THROW __attribute_const__;
268extern unsigned int stdc_first_leading_zero_ui (unsigned int __x)
269 __THROW __attribute_const__;
270extern unsigned int stdc_first_leading_zero_ul (unsigned long int __x)
271 __THROW __attribute_const__;
272__extension__
273extern 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) \
48ef5aeb
AZ
278 : sizeof (x) == 2 ? stdc_first_leading_zero_us (__pacify_uint16 (x)) \
279 : stdc_first_leading_zero_uc (__pacify_uint8 (x)))
b34b46b8
JM
280
281#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
282static __always_inline unsigned int
283__flz64_inline (uint64_t __x)
284{
285 return __x == (uint64_t) -1 ? 0 : 1 + __clo64_inline (__x);
286}
287
288static __always_inline unsigned int
289__flz32_inline (uint32_t __x)
290{
291 return __x == (uint32_t) -1 ? 0 : 1 + __clo32_inline (__x);
292}
293
294static __always_inline unsigned int
295__flz16_inline (uint16_t __x)
296{
297 return __x == (uint16_t) -1 ? 0 : 1 + __clo16_inline (__x);
298}
299
300static __always_inline unsigned int
301__flz8_inline (uint8_t __x)
302{
303 return __x == (uint8_t) -1 ? 0 : 1 + __clo8_inline (__x);
304}
305
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
316
317/* First leading one. */
318extern unsigned int stdc_first_leading_one_uc (unsigned char __x)
319 __THROW __attribute_const__;
320extern unsigned int stdc_first_leading_one_us (unsigned short __x)
321 __THROW __attribute_const__;
322extern unsigned int stdc_first_leading_one_ui (unsigned int __x)
323 __THROW __attribute_const__;
324extern unsigned int stdc_first_leading_one_ul (unsigned long int __x)
325 __THROW __attribute_const__;
326__extension__
327extern 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) \
48ef5aeb
AZ
332 : sizeof (x) == 2 ? stdc_first_leading_one_us (__pacify_uint16 (x)) \
333 : stdc_first_leading_one_uc (__pacify_uint8 (x)))
b34b46b8
JM
334
335#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
336static __always_inline unsigned int
337__flo64_inline (uint64_t __x)
338{
339 return __x == 0 ? 0 : 1 + __clz64_inline (__x);
340}
341
342static __always_inline unsigned int
343__flo32_inline (uint32_t __x)
344{
345 return __x == 0 ? 0 : 1 + __clz32_inline (__x);
346}
347
348static __always_inline unsigned int
349__flo16_inline (uint16_t __x)
350{
351 return __x == 0 ? 0 : 1 + __clz16_inline (__x);
352}
353
354static __always_inline unsigned int
355__flo8_inline (uint8_t __x)
356{
357 return __x == 0 ? 0 : 1 + __clz8_inline (__x);
358}
359
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
370
371/* First trailing zero. */
372extern unsigned int stdc_first_trailing_zero_uc (unsigned char __x)
373 __THROW __attribute_const__;
374extern unsigned int stdc_first_trailing_zero_us (unsigned short __x)
375 __THROW __attribute_const__;
376extern unsigned int stdc_first_trailing_zero_ui (unsigned int __x)
377 __THROW __attribute_const__;
378extern unsigned int stdc_first_trailing_zero_ul (unsigned long int __x)
379 __THROW __attribute_const__;
380__extension__
381extern 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) \
48ef5aeb
AZ
386 : sizeof (x) == 2 ? stdc_first_trailing_zero_us (__pacify_uint16 (x)) \
387 : stdc_first_trailing_zero_uc (__pacify_uint8 (x)))
b34b46b8
JM
388
389#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
390static __always_inline unsigned int
391__ftz64_inline (uint64_t __x)
392{
393 return __x == (uint64_t) -1 ? 0 : 1 + __cto64_inline (__x);
394}
395
396static __always_inline unsigned int
397__ftz32_inline (uint32_t __x)
398{
399 return __x == (uint32_t) -1 ? 0 : 1 + __cto32_inline (__x);
400}
401
402static __always_inline unsigned int
403__ftz16_inline (uint16_t __x)
404{
405 return __x == (uint16_t) -1 ? 0 : 1 + __cto16_inline (__x);
406}
407
408static __always_inline unsigned int
409__ftz8_inline (uint8_t __x)
410{
411 return __x == (uint8_t) -1 ? 0 : 1 + __cto8_inline (__x);
412}
413
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
424
425/* First trailing one. */
426extern unsigned int stdc_first_trailing_one_uc (unsigned char __x)
427 __THROW __attribute_const__;
428extern unsigned int stdc_first_trailing_one_us (unsigned short __x)
429 __THROW __attribute_const__;
430extern unsigned int stdc_first_trailing_one_ui (unsigned int __x)
431 __THROW __attribute_const__;
432extern unsigned int stdc_first_trailing_one_ul (unsigned long int __x)
433 __THROW __attribute_const__;
434__extension__
435extern 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) \
48ef5aeb
AZ
440 : sizeof (x) == 2 ? stdc_first_trailing_one_us (__pacify_uint16 (x)) \
441 : stdc_first_trailing_one_uc (__pacify_uint8 (x)))
b34b46b8
JM
442
443#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll)
444static __always_inline unsigned int
445__fto64_inline (uint64_t __x)
446{
447 return __x == 0 ? 0 : 1 + __ctz64_inline (__x);
448}
449
450static __always_inline unsigned int
451__fto32_inline (uint32_t __x)
452{
453 return __x == 0 ? 0 : 1 + __ctz32_inline (__x);
454}
455
456static __always_inline unsigned int
457__fto16_inline (uint16_t __x)
458{
459 return __x == 0 ? 0 : 1 + __ctz16_inline (__x);
460}
461
462static __always_inline unsigned int
463__fto8_inline (uint8_t __x)
464{
465 return __x == 0 ? 0 : 1 + __ctz8_inline (__x);
466}
467
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
478
479/* Count zeros. */
480extern unsigned int stdc_count_zeros_uc (unsigned char __x)
481 __THROW __attribute_const__;
482extern unsigned int stdc_count_zeros_us (unsigned short __x)
483 __THROW __attribute_const__;
484extern unsigned int stdc_count_zeros_ui (unsigned int __x)
485 __THROW __attribute_const__;
486extern unsigned int stdc_count_zeros_ul (unsigned long int __x)
487 __THROW __attribute_const__;
488__extension__
489extern 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))))
494
495#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
496static __always_inline unsigned int
497__cz64_inline (uint64_t __x)
498{
499 return 64U - (unsigned int) __builtin_popcountll (__x);
500}
501
502static __always_inline unsigned int
503__cz32_inline (uint32_t __x)
504{
505 return 32U - (unsigned int) __builtin_popcount (__x);
506}
507
508static __always_inline unsigned int
509__cz16_inline (uint16_t __x)
510{
511 return 16U - (unsigned int) __builtin_popcount (__x);
512}
513
514static __always_inline unsigned int
515__cz8_inline (uint8_t __x)
516{
517 return 8U - (unsigned int) __builtin_popcount (__x);
518}
519
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
530
531/* Count ones. */
532extern unsigned int stdc_count_ones_uc (unsigned char __x)
533 __THROW __attribute_const__;
534extern unsigned int stdc_count_ones_us (unsigned short __x)
535 __THROW __attribute_const__;
536extern unsigned int stdc_count_ones_ui (unsigned int __x)
537 __THROW __attribute_const__;
538extern unsigned int stdc_count_ones_ul (unsigned long int __x)
539 __THROW __attribute_const__;
540__extension__
541extern 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))
544
545#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll)
546static __always_inline unsigned int
547__co64_inline (uint64_t __x)
548{
549 return (unsigned int) __builtin_popcountll (__x);
550}
551
552static __always_inline unsigned int
553__co32_inline (uint32_t __x)
554{
555 return (unsigned int) __builtin_popcount (__x);
556}
557
558static __always_inline unsigned int
559__co16_inline (uint16_t __x)
560{
561 return (unsigned int) __builtin_popcount (__x);
562}
563
564static __always_inline unsigned int
565__co8_inline (uint8_t __x)
566{
567 return (unsigned int) __builtin_popcount (__x);
568}
569
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
580
581/* Single-bit check. */
582extern bool stdc_has_single_bit_uc (unsigned char __x)
583 __THROW __attribute_const__;
584extern bool stdc_has_single_bit_us (unsigned short __x)
585 __THROW __attribute_const__;
586extern bool stdc_has_single_bit_ui (unsigned int __x)
587 __THROW __attribute_const__;
588extern bool stdc_has_single_bit_ul (unsigned long int __x)
589 __THROW __attribute_const__;
590__extension__
591extern 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)))
597
598static __always_inline bool
599__hsb64_inline (uint64_t __x)
600{
601 return (__x ^ (__x - 1)) > __x - 1;
602}
603
604static __always_inline bool
605__hsb32_inline (uint32_t __x)
606{
607 return (__x ^ (__x - 1)) > __x - 1;
608}
609
610static __always_inline bool
611__hsb16_inline (uint16_t __x)
612{
613 return (__x ^ (__x - 1)) > __x - 1;
614}
615
616static __always_inline bool
617__hsb8_inline (uint8_t __x)
618{
619 return (__x ^ (__x - 1)) > __x - 1;
620}
621
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))
631
632/* Bit width. */
633extern unsigned int stdc_bit_width_uc (unsigned char __x)
634 __THROW __attribute_const__;
635extern unsigned int stdc_bit_width_us (unsigned short __x)
636 __THROW __attribute_const__;
637extern unsigned int stdc_bit_width_ui (unsigned int __x)
638 __THROW __attribute_const__;
639extern unsigned int stdc_bit_width_ul (unsigned long int __x)
640 __THROW __attribute_const__;
641__extension__
642extern 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))
645
646#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
647static __always_inline unsigned int
648__bw64_inline (uint64_t __x)
649{
650 return 64 - __clz64_inline (__x);
651}
652
653static __always_inline unsigned int
654__bw32_inline (uint32_t __x)
655{
656 return 32 - __clz32_inline (__x);
657}
658
659static __always_inline unsigned int
660__bw16_inline (uint16_t __x)
661{
662 return 16 - __clz16_inline (__x);
663}
664
665static __always_inline unsigned int
666__bw8_inline (uint8_t __x)
667{
668 return 8 - __clz8_inline (__x);
669}
670
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
681
682/* Bit floor. */
683extern unsigned char stdc_bit_floor_uc (unsigned char __x)
684 __THROW __attribute_const__;
685extern unsigned short stdc_bit_floor_us (unsigned short __x)
686 __THROW __attribute_const__;
687extern unsigned int stdc_bit_floor_ui (unsigned int __x)
688 __THROW __attribute_const__;
689extern unsigned long int stdc_bit_floor_ul (unsigned long int __x)
690 __THROW __attribute_const__;
691__extension__
692extern 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))
695
696#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
697static __always_inline uint64_t
698__bf64_inline (uint64_t __x)
699{
700 return __x == 0 ? 0 : ((uint64_t) 1) << (__bw64_inline (__x) - 1);
701}
702
703static __always_inline uint32_t
704__bf32_inline (uint32_t __x)
705{
706 return __x == 0 ? 0 : ((uint32_t) 1) << (__bw32_inline (__x) - 1);
707}
708
709static __always_inline uint16_t
710__bf16_inline (uint16_t __x)
711{
c8e31fbf
AZ
712 return __pacify_uint16 (__x == 0
713 ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1));
b34b46b8
JM
714}
715
716static __always_inline uint8_t
717__bf8_inline (uint8_t __x)
718{
c8e31fbf
AZ
719 return __pacify_uint8 (__x == 0
720 ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1));
b34b46b8
JM
721}
722
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
733
734/* Bit ceiling. */
735extern unsigned char stdc_bit_ceil_uc (unsigned char __x)
736 __THROW __attribute_const__;
737extern unsigned short stdc_bit_ceil_us (unsigned short __x)
738 __THROW __attribute_const__;
739extern unsigned int stdc_bit_ceil_ui (unsigned int __x)
740 __THROW __attribute_const__;
741extern unsigned long int stdc_bit_ceil_ul (unsigned long int __x)
742 __THROW __attribute_const__;
743__extension__
744extern 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))
747
748#if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll)
749static __always_inline uint64_t
750__bc64_inline (uint64_t __x)
751{
752 return __x <= 1 ? 1 : ((uint64_t) 2) << (__bw64_inline (__x - 1) - 1);
753}
754
755static __always_inline uint32_t
756__bc32_inline (uint32_t __x)
757{
758 return __x <= 1 ? 1 : ((uint32_t) 2) << (__bw32_inline (__x - 1) - 1);
759}
760
761static __always_inline uint16_t
762__bc16_inline (uint16_t __x)
763{
c8e31fbf
AZ
764 return __pacify_uint16 (__x <= 1
765 ? 1
766 : ((uint16_t) 2)
767 << (__bw16_inline ((uint16_t) (__x - 1)) - 1));
b34b46b8
JM
768}
769
770static __always_inline uint8_t
771__bc8_inline (uint8_t __x)
772{
c8e31fbf
AZ
773 return __pacify_uint8 (__x <= 1
774 ? 1
775 : ((uint8_t) 2)
776 << (__bw8_inline ((uint8_t) (__x - 1)) - 1));
b34b46b8
JM
777}
778
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
789
790__END_DECLS
791
792#endif /* _STDBIT_H */