| 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 | |
| 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 |
| 54 | |
| 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 | #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) \ |
| 71 | (stdc_leading_zeros_ull (x) \ |
| 72 | - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x)))) |
| 73 | #endif |
| 74 | |
| 75 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) |
| 76 | static __always_inline unsigned int |
| 77 | __clz64_inline (uint64_t __x) |
| 78 | { |
| 79 | return __x == 0 ? 64U : (unsigned int) __builtin_clzll (__x); |
| 80 | } |
| 81 | |
| 82 | static __always_inline unsigned int |
| 83 | __clz32_inline (uint32_t __x) |
| 84 | { |
| 85 | return __x == 0 ? 32U : (unsigned int) __builtin_clz (__x); |
| 86 | } |
| 87 | |
| 88 | static __always_inline unsigned int |
| 89 | __clz16_inline (uint16_t __x) |
| 90 | { |
| 91 | return __clz32_inline (__x) - 16; |
| 92 | } |
| 93 | |
| 94 | static __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. */ |
| 112 | extern unsigned int stdc_leading_ones_uc (unsigned char __x) |
| 113 | __THROW __attribute_const__; |
| 114 | extern unsigned int stdc_leading_ones_us (unsigned short __x) |
| 115 | __THROW __attribute_const__; |
| 116 | extern unsigned int stdc_leading_ones_ui (unsigned int __x) |
| 117 | __THROW __attribute_const__; |
| 118 | extern unsigned int stdc_leading_ones_ul (unsigned long int __x) |
| 119 | __THROW __attribute_const__; |
| 120 | __extension__ |
| 121 | extern unsigned int stdc_leading_ones_ull (unsigned long long int __x) |
| 122 | __THROW __attribute_const__; |
| 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) \ |
| 127 | (stdc_leading_ones_ull ((unsigned long long int) (x) \ |
| 128 | << 8 * (sizeof (0ULL) - sizeof (x)))) |
| 129 | #endif |
| 130 | |
| 131 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) |
| 132 | static __always_inline unsigned int |
| 133 | __clo64_inline (uint64_t __x) |
| 134 | { |
| 135 | return __clz64_inline (x: ~__x); |
| 136 | } |
| 137 | |
| 138 | static __always_inline unsigned int |
| 139 | __clo32_inline (uint32_t __x) |
| 140 | { |
| 141 | return __clz32_inline (x: ~__x); |
| 142 | } |
| 143 | |
| 144 | static __always_inline unsigned int |
| 145 | __clo16_inline (uint16_t __x) |
| 146 | { |
| 147 | return __clz16_inline (__pacify_uint16 (~__x)); |
| 148 | } |
| 149 | |
| 150 | static __always_inline unsigned int |
| 151 | __clo8_inline (uint8_t __x) |
| 152 | { |
| 153 | return __clz8_inline (__pacify_uint8 (~__x)); |
| 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. */ |
| 168 | extern unsigned int stdc_trailing_zeros_uc (unsigned char __x) |
| 169 | __THROW __attribute_const__; |
| 170 | extern unsigned int stdc_trailing_zeros_us (unsigned short __x) |
| 171 | __THROW __attribute_const__; |
| 172 | extern unsigned int stdc_trailing_zeros_ui (unsigned int __x) |
| 173 | __THROW __attribute_const__; |
| 174 | extern unsigned int stdc_trailing_zeros_ul (unsigned long int __x) |
| 175 | __THROW __attribute_const__; |
| 176 | __extension__ |
| 177 | extern unsigned int stdc_trailing_zeros_ull (unsigned long long int __x) |
| 178 | __THROW __attribute_const__; |
| 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) \ |
| 183 | (sizeof (x) == 8 ? stdc_trailing_zeros_ull (x) \ |
| 184 | : sizeof (x) == 4 ? stdc_trailing_zeros_ui (x) \ |
| 185 | : sizeof (x) == 2 ? stdc_trailing_zeros_us (__pacify_uint16 (x)) \ |
| 186 | : stdc_trailing_zeros_uc (__pacify_uint8 (x))) |
| 187 | #endif |
| 188 | |
| 189 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll) |
| 190 | static __always_inline unsigned int |
| 191 | __ctz64_inline (uint64_t __x) |
| 192 | { |
| 193 | return __x == 0 ? 64U : (unsigned int) __builtin_ctzll (__x); |
| 194 | } |
| 195 | |
| 196 | static __always_inline unsigned int |
| 197 | __ctz32_inline (uint32_t __x) |
| 198 | { |
| 199 | return __x == 0 ? 32U : (unsigned int) __builtin_ctz (__x); |
| 200 | } |
| 201 | |
| 202 | static __always_inline unsigned int |
| 203 | __ctz16_inline (uint16_t __x) |
| 204 | { |
| 205 | return __x == 0 ? 16U : (unsigned int) __builtin_ctz (__x); |
| 206 | } |
| 207 | |
| 208 | static __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. */ |
| 226 | extern unsigned int stdc_trailing_ones_uc (unsigned char __x) |
| 227 | __THROW __attribute_const__; |
| 228 | extern unsigned int stdc_trailing_ones_us (unsigned short __x) |
| 229 | __THROW __attribute_const__; |
| 230 | extern unsigned int stdc_trailing_ones_ui (unsigned int __x) |
| 231 | __THROW __attribute_const__; |
| 232 | extern unsigned int stdc_trailing_ones_ul (unsigned long int __x) |
| 233 | __THROW __attribute_const__; |
| 234 | __extension__ |
| 235 | extern unsigned int stdc_trailing_ones_ull (unsigned long long int __x) |
| 236 | __THROW __attribute_const__; |
| 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 |
| 242 | |
| 243 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll) |
| 244 | static __always_inline unsigned int |
| 245 | __cto64_inline (uint64_t __x) |
| 246 | { |
| 247 | return __ctz64_inline (x: ~__x); |
| 248 | } |
| 249 | |
| 250 | static __always_inline unsigned int |
| 251 | __cto32_inline (uint32_t __x) |
| 252 | { |
| 253 | return __ctz32_inline (x: ~__x); |
| 254 | } |
| 255 | |
| 256 | static __always_inline unsigned int |
| 257 | __cto16_inline (uint16_t __x) |
| 258 | { |
| 259 | return __ctz16_inline (__pacify_uint16 (~__x)); |
| 260 | } |
| 261 | |
| 262 | static __always_inline unsigned int |
| 263 | __cto8_inline (uint8_t __x) |
| 264 | { |
| 265 | return __ctz8_inline (__pacify_uint8 (~__x)); |
| 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. */ |
| 280 | extern unsigned int stdc_first_leading_zero_uc (unsigned char __x) |
| 281 | __THROW __attribute_const__; |
| 282 | extern unsigned int stdc_first_leading_zero_us (unsigned short __x) |
| 283 | __THROW __attribute_const__; |
| 284 | extern unsigned int stdc_first_leading_zero_ui (unsigned int __x) |
| 285 | __THROW __attribute_const__; |
| 286 | extern unsigned int stdc_first_leading_zero_ul (unsigned long int __x) |
| 287 | __THROW __attribute_const__; |
| 288 | __extension__ |
| 289 | extern unsigned int stdc_first_leading_zero_ull (unsigned long long int __x) |
| 290 | __THROW __attribute_const__; |
| 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) \ |
| 295 | (sizeof (x) == 8 ? stdc_first_leading_zero_ull (x) \ |
| 296 | : sizeof (x) == 4 ? stdc_first_leading_zero_ui (x) \ |
| 297 | : sizeof (x) == 2 ? stdc_first_leading_zero_us (__pacify_uint16 (x)) \ |
| 298 | : stdc_first_leading_zero_uc (__pacify_uint8 (x))) |
| 299 | #endif |
| 300 | |
| 301 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) |
| 302 | static __always_inline unsigned int |
| 303 | __flz64_inline (uint64_t __x) |
| 304 | { |
| 305 | return __x == (uint64_t) -1 ? 0 : 1 + __clo64_inline (__x); |
| 306 | } |
| 307 | |
| 308 | static __always_inline unsigned int |
| 309 | __flz32_inline (uint32_t __x) |
| 310 | { |
| 311 | return __x == (uint32_t) -1 ? 0 : 1 + __clo32_inline (__x); |
| 312 | } |
| 313 | |
| 314 | static __always_inline unsigned int |
| 315 | __flz16_inline (uint16_t __x) |
| 316 | { |
| 317 | return __x == (uint16_t) -1 ? 0 : 1 + __clo16_inline (__x); |
| 318 | } |
| 319 | |
| 320 | static __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. */ |
| 338 | extern unsigned int stdc_first_leading_one_uc (unsigned char __x) |
| 339 | __THROW __attribute_const__; |
| 340 | extern unsigned int stdc_first_leading_one_us (unsigned short __x) |
| 341 | __THROW __attribute_const__; |
| 342 | extern unsigned int stdc_first_leading_one_ui (unsigned int __x) |
| 343 | __THROW __attribute_const__; |
| 344 | extern unsigned int stdc_first_leading_one_ul (unsigned long int __x) |
| 345 | __THROW __attribute_const__; |
| 346 | __extension__ |
| 347 | extern unsigned int stdc_first_leading_one_ull (unsigned long long int __x) |
| 348 | __THROW __attribute_const__; |
| 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) \ |
| 353 | (sizeof (x) == 8 ? stdc_first_leading_one_ull (x) \ |
| 354 | : sizeof (x) == 4 ? stdc_first_leading_one_ui (x) \ |
| 355 | : sizeof (x) == 2 ? stdc_first_leading_one_us (__pacify_uint16 (x)) \ |
| 356 | : stdc_first_leading_one_uc (__pacify_uint8 (x))) |
| 357 | #endif |
| 358 | |
| 359 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) |
| 360 | static __always_inline unsigned int |
| 361 | __flo64_inline (uint64_t __x) |
| 362 | { |
| 363 | return __x == 0 ? 0 : 1 + __clz64_inline (__x); |
| 364 | } |
| 365 | |
| 366 | static __always_inline unsigned int |
| 367 | __flo32_inline (uint32_t __x) |
| 368 | { |
| 369 | return __x == 0 ? 0 : 1 + __clz32_inline (__x); |
| 370 | } |
| 371 | |
| 372 | static __always_inline unsigned int |
| 373 | __flo16_inline (uint16_t __x) |
| 374 | { |
| 375 | return __x == 0 ? 0 : 1 + __clz16_inline (__x); |
| 376 | } |
| 377 | |
| 378 | static __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. */ |
| 396 | extern unsigned int stdc_first_trailing_zero_uc (unsigned char __x) |
| 397 | __THROW __attribute_const__; |
| 398 | extern unsigned int stdc_first_trailing_zero_us (unsigned short __x) |
| 399 | __THROW __attribute_const__; |
| 400 | extern unsigned int stdc_first_trailing_zero_ui (unsigned int __x) |
| 401 | __THROW __attribute_const__; |
| 402 | extern unsigned int stdc_first_trailing_zero_ul (unsigned long int __x) |
| 403 | __THROW __attribute_const__; |
| 404 | __extension__ |
| 405 | extern unsigned int stdc_first_trailing_zero_ull (unsigned long long int __x) |
| 406 | __THROW __attribute_const__; |
| 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) \ |
| 411 | (sizeof (x) == 8 ? stdc_first_trailing_zero_ull (x) \ |
| 412 | : sizeof (x) == 4 ? stdc_first_trailing_zero_ui (x) \ |
| 413 | : sizeof (x) == 2 ? stdc_first_trailing_zero_us (__pacify_uint16 (x)) \ |
| 414 | : stdc_first_trailing_zero_uc (__pacify_uint8 (x))) |
| 415 | #endif |
| 416 | |
| 417 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll) |
| 418 | static __always_inline unsigned int |
| 419 | __ftz64_inline (uint64_t __x) |
| 420 | { |
| 421 | return __x == (uint64_t) -1 ? 0 : 1 + __cto64_inline (__x); |
| 422 | } |
| 423 | |
| 424 | static __always_inline unsigned int |
| 425 | __ftz32_inline (uint32_t __x) |
| 426 | { |
| 427 | return __x == (uint32_t) -1 ? 0 : 1 + __cto32_inline (__x); |
| 428 | } |
| 429 | |
| 430 | static __always_inline unsigned int |
| 431 | __ftz16_inline (uint16_t __x) |
| 432 | { |
| 433 | return __x == (uint16_t) -1 ? 0 : 1 + __cto16_inline (__x); |
| 434 | } |
| 435 | |
| 436 | static __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. */ |
| 454 | extern unsigned int stdc_first_trailing_one_uc (unsigned char __x) |
| 455 | __THROW __attribute_const__; |
| 456 | extern unsigned int stdc_first_trailing_one_us (unsigned short __x) |
| 457 | __THROW __attribute_const__; |
| 458 | extern unsigned int stdc_first_trailing_one_ui (unsigned int __x) |
| 459 | __THROW __attribute_const__; |
| 460 | extern unsigned int stdc_first_trailing_one_ul (unsigned long int __x) |
| 461 | __THROW __attribute_const__; |
| 462 | __extension__ |
| 463 | extern unsigned int stdc_first_trailing_one_ull (unsigned long long int __x) |
| 464 | __THROW __attribute_const__; |
| 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) \ |
| 469 | (sizeof (x) == 8 ? stdc_first_trailing_one_ull (x) \ |
| 470 | : sizeof (x) == 4 ? stdc_first_trailing_one_ui (x) \ |
| 471 | : sizeof (x) == 2 ? stdc_first_trailing_one_us (__pacify_uint16 (x)) \ |
| 472 | : stdc_first_trailing_one_uc (__pacify_uint8 (x))) |
| 473 | #endif |
| 474 | |
| 475 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_ctzll) |
| 476 | static __always_inline unsigned int |
| 477 | __fto64_inline (uint64_t __x) |
| 478 | { |
| 479 | return __x == 0 ? 0 : 1 + __ctz64_inline (__x); |
| 480 | } |
| 481 | |
| 482 | static __always_inline unsigned int |
| 483 | __fto32_inline (uint32_t __x) |
| 484 | { |
| 485 | return __x == 0 ? 0 : 1 + __ctz32_inline (__x); |
| 486 | } |
| 487 | |
| 488 | static __always_inline unsigned int |
| 489 | __fto16_inline (uint16_t __x) |
| 490 | { |
| 491 | return __x == 0 ? 0 : 1 + __ctz16_inline (__x); |
| 492 | } |
| 493 | |
| 494 | static __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. */ |
| 512 | extern unsigned int stdc_count_zeros_uc (unsigned char __x) |
| 513 | __THROW __attribute_const__; |
| 514 | extern unsigned int stdc_count_zeros_us (unsigned short __x) |
| 515 | __THROW __attribute_const__; |
| 516 | extern unsigned int stdc_count_zeros_ui (unsigned int __x) |
| 517 | __THROW __attribute_const__; |
| 518 | extern unsigned int stdc_count_zeros_ul (unsigned long int __x) |
| 519 | __THROW __attribute_const__; |
| 520 | __extension__ |
| 521 | extern unsigned int stdc_count_zeros_ull (unsigned long long int __x) |
| 522 | __THROW __attribute_const__; |
| 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) \ |
| 527 | (stdc_count_zeros_ull (x) \ |
| 528 | - (unsigned int) (8 * (sizeof (0ULL) - sizeof (x)))) |
| 529 | #endif |
| 530 | |
| 531 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll) |
| 532 | static __always_inline unsigned int |
| 533 | __cz64_inline (uint64_t __x) |
| 534 | { |
| 535 | return 64U - (unsigned int) __builtin_popcountll (__x); |
| 536 | } |
| 537 | |
| 538 | static __always_inline unsigned int |
| 539 | __cz32_inline (uint32_t __x) |
| 540 | { |
| 541 | return 32U - (unsigned int) __builtin_popcount (__x); |
| 542 | } |
| 543 | |
| 544 | static __always_inline unsigned int |
| 545 | __cz16_inline (uint16_t __x) |
| 546 | { |
| 547 | return 16U - (unsigned int) __builtin_popcount (__x); |
| 548 | } |
| 549 | |
| 550 | static __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. */ |
| 568 | extern unsigned int stdc_count_ones_uc (unsigned char __x) |
| 569 | __THROW __attribute_const__; |
| 570 | extern unsigned int stdc_count_ones_us (unsigned short __x) |
| 571 | __THROW __attribute_const__; |
| 572 | extern unsigned int stdc_count_ones_ui (unsigned int __x) |
| 573 | __THROW __attribute_const__; |
| 574 | extern unsigned int stdc_count_ones_ul (unsigned long int __x) |
| 575 | __THROW __attribute_const__; |
| 576 | __extension__ |
| 577 | extern unsigned int stdc_count_ones_ull (unsigned long long int __x) |
| 578 | __THROW __attribute_const__; |
| 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 |
| 584 | |
| 585 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_popcountll) |
| 586 | static __always_inline unsigned int |
| 587 | __co64_inline (uint64_t __x) |
| 588 | { |
| 589 | return (unsigned int) __builtin_popcountll (__x); |
| 590 | } |
| 591 | |
| 592 | static __always_inline unsigned int |
| 593 | __co32_inline (uint32_t __x) |
| 594 | { |
| 595 | return (unsigned int) __builtin_popcount (__x); |
| 596 | } |
| 597 | |
| 598 | static __always_inline unsigned int |
| 599 | __co16_inline (uint16_t __x) |
| 600 | { |
| 601 | return (unsigned int) __builtin_popcount (__x); |
| 602 | } |
| 603 | |
| 604 | static __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. */ |
| 622 | extern bool stdc_has_single_bit_uc (unsigned char __x) |
| 623 | __THROW __attribute_const__; |
| 624 | extern bool stdc_has_single_bit_us (unsigned short __x) |
| 625 | __THROW __attribute_const__; |
| 626 | extern bool stdc_has_single_bit_ui (unsigned int __x) |
| 627 | __THROW __attribute_const__; |
| 628 | extern bool stdc_has_single_bit_ul (unsigned long int __x) |
| 629 | __THROW __attribute_const__; |
| 630 | __extension__ |
| 631 | extern bool stdc_has_single_bit_ull (unsigned long long int __x) |
| 632 | __THROW __attribute_const__; |
| 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) \ |
| 637 | ((bool) (sizeof (x) <= sizeof (unsigned int) \ |
| 638 | ? stdc_has_single_bit_ui (x) \ |
| 639 | : stdc_has_single_bit_ull (x))) |
| 640 | #endif |
| 641 | |
| 642 | static __always_inline bool |
| 643 | __hsb64_inline (uint64_t __x) |
| 644 | { |
| 645 | return (__x ^ (__x - 1)) > __x - 1; |
| 646 | } |
| 647 | |
| 648 | static __always_inline bool |
| 649 | __hsb32_inline (uint32_t __x) |
| 650 | { |
| 651 | return (__x ^ (__x - 1)) > __x - 1; |
| 652 | } |
| 653 | |
| 654 | static __always_inline bool |
| 655 | __hsb16_inline (uint16_t __x) |
| 656 | { |
| 657 | return (__x ^ (__x - 1)) > __x - 1; |
| 658 | } |
| 659 | |
| 660 | static __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. */ |
| 677 | extern unsigned int stdc_bit_width_uc (unsigned char __x) |
| 678 | __THROW __attribute_const__; |
| 679 | extern unsigned int stdc_bit_width_us (unsigned short __x) |
| 680 | __THROW __attribute_const__; |
| 681 | extern unsigned int stdc_bit_width_ui (unsigned int __x) |
| 682 | __THROW __attribute_const__; |
| 683 | extern unsigned int stdc_bit_width_ul (unsigned long int __x) |
| 684 | __THROW __attribute_const__; |
| 685 | __extension__ |
| 686 | extern unsigned int stdc_bit_width_ull (unsigned long long int __x) |
| 687 | __THROW __attribute_const__; |
| 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 |
| 693 | |
| 694 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) |
| 695 | static __always_inline unsigned int |
| 696 | __bw64_inline (uint64_t __x) |
| 697 | { |
| 698 | return 64 - __clz64_inline (__x); |
| 699 | } |
| 700 | |
| 701 | static __always_inline unsigned int |
| 702 | __bw32_inline (uint32_t __x) |
| 703 | { |
| 704 | return 32 - __clz32_inline (__x); |
| 705 | } |
| 706 | |
| 707 | static __always_inline unsigned int |
| 708 | __bw16_inline (uint16_t __x) |
| 709 | { |
| 710 | return 16 - __clz16_inline (__x); |
| 711 | } |
| 712 | |
| 713 | static __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. */ |
| 731 | extern unsigned char stdc_bit_floor_uc (unsigned char __x) |
| 732 | __THROW __attribute_const__; |
| 733 | extern unsigned short stdc_bit_floor_us (unsigned short __x) |
| 734 | __THROW __attribute_const__; |
| 735 | extern unsigned int stdc_bit_floor_ui (unsigned int __x) |
| 736 | __THROW __attribute_const__; |
| 737 | extern unsigned long int stdc_bit_floor_ul (unsigned long int __x) |
| 738 | __THROW __attribute_const__; |
| 739 | __extension__ |
| 740 | extern unsigned long long int stdc_bit_floor_ull (unsigned long long int __x) |
| 741 | __THROW __attribute_const__; |
| 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 |
| 747 | |
| 748 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) |
| 749 | static __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 | |
| 755 | static __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 | |
| 761 | static __always_inline uint16_t |
| 762 | __bf16_inline (uint16_t __x) |
| 763 | { |
| 764 | return __pacify_uint16 (__x == 0 |
| 765 | ? 0 : ((uint16_t) 1) << (__bw16_inline (__x) - 1)); |
| 766 | } |
| 767 | |
| 768 | static __always_inline uint8_t |
| 769 | __bf8_inline (uint8_t __x) |
| 770 | { |
| 771 | return __pacify_uint8 (__x == 0 |
| 772 | ? 0 : ((uint8_t) 1) << (__bw8_inline (__x) - 1)); |
| 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. */ |
| 787 | extern unsigned char stdc_bit_ceil_uc (unsigned char __x) |
| 788 | __THROW __attribute_const__; |
| 789 | extern unsigned short stdc_bit_ceil_us (unsigned short __x) |
| 790 | __THROW __attribute_const__; |
| 791 | extern unsigned int stdc_bit_ceil_ui (unsigned int __x) |
| 792 | __THROW __attribute_const__; |
| 793 | extern unsigned long int stdc_bit_ceil_ul (unsigned long int __x) |
| 794 | __THROW __attribute_const__; |
| 795 | __extension__ |
| 796 | extern unsigned long long int stdc_bit_ceil_ull (unsigned long long int __x) |
| 797 | __THROW __attribute_const__; |
| 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 |
| 803 | |
| 804 | #if __GNUC_PREREQ (3, 4) || __glibc_has_builtin (__builtin_clzll) |
| 805 | static __always_inline uint64_t |
| 806 | __bc64_inline (uint64_t __x) |
| 807 | { |
| 808 | return __x <= 1 ? 1 : ((uint64_t) 2) << (__bw64_inline (x: __x - 1) - 1); |
| 809 | } |
| 810 | |
| 811 | static __always_inline uint32_t |
| 812 | __bc32_inline (uint32_t __x) |
| 813 | { |
| 814 | return __x <= 1 ? 1 : ((uint32_t) 2) << (__bw32_inline (x: __x - 1) - 1); |
| 815 | } |
| 816 | |
| 817 | static __always_inline uint16_t |
| 818 | __bc16_inline (uint16_t __x) |
| 819 | { |
| 820 | return __pacify_uint16 (__x <= 1 |
| 821 | ? 1 |
| 822 | : ((uint16_t) 2) |
| 823 | << (__bw16_inline ((uint16_t) (__x - 1)) - 1)); |
| 824 | } |
| 825 | |
| 826 | static __always_inline uint8_t |
| 827 | __bc8_inline (uint8_t __x) |
| 828 | { |
| 829 | return __pacify_uint8 (__x <= 1 |
| 830 | ? 1 |
| 831 | : ((uint8_t) 2) |
| 832 | << (__bw8_inline ((uint8_t) (__x - 1)) - 1)); |
| 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 */ |
| 849 | |