]>
Commit | Line | Data |
---|---|---|
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. */ |
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)))) | |
70 | ||
71 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) | |
72 | static __always_inline unsigned int | |
73 | __clz64_inline (uint64_t __x) | |
74 | { | |
75 | return __x == 0 ? 64U : (unsigned int) __builtin_clzll (__x); | |
76 | } | |
77 | ||
78 | static __always_inline unsigned int | |
79 | __clz32_inline (uint32_t __x) | |
80 | { | |
81 | return __x == 0 ? 32U : (unsigned int) __builtin_clz (__x); | |
82 | } | |
83 | ||
84 | static __always_inline unsigned int | |
85 | __clz16_inline (uint16_t __x) | |
86 | { | |
87 | return __clz32_inline (__x) - 16; | |
88 | } | |
89 | ||
90 | static __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. */ | |
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)))) | |
122 | ||
123 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) | |
124 | static __always_inline unsigned int | |
125 | __clo64_inline (uint64_t __x) | |
126 | { | |
127 | return __clz64_inline (~__x); | |
128 | } | |
129 | ||
130 | static __always_inline unsigned int | |
131 | __clo32_inline (uint32_t __x) | |
132 | { | |
133 | return __clz32_inline (~__x); | |
134 | } | |
135 | ||
136 | static __always_inline unsigned int | |
137 | __clo16_inline (uint16_t __x) | |
138 | { | |
c8e31fbf | 139 | return __clz16_inline (__pacify_uint16 (~__x)); |
b34b46b8 JM |
140 | } |
141 | ||
142 | static __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. */ | |
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) \ | |
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) | |
178 | static __always_inline unsigned int | |
179 | __ctz64_inline (uint64_t __x) | |
180 | { | |
181 | return __x == 0 ? 64U : (unsigned int) __builtin_ctzll (__x); | |
182 | } | |
183 | ||
184 | static __always_inline unsigned int | |
185 | __ctz32_inline (uint32_t __x) | |
186 | { | |
187 | return __x == 0 ? 32U : (unsigned int) __builtin_ctz (__x); | |
188 | } | |
189 | ||
190 | static __always_inline unsigned int | |
191 | __ctz16_inline (uint16_t __x) | |
192 | { | |
193 | return __x == 0 ? 16U : (unsigned int) __builtin_ctz (__x); | |
194 | } | |
195 | ||
196 | static __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. */ | |
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)) | |
226 | ||
227 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll) | |
228 | static __always_inline unsigned int | |
229 | __cto64_inline (uint64_t __x) | |
230 | { | |
231 | return __ctz64_inline (~__x); | |
232 | } | |
233 | ||
234 | static __always_inline unsigned int | |
235 | __cto32_inline (uint32_t __x) | |
236 | { | |
237 | return __ctz32_inline (~__x); | |
238 | } | |
239 | ||
240 | static __always_inline unsigned int | |
241 | __cto16_inline (uint16_t __x) | |
242 | { | |
c8e31fbf | 243 | return __ctz16_inline (__pacify_uint16 (~__x)); |
b34b46b8 JM |
244 | } |
245 | ||
246 | static __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. */ | |
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) \ | |
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) | |
282 | static __always_inline unsigned int | |
283 | __flz64_inline (uint64_t __x) | |
284 | { | |
285 | return __x == (uint64_t) -1 ? 0 : 1 + __clo64_inline (__x); | |
286 | } | |
287 | ||
288 | static __always_inline unsigned int | |
289 | __flz32_inline (uint32_t __x) | |
290 | { | |
291 | return __x == (uint32_t) -1 ? 0 : 1 + __clo32_inline (__x); | |
292 | } | |
293 | ||
294 | static __always_inline unsigned int | |
295 | __flz16_inline (uint16_t __x) | |
296 | { | |
297 | return __x == (uint16_t) -1 ? 0 : 1 + __clo16_inline (__x); | |
298 | } | |
299 | ||
300 | static __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. */ | |
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) \ | |
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) | |
336 | static __always_inline unsigned int | |
337 | __flo64_inline (uint64_t __x) | |
338 | { | |
339 | return __x == 0 ? 0 : 1 + __clz64_inline (__x); | |
340 | } | |
341 | ||
342 | static __always_inline unsigned int | |
343 | __flo32_inline (uint32_t __x) | |
344 | { | |
345 | return __x == 0 ? 0 : 1 + __clz32_inline (__x); | |
346 | } | |
347 | ||
348 | static __always_inline unsigned int | |
349 | __flo16_inline (uint16_t __x) | |
350 | { | |
351 | return __x == 0 ? 0 : 1 + __clz16_inline (__x); | |
352 | } | |
353 | ||
354 | static __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. */ | |
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) \ | |
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) | |
390 | static __always_inline unsigned int | |
391 | __ftz64_inline (uint64_t __x) | |
392 | { | |
393 | return __x == (uint64_t) -1 ? 0 : 1 + __cto64_inline (__x); | |
394 | } | |
395 | ||
396 | static __always_inline unsigned int | |
397 | __ftz32_inline (uint32_t __x) | |
398 | { | |
399 | return __x == (uint32_t) -1 ? 0 : 1 + __cto32_inline (__x); | |
400 | } | |
401 | ||
402 | static __always_inline unsigned int | |
403 | __ftz16_inline (uint16_t __x) | |
404 | { | |
405 | return __x == (uint16_t) -1 ? 0 : 1 + __cto16_inline (__x); | |
406 | } | |
407 | ||
408 | static __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. */ | |
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) \ | |
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) | |
444 | static __always_inline unsigned int | |
445 | __fto64_inline (uint64_t __x) | |
446 | { | |
447 | return __x == 0 ? 0 : 1 + __ctz64_inline (__x); | |
448 | } | |
449 | ||
450 | static __always_inline unsigned int | |
451 | __fto32_inline (uint32_t __x) | |
452 | { | |
453 | return __x == 0 ? 0 : 1 + __ctz32_inline (__x); | |
454 | } | |
455 | ||
456 | static __always_inline unsigned int | |
457 | __fto16_inline (uint16_t __x) | |
458 | { | |
459 | return __x == 0 ? 0 : 1 + __ctz16_inline (__x); | |
460 | } | |
461 | ||
462 | static __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. */ | |
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)))) | |
494 | ||
495 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll) | |
496 | static __always_inline unsigned int | |
497 | __cz64_inline (uint64_t __x) | |
498 | { | |
499 | return 64U - (unsigned int) __builtin_popcountll (__x); | |
500 | } | |
501 | ||
502 | static __always_inline unsigned int | |
503 | __cz32_inline (uint32_t __x) | |
504 | { | |
505 | return 32U - (unsigned int) __builtin_popcount (__x); | |
506 | } | |
507 | ||
508 | static __always_inline unsigned int | |
509 | __cz16_inline (uint16_t __x) | |
510 | { | |
511 | return 16U - (unsigned int) __builtin_popcount (__x); | |
512 | } | |
513 | ||
514 | static __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. */ | |
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)) | |
544 | ||
545 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll) | |
546 | static __always_inline unsigned int | |
547 | __co64_inline (uint64_t __x) | |
548 | { | |
549 | return (unsigned int) __builtin_popcountll (__x); | |
550 | } | |
551 | ||
552 | static __always_inline unsigned int | |
553 | __co32_inline (uint32_t __x) | |
554 | { | |
555 | return (unsigned int) __builtin_popcount (__x); | |
556 | } | |
557 | ||
558 | static __always_inline unsigned int | |
559 | __co16_inline (uint16_t __x) | |
560 | { | |
561 | return (unsigned int) __builtin_popcount (__x); | |
562 | } | |
563 | ||
564 | static __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. */ | |
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))) | |
597 | ||
598 | static __always_inline bool | |
599 | __hsb64_inline (uint64_t __x) | |
600 | { | |
601 | return (__x ^ (__x - 1)) > __x - 1; | |
602 | } | |
603 | ||
604 | static __always_inline bool | |
605 | __hsb32_inline (uint32_t __x) | |
606 | { | |
607 | return (__x ^ (__x - 1)) > __x - 1; | |
608 | } | |
609 | ||
610 | static __always_inline bool | |
611 | __hsb16_inline (uint16_t __x) | |
612 | { | |
613 | return (__x ^ (__x - 1)) > __x - 1; | |
614 | } | |
615 | ||
616 | static __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. */ | |
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)) | |
645 | ||
646 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) | |
647 | static __always_inline unsigned int | |
648 | __bw64_inline (uint64_t __x) | |
649 | { | |
650 | return 64 - __clz64_inline (__x); | |
651 | } | |
652 | ||
653 | static __always_inline unsigned int | |
654 | __bw32_inline (uint32_t __x) | |
655 | { | |
656 | return 32 - __clz32_inline (__x); | |
657 | } | |
658 | ||
659 | static __always_inline unsigned int | |
660 | __bw16_inline (uint16_t __x) | |
661 | { | |
662 | return 16 - __clz16_inline (__x); | |
663 | } | |
664 | ||
665 | static __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. */ | |
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)) | |
695 | ||
696 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) | |
697 | static __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 | ||
703 | static __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 | ||
709 | static __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 | ||
716 | static __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. */ | |
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)) | |
747 | ||
748 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) | |
749 | static __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 | ||
755 | static __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 | ||
761 | static __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 | ||
770 | static __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 */ |