| 1 |
/* stdbit.h - C23 bit and byte utilities for non-C23 platforms |
| 2 |
|
| 3 |
Copyright 2024-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is free software: you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as |
| 7 |
published by the Free Software Foundation; either version 2.1 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
This file 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 |
| 13 |
GNU Lesser General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written by Paul Eggert. */ |
| 19 |
|
| 20 |
#ifndef STDBIT_H |
| 21 |
#define STDBIT_H 1 |
| 22 |
|
| 23 |
/* This file uses _GL_INLINE, WORDS_BIGENDIAN. */ |
| 24 |
#if !_GL_CONFIG_H_INCLUDED |
| 25 |
#error "Please include config.h first." |
| 26 |
#endif |
| 27 |
|
| 28 |
_GL_INLINE_HEADER_BEGIN |
| 29 |
|
| 30 |
#ifndef _GL_STDBIT_INLINE |
| 31 |
# define _GL_STDBIT_INLINE _GL_INLINE |
| 32 |
#endif |
| 33 |
#ifndef _GL_STDC_LEADING_ZEROS_INLINE |
| 34 |
# define _GL_STDC_LEADING_ZEROS_INLINE _GL_INLINE |
| 35 |
#endif |
| 36 |
#ifndef _GL_STDC_LEADING_ONES_INLINE |
| 37 |
# define _GL_STDC_LEADING_ONES_INLINE _GL_INLINE |
| 38 |
#endif |
| 39 |
#ifndef _GL_STDC_TRAILING_ZEROS_INLINE |
| 40 |
# define _GL_STDC_TRAILING_ZEROS_INLINE _GL_INLINE |
| 41 |
#endif |
| 42 |
#ifndef _GL_STDC_TRAILING_ONES_INLINE |
| 43 |
# define _GL_STDC_TRAILING_ONES_INLINE _GL_INLINE |
| 44 |
#endif |
| 45 |
#ifndef _GL_STDC_FIRST_LEADING_ZERO_INLINE |
| 46 |
# define _GL_STDC_FIRST_LEADING_ZERO_INLINE _GL_INLINE |
| 47 |
#endif |
| 48 |
#ifndef _GL_STDC_FIRST_LEADING_ONE_INLINE |
| 49 |
# define _GL_STDC_FIRST_LEADING_ONE_INLINE _GL_INLINE |
| 50 |
#endif |
| 51 |
#ifndef _GL_STDC_FIRST_TRAILING_ZERO_INLINE |
| 52 |
# define _GL_STDC_FIRST_TRAILING_ZERO_INLINE _GL_INLINE |
| 53 |
#endif |
| 54 |
#ifndef _GL_STDC_FIRST_TRAILING_ONE_INLINE |
| 55 |
# define _GL_STDC_FIRST_TRAILING_ONE_INLINE _GL_INLINE |
| 56 |
#endif |
| 57 |
#ifndef _GL_STDC_COUNT_ZEROS_INLINE |
| 58 |
# define _GL_STDC_COUNT_ZEROS_INLINE _GL_INLINE |
| 59 |
#endif |
| 60 |
#ifndef _GL_STDC_COUNT_ONES_INLINE |
| 61 |
# define _GL_STDC_COUNT_ONES_INLINE _GL_INLINE |
| 62 |
#endif |
| 63 |
#ifndef _GL_STDC_HAS_SINGLE_BIT_INLINE |
| 64 |
# define _GL_STDC_HAS_SINGLE_BIT_INLINE _GL_INLINE |
| 65 |
#endif |
| 66 |
#ifndef _GL_STDC_BIT_WIDTH_INLINE |
| 67 |
# define _GL_STDC_BIT_WIDTH_INLINE _GL_INLINE |
| 68 |
#endif |
| 69 |
#ifndef _GL_STDC_BIT_FLOOR_INLINE |
| 70 |
# define _GL_STDC_BIT_FLOOR_INLINE _GL_INLINE |
| 71 |
#endif |
| 72 |
#ifndef _GL_STDC_BIT_CEIL_INLINE |
| 73 |
# define _GL_STDC_BIT_CEIL_INLINE _GL_INLINE |
| 74 |
#endif |
| 75 |
|
| 76 |
/* An expression, preferably with the type of A, that has the value of B. */ |
| 77 |
#if ((defined __GNUC__ && 2 <= __GNUC__) \ |
| 78 |
|| (defined __clang_major__ && 4 <= __clang_major__) \ |
| 79 |
|| (defined __IBMC__ && 1210 <= __IBMC__ && defined __IBM__TYPEOF__) \ |
| 80 |
|| (defined __SUNPRO_C && 0x5110 <= __SUNPRO_C && !__STDC__) \ |
| 81 |
|| (defined _MSC_VER && 1939 <= _MSC_VER)) |
| 82 |
# define _GL_STDBIT_TYPEOF_CAST(a, b) ((__typeof__ (a)) (b)) |
| 83 |
#elif 202311 <= __STDC_VERSION__ |
| 84 |
# define _GL_STDBIT_TYPEOF_CAST(a, b) ((typeof (a)) (b)) |
| 85 |
#else |
| 86 |
/* This platform is so old that it lacks typeof, so _Generic is likely |
| 87 |
missing or unreliable. The C23 standard seems to allow yielding B |
| 88 |
(which is always unsigned long long int), so do that. */ |
| 89 |
# define _GL_STDBIT_TYPEOF_CAST(a, b) (b) |
| 90 |
#endif |
| 91 |
|
| 92 |
|
| 93 |
/* ISO C 23 § 7.18.1 General */ |
| 94 |
|
| 95 |
#define __STDC_VERSION_STDBIT_H__ 202311L |
| 96 |
|
| 97 |
|
| 98 |
/* ISO C 23 § 7.18.2 Endian */ |
| 99 |
|
| 100 |
#define __STDC_ENDIAN_BIG__ 4321 |
| 101 |
#define __STDC_ENDIAN_LITTLE__ 1234 |
| 102 |
#ifdef WORDS_BIGENDIAN |
| 103 |
# define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__ |
| 104 |
#else |
| 105 |
# define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__ |
| 106 |
#endif |
| 107 |
|
| 108 |
|
| 109 |
#ifdef __cplusplus |
| 110 |
extern "C" { |
| 111 |
#endif |
| 112 |
|
| 113 |
#if 3 < __GNUC__ + (4 <= __GNUC_MINOR__) || 4 <= __clang_major__ |
| 114 |
# define _GL_STDBIT_HAS_BUILTIN_CLZ true |
| 115 |
# define _GL_STDBIT_HAS_BUILTIN_CTZ true |
| 116 |
# define _GL_STDBIT_HAS_BUILTIN_POPCOUNT true |
| 117 |
#elif defined __has_builtin |
| 118 |
# if (__has_builtin (__builtin_clz) \ |
| 119 |
&& __has_builtin (__builtin_clzl) \ |
| 120 |
&& __has_builtin (__builtin_clzll)) |
| 121 |
# define _GL_STDBIT_HAS_BUILTIN_CLZ true |
| 122 |
# endif |
| 123 |
# if (__has_builtin (__builtin_ctz) \ |
| 124 |
&& __has_builtin (__builtin_ctzl) \ |
| 125 |
&& __has_builtin (__builtin_ctzll)) |
| 126 |
# define _GL_STDBIT_HAS_BUILTIN_CTZ true |
| 127 |
# endif |
| 128 |
# if (__has_builtin (__builtin_popcount) \ |
| 129 |
&& __has_builtin (__builtin_popcountl) \ |
| 130 |
&& __has_builtin (__builtin_popcountll)) |
| 131 |
# define _GL_STDBIT_HAS_BUILTIN_POPCOUNT true |
| 132 |
# endif |
| 133 |
#endif |
| 134 |
|
| 135 |
/* Count leading 0 bits of N, even if N is 0. */ |
| 136 |
#ifdef _GL_STDBIT_HAS_BUILTIN_CLZ |
| 137 |
_GL_STDBIT_INLINE int |
| 138 |
__gl_stdbit_clz (unsigned int n) |
| 139 |
{ |
| 140 |
return n ? __builtin_clz (n) : 8 * sizeof n; |
| 141 |
} |
| 142 |
_GL_STDBIT_INLINE int |
| 143 |
__gl_stdbit_clzl (unsigned long int n) |
| 144 |
{ |
| 145 |
return n ? __builtin_clzl (n) : 8 * sizeof n; |
| 146 |
} |
| 147 |
_GL_STDBIT_INLINE int |
| 148 |
__gl_stdbit_clzll (unsigned long long int n) |
| 149 |
{ |
| 150 |
return n ? __builtin_clzll (n) : 8 * sizeof n; |
| 151 |
} |
| 152 |
#elif defined _MSC_VER |
| 153 |
|
| 154 |
/* Declare the few MSVC intrinsics that we need. We prefer not to include |
| 155 |
<intrin.h> because it would pollute the namespace. */ |
| 156 |
extern unsigned char _BitScanReverse (unsigned long *, unsigned long); |
| 157 |
# pragma intrinsic (_BitScanReverse) |
| 158 |
# ifdef _M_X64 |
| 159 |
extern unsigned char _BitScanReverse64 (unsigned long *, unsigned long long); |
| 160 |
# pragma intrinsic (_BitScanReverse64) |
| 161 |
# endif |
| 162 |
|
| 163 |
_GL_STDBIT_INLINE int |
| 164 |
__gl_stdbit_clzl (unsigned long int n) |
| 165 |
{ |
| 166 |
unsigned long int r; |
| 167 |
return 8 * sizeof n - (_BitScanReverse (&r, n) ? r + 1 : 0); |
| 168 |
} |
| 169 |
_GL_STDBIT_INLINE int |
| 170 |
__gl_stdbit_clz (unsigned int n) |
| 171 |
{ |
| 172 |
return __gl_stdbit_clzl (n) - 8 * (sizeof 0ul - sizeof n); |
| 173 |
} |
| 174 |
_GL_STDBIT_INLINE int |
| 175 |
__gl_stdbit_clzll (unsigned long long int n) |
| 176 |
{ |
| 177 |
# ifdef _M_X64 |
| 178 |
unsigned long int r; |
| 179 |
return 8 * sizeof n - (_BitScanReverse64 (&r, n) ? r + 1 : 0); |
| 180 |
# else |
| 181 |
unsigned long int hi = n >> 32; |
| 182 |
return __gl_stdbit_clzl (hi ? hi : n) + (hi ? 0 : 32); |
| 183 |
# endif |
| 184 |
} |
| 185 |
|
| 186 |
#else /* !_MSC_VER */ |
| 187 |
|
| 188 |
_GL_STDBIT_INLINE int |
| 189 |
__gl_stdbit_clzll (unsigned long long int n) |
| 190 |
{ |
| 191 |
int r = 0; |
| 192 |
for (int i = 8 * sizeof n >> 1; 1 << 6 <= i; i >>= 1) |
| 193 |
{ |
| 194 |
int a = (1ull << i <= n) * i; n >>= a; r += a; |
| 195 |
} |
| 196 |
int a5 = (0x00000000ffffffff < n) << 5; n >>= a5; r += a5; |
| 197 |
int a4 = (0x000000000000ffff < n) << 4; n >>= a4; r += a4; |
| 198 |
int a3 = (0x00000000000000ff < n) << 3; n >>= a3; r += a3; |
| 199 |
int a2 = (0x000000000000000f < n) << 2; n >>= a2; r += a2; |
| 200 |
return (8 * sizeof n - (1 << 2) - r) + ((0x11112234ull >> (n << 2)) & 0xf); |
| 201 |
} |
| 202 |
_GL_STDBIT_INLINE int |
| 203 |
__gl_stdbit_clz (unsigned int n) |
| 204 |
{ |
| 205 |
return __gl_stdbit_clzll (n) - 8 * (sizeof 0ull - sizeof 0u); |
| 206 |
} |
| 207 |
_GL_STDBIT_INLINE int |
| 208 |
__gl_stdbit_clzl (unsigned long int n) |
| 209 |
{ |
| 210 |
return __gl_stdbit_clzll (n) - 8 * (sizeof 0ull - sizeof 0ul); |
| 211 |
} |
| 212 |
#endif |
| 213 |
|
| 214 |
/* Count trailing 0 bits of N, even if N is 0. */ |
| 215 |
#ifdef _GL_STDBIT_HAS_BUILTIN_CTZ |
| 216 |
_GL_STDBIT_INLINE int |
| 217 |
__gl_stdbit_ctz (unsigned int n) |
| 218 |
{ |
| 219 |
return n ? __builtin_ctz (n) : 8 * sizeof n; |
| 220 |
} |
| 221 |
_GL_STDBIT_INLINE int |
| 222 |
__gl_stdbit_ctzl (unsigned long int n) |
| 223 |
{ |
| 224 |
return n ? __builtin_ctzl (n) : 8 * sizeof n; |
| 225 |
} |
| 226 |
_GL_STDBIT_INLINE int |
| 227 |
__gl_stdbit_ctzll (unsigned long long int n) |
| 228 |
{ |
| 229 |
return n ? __builtin_ctzll (n) : 8 * sizeof n; |
| 230 |
} |
| 231 |
#elif defined _MSC_VER |
| 232 |
|
| 233 |
/* Declare the few MSVC intrinsics that we need. We prefer not to include |
| 234 |
<intrin.h> because it would pollute the namespace. */ |
| 235 |
extern unsigned char _BitScanForward (unsigned long *, unsigned long); |
| 236 |
# pragma intrinsic (_BitScanForward) |
| 237 |
# ifdef _M_X64 |
| 238 |
extern unsigned char _BitScanForward64 (unsigned long *, unsigned long long); |
| 239 |
# pragma intrinsic (_BitScanForward64) |
| 240 |
# endif |
| 241 |
|
| 242 |
_GL_STDBIT_INLINE int |
| 243 |
__gl_stdbit_ctzl (unsigned long int n) |
| 244 |
{ |
| 245 |
unsigned long int r; |
| 246 |
return _BitScanForward (&r, n) ? r : 8 * sizeof n; |
| 247 |
} |
| 248 |
_GL_STDBIT_INLINE int |
| 249 |
__gl_stdbit_ctz (unsigned int n) |
| 250 |
{ |
| 251 |
return __gl_stdbit_ctzl (n | (1ul << (8 * sizeof n - 1) << 1)); |
| 252 |
} |
| 253 |
_GL_STDBIT_INLINE int |
| 254 |
__gl_stdbit_ctzll (unsigned long long int n) |
| 255 |
{ |
| 256 |
# ifdef _M_X64 |
| 257 |
unsigned long int r; |
| 258 |
return _BitScanForward64 (&r, n) ? r : 8 * sizeof n; |
| 259 |
# else |
| 260 |
unsigned int lo = n; |
| 261 |
return __gl_stdbit_ctzl (lo ? lo : n >> 32) + (lo ? 0 : 32); |
| 262 |
# endif |
| 263 |
} |
| 264 |
|
| 265 |
#else /* !_MSC_VER */ |
| 266 |
|
| 267 |
_GL_STDBIT_INLINE int |
| 268 |
__gl_stdbit_ctz (unsigned int n) |
| 269 |
{ |
| 270 |
return 8 * sizeof n - (n ? __gl_stdbit_clz (n & -n) + 1 : 0); |
| 271 |
} |
| 272 |
_GL_STDBIT_INLINE int |
| 273 |
__gl_stdbit_ctzl (unsigned long int n) |
| 274 |
{ |
| 275 |
return 8 * sizeof n - (n ? __gl_stdbit_clzl (n & -n) + 1 : 0); |
| 276 |
} |
| 277 |
_GL_STDBIT_INLINE int |
| 278 |
__gl_stdbit_ctzll (unsigned long long int n) |
| 279 |
{ |
| 280 |
return 8 * sizeof n - (n ? __gl_stdbit_clzll (n & -n) + 1 : 0); |
| 281 |
} |
| 282 |
#endif |
| 283 |
|
| 284 |
#if @GL_STDC_COUNT_ONES@ |
| 285 |
/* Count 1 bits in N. */ |
| 286 |
# ifdef _GL_STDBIT_HAS_BUILTIN_POPCOUNT |
| 287 |
# define __gl_stdbit_popcount __builtin_popcount |
| 288 |
# define __gl_stdbit_popcountl __builtin_popcountl |
| 289 |
# define __gl_stdbit_popcountll __builtin_popcountll |
| 290 |
# else |
| 291 |
_GL_STDC_COUNT_ONES_INLINE int |
| 292 |
__gl_stdbit_popcount_wide (unsigned long long int n) |
| 293 |
{ |
| 294 |
if (sizeof n & (sizeof n - 1)) |
| 295 |
{ |
| 296 |
/* Use a simple O(log N) loop on theoretical platforms where N's |
| 297 |
width is not a power of 2. */ |
| 298 |
int count = 0; |
| 299 |
for (int i = 0; i < 8 * sizeof n; i++, n >>= 1) |
| 300 |
count += n & 1; |
| 301 |
return count; |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
/* N's width is a power of 2; count in parallel. */ |
| 306 |
unsigned long long int |
| 307 |
max = -1ull, |
| 308 |
x555555 = max / (1 << 1 | 1), /* 0x555555... */ |
| 309 |
x333333 = max / (1 << 2 | 1), /* 0x333333... */ |
| 310 |
x0f0f0f = max / (1 << 4 | 1), /* 0x0f0f0f... */ |
| 311 |
x010101 = max / ((1 << 8) - 1), /* 0x010101... */ |
| 312 |
x000_7f = max / 0xffffffffffffffffLL * 0x7f; /* 0x000000000000007f... */ |
| 313 |
n -= (n >> 1) & x555555; |
| 314 |
n = (n & x333333) + ((n >> 2) & x333333); |
| 315 |
n = (n + (n >> 4)) & x0f0f0f; |
| 316 |
|
| 317 |
/* If the popcount always fits in 8 bits, multiply so that the |
| 318 |
popcount is in the leading 8 bits of the product; these days |
| 319 |
this is typically faster than the alternative below. */ |
| 320 |
if (8 * sizeof n < 1 << 8) |
| 321 |
return n * x010101 >> 8 * (sizeof n - 1); |
| 322 |
|
| 323 |
/* N is at least 256 bits wide! Fall back on an O(log log N) |
| 324 |
loop that a compiler could unroll. Unroll the first three |
| 325 |
iterations by hand, to skip some division and masking. This |
| 326 |
is the most we can easily do without hassling with constants |
| 327 |
that a typical-platform compiler would reject. */ |
| 328 |
n += n >> (1 << 3); |
| 329 |
n += n >> (1 << 4); |
| 330 |
n += n >> (1 << 5); |
| 331 |
n &= x000_7f; |
| 332 |
for (int i = 64; i < 8 * sizeof n; i <<= 1) |
| 333 |
n = (n + (n >> i)) & max / (1ull << i | 1); |
| 334 |
return n; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
# ifdef _MSC_VER |
| 339 |
# if 1500 <= _MSC_VER && (defined _M_IX86 || defined _M_X64) |
| 340 |
/* Declare the few MSVC intrinsics that we need. We prefer not to include |
| 341 |
<intrin.h> because it would pollute the namespace. */ |
| 342 |
extern void __cpuid (int[4], int); |
| 343 |
# pragma intrinsic (__cpuid) |
| 344 |
extern unsigned int __popcnt (unsigned int); |
| 345 |
# pragma intrinsic (__popcnt) |
| 346 |
# ifdef _M_X64 |
| 347 |
extern unsigned long long __popcnt64 (unsigned long long); |
| 348 |
# pragma intrinsic (__popcnt64) |
| 349 |
# else |
| 350 |
_GL_STDC_COUNT_ONES_INLINE int |
| 351 |
__popcnt64 (unsigned long long int n) |
| 352 |
{ |
| 353 |
return __popcnt (n >> 32) + __popcnt (n); |
| 354 |
} |
| 355 |
# endif |
| 356 |
# endif |
| 357 |
|
| 358 |
/* 1 if supported, -1 if not, 0 if unknown. */ |
| 359 |
extern signed char __gl_stdbit_popcount_support; |
| 360 |
|
| 361 |
_GL_STDC_COUNT_ONES_INLINE bool |
| 362 |
__gl_stdbit_popcount_supported (void) |
| 363 |
{ |
| 364 |
if (!__gl_stdbit_popcount_support) |
| 365 |
{ |
| 366 |
/* Do as described in |
| 367 |
<https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64> |
| 368 |
Although Microsoft started requiring POPCNT in MS-Windows 11 24H2, |
| 369 |
we'll be more cautious. */ |
| 370 |
int cpu_info[4]; |
| 371 |
__cpuid (cpu_info, 1); |
| 372 |
__gl_stdbit_popcount_support = cpu_info[2] & 1 << 23 ? 1 : -1; |
| 373 |
} |
| 374 |
return 0 < __gl_stdbit_popcount_support; |
| 375 |
} |
| 376 |
_GL_STDC_COUNT_ONES_INLINE int |
| 377 |
__gl_stdbit_popcount (unsigned int n) |
| 378 |
{ |
| 379 |
return (__gl_stdbit_popcount_supported () |
| 380 |
? __popcnt (n) |
| 381 |
: __gl_stdbit_popcount_wide (n)); |
| 382 |
} |
| 383 |
_GL_STDC_COUNT_ONES_INLINE int |
| 384 |
__gl_stdbit_popcountl (unsigned long int n) |
| 385 |
{ |
| 386 |
return (__gl_stdbit_popcount_supported () |
| 387 |
? __popcnt (n) |
| 388 |
: __gl_stdbit_popcount_wide (n)); |
| 389 |
} |
| 390 |
_GL_STDC_COUNT_ONES_INLINE int |
| 391 |
__gl_stdbit_popcountll (unsigned long long int n) |
| 392 |
{ |
| 393 |
return (__gl_stdbit_popcount_supported () |
| 394 |
? __popcnt64 (n) |
| 395 |
: __gl_stdbit_popcount_wide (n)); |
| 396 |
} |
| 397 |
# else /* !_MSC_VER */ |
| 398 |
# define __gl_stdbit_popcount __gl_stdbit_popcount_wide |
| 399 |
# define __gl_stdbit_popcountl __gl_stdbit_popcount_wide |
| 400 |
# define __gl_stdbit_popcountll __gl_stdbit_popcount_wide |
| 401 |
# endif |
| 402 |
# endif |
| 403 |
#endif |
| 404 |
|
| 405 |
|
| 406 |
/* ISO C 23 § 7.18.3 Count Leading Zeros */ |
| 407 |
|
| 408 |
#if @GL_STDC_LEADING_ZEROS@ |
| 409 |
|
| 410 |
_GL_STDC_LEADING_ZEROS_INLINE unsigned int |
| 411 |
stdc_leading_zeros_ui (unsigned int n) |
| 412 |
{ |
| 413 |
return __gl_stdbit_clz (n); |
| 414 |
} |
| 415 |
|
| 416 |
_GL_STDC_LEADING_ZEROS_INLINE unsigned int |
| 417 |
stdc_leading_zeros_uc (unsigned char n) |
| 418 |
{ |
| 419 |
return stdc_leading_zeros_ui (n) - 8 * (sizeof 0u - sizeof n); |
| 420 |
} |
| 421 |
|
| 422 |
_GL_STDC_LEADING_ZEROS_INLINE unsigned int |
| 423 |
stdc_leading_zeros_us (unsigned short int n) |
| 424 |
{ |
| 425 |
return stdc_leading_zeros_ui (n) - 8 * (sizeof 0u - sizeof n); |
| 426 |
} |
| 427 |
|
| 428 |
_GL_STDC_LEADING_ZEROS_INLINE unsigned int |
| 429 |
stdc_leading_zeros_ul (unsigned long int n) |
| 430 |
{ |
| 431 |
return __gl_stdbit_clzl (n); |
| 432 |
} |
| 433 |
|
| 434 |
_GL_STDC_LEADING_ZEROS_INLINE unsigned int |
| 435 |
stdc_leading_zeros_ull (unsigned long long int n) |
| 436 |
{ |
| 437 |
return __gl_stdbit_clzll (n); |
| 438 |
} |
| 439 |
|
| 440 |
# define stdc_leading_zeros(n) \ |
| 441 |
(sizeof (n) == 1 ? stdc_leading_zeros_uc (n) \ |
| 442 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_leading_zeros_us (n) \ |
| 443 |
: sizeof (n) == sizeof 0u ? stdc_leading_zeros_ui (n) \ |
| 444 |
: sizeof (n) == sizeof 0ul ? stdc_leading_zeros_ul (n) \ |
| 445 |
: stdc_leading_zeros_ull (n)) |
| 446 |
|
| 447 |
#endif |
| 448 |
|
| 449 |
|
| 450 |
/* ISO C 23 § 7.18.4 Count Leading Ones */ |
| 451 |
|
| 452 |
#if @GL_STDC_LEADING_ONES@ |
| 453 |
|
| 454 |
_GL_STDC_LEADING_ONES_INLINE unsigned int |
| 455 |
stdc_leading_ones_uc (unsigned char n) |
| 456 |
{ |
| 457 |
return stdc_leading_zeros_uc (~n); |
| 458 |
} |
| 459 |
|
| 460 |
_GL_STDC_LEADING_ONES_INLINE unsigned int |
| 461 |
stdc_leading_ones_us (unsigned short int n) |
| 462 |
{ |
| 463 |
return stdc_leading_zeros_us (~n); |
| 464 |
} |
| 465 |
|
| 466 |
_GL_STDC_LEADING_ONES_INLINE unsigned int |
| 467 |
stdc_leading_ones_ui (unsigned int n) |
| 468 |
{ |
| 469 |
return stdc_leading_zeros_ui (~n); |
| 470 |
} |
| 471 |
|
| 472 |
_GL_STDC_LEADING_ONES_INLINE unsigned int |
| 473 |
stdc_leading_ones_ul (unsigned long int n) |
| 474 |
{ |
| 475 |
return stdc_leading_zeros_ul (~n); |
| 476 |
} |
| 477 |
|
| 478 |
_GL_STDC_LEADING_ONES_INLINE unsigned int |
| 479 |
stdc_leading_ones_ull (unsigned long long int n) |
| 480 |
{ |
| 481 |
return stdc_leading_zeros_ull (~n); |
| 482 |
} |
| 483 |
|
| 484 |
# define stdc_leading_ones(n) \ |
| 485 |
(sizeof (n) == 1 ? stdc_leading_ones_uc (n) \ |
| 486 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_leading_ones_us (n) \ |
| 487 |
: sizeof (n) == sizeof 0u ? stdc_leading_ones_ui (n) \ |
| 488 |
: sizeof (n) == sizeof 0ul ? stdc_leading_ones_ul (n) \ |
| 489 |
: stdc_leading_ones_ull (n)) |
| 490 |
|
| 491 |
#endif |
| 492 |
|
| 493 |
|
| 494 |
/* ISO C 23 § 7.18.5 Count Trailing Zeros */ |
| 495 |
|
| 496 |
#if @GL_STDC_TRAILING_ZEROS@ |
| 497 |
|
| 498 |
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int |
| 499 |
stdc_trailing_zeros_ui (unsigned int n) |
| 500 |
{ |
| 501 |
return __gl_stdbit_ctz (n); |
| 502 |
} |
| 503 |
|
| 504 |
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int |
| 505 |
stdc_trailing_zeros_uc (unsigned char n) |
| 506 |
{ |
| 507 |
return stdc_trailing_zeros_ui (n | (1 + (unsigned char) -1)); |
| 508 |
} |
| 509 |
|
| 510 |
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int |
| 511 |
stdc_trailing_zeros_us (unsigned short int n) |
| 512 |
{ |
| 513 |
return stdc_trailing_zeros_ui (n | (1 + (unsigned short int) -1)); |
| 514 |
} |
| 515 |
|
| 516 |
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int |
| 517 |
stdc_trailing_zeros_ul (unsigned long int n) |
| 518 |
{ |
| 519 |
return __gl_stdbit_ctzl (n); |
| 520 |
} |
| 521 |
|
| 522 |
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int |
| 523 |
stdc_trailing_zeros_ull (unsigned long long int n) |
| 524 |
{ |
| 525 |
return __gl_stdbit_ctzll (n); |
| 526 |
} |
| 527 |
|
| 528 |
# define stdc_trailing_zeros(n) \ |
| 529 |
(sizeof (n) == 1 ? stdc_trailing_zeros_uc (n) \ |
| 530 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_trailing_zeros_us (n) \ |
| 531 |
: sizeof (n) == sizeof 0u ? stdc_trailing_zeros_ui (n) \ |
| 532 |
: sizeof (n) == sizeof 0ul ? stdc_trailing_zeros_ul (n) \ |
| 533 |
: stdc_trailing_zeros_ull (n)) |
| 534 |
|
| 535 |
#endif |
| 536 |
|
| 537 |
|
| 538 |
/* ISO C 23 § 7.18.6 Count Trailing Ones */ |
| 539 |
|
| 540 |
#if @GL_STDC_TRAILING_ONES@ |
| 541 |
|
| 542 |
_GL_STDC_TRAILING_ONES_INLINE unsigned int |
| 543 |
stdc_trailing_ones_uc (unsigned char n) |
| 544 |
{ |
| 545 |
return stdc_trailing_zeros_uc (~n); |
| 546 |
} |
| 547 |
|
| 548 |
_GL_STDC_TRAILING_ONES_INLINE unsigned int |
| 549 |
stdc_trailing_ones_us (unsigned short int n) |
| 550 |
{ |
| 551 |
return stdc_trailing_zeros_us (~n); |
| 552 |
} |
| 553 |
|
| 554 |
_GL_STDC_TRAILING_ONES_INLINE unsigned int |
| 555 |
stdc_trailing_ones_ui (unsigned int n) |
| 556 |
{ |
| 557 |
return stdc_trailing_zeros_ui (~n); |
| 558 |
} |
| 559 |
|
| 560 |
_GL_STDC_TRAILING_ONES_INLINE unsigned int |
| 561 |
stdc_trailing_ones_ul (unsigned long int n) |
| 562 |
{ |
| 563 |
return stdc_trailing_zeros_ul (~n); |
| 564 |
} |
| 565 |
|
| 566 |
_GL_STDC_TRAILING_ONES_INLINE unsigned int |
| 567 |
stdc_trailing_ones_ull (unsigned long long int n) |
| 568 |
{ |
| 569 |
return stdc_trailing_zeros_ull (~n); |
| 570 |
} |
| 571 |
|
| 572 |
# define stdc_trailing_ones(n) \ |
| 573 |
(sizeof (n) == 1 ? stdc_trailing_ones_uc (n) \ |
| 574 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_trailing_ones_us (n) \ |
| 575 |
: sizeof (n) == sizeof 0u ? stdc_trailing_ones_ui (n) \ |
| 576 |
: sizeof (n) == sizeof 0ul ? stdc_trailing_ones_ul (n) \ |
| 577 |
: stdc_trailing_ones_ull (n)) |
| 578 |
|
| 579 |
#endif |
| 580 |
|
| 581 |
|
| 582 |
/* ISO C 23 § 7.18.7 First Leading Zero */ |
| 583 |
|
| 584 |
#if @GL_STDC_FIRST_LEADING_ZERO@ |
| 585 |
|
| 586 |
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int |
| 587 |
stdc_first_leading_zero_uc (unsigned char n) |
| 588 |
{ |
| 589 |
unsigned int count = stdc_leading_ones_uc (n); |
| 590 |
unsigned int bits = 8 * sizeof n; |
| 591 |
return count % bits + (count < bits); |
| 592 |
} |
| 593 |
|
| 594 |
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int |
| 595 |
stdc_first_leading_zero_us (unsigned short int n) |
| 596 |
{ |
| 597 |
unsigned int count = stdc_leading_ones_us (n); |
| 598 |
unsigned int bits = 8 * sizeof n; |
| 599 |
return count % bits + (count < bits); |
| 600 |
} |
| 601 |
|
| 602 |
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int |
| 603 |
stdc_first_leading_zero_ui (unsigned int n) |
| 604 |
{ |
| 605 |
unsigned int count = stdc_leading_ones_ui (n); |
| 606 |
unsigned int bits = 8 * sizeof n; |
| 607 |
return count % bits + (count < bits); |
| 608 |
} |
| 609 |
|
| 610 |
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int |
| 611 |
stdc_first_leading_zero_ul (unsigned long int n) |
| 612 |
{ |
| 613 |
unsigned int count = stdc_leading_ones_ul (n); |
| 614 |
unsigned int bits = 8 * sizeof n; |
| 615 |
return count % bits + (count < bits); |
| 616 |
} |
| 617 |
|
| 618 |
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int |
| 619 |
stdc_first_leading_zero_ull (unsigned long long int n) |
| 620 |
{ |
| 621 |
unsigned int count = stdc_leading_ones_ull (n); |
| 622 |
unsigned int bits = 8 * sizeof n; |
| 623 |
return count % bits + (count < bits); |
| 624 |
} |
| 625 |
|
| 626 |
# define stdc_first_leading_zero(n) \ |
| 627 |
(sizeof (n) == 1 ? stdc_first_leading_zero_uc (n) \ |
| 628 |
: sizeof (n) == sizeof (unsigned short) ? stdc_first_leading_zero_us (n) \ |
| 629 |
: sizeof (n) == sizeof 0u ? stdc_first_leading_zero_ui (n) \ |
| 630 |
: sizeof (n) == sizeof 0ul ? stdc_first_leading_zero_ul (n) \ |
| 631 |
: stdc_first_leading_zero_ull (n)) |
| 632 |
|
| 633 |
#endif |
| 634 |
|
| 635 |
|
| 636 |
/* ISO C 23 § 7.18.8 First Leading One */ |
| 637 |
|
| 638 |
#if @GL_STDC_FIRST_LEADING_ONE@ |
| 639 |
|
| 640 |
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int |
| 641 |
stdc_first_leading_one_uc (unsigned char n) |
| 642 |
{ |
| 643 |
unsigned int count = stdc_leading_zeros_uc (n); |
| 644 |
unsigned int bits = 8 * sizeof n; |
| 645 |
return count % bits + (count < bits); |
| 646 |
} |
| 647 |
|
| 648 |
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int |
| 649 |
stdc_first_leading_one_us (unsigned short int n) |
| 650 |
{ |
| 651 |
unsigned int count = stdc_leading_zeros_us (n); |
| 652 |
unsigned int bits = 8 * sizeof n; |
| 653 |
return count % bits + (count < bits); |
| 654 |
} |
| 655 |
|
| 656 |
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int |
| 657 |
stdc_first_leading_one_ui (unsigned int n) |
| 658 |
{ |
| 659 |
unsigned int count = stdc_leading_zeros_ui (n); |
| 660 |
unsigned int bits = 8 * sizeof n; |
| 661 |
return count % bits + (count < bits); |
| 662 |
} |
| 663 |
|
| 664 |
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int |
| 665 |
stdc_first_leading_one_ul (unsigned long int n) |
| 666 |
{ |
| 667 |
unsigned int count = stdc_leading_zeros_ul (n); |
| 668 |
unsigned int bits = 8 * sizeof n; |
| 669 |
return count % bits + (count < bits); |
| 670 |
} |
| 671 |
|
| 672 |
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int |
| 673 |
stdc_first_leading_one_ull (unsigned long long int n) |
| 674 |
{ |
| 675 |
unsigned int count = stdc_leading_zeros_ull (n); |
| 676 |
unsigned int bits = 8 * sizeof n; |
| 677 |
return count % bits + (count < bits); |
| 678 |
} |
| 679 |
|
| 680 |
# define stdc_first_leading_one(n) \ |
| 681 |
(sizeof (n) == 1 ? stdc_first_leading_one_uc (n) \ |
| 682 |
: sizeof (n) == sizeof (unsigned short) ? stdc_first_leading_one_us (n) \ |
| 683 |
: sizeof (n) == sizeof 0u ? stdc_first_leading_one_ui (n) \ |
| 684 |
: sizeof (n) == sizeof 0ul ? stdc_first_leading_one_ul (n) \ |
| 685 |
: stdc_first_leading_one_ull (n)) |
| 686 |
|
| 687 |
#endif |
| 688 |
|
| 689 |
|
| 690 |
/* ISO C 23 § 7.18.9 First Trailing Zero */ |
| 691 |
|
| 692 |
#if @GL_STDC_FIRST_TRAILING_ZERO@ |
| 693 |
|
| 694 |
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int |
| 695 |
stdc_first_trailing_zero_uc (unsigned char n) |
| 696 |
{ |
| 697 |
unsigned int count = stdc_trailing_ones_uc (n); |
| 698 |
unsigned int bits = 8 * sizeof n; |
| 699 |
return count % bits + (count < bits); |
| 700 |
} |
| 701 |
|
| 702 |
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int |
| 703 |
stdc_first_trailing_zero_us (unsigned short int n) |
| 704 |
{ |
| 705 |
unsigned int count = stdc_trailing_ones_us (n); |
| 706 |
unsigned int bits = 8 * sizeof n; |
| 707 |
return count % bits + (count < bits); |
| 708 |
} |
| 709 |
|
| 710 |
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int |
| 711 |
stdc_first_trailing_zero_ui (unsigned int n) |
| 712 |
{ |
| 713 |
unsigned int count = stdc_trailing_ones_ui (n); |
| 714 |
unsigned int bits = 8 * sizeof n; |
| 715 |
return count % bits + (count < bits); |
| 716 |
} |
| 717 |
|
| 718 |
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int |
| 719 |
stdc_first_trailing_zero_ul (unsigned long int n) |
| 720 |
{ |
| 721 |
unsigned int count = stdc_trailing_ones_ul (n); |
| 722 |
unsigned int bits = 8 * sizeof n; |
| 723 |
return count % bits + (count < bits); |
| 724 |
} |
| 725 |
|
| 726 |
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int |
| 727 |
stdc_first_trailing_zero_ull (unsigned long long int n) |
| 728 |
{ |
| 729 |
unsigned int count = stdc_trailing_ones_ull (n); |
| 730 |
unsigned int bits = 8 * sizeof n; |
| 731 |
return count % bits + (count < bits); |
| 732 |
} |
| 733 |
|
| 734 |
# define stdc_first_trailing_zero(n) \ |
| 735 |
(sizeof (n) == 1 ? stdc_first_trailing_zero_uc (n) \ |
| 736 |
: sizeof (n) == sizeof (unsigned short) ? stdc_first_trailing_zero_us (n) \ |
| 737 |
: sizeof (n) == sizeof 0u ? stdc_first_trailing_zero_ui (n) \ |
| 738 |
: sizeof (n) == sizeof 0ul ? stdc_first_trailing_zero_ul (n) \ |
| 739 |
: stdc_first_trailing_zero_ull (n)) |
| 740 |
|
| 741 |
#endif |
| 742 |
|
| 743 |
|
| 744 |
/* ISO C 23 § 7.18.10 First Trailing One */ |
| 745 |
|
| 746 |
#if @GL_STDC_FIRST_TRAILING_ONE@ |
| 747 |
|
| 748 |
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int |
| 749 |
stdc_first_trailing_one_uc (unsigned char n) |
| 750 |
{ |
| 751 |
unsigned int count = stdc_trailing_zeros_uc (n); |
| 752 |
unsigned int bits = 8 * sizeof n; |
| 753 |
return count % bits + (count < bits); |
| 754 |
} |
| 755 |
|
| 756 |
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int |
| 757 |
stdc_first_trailing_one_us (unsigned short int n) |
| 758 |
{ |
| 759 |
unsigned int count = stdc_trailing_zeros_us (n); |
| 760 |
unsigned int bits = 8 * sizeof n; |
| 761 |
return count % bits + (count < bits); |
| 762 |
} |
| 763 |
|
| 764 |
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int |
| 765 |
stdc_first_trailing_one_ui (unsigned int n) |
| 766 |
{ |
| 767 |
unsigned int count = stdc_trailing_zeros_ui (n); |
| 768 |
unsigned int bits = 8 * sizeof n; |
| 769 |
return count % bits + (count < bits); |
| 770 |
} |
| 771 |
|
| 772 |
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int |
| 773 |
stdc_first_trailing_one_ul (unsigned long int n) |
| 774 |
{ |
| 775 |
unsigned int count = stdc_trailing_zeros_ul (n); |
| 776 |
unsigned int bits = 8 * sizeof n; |
| 777 |
return count % bits + (count < bits); |
| 778 |
} |
| 779 |
|
| 780 |
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int |
| 781 |
stdc_first_trailing_one_ull (unsigned long long int n) |
| 782 |
{ |
| 783 |
unsigned int count = stdc_trailing_zeros_ull (n); |
| 784 |
unsigned int bits = 8 * sizeof n; |
| 785 |
return count % bits + (count < bits); |
| 786 |
} |
| 787 |
|
| 788 |
#define stdc_first_trailing_one(n) \ |
| 789 |
(sizeof (n) == 1 ? stdc_first_trailing_one_uc (n) \ |
| 790 |
: sizeof (n) == sizeof (unsigned short) ? stdc_first_trailing_one_us (n) \ |
| 791 |
: sizeof (n) == sizeof 0u ? stdc_first_trailing_one_ui (n) \ |
| 792 |
: sizeof (n) == sizeof 0ul ? stdc_first_trailing_one_ul (n) \ |
| 793 |
: stdc_first_trailing_one_ull (n)) |
| 794 |
|
| 795 |
#endif |
| 796 |
|
| 797 |
|
| 798 |
/* ISO C 23 § 7.18.12 Count Ones */ |
| 799 |
|
| 800 |
#if @GL_STDC_COUNT_ONES@ |
| 801 |
|
| 802 |
_GL_STDC_COUNT_ONES_INLINE unsigned int |
| 803 |
stdc_count_ones_ui (unsigned int n) |
| 804 |
{ |
| 805 |
return __gl_stdbit_popcount (n); |
| 806 |
} |
| 807 |
|
| 808 |
_GL_STDC_COUNT_ONES_INLINE unsigned int |
| 809 |
stdc_count_ones_uc (unsigned char n) |
| 810 |
{ |
| 811 |
return stdc_count_ones_ui (n); |
| 812 |
} |
| 813 |
|
| 814 |
_GL_STDC_COUNT_ONES_INLINE unsigned int |
| 815 |
stdc_count_ones_us (unsigned short int n) |
| 816 |
{ |
| 817 |
return stdc_count_ones_ui (n); |
| 818 |
} |
| 819 |
|
| 820 |
_GL_STDC_COUNT_ONES_INLINE unsigned int |
| 821 |
stdc_count_ones_ul (unsigned long int n) |
| 822 |
{ |
| 823 |
return __gl_stdbit_popcountl (n); |
| 824 |
} |
| 825 |
|
| 826 |
_GL_STDC_COUNT_ONES_INLINE unsigned int |
| 827 |
stdc_count_ones_ull (unsigned long long int n) |
| 828 |
{ |
| 829 |
return __gl_stdbit_popcountll (n); |
| 830 |
} |
| 831 |
|
| 832 |
# define stdc_count_ones(n) \ |
| 833 |
(sizeof (n) == 1 ? stdc_count_ones_uc (n) \ |
| 834 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_count_ones_us (n) \ |
| 835 |
: sizeof (n) == sizeof 0u ? stdc_count_ones_ui (n) \ |
| 836 |
: sizeof (n) == sizeof 0ul ? stdc_count_ones_ul (n) \ |
| 837 |
: stdc_count_ones_ull (n)) |
| 838 |
|
| 839 |
#endif |
| 840 |
|
| 841 |
|
| 842 |
/* ISO C 23 § 7.18.11 Count Zeros */ |
| 843 |
|
| 844 |
#if @GL_STDC_COUNT_ZEROS@ |
| 845 |
|
| 846 |
_GL_STDC_COUNT_ZEROS_INLINE unsigned int |
| 847 |
stdc_count_zeros_uc (unsigned char n) |
| 848 |
{ |
| 849 |
return stdc_count_ones_uc (~n); |
| 850 |
} |
| 851 |
|
| 852 |
_GL_STDC_COUNT_ZEROS_INLINE unsigned int |
| 853 |
stdc_count_zeros_us (unsigned short int n) |
| 854 |
{ |
| 855 |
return stdc_count_ones_us (~n); |
| 856 |
} |
| 857 |
|
| 858 |
_GL_STDC_COUNT_ZEROS_INLINE unsigned int |
| 859 |
stdc_count_zeros_ui (unsigned int n) |
| 860 |
{ |
| 861 |
return stdc_count_ones_ui (~n); |
| 862 |
} |
| 863 |
|
| 864 |
_GL_STDC_COUNT_ZEROS_INLINE unsigned int |
| 865 |
stdc_count_zeros_ul (unsigned long int n) |
| 866 |
{ |
| 867 |
return stdc_count_ones_ul (~n); |
| 868 |
} |
| 869 |
|
| 870 |
_GL_STDC_COUNT_ZEROS_INLINE unsigned int |
| 871 |
stdc_count_zeros_ull (unsigned long long int n) |
| 872 |
{ |
| 873 |
return stdc_count_ones_ull (~n); |
| 874 |
} |
| 875 |
|
| 876 |
# define stdc_count_zeros(n) \ |
| 877 |
(sizeof (n) == 1 ? stdc_count_zeros_uc (n) \ |
| 878 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_count_zeros_us (n) \ |
| 879 |
: sizeof (n) == sizeof 0u ? stdc_count_zeros_ui (n) \ |
| 880 |
: sizeof (n) == sizeof 0ul ? stdc_count_zeros_ul (n) \ |
| 881 |
: stdc_count_zeros_ull (n)) |
| 882 |
|
| 883 |
#endif |
| 884 |
|
| 885 |
|
| 886 |
/* ISO C 23 § 7.18.13 Single-bit Check */ |
| 887 |
|
| 888 |
#if @GL_STDC_HAS_SINGLE_BIT@ |
| 889 |
|
| 890 |
_GL_STDC_HAS_SINGLE_BIT_INLINE bool |
| 891 |
stdc_has_single_bit_uc (unsigned char n) |
| 892 |
{ |
| 893 |
unsigned char n_1 = n - 1, nx = n_1 ^ n; |
| 894 |
return n_1 < nx; |
| 895 |
} |
| 896 |
|
| 897 |
_GL_STDC_HAS_SINGLE_BIT_INLINE bool |
| 898 |
stdc_has_single_bit_us (unsigned short int n) |
| 899 |
{ |
| 900 |
unsigned short int n_1 = n - 1, nx = n_1 ^ n; |
| 901 |
return n_1 < nx; |
| 902 |
} |
| 903 |
|
| 904 |
_GL_STDC_HAS_SINGLE_BIT_INLINE bool |
| 905 |
stdc_has_single_bit_ui (unsigned int n) |
| 906 |
{ |
| 907 |
unsigned int n_1 = n - 1, nx = n_1 ^ n; |
| 908 |
return n_1 < nx; |
| 909 |
} |
| 910 |
|
| 911 |
_GL_STDC_HAS_SINGLE_BIT_INLINE bool |
| 912 |
stdc_has_single_bit_ul (unsigned long int n) |
| 913 |
{ |
| 914 |
unsigned long int n_1 = n - 1, nx = n_1 ^ n; |
| 915 |
return n_1 < nx; |
| 916 |
} |
| 917 |
|
| 918 |
_GL_STDC_HAS_SINGLE_BIT_INLINE bool |
| 919 |
stdc_has_single_bit_ull (unsigned long long int n) |
| 920 |
{ |
| 921 |
unsigned long long int n_1 = n - 1, nx = n_1 ^ n; |
| 922 |
return n_1 < nx; |
| 923 |
} |
| 924 |
|
| 925 |
# define stdc_has_single_bit(n) \ |
| 926 |
((bool) \ |
| 927 |
(sizeof (n) == 1 ? stdc_has_single_bit_uc (n) \ |
| 928 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_has_single_bit_us (n) \ |
| 929 |
: sizeof (n) == sizeof 0u ? stdc_has_single_bit_ui (n) \ |
| 930 |
: sizeof (n) == sizeof 0ul ? stdc_has_single_bit_ul (n) \ |
| 931 |
: stdc_has_single_bit_ull (n))) |
| 932 |
|
| 933 |
#endif |
| 934 |
|
| 935 |
|
| 936 |
/* ISO C 23 § 7.18.14 Bit Width */ |
| 937 |
|
| 938 |
#if @GL_STDC_BIT_WIDTH@ |
| 939 |
|
| 940 |
_GL_STDC_BIT_WIDTH_INLINE unsigned int |
| 941 |
stdc_bit_width_uc (unsigned char n) |
| 942 |
{ |
| 943 |
return 8 * sizeof n - stdc_leading_zeros_uc (n); |
| 944 |
} |
| 945 |
|
| 946 |
_GL_STDC_BIT_WIDTH_INLINE unsigned int |
| 947 |
stdc_bit_width_us (unsigned short int n) |
| 948 |
{ |
| 949 |
return 8 * sizeof n - stdc_leading_zeros_us (n); |
| 950 |
} |
| 951 |
|
| 952 |
_GL_STDC_BIT_WIDTH_INLINE unsigned int |
| 953 |
stdc_bit_width_ui (unsigned int n) |
| 954 |
{ |
| 955 |
return 8 * sizeof n - stdc_leading_zeros_ui (n); |
| 956 |
} |
| 957 |
|
| 958 |
_GL_STDC_BIT_WIDTH_INLINE unsigned int |
| 959 |
stdc_bit_width_ul (unsigned long int n) |
| 960 |
{ |
| 961 |
return 8 * sizeof n - stdc_leading_zeros_ul (n); |
| 962 |
} |
| 963 |
|
| 964 |
_GL_STDC_BIT_WIDTH_INLINE unsigned int |
| 965 |
stdc_bit_width_ull (unsigned long long int n) |
| 966 |
{ |
| 967 |
return 8 * sizeof n - stdc_leading_zeros_ull (n); |
| 968 |
} |
| 969 |
|
| 970 |
# define stdc_bit_width(n) \ |
| 971 |
(sizeof (n) == 1 ? stdc_bit_width_uc (n) \ |
| 972 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_bit_width_us (n) \ |
| 973 |
: sizeof (n) == sizeof 0u ? stdc_bit_width_ui (n) \ |
| 974 |
: sizeof (n) == sizeof 0ul ? stdc_bit_width_ul (n) \ |
| 975 |
: stdc_bit_width_ull (n)) |
| 976 |
|
| 977 |
#endif |
| 978 |
|
| 979 |
|
| 980 |
/* ISO C 23 § 7.18.15 Bit Floor */ |
| 981 |
|
| 982 |
#if @GL_STDC_BIT_FLOOR@ |
| 983 |
|
| 984 |
_GL_STDC_BIT_FLOOR_INLINE unsigned char |
| 985 |
stdc_bit_floor_uc (unsigned char n) |
| 986 |
{ |
| 987 |
return n ? 1u << (stdc_bit_width_uc (n) - 1) : 0; |
| 988 |
} |
| 989 |
|
| 990 |
_GL_STDC_BIT_FLOOR_INLINE unsigned short int |
| 991 |
stdc_bit_floor_us (unsigned short int n) |
| 992 |
{ |
| 993 |
return n ? 1u << (stdc_bit_width_us (n) - 1) : 0; |
| 994 |
} |
| 995 |
|
| 996 |
_GL_STDC_BIT_FLOOR_INLINE unsigned int |
| 997 |
stdc_bit_floor_ui (unsigned int n) |
| 998 |
{ |
| 999 |
return n ? 1u << (stdc_bit_width_ui (n) - 1) : 0; |
| 1000 |
} |
| 1001 |
|
| 1002 |
_GL_STDC_BIT_FLOOR_INLINE unsigned long int |
| 1003 |
stdc_bit_floor_ul (unsigned long int n) |
| 1004 |
{ |
| 1005 |
return n ? 1ul << (stdc_bit_width_ul (n) - 1) : 0; |
| 1006 |
} |
| 1007 |
|
| 1008 |
_GL_STDC_BIT_FLOOR_INLINE unsigned long long int |
| 1009 |
stdc_bit_floor_ull (unsigned long long int n) |
| 1010 |
{ |
| 1011 |
return n ? 1ull << (stdc_bit_width_ull (n) - 1) : 0; |
| 1012 |
} |
| 1013 |
|
| 1014 |
# define stdc_bit_floor(n) \ |
| 1015 |
(_GL_STDBIT_TYPEOF_CAST \ |
| 1016 |
(n, \ |
| 1017 |
(sizeof (n) == 1 ? stdc_bit_floor_uc (n) \ |
| 1018 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_bit_floor_us (n) \ |
| 1019 |
: sizeof (n) == sizeof 0u ? stdc_bit_floor_ui (n) \ |
| 1020 |
: sizeof (n) == sizeof 0ul ? stdc_bit_floor_ul (n) \ |
| 1021 |
: stdc_bit_floor_ull (n)))) |
| 1022 |
|
| 1023 |
#endif |
| 1024 |
|
| 1025 |
|
| 1026 |
/* ISO C 23 § 7.18.16 Bit Ceiling */ |
| 1027 |
|
| 1028 |
#if @GL_STDC_BIT_CEIL@ |
| 1029 |
|
| 1030 |
_GL_STDC_BIT_CEIL_INLINE unsigned char |
| 1031 |
stdc_bit_ceil_uc (unsigned char n) |
| 1032 |
{ |
| 1033 |
return n <= 1 ? 1 : 2u << (stdc_bit_width_uc (n - 1) - 1); |
| 1034 |
} |
| 1035 |
|
| 1036 |
_GL_STDC_BIT_CEIL_INLINE unsigned short int |
| 1037 |
stdc_bit_ceil_us (unsigned short int n) |
| 1038 |
{ |
| 1039 |
return n <= 1 ? 1 : 2u << (stdc_bit_width_us (n - 1) - 1); |
| 1040 |
} |
| 1041 |
|
| 1042 |
_GL_STDC_BIT_CEIL_INLINE unsigned int |
| 1043 |
stdc_bit_ceil_ui (unsigned int n) |
| 1044 |
{ |
| 1045 |
return n <= 1 ? 1 : 2u << (stdc_bit_width_ui (n - 1) - 1); |
| 1046 |
} |
| 1047 |
|
| 1048 |
_GL_STDC_BIT_CEIL_INLINE unsigned long int |
| 1049 |
stdc_bit_ceil_ul (unsigned long int n) |
| 1050 |
{ |
| 1051 |
return n <= 1 ? 1 : 2ul << (stdc_bit_width_ul (n - 1) - 1); |
| 1052 |
} |
| 1053 |
|
| 1054 |
_GL_STDC_BIT_CEIL_INLINE unsigned long long int |
| 1055 |
stdc_bit_ceil_ull (unsigned long long int n) |
| 1056 |
{ |
| 1057 |
return n <= 1 ? 1 : 2ull << (stdc_bit_width_ull (n - 1) - 1); |
| 1058 |
} |
| 1059 |
|
| 1060 |
# define stdc_bit_ceil(n) \ |
| 1061 |
(_GL_STDBIT_TYPEOF_CAST \ |
| 1062 |
(n, \ |
| 1063 |
(sizeof (n) == 1 ? stdc_bit_ceil_uc (n) \ |
| 1064 |
: sizeof (n) == sizeof (unsigned short int) ? stdc_bit_ceil_us (n) \ |
| 1065 |
: sizeof (n) == sizeof 0u ? stdc_bit_ceil_ui (n) \ |
| 1066 |
: sizeof (n) == sizeof 0ul ? stdc_bit_ceil_ul (n) \ |
| 1067 |
: stdc_bit_ceil_ull (n)))) |
| 1068 |
|
| 1069 |
#endif |
| 1070 |
|
| 1071 |
|
| 1072 |
#ifdef __cplusplus |
| 1073 |
} |
| 1074 |
#endif |
| 1075 |
|
| 1076 |
_GL_INLINE_HEADER_END |
| 1077 |
|
| 1078 |
#endif /* STDBIT_H */ |