Coverage Report

Created: 2026-11-12 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnutls/gl/stdbit.h
Line
Count
Source
1
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
2
/* stdbit.h - C23 bit and byte utilities for non-C23 platforms
3
4
   Copyright 2024-2026 Free Software Foundation, Inc.
5
6
   This file is free software: you can redistribute it and/or modify
7
   it under the terms of the GNU Lesser General Public License as
8
   published by the Free Software Foundation; either version 2.1 of the
9
   License, or (at your option) any later version.
10
11
   This file is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU Lesser General Public License for more details.
15
16
   You should have received a copy of the GNU Lesser General Public License
17
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
18
19
/* Written by Paul Eggert.  */
20
21
#ifndef __STDBIT_H
22
23
#if __GNUC__ >= 3
24
25
#endif
26
27
28
/* The include_next requires a split double-inclusion guard.  */
29
#if 0
30
/* The FreeBSD 15.1 <stdbit.h> uses the _Bool type.  */
31
# if defined __FreeBSD__ && defined __cplusplus
32
#  define _Bool bool
33
# endif
34
# include_next <stdbit.h>
35
#endif
36
37
#ifndef __STDBIT_H
38
#define __STDBIT_H
39
40
/* This file uses _GL_INLINE, WORDS_BIGENDIAN.  */
41
#if !_GL_CONFIG_H_INCLUDED
42
 #error "Please include config.h first."
43
#endif
44
45
/* If needed for APIs, get size_t, avoiding namespace pollution on GNU.  */
46
#if 0 && !defined __STDC_VERSION_STDBIT_H__
47
# define __need_size_t
48
# include <stddef.h>
49
#endif
50
51
/* If needed for APIs, get intN_t, uintN_t, int_leastN_t,
52
   uint_leastN_t, and (for internal use) get equivalents of
53
   uint_fast{16,32,64}_t.  Avoid namespace pollution on GNU.  */
54
#if (1 \
55
     || 0 || 0 \
56
     || 0 || 0)
57
# if !(defined __STDC_VERSION_STDBIT_H__ && defined __UINT_FAST64_TYPE__)
58
#  include <stdint.h>
59
#  define _GL_STDBIT_UINT_FAST16 uint_fast16_t
60
#  define _GL_STDBIT_UINT_FAST32 uint_fast32_t
61
#  define _GL_STDBIT_UINT_FAST64 uint_fast64_t
62
# else
63
#  define _GL_STDBIT_UINT_FAST16 __UINT_FAST16_TYPE__
64
#  define _GL_STDBIT_UINT_FAST32 __UINT_FAST32_TYPE__
65
#  define _GL_STDBIT_UINT_FAST64 __UINT_FAST64_TYPE__
66
# endif
67
#endif
68
69
#if 1 || 0 || 0
70
71
/* Determine whether the compiler supports the __builtin_bswap{16,32,64}
72
   builtins.  */
73
# if defined __GNUC__ && 4 < __GNUC__ + (8 <= __GNUC_MINOR__)
74
#  define _GL_STDBIT_HAS_BUILTIN_BSWAP16 1
75
# elif defined __has_builtin
76
#  if __has_builtin (__builtin_bswap16)
77
#   define _GL_STDBIT_HAS_BUILTIN_BSWAP16 1
78
#  endif
79
# endif
80
# if defined __GNUC__ && 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
81
#  define _GL_STDBIT_HAS_BUILTIN_BSWAP32 1
82
#  define _GL_STDBIT_HAS_BUILTIN_BSWAP64 1
83
# elif defined __has_builtin
84
#  if __has_builtin (__builtin_bswap32)
85
#   define _GL_STDBIT_HAS_BUILTIN_BSWAP32 1
86
#  endif
87
#  if __has_builtin (__builtin_bswap64)
88
#   define _GL_STDBIT_HAS_BUILTIN_BSWAP64 1
89
#  endif
90
# endif
91
92
#endif
93
94
#if 0 || 0
95
96
/* Get memcpy, but keep namespace clean on GNU.  */
97
# ifdef __has_builtin
98
#  if __has_builtin (__builtin_memcpy)
99
#   define _GL_STDBIT_MEMCPY(dest, src, n) __builtin_memcpy (dest, src, n)
100
#  endif
101
# endif
102
# ifndef _GL_STDBIT_MEMCPY
103
#  include <string.h>
104
#  define _GL_STDBIT_MEMCPY(dest, src, n) memcpy (dest, src, n)
105
# endif
106
107
#endif
108
109
_GL_INLINE_HEADER_BEGIN
110
111
#ifndef _GL_STDC_LEADING_ZEROS_INLINE
112
# define _GL_STDC_LEADING_ZEROS_INLINE _GL_INLINE
113
#endif
114
#ifndef _GL_STDC_LEADING_ONES_INLINE
115
# define _GL_STDC_LEADING_ONES_INLINE _GL_INLINE
116
#endif
117
#ifndef _GL_STDC_TRAILING_ZEROS_INLINE
118
# define _GL_STDC_TRAILING_ZEROS_INLINE _GL_INLINE
119
#endif
120
#ifndef _GL_STDC_TRAILING_ONES_INLINE
121
# define _GL_STDC_TRAILING_ONES_INLINE _GL_INLINE
122
#endif
123
#ifndef _GL_STDC_FIRST_LEADING_ZERO_INLINE
124
# define _GL_STDC_FIRST_LEADING_ZERO_INLINE _GL_INLINE
125
#endif
126
#ifndef _GL_STDC_FIRST_LEADING_ONE_INLINE
127
# define _GL_STDC_FIRST_LEADING_ONE_INLINE _GL_INLINE
128
#endif
129
#ifndef _GL_STDC_FIRST_TRAILING_ZERO_INLINE
130
# define _GL_STDC_FIRST_TRAILING_ZERO_INLINE _GL_INLINE
131
#endif
132
#ifndef _GL_STDC_FIRST_TRAILING_ONE_INLINE
133
# define _GL_STDC_FIRST_TRAILING_ONE_INLINE _GL_INLINE
134
#endif
135
#ifndef _GL_STDC_COUNT_ZEROS_INLINE
136
# define _GL_STDC_COUNT_ZEROS_INLINE _GL_INLINE
137
#endif
138
#ifndef _GL_STDC_COUNT_ONES_INLINE
139
# define _GL_STDC_COUNT_ONES_INLINE _GL_INLINE
140
#endif
141
#ifndef _GL_STDC_HAS_SINGLE_BIT_INLINE
142
# define _GL_STDC_HAS_SINGLE_BIT_INLINE _GL_INLINE
143
#endif
144
#ifndef _GL_STDC_BIT_WIDTH_INLINE
145
# define _GL_STDC_BIT_WIDTH_INLINE _GL_INLINE
146
#endif
147
#ifndef _GL_STDC_BIT_FLOOR_INLINE
148
# define _GL_STDC_BIT_FLOOR_INLINE _GL_INLINE
149
#endif
150
#ifndef _GL_STDC_BIT_CEIL_INLINE
151
# define _GL_STDC_BIT_CEIL_INLINE _GL_INLINE
152
#endif
153
#ifndef _GL_STDC_ROTATE_LEFT_INLINE
154
# define _GL_STDC_ROTATE_LEFT_INLINE _GL_INLINE
155
#endif
156
#ifndef _GL_STDC_ROTATE_RIGHT_INLINE
157
# define _GL_STDC_ROTATE_RIGHT_INLINE _GL_INLINE
158
#endif
159
#ifndef _GL_STDC_MEMREVERSE8_INLINE
160
# define _GL_STDC_MEMREVERSE8_INLINE _GL_INLINE
161
#endif
162
#ifndef _GL_STDC_MEMREVERSE8U_INLINE
163
# define _GL_STDC_MEMREVERSE8U_INLINE _GL_INLINE
164
#endif
165
#ifndef _GL_STDC_LOAD8_ALIGNED_INLINE
166
# define _GL_STDC_LOAD8_ALIGNED_INLINE _GL_INLINE
167
#endif
168
#ifndef _GL_STDC_LOAD8_INLINE
169
# define _GL_STDC_LOAD8_INLINE _GL_INLINE
170
#endif
171
#ifndef _GL_STDC_STORE8_ALIGNED_INLINE
172
# define _GL_STDC_STORE8_ALIGNED_INLINE _GL_INLINE
173
#endif
174
#ifndef _GL_STDC_STORE8_INLINE
175
# define _GL_STDC_STORE8_INLINE _GL_INLINE
176
#endif
177
178
/* An expression, preferably with the type of A, that has the value of B.  */
179
#if ((defined __GNUC__ && 2 <= __GNUC__) \
180
     || (defined __clang_major__ && 4 <= __clang_major__) \
181
     || (defined __IBMC__ && 1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
182
     || (defined __SUNPRO_C && 0x5110 <= __SUNPRO_C && !__STDC__) \
183
     || (defined _MSC_VER && 1939 <= _MSC_VER && !defined __cplusplus))
184
0
# define _GL_STDBIT_TYPEOF_CAST(a, b) ((__typeof__ (a)) (b))
185
#elif 202311 <= __STDC_VERSION__
186
# define _GL_STDBIT_TYPEOF_CAST(a, b) ((typeof (a)) (b))
187
#else
188
/* This platform is so old that it lacks typeof, so _Generic is likely
189
   missing or unreliable.  The C23 standard seems to allow yielding B
190
   (which is always unsigned long long int), so do that.  */
191
# define _GL_STDBIT_TYPEOF_CAST(a, b) (b)
192
#endif
193
194
195
#ifdef __cplusplus
196
extern "C" {
197
#endif
198
199
200
/* Some systems are only missing C2y features in stdbit.h.  */
201
#ifndef __STDC_VERSION_STDBIT_H__
202
203
/* ISO C 23 § 7.18.2 Endian  */
204
205
#define __STDC_ENDIAN_BIG__ 4321
206
#define __STDC_ENDIAN_LITTLE__ 1234
207
#ifdef WORDS_BIGENDIAN
208
# define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_BIG__
209
#else
210
# define __STDC_ENDIAN_NATIVE__ __STDC_ENDIAN_LITTLE__
211
#endif
212
213
#endif /* !__STDC_VERSION_STDBIT_H__ */
214
215
216
/* Some systems are only missing C2y features in stdbit.h.  */
217
#ifndef __STDC_VERSION_STDBIT_H__
218
219
/* ISO C 23 § 7.18.3 Count Leading Zeros  */
220
221
#if 0
222
223
# if 3 < __GNUC__ + (4 <= __GNUC_MINOR__) || 4 <= __clang_major__
224
#  define _GL_STDBIT_HAS_BUILTIN_CLZ true
225
# elif defined __has_builtin
226
#  if (__has_builtin (__builtin_clz) \
227
       && __has_builtin (__builtin_clzl) \
228
       && __has_builtin (__builtin_clzll))
229
#   define _GL_STDBIT_HAS_BUILTIN_CLZ true
230
#  endif
231
# endif
232
233
/* Count leading 0 bits of N, even if N is 0.  */
234
# if !GNULIB_defined_clz_functions
235
#  ifdef _GL_STDBIT_HAS_BUILTIN_CLZ
236
_GL_STDC_LEADING_ZEROS_INLINE int
237
_gl_stdbit_clz (unsigned int n)
238
{
239
  return n ? __builtin_clz (n) : 8 * sizeof n;
240
}
241
_GL_STDC_LEADING_ZEROS_INLINE int
242
_gl_stdbit_clzl (unsigned long int n)
243
{
244
  return n ? __builtin_clzl (n) : 8 * sizeof n;
245
}
246
_GL_STDC_LEADING_ZEROS_INLINE int
247
_gl_stdbit_clzll (unsigned long long int n)
248
{
249
  return n ? __builtin_clzll (n) : 8 * sizeof n;
250
}
251
#  elif defined _MSC_VER
252
253
/* Declare the few MSVC intrinsics that we need.  We prefer not to include
254
   <intrin.h> because it would pollute the namespace.  */
255
extern unsigned char _BitScanReverse (unsigned long *, unsigned long);
256
#   pragma intrinsic (_BitScanReverse)
257
#   ifdef _M_X64
258
extern unsigned char _BitScanReverse64 (unsigned long *, unsigned long long);
259
#    pragma intrinsic (_BitScanReverse64)
260
#   endif
261
262
_GL_STDC_LEADING_ZEROS_INLINE int
263
_gl_stdbit_clzl (unsigned long int n)
264
{
265
  unsigned long int r;
266
  return 8 * sizeof n - (_BitScanReverse (&r, n) ? r + 1 : 0);
267
}
268
_GL_STDC_LEADING_ZEROS_INLINE int
269
_gl_stdbit_clz (unsigned int n)
270
{
271
  return _gl_stdbit_clzl (n) - 8 * (sizeof 0ul - sizeof n);
272
}
273
_GL_STDC_LEADING_ZEROS_INLINE int
274
_gl_stdbit_clzll (unsigned long long int n)
275
{
276
#   ifdef _M_X64
277
  unsigned long int r;
278
  return 8 * sizeof n - (_BitScanReverse64 (&r, n) ? r + 1 : 0);
279
#   else
280
  unsigned long int hi = n >> 32;
281
  return _gl_stdbit_clzl (hi ? hi : n) + (hi ? 0 : 32);
282
#   endif
283
}
284
285
#  else /* !_MSC_VER */
286
287
_GL_STDC_LEADING_ZEROS_INLINE int
288
_gl_stdbit_clzll (unsigned long long int n)
289
{
290
  int r = 0;
291
  for (int i = 8 * sizeof n >> 1; 1 << 6 <= i; i >>= 1)
292
    {
293
      int a = (1ull << i <= n) * i; n >>= a; r += a;
294
    }
295
  int a5 = (0x00000000ffffffff < n) << 5; n >>= a5; r += a5;
296
  int a4 = (0x000000000000ffff < n) << 4; n >>= a4; r += a4;
297
  int a3 = (0x00000000000000ff < n) << 3; n >>= a3; r += a3;
298
  int a2 = (0x000000000000000f < n) << 2; n >>= a2; r += a2;
299
  return (8 * sizeof n - (1 << 2) - r) + ((0x11112234ull >> (n << 2)) & 0xf);
300
}
301
_GL_STDC_LEADING_ZEROS_INLINE int
302
_gl_stdbit_clz (unsigned int n)
303
{
304
  return _gl_stdbit_clzll (n) - 8 * (sizeof 0ull - sizeof 0u);
305
}
306
_GL_STDC_LEADING_ZEROS_INLINE int
307
_gl_stdbit_clzl (unsigned long int n)
308
{
309
  return _gl_stdbit_clzll (n) - 8 * (sizeof 0ull - sizeof 0ul);
310
}
311
#  endif
312
313
#  define GNULIB_defined_clz_functions 1
314
# endif
315
316
# if !GNULIB_defined_stdc_leading_zeros_functions
317
318
_GL_STDC_LEADING_ZEROS_INLINE unsigned int
319
stdc_leading_zeros_ui (unsigned int n)
320
{
321
  return _gl_stdbit_clz (n);
322
}
323
324
_GL_STDC_LEADING_ZEROS_INLINE unsigned int
325
stdc_leading_zeros_uc (unsigned char n)
326
{
327
  return stdc_leading_zeros_ui (n) - 8 * (sizeof 0u - sizeof n);
328
}
329
330
_GL_STDC_LEADING_ZEROS_INLINE unsigned int
331
stdc_leading_zeros_us (unsigned short int n)
332
{
333
  return stdc_leading_zeros_ui (n) - 8 * (sizeof 0u - sizeof n);
334
}
335
336
_GL_STDC_LEADING_ZEROS_INLINE unsigned int
337
stdc_leading_zeros_ul (unsigned long int n)
338
{
339
  return _gl_stdbit_clzl (n);
340
}
341
342
_GL_STDC_LEADING_ZEROS_INLINE unsigned int
343
stdc_leading_zeros_ull (unsigned long long int n)
344
{
345
  return _gl_stdbit_clzll (n);
346
}
347
348
#  define GNULIB_defined_stdc_leading_zeros_functions 1
349
# endif
350
351
# define stdc_leading_zeros(n) \
352
  (sizeof (n) == 1 ? stdc_leading_zeros_uc (n)  \
353
   : sizeof (n) == sizeof (unsigned short int) ? stdc_leading_zeros_us (n) \
354
   : sizeof (n) == sizeof 0u ? stdc_leading_zeros_ui (n) \
355
   : sizeof (n) == sizeof 0ul ? stdc_leading_zeros_ul (n) \
356
   : stdc_leading_zeros_ull (n))
357
358
#endif
359
360
361
/* ISO C 23 § 7.18.4 Count Leading Ones  */
362
363
#if 0
364
365
# if !GNULIB_defined_stdc_leading_ones_functions
366
367
_GL_STDC_LEADING_ONES_INLINE unsigned int
368
stdc_leading_ones_uc (unsigned char n)
369
{
370
  return stdc_leading_zeros_uc (~n);
371
}
372
373
_GL_STDC_LEADING_ONES_INLINE unsigned int
374
stdc_leading_ones_us (unsigned short int n)
375
{
376
  return stdc_leading_zeros_us (~n);
377
}
378
379
_GL_STDC_LEADING_ONES_INLINE unsigned int
380
stdc_leading_ones_ui (unsigned int n)
381
{
382
  return stdc_leading_zeros_ui (~n);
383
}
384
385
_GL_STDC_LEADING_ONES_INLINE unsigned int
386
stdc_leading_ones_ul (unsigned long int n)
387
{
388
  return stdc_leading_zeros_ul (~n);
389
}
390
391
_GL_STDC_LEADING_ONES_INLINE unsigned int
392
stdc_leading_ones_ull (unsigned long long int n)
393
{
394
  return stdc_leading_zeros_ull (~n);
395
}
396
397
#  define GNULIB_defined_stdc_leading_ones_functions 1
398
# endif
399
400
# define stdc_leading_ones(n) \
401
  (sizeof (n) == 1 ? stdc_leading_ones_uc (n) \
402
   : sizeof (n) == sizeof (unsigned short int) ? stdc_leading_ones_us (n) \
403
   : sizeof (n) == sizeof 0u ? stdc_leading_ones_ui (n) \
404
   : sizeof (n) == sizeof 0ul ? stdc_leading_ones_ul (n) \
405
   : stdc_leading_ones_ull (n))
406
407
#endif
408
409
410
/* ISO C 23 § 7.18.5 Count Trailing Zeros  */
411
412
#if 0
413
414
# if 3 < __GNUC__ + (4 <= __GNUC_MINOR__) || 4 <= __clang_major__
415
#  define _GL_STDBIT_HAS_BUILTIN_CTZ true
416
# elif defined __has_builtin
417
#  if (__has_builtin (__builtin_ctz) \
418
       && __has_builtin (__builtin_ctzl) \
419
       && __has_builtin (__builtin_ctzll))
420
#   define _GL_STDBIT_HAS_BUILTIN_CTZ true
421
#  endif
422
# endif
423
424
/* Count trailing 0 bits of N, even if N is 0.  */
425
# if !GNULIB_defined_ctz_functions
426
#  ifdef _GL_STDBIT_HAS_BUILTIN_CTZ
427
_GL_STDC_TRAILING_ZEROS_INLINE int
428
_gl_stdbit_ctz (unsigned int n)
429
{
430
  return n ? __builtin_ctz (n) : 8 * sizeof n;
431
}
432
_GL_STDC_TRAILING_ZEROS_INLINE int
433
_gl_stdbit_ctzl (unsigned long int n)
434
{
435
  return n ? __builtin_ctzl (n) : 8 * sizeof n;
436
}
437
_GL_STDC_TRAILING_ZEROS_INLINE int
438
_gl_stdbit_ctzll (unsigned long long int n)
439
{
440
  return n ? __builtin_ctzll (n) : 8 * sizeof n;
441
}
442
#  elif defined _MSC_VER
443
444
/* Declare the few MSVC intrinsics that we need.  We prefer not to include
445
   <intrin.h> because it would pollute the namespace.  */
446
extern unsigned char _BitScanForward (unsigned long *, unsigned long);
447
#   pragma intrinsic (_BitScanForward)
448
#   ifdef _M_X64
449
extern unsigned char _BitScanForward64 (unsigned long *, unsigned long long);
450
#    pragma intrinsic (_BitScanForward64)
451
#   endif
452
453
_GL_STDC_TRAILING_ZEROS_INLINE int
454
_gl_stdbit_ctzl (unsigned long int n)
455
{
456
  unsigned long int r;
457
  return _BitScanForward (&r, n) ? r : 8 * sizeof n;
458
}
459
_GL_STDC_TRAILING_ZEROS_INLINE int
460
_gl_stdbit_ctz (unsigned int n)
461
{
462
  return _gl_stdbit_ctzl (n | (1ul << (8 * sizeof n - 1) << 1));
463
}
464
_GL_STDC_TRAILING_ZEROS_INLINE int
465
_gl_stdbit_ctzll (unsigned long long int n)
466
{
467
#   ifdef _M_X64
468
  unsigned long int r;
469
  return _BitScanForward64 (&r, n) ? r : 8 * sizeof n;
470
#   else
471
  unsigned int lo = n;
472
  return _gl_stdbit_ctzl (lo ? lo : n >> 32) + (lo ? 0 : 32);
473
#   endif
474
}
475
476
#  else /* !_MSC_VER */
477
478
_GL_STDC_TRAILING_ZEROS_INLINE int
479
_gl_stdbit_ctz (unsigned int n)
480
{
481
  return 8 * sizeof n - (n ? _gl_stdbit_clz (n & -n) + 1 : 0);
482
}
483
_GL_STDC_TRAILING_ZEROS_INLINE int
484
_gl_stdbit_ctzl (unsigned long int n)
485
{
486
  return 8 * sizeof n - (n ? _gl_stdbit_clzl (n & -n) + 1 : 0);
487
}
488
_GL_STDC_TRAILING_ZEROS_INLINE int
489
_gl_stdbit_ctzll (unsigned long long int n)
490
{
491
  return 8 * sizeof n - (n ? _gl_stdbit_clzll (n & -n) + 1 : 0);
492
}
493
#  endif
494
495
#  define GNULIB_defined_ctz_functions 1
496
# endif
497
498
# if !GNULIB_defined_stdc_trailing_zeros_functions
499
500
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int
501
stdc_trailing_zeros_ui (unsigned int n)
502
{
503
  return _gl_stdbit_ctz (n);
504
}
505
506
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int
507
stdc_trailing_zeros_uc (unsigned char n)
508
{
509
  return stdc_trailing_zeros_ui (n | (1 + (unsigned char) -1));
510
}
511
512
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int
513
stdc_trailing_zeros_us (unsigned short int n)
514
{
515
  return stdc_trailing_zeros_ui (n | (1 + (unsigned short int) -1));
516
}
517
518
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int
519
stdc_trailing_zeros_ul (unsigned long int n)
520
{
521
  return _gl_stdbit_ctzl (n);
522
}
523
524
_GL_STDC_TRAILING_ZEROS_INLINE unsigned int
525
stdc_trailing_zeros_ull (unsigned long long int n)
526
{
527
  return _gl_stdbit_ctzll (n);
528
}
529
530
#  define GNULIB_defined_stdc_trailing_zeros_functions 1
531
# endif
532
533
# define stdc_trailing_zeros(n) \
534
  (sizeof (n) == 1 ? stdc_trailing_zeros_uc (n) \
535
   : sizeof (n) == sizeof (unsigned short int) ? stdc_trailing_zeros_us (n) \
536
   : sizeof (n) == sizeof 0u ? stdc_trailing_zeros_ui (n) \
537
   : sizeof (n) == sizeof 0ul ? stdc_trailing_zeros_ul (n) \
538
   : stdc_trailing_zeros_ull (n))
539
540
#endif
541
542
543
/* ISO C 23 § 7.18.6 Count Trailing Ones  */
544
545
#if 0
546
547
# if !GNULIB_defined_stdc_trailing_ones_functions
548
549
_GL_STDC_TRAILING_ONES_INLINE unsigned int
550
stdc_trailing_ones_uc (unsigned char n)
551
{
552
  return stdc_trailing_zeros_uc (~n);
553
}
554
555
_GL_STDC_TRAILING_ONES_INLINE unsigned int
556
stdc_trailing_ones_us (unsigned short int n)
557
{
558
  return stdc_trailing_zeros_us (~n);
559
}
560
561
_GL_STDC_TRAILING_ONES_INLINE unsigned int
562
stdc_trailing_ones_ui (unsigned int n)
563
{
564
  return stdc_trailing_zeros_ui (~n);
565
}
566
567
_GL_STDC_TRAILING_ONES_INLINE unsigned int
568
stdc_trailing_ones_ul (unsigned long int n)
569
{
570
  return stdc_trailing_zeros_ul (~n);
571
}
572
573
_GL_STDC_TRAILING_ONES_INLINE unsigned int
574
stdc_trailing_ones_ull (unsigned long long int n)
575
{
576
  return stdc_trailing_zeros_ull (~n);
577
}
578
579
#  define GNULIB_defined_stdc_trailing_ones_functions 1
580
# endif
581
582
# define stdc_trailing_ones(n) \
583
  (sizeof (n) == 1 ? stdc_trailing_ones_uc (n)  \
584
   : sizeof (n) == sizeof (unsigned short int) ? stdc_trailing_ones_us (n) \
585
   : sizeof (n) == sizeof 0u ? stdc_trailing_ones_ui (n) \
586
   : sizeof (n) == sizeof 0ul ? stdc_trailing_ones_ul (n) \
587
   : stdc_trailing_ones_ull (n))
588
589
#endif
590
591
592
/* ISO C 23 § 7.18.7 First Leading Zero  */
593
594
#if 0
595
596
# if !GNULIB_defined_stdc_first_leading_zero_functions
597
598
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int
599
stdc_first_leading_zero_uc (unsigned char n)
600
{
601
  unsigned int count = stdc_leading_ones_uc (n);
602
  unsigned int bits = 8 * sizeof n;
603
  return count % bits + (count < bits);
604
}
605
606
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int
607
stdc_first_leading_zero_us (unsigned short int n)
608
{
609
  unsigned int count = stdc_leading_ones_us (n);
610
  unsigned int bits = 8 * sizeof n;
611
  return count % bits + (count < bits);
612
}
613
614
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int
615
stdc_first_leading_zero_ui (unsigned int n)
616
{
617
  unsigned int count = stdc_leading_ones_ui (n);
618
  unsigned int bits = 8 * sizeof n;
619
  return count % bits + (count < bits);
620
}
621
622
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int
623
stdc_first_leading_zero_ul (unsigned long int n)
624
{
625
  unsigned int count = stdc_leading_ones_ul (n);
626
  unsigned int bits = 8 * sizeof n;
627
  return count % bits + (count < bits);
628
}
629
630
_GL_STDC_FIRST_LEADING_ZERO_INLINE unsigned int
631
stdc_first_leading_zero_ull (unsigned long long int n)
632
{
633
  unsigned int count = stdc_leading_ones_ull (n);
634
  unsigned int bits = 8 * sizeof n;
635
  return count % bits + (count < bits);
636
}
637
638
#  define GNULIB_defined_stdc_first_leading_zero_functions 1
639
# endif
640
641
# define stdc_first_leading_zero(n) \
642
  (sizeof (n) == 1 ? stdc_first_leading_zero_uc (n) \
643
   : sizeof (n) == sizeof (unsigned short) ? stdc_first_leading_zero_us (n) \
644
   : sizeof (n) == sizeof 0u ? stdc_first_leading_zero_ui (n) \
645
   : sizeof (n) == sizeof 0ul ? stdc_first_leading_zero_ul (n) \
646
   : stdc_first_leading_zero_ull (n))
647
648
#endif
649
650
651
/* ISO C 23 § 7.18.8 First Leading One  */
652
653
#if 0
654
655
# if !GNULIB_defined_stdc_first_leading_one_functions
656
657
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int
658
stdc_first_leading_one_uc (unsigned char n)
659
{
660
  unsigned int count = stdc_leading_zeros_uc (n);
661
  unsigned int bits = 8 * sizeof n;
662
  return count % bits + (count < bits);
663
}
664
665
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int
666
stdc_first_leading_one_us (unsigned short int n)
667
{
668
  unsigned int count = stdc_leading_zeros_us (n);
669
  unsigned int bits = 8 * sizeof n;
670
  return count % bits + (count < bits);
671
}
672
673
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int
674
stdc_first_leading_one_ui (unsigned int n)
675
{
676
  unsigned int count = stdc_leading_zeros_ui (n);
677
  unsigned int bits = 8 * sizeof n;
678
  return count % bits + (count < bits);
679
}
680
681
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int
682
stdc_first_leading_one_ul (unsigned long int n)
683
{
684
  unsigned int count = stdc_leading_zeros_ul (n);
685
  unsigned int bits = 8 * sizeof n;
686
  return count % bits + (count < bits);
687
}
688
689
_GL_STDC_FIRST_LEADING_ONE_INLINE unsigned int
690
stdc_first_leading_one_ull (unsigned long long int n)
691
{
692
  unsigned int count = stdc_leading_zeros_ull (n);
693
  unsigned int bits = 8 * sizeof n;
694
  return count % bits + (count < bits);
695
}
696
697
#  define GNULIB_defined_stdc_first_leading_one_functions 1
698
# endif
699
700
# define stdc_first_leading_one(n) \
701
  (sizeof (n) == 1 ? stdc_first_leading_one_uc (n) \
702
   : sizeof (n) == sizeof (unsigned short) ? stdc_first_leading_one_us (n) \
703
   : sizeof (n) == sizeof 0u ? stdc_first_leading_one_ui (n) \
704
   : sizeof (n) == sizeof 0ul ? stdc_first_leading_one_ul (n) \
705
   : stdc_first_leading_one_ull (n))
706
707
#endif
708
709
710
/* ISO C 23 § 7.18.9 First Trailing Zero  */
711
712
#if 0
713
714
# if !GNULIB_defined_stdc_first_trailing_zero_functions
715
716
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int
717
stdc_first_trailing_zero_uc (unsigned char n)
718
{
719
  unsigned int count = stdc_trailing_ones_uc (n);
720
  unsigned int bits = 8 * sizeof n;
721
  return count % bits + (count < bits);
722
}
723
724
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int
725
stdc_first_trailing_zero_us (unsigned short int n)
726
{
727
  unsigned int count = stdc_trailing_ones_us (n);
728
  unsigned int bits = 8 * sizeof n;
729
  return count % bits + (count < bits);
730
}
731
732
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int
733
stdc_first_trailing_zero_ui (unsigned int n)
734
{
735
  unsigned int count = stdc_trailing_ones_ui (n);
736
  unsigned int bits = 8 * sizeof n;
737
  return count % bits + (count < bits);
738
}
739
740
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int
741
stdc_first_trailing_zero_ul (unsigned long int n)
742
{
743
  unsigned int count = stdc_trailing_ones_ul (n);
744
  unsigned int bits = 8 * sizeof n;
745
  return count % bits + (count < bits);
746
}
747
748
_GL_STDC_FIRST_TRAILING_ZERO_INLINE unsigned int
749
stdc_first_trailing_zero_ull (unsigned long long int n)
750
{
751
  unsigned int count = stdc_trailing_ones_ull (n);
752
  unsigned int bits = 8 * sizeof n;
753
  return count % bits + (count < bits);
754
}
755
756
#  define GNULIB_defined_stdc_first_trailing_zero_functions 1
757
# endif
758
759
# define stdc_first_trailing_zero(n) \
760
  (sizeof (n) == 1 ? stdc_first_trailing_zero_uc (n) \
761
   : sizeof (n) == sizeof (unsigned short) ? stdc_first_trailing_zero_us (n) \
762
   : sizeof (n) == sizeof 0u ? stdc_first_trailing_zero_ui (n) \
763
   : sizeof (n) == sizeof 0ul ? stdc_first_trailing_zero_ul (n) \
764
   : stdc_first_trailing_zero_ull (n))
765
766
#endif
767
768
769
/* ISO C 23 § 7.18.10 First Trailing One  */
770
771
#if 0
772
773
# if !GNULIB_defined_stdc_first_trailing_one_functions
774
775
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int
776
stdc_first_trailing_one_uc (unsigned char n)
777
{
778
  unsigned int count = stdc_trailing_zeros_uc (n);
779
  unsigned int bits = 8 * sizeof n;
780
  return count % bits + (count < bits);
781
}
782
783
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int
784
stdc_first_trailing_one_us (unsigned short int n)
785
{
786
  unsigned int count = stdc_trailing_zeros_us (n);
787
  unsigned int bits = 8 * sizeof n;
788
  return count % bits + (count < bits);
789
}
790
791
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int
792
stdc_first_trailing_one_ui (unsigned int n)
793
{
794
  unsigned int count = stdc_trailing_zeros_ui (n);
795
  unsigned int bits = 8 * sizeof n;
796
  return count % bits + (count < bits);
797
}
798
799
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int
800
stdc_first_trailing_one_ul (unsigned long int n)
801
{
802
  unsigned int count = stdc_trailing_zeros_ul (n);
803
  unsigned int bits = 8 * sizeof n;
804
  return count % bits + (count < bits);
805
}
806
807
_GL_STDC_FIRST_TRAILING_ONE_INLINE unsigned int
808
stdc_first_trailing_one_ull (unsigned long long int n)
809
{
810
  unsigned int count = stdc_trailing_zeros_ull (n);
811
  unsigned int bits = 8 * sizeof n;
812
  return count % bits + (count < bits);
813
}
814
815
#  define GNULIB_defined_stdc_first_trailing_one_functions 1
816
# endif
817
818
# define stdc_first_trailing_one(n) \
819
  (sizeof (n) == 1 ? stdc_first_trailing_one_uc (n) \
820
   : sizeof (n) == sizeof (unsigned short) ? stdc_first_trailing_one_us (n) \
821
   : sizeof (n) == sizeof 0u ? stdc_first_trailing_one_ui (n) \
822
   : sizeof (n) == sizeof 0ul ? stdc_first_trailing_one_ul (n) \
823
   : stdc_first_trailing_one_ull (n))
824
825
#endif
826
827
828
/* ISO C 23 § 7.18.12 Count Ones  */
829
830
#if 0
831
832
# if 3 < __GNUC__ + (4 <= __GNUC_MINOR__) || 4 <= __clang_major__
833
#  define _GL_STDBIT_HAS_BUILTIN_POPCOUNT true
834
# elif defined __has_builtin
835
#  if (__has_builtin (__builtin_popcount) \
836
       && __has_builtin (__builtin_popcountl) \
837
       && __has_builtin (__builtin_popcountll))
838
#   define _GL_STDBIT_HAS_BUILTIN_POPCOUNT true
839
#  endif
840
# endif
841
842
/* Count 1 bits in N.  */
843
# if !GNULIB_defined_popcount_functions
844
#  ifdef _GL_STDBIT_HAS_BUILTIN_POPCOUNT
845
#   define _gl_stdbit_popcount __builtin_popcount
846
#   define _gl_stdbit_popcountl __builtin_popcountl
847
#   define _gl_stdbit_popcountll __builtin_popcountll
848
#  else
849
_GL_STDC_COUNT_ONES_INLINE int
850
_gl_stdbit_popcount_wide (unsigned long long int n)
851
{
852
  if (sizeof n & (sizeof n - 1))
853
    {
854
      /* Use a simple O(log N) loop on theoretical platforms where N's
855
         width is not a power of 2.  */
856
      int count = 0;
857
      for (int i = 0; i < 8 * sizeof n; i++, n >>= 1)
858
        count += n & 1;
859
      return count;
860
    }
861
  else
862
    {
863
      /* N's width is a power of 2; count in parallel.  */
864
      unsigned long long int
865
        max = -1ull,
866
        x555555 = max / (1 << 1 | 1), /* 0x555555... */
867
        x333333 = max / (1 << 2 | 1), /* 0x333333... */
868
        x0f0f0f = max / (1 << 4 | 1), /* 0x0f0f0f... */
869
        x010101 = max / ((1 << 8) - 1), /* 0x010101... */
870
        x000_7f = max / 0xffffffffffffffffLL * 0x7f; /* 0x000000000000007f... */
871
      n -= (n >> 1) & x555555;
872
      n = (n & x333333) + ((n >> 2) & x333333);
873
      n = (n + (n >> 4)) & x0f0f0f;
874
875
      /* If the popcount always fits in 8 bits, multiply so that the
876
         popcount is in the leading 8 bits of the product; these days
877
         this is typically faster than the alternative below.  */
878
      if (8 * sizeof n < 1 << 8)
879
        return n * x010101 >> 8 * (sizeof n - 1);
880
881
      /* N is at least 256 bits wide!  Fall back on an O(log log N)
882
         loop that a compiler could unroll.  Unroll the first three
883
         iterations by hand, to skip some division and masking.  This
884
         is the most we can easily do without hassling with constants
885
         that a typical-platform compiler would reject.  */
886
      n += n >> (1 << 3);
887
      n += n >> (1 << 4);
888
      n += n >> (1 << 5);
889
      n &= x000_7f;
890
      for (int i = 64; i < 8 * sizeof n; i <<= 1)
891
        n = (n + (n >> i)) & max / (1ull << i | 1);
892
      return n;
893
    }
894
}
895
896
#   ifdef _MSC_VER
897
#    if 1500 <= _MSC_VER && (defined _M_IX86 || defined _M_X64)
898
/* Declare the few MSVC intrinsics that we need.  We prefer not to include
899
   <intrin.h> because it would pollute the namespace.  */
900
extern void __cpuid (int[4], int);
901
#     pragma intrinsic (__cpuid)
902
extern unsigned int __popcnt (unsigned int);
903
#     pragma intrinsic (__popcnt)
904
#     ifdef _M_X64
905
extern unsigned long long __popcnt64 (unsigned long long);
906
#      pragma intrinsic (__popcnt64)
907
#     else
908
_GL_STDC_COUNT_ONES_INLINE int
909
__popcnt64 (unsigned long long int n)
910
{
911
  return __popcnt (n >> 32) + __popcnt (n);
912
}
913
#     endif
914
#    endif
915
916
/* 1 if supported, -1 if not, 0 if unknown.  */
917
extern signed char _gl_stdbit_popcount_support;
918
919
_GL_STDC_COUNT_ONES_INLINE bool
920
_gl_stdbit_popcount_supported (void)
921
{
922
  if (!_gl_stdbit_popcount_support)
923
    {
924
      /* Do as described in
925
         <https://learn.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64>
926
         Although Microsoft started requiring POPCNT in MS-Windows 11 24H2,
927
         we'll be more cautious.  */
928
      int cpu_info[4];
929
      __cpuid (cpu_info, 1);
930
      _gl_stdbit_popcount_support = cpu_info[2] & 1 << 23 ? 1 : -1;
931
    }
932
  return 0 < _gl_stdbit_popcount_support;
933
}
934
_GL_STDC_COUNT_ONES_INLINE int
935
_gl_stdbit_popcount (unsigned int n)
936
{
937
  return (_gl_stdbit_popcount_supported ()
938
          ? __popcnt (n)
939
          : _gl_stdbit_popcount_wide (n));
940
}
941
_GL_STDC_COUNT_ONES_INLINE int
942
_gl_stdbit_popcountl (unsigned long int n)
943
{
944
  return (_gl_stdbit_popcount_supported ()
945
          ? __popcnt (n)
946
          : _gl_stdbit_popcount_wide (n));
947
}
948
_GL_STDC_COUNT_ONES_INLINE int
949
_gl_stdbit_popcountll (unsigned long long int n)
950
{
951
  return (_gl_stdbit_popcount_supported ()
952
          ? __popcnt64 (n)
953
          : _gl_stdbit_popcount_wide (n));
954
}
955
#   else /* !_MSC_VER */
956
#    define _gl_stdbit_popcount _gl_stdbit_popcount_wide
957
#    define _gl_stdbit_popcountl _gl_stdbit_popcount_wide
958
#    define _gl_stdbit_popcountll _gl_stdbit_popcount_wide
959
#   endif
960
#  endif
961
#  define GNULIB_defined_popcount_functions 1
962
# endif
963
964
# if !GNULIB_defined_stdc_count_ones_functions
965
966
_GL_STDC_COUNT_ONES_INLINE unsigned int
967
stdc_count_ones_ui (unsigned int n)
968
{
969
  return _gl_stdbit_popcount (n);
970
}
971
972
_GL_STDC_COUNT_ONES_INLINE unsigned int
973
stdc_count_ones_uc (unsigned char n)
974
{
975
  return stdc_count_ones_ui (n);
976
}
977
978
_GL_STDC_COUNT_ONES_INLINE unsigned int
979
stdc_count_ones_us (unsigned short int n)
980
{
981
  return stdc_count_ones_ui (n);
982
}
983
984
_GL_STDC_COUNT_ONES_INLINE unsigned int
985
stdc_count_ones_ul (unsigned long int n)
986
{
987
  return _gl_stdbit_popcountl (n);
988
}
989
990
_GL_STDC_COUNT_ONES_INLINE unsigned int
991
stdc_count_ones_ull (unsigned long long int n)
992
{
993
  return _gl_stdbit_popcountll (n);
994
}
995
996
#  define GNULIB_defined_stdc_count_ones_functions 1
997
# endif
998
999
# define stdc_count_ones(n) \
1000
  (sizeof (n) == 1 ? stdc_count_ones_uc (n) \
1001
   : sizeof (n) == sizeof (unsigned short int) ? stdc_count_ones_us (n) \
1002
   : sizeof (n) == sizeof 0u ? stdc_count_ones_ui (n) \
1003
   : sizeof (n) == sizeof 0ul ? stdc_count_ones_ul (n) \
1004
   : stdc_count_ones_ull (n))
1005
1006
#endif
1007
1008
1009
/* ISO C 23 § 7.18.11 Count Zeros  */
1010
1011
#if 0
1012
1013
# if !GNULIB_defined_stdc_count_zeros_functions
1014
1015
_GL_STDC_COUNT_ZEROS_INLINE unsigned int
1016
stdc_count_zeros_uc (unsigned char n)
1017
{
1018
  return stdc_count_ones_uc (~n);
1019
}
1020
1021
_GL_STDC_COUNT_ZEROS_INLINE unsigned int
1022
stdc_count_zeros_us (unsigned short int n)
1023
{
1024
  return stdc_count_ones_us (~n);
1025
}
1026
1027
_GL_STDC_COUNT_ZEROS_INLINE unsigned int
1028
stdc_count_zeros_ui (unsigned int n)
1029
{
1030
  return stdc_count_ones_ui (~n);
1031
}
1032
1033
_GL_STDC_COUNT_ZEROS_INLINE unsigned int
1034
stdc_count_zeros_ul (unsigned long int n)
1035
{
1036
  return stdc_count_ones_ul (~n);
1037
}
1038
1039
_GL_STDC_COUNT_ZEROS_INLINE unsigned int
1040
stdc_count_zeros_ull (unsigned long long int n)
1041
{
1042
  return stdc_count_ones_ull (~n);
1043
}
1044
1045
#  define GNULIB_defined_stdc_count_zeros_functions 1
1046
# endif
1047
1048
# define stdc_count_zeros(n) \
1049
  (sizeof (n) == 1 ? stdc_count_zeros_uc (n) \
1050
   : sizeof (n) == sizeof (unsigned short int) ? stdc_count_zeros_us (n) \
1051
   : sizeof (n) == sizeof 0u ? stdc_count_zeros_ui (n) \
1052
   : sizeof (n) == sizeof 0ul ? stdc_count_zeros_ul (n) \
1053
   : stdc_count_zeros_ull (n))
1054
1055
#endif
1056
1057
1058
/* ISO C 23 § 7.18.13 Single-bit Check  */
1059
1060
#if 0
1061
1062
# if !GNULIB_defined_stdc_has_single_bit_functions
1063
1064
_GL_STDC_HAS_SINGLE_BIT_INLINE bool
1065
stdc_has_single_bit_uc (unsigned char n)
1066
{
1067
  unsigned char n_1 = n - 1, nx = n_1 ^ n;
1068
  return n_1 < nx;
1069
}
1070
1071
_GL_STDC_HAS_SINGLE_BIT_INLINE bool
1072
stdc_has_single_bit_us (unsigned short int n)
1073
{
1074
  unsigned short int n_1 = n - 1, nx = n_1 ^ n;
1075
  return n_1 < nx;
1076
}
1077
1078
_GL_STDC_HAS_SINGLE_BIT_INLINE bool
1079
stdc_has_single_bit_ui (unsigned int n)
1080
{
1081
  unsigned int n_1 = n - 1, nx = n_1 ^ n;
1082
  return n_1 < nx;
1083
}
1084
1085
_GL_STDC_HAS_SINGLE_BIT_INLINE bool
1086
stdc_has_single_bit_ul (unsigned long int n)
1087
{
1088
  unsigned long int n_1 = n - 1, nx = n_1 ^ n;
1089
  return n_1 < nx;
1090
}
1091
1092
_GL_STDC_HAS_SINGLE_BIT_INLINE bool
1093
stdc_has_single_bit_ull (unsigned long long int n)
1094
{
1095
  unsigned long long int n_1 = n - 1, nx = n_1 ^ n;
1096
  return n_1 < nx;
1097
}
1098
1099
#  define GNULIB_defined_stdc_has_single_bit_functions 1
1100
# endif
1101
1102
# define stdc_has_single_bit(n) \
1103
  ((bool) \
1104
   (sizeof (n) == 1 ? stdc_has_single_bit_uc (n) \
1105
    : sizeof (n) == sizeof (unsigned short int) ? stdc_has_single_bit_us (n) \
1106
    : sizeof (n) == sizeof 0u ? stdc_has_single_bit_ui (n) \
1107
    : sizeof (n) == sizeof 0ul ? stdc_has_single_bit_ul (n) \
1108
    : stdc_has_single_bit_ull (n)))
1109
1110
#endif
1111
1112
1113
/* ISO C 23 § 7.18.14 Bit Width  */
1114
1115
#if 0
1116
1117
# if !GNULIB_defined_stdc_bit_width_functions
1118
1119
_GL_STDC_BIT_WIDTH_INLINE unsigned int
1120
stdc_bit_width_uc (unsigned char n)
1121
{
1122
  return 8 * sizeof n - stdc_leading_zeros_uc (n);
1123
}
1124
1125
_GL_STDC_BIT_WIDTH_INLINE unsigned int
1126
stdc_bit_width_us (unsigned short int n)
1127
{
1128
  return 8 * sizeof n - stdc_leading_zeros_us (n);
1129
}
1130
1131
_GL_STDC_BIT_WIDTH_INLINE unsigned int
1132
stdc_bit_width_ui (unsigned int n)
1133
{
1134
  return 8 * sizeof n - stdc_leading_zeros_ui (n);
1135
}
1136
1137
_GL_STDC_BIT_WIDTH_INLINE unsigned int
1138
stdc_bit_width_ul (unsigned long int n)
1139
{
1140
  return 8 * sizeof n - stdc_leading_zeros_ul (n);
1141
}
1142
1143
_GL_STDC_BIT_WIDTH_INLINE unsigned int
1144
stdc_bit_width_ull (unsigned long long int n)
1145
{
1146
  return 8 * sizeof n - stdc_leading_zeros_ull (n);
1147
}
1148
1149
#  define GNULIB_defined_stdc_bit_width_functions 1
1150
# endif
1151
1152
# define stdc_bit_width(n) \
1153
  (sizeof (n) == 1 ? stdc_bit_width_uc (n) \
1154
   : sizeof (n) == sizeof (unsigned short int) ? stdc_bit_width_us (n) \
1155
   : sizeof (n) == sizeof 0u ? stdc_bit_width_ui (n) \
1156
   : sizeof (n) == sizeof 0ul ? stdc_bit_width_ul (n) \
1157
   : stdc_bit_width_ull (n))
1158
1159
#endif
1160
1161
#endif /* !__STDC_VERSION_STDBIT_H__ */
1162
1163
1164
/* ISO C 23 § 7.18.15 Bit Floor  */
1165
1166
#if 0
1167
1168
# if !defined __STDC_VERSION_STDBIT_H__
1169
#  if !GNULIB_defined_stdc_bit_floor_functions
1170
1171
_GL_STDC_BIT_FLOOR_INLINE unsigned char
1172
stdc_bit_floor_uc (unsigned char n)
1173
{
1174
  return n ? 1u << (stdc_bit_width_uc (n) - 1) : 0;
1175
}
1176
1177
_GL_STDC_BIT_FLOOR_INLINE unsigned short int
1178
stdc_bit_floor_us (unsigned short int n)
1179
{
1180
  return n ? 1u << (stdc_bit_width_us (n) - 1) : 0;
1181
}
1182
1183
_GL_STDC_BIT_FLOOR_INLINE unsigned int
1184
stdc_bit_floor_ui (unsigned int n)
1185
{
1186
  return n ? 1u << (stdc_bit_width_ui (n) - 1) : 0;
1187
}
1188
1189
_GL_STDC_BIT_FLOOR_INLINE unsigned long int
1190
stdc_bit_floor_ul (unsigned long int n)
1191
{
1192
  return n ? 1ul << (stdc_bit_width_ul (n) - 1) : 0;
1193
}
1194
1195
_GL_STDC_BIT_FLOOR_INLINE unsigned long long int
1196
stdc_bit_floor_ull (unsigned long long int n)
1197
{
1198
  return n ? 1ull << (stdc_bit_width_ull (n) - 1) : 0;
1199
}
1200
1201
#   define GNULIB_defined_stdc_bit_floor_functions 1
1202
#  endif
1203
# endif
1204
1205
# if !defined __STDC_VERSION_STDBIT_H__ \
1206
     || (defined __sun && defined _SYS_STDBIT_H)
1207
#  undef stdc_bit_floor
1208
#  define stdc_bit_floor(n) \
1209
     (_GL_STDBIT_TYPEOF_CAST \
1210
      (n, \
1211
       (sizeof (n) == 1 ? stdc_bit_floor_uc (n) \
1212
        : sizeof (n) == sizeof (unsigned short int) ? stdc_bit_floor_us (n) \
1213
        : sizeof (n) == sizeof 0u ? stdc_bit_floor_ui (n) \
1214
        : sizeof (n) == sizeof 0ul ? stdc_bit_floor_ul (n) \
1215
        : stdc_bit_floor_ull (n))))
1216
# endif
1217
1218
#endif
1219
1220
1221
/* ISO C 23 § 7.18.16 Bit Ceiling  */
1222
1223
#if 0
1224
1225
# if !defined __STDC_VERSION_STDBIT_H__
1226
#  if !GNULIB_defined_stdc_bit_ceil_functions
1227
1228
_GL_STDC_BIT_CEIL_INLINE unsigned char
1229
stdc_bit_ceil_uc (unsigned char n)
1230
{
1231
  return n <= 1 ? 1 : 2u << (stdc_bit_width_uc (n - 1) - 1);
1232
}
1233
1234
_GL_STDC_BIT_CEIL_INLINE unsigned short int
1235
stdc_bit_ceil_us (unsigned short int n)
1236
{
1237
  return n <= 1 ? 1 : 2u << (stdc_bit_width_us (n - 1) - 1);
1238
}
1239
1240
_GL_STDC_BIT_CEIL_INLINE unsigned int
1241
stdc_bit_ceil_ui (unsigned int n)
1242
{
1243
  return n <= 1 ? 1 : 2u << (stdc_bit_width_ui (n - 1) - 1);
1244
}
1245
1246
_GL_STDC_BIT_CEIL_INLINE unsigned long int
1247
stdc_bit_ceil_ul (unsigned long int n)
1248
{
1249
  return n <= 1 ? 1 : 2ul << (stdc_bit_width_ul (n - 1) - 1);
1250
}
1251
1252
_GL_STDC_BIT_CEIL_INLINE unsigned long long int
1253
stdc_bit_ceil_ull (unsigned long long int n)
1254
{
1255
  return n <= 1 ? 1 : 2ull << (stdc_bit_width_ull (n - 1) - 1);
1256
}
1257
1258
#   define GNULIB_defined_stdc_bit_ceil_functions 1
1259
#  endif
1260
# endif
1261
1262
# if !defined __STDC_VERSION_STDBIT_H__ \
1263
     || (defined __sun && defined _SYS_STDBIT_H)
1264
#  undef stdc_bit_ceil
1265
#  define stdc_bit_ceil(n) \
1266
     (_GL_STDBIT_TYPEOF_CAST \
1267
      (n, \
1268
       (sizeof (n) == 1 ? stdc_bit_ceil_uc (n) \
1269
        : sizeof (n) == sizeof (unsigned short int) ? stdc_bit_ceil_us (n) \
1270
        : sizeof (n) == sizeof 0u ? stdc_bit_ceil_ui (n) \
1271
        : sizeof (n) == sizeof 0ul ? stdc_bit_ceil_ul (n) \
1272
        : stdc_bit_ceil_ull (n))))
1273
# endif
1274
1275
#endif
1276
1277
1278
/* ISO C2y § 7.18.17 Rotate Left  */
1279
1280
#if 1
1281
1282
# ifdef __has_builtin
1283
#  if __has_builtin (__builtin_stdc_rotate_left)
1284
#   define _gl_stdc_rotate_left __builtin_stdc_rotate_left
1285
#   define stdc_rotate_left __builtin_stdc_rotate_left
1286
#  endif
1287
# endif
1288
1289
# ifndef _gl_stdc_rotate_left
1290
#  define _gl_stdc_rotate_left(v, c)                \
1291
0
  (((v) << ((c) & (sizeof (v) * 8 - 1)))            \
1292
0
   | ((v) >> (-(c) & (sizeof (v) * 8 - 1))))
1293
# endif
1294
1295
# if !GNULIB_defined_stdc_rotate_left_functions
1296
1297
_GL_STDC_ROTATE_LEFT_INLINE unsigned char
1298
stdc_rotate_left_uc (unsigned char v, unsigned int c)
1299
0
{
1300
0
  return _gl_stdc_rotate_left (v, c);
1301
0
}
1302
1303
_GL_STDC_ROTATE_LEFT_INLINE unsigned short int
1304
stdc_rotate_left_us (unsigned short int v, unsigned int c)
1305
0
{
1306
0
  return _gl_stdc_rotate_left (v, c);
1307
0
}
1308
1309
_GL_STDC_ROTATE_LEFT_INLINE unsigned int
1310
stdc_rotate_left_ui (unsigned int v, unsigned int c)
1311
0
{
1312
0
  return _gl_stdc_rotate_left (v, c);
1313
0
}
1314
1315
_GL_STDC_ROTATE_LEFT_INLINE unsigned long int
1316
stdc_rotate_left_ul (unsigned long int v, unsigned int c)
1317
0
{
1318
0
  return _gl_stdc_rotate_left (v, c);
1319
0
}
1320
1321
_GL_STDC_ROTATE_LEFT_INLINE unsigned long long int
1322
stdc_rotate_left_ull (unsigned long long int v, unsigned int c)
1323
0
{
1324
0
  return _gl_stdc_rotate_left (v, c);
1325
0
}
1326
1327
#  define GNULIB_defined_stdc_rotate_left_functions 1
1328
# endif
1329
1330
# ifndef stdc_rotate_left
1331
#  define stdc_rotate_left(v, c)                                        \
1332
0
  (_GL_STDBIT_TYPEOF_CAST                                               \
1333
0
   (v,                                                                  \
1334
0
    (sizeof (v) == 1 ? stdc_rotate_left_uc (v, c)                       \
1335
0
     : sizeof (v) == sizeof (unsigned short int) ? stdc_rotate_left_us (v, c) \
1336
0
     : sizeof (v) == sizeof 0u ? stdc_rotate_left_ui (v, c)             \
1337
0
     : sizeof (v) == sizeof 0ul ? stdc_rotate_left_ul (v, c)            \
1338
0
     : stdc_rotate_left_ull (v, c))))
1339
# endif
1340
1341
#endif
1342
1343
1344
/* ISO C2y § 7.18.18 Rotate Right  */
1345
1346
#if 0
1347
1348
# ifdef __has_builtin
1349
#  if __has_builtin (__builtin_stdc_rotate_right)
1350
#   define _gl_stdc_rotate_right __builtin_stdc_rotate_right
1351
#   define stdc_rotate_right __builtin_stdc_rotate_right
1352
#  endif
1353
# endif
1354
1355
# ifndef _gl_stdc_rotate_right
1356
#  define _gl_stdc_rotate_right(v, c)               \
1357
  (((v) >> ((c) & (sizeof (v) * 8 - 1)))            \
1358
   | ((v) << (-(c) & (sizeof (v) * 8 - 1))))
1359
# endif
1360
1361
# if !GNULIB_defined_stdc_rotate_right_functions
1362
1363
_GL_STDC_ROTATE_RIGHT_INLINE unsigned char
1364
stdc_rotate_right_uc (unsigned char v, unsigned int c)
1365
{
1366
  return _gl_stdc_rotate_right (v, c);
1367
}
1368
1369
_GL_STDC_ROTATE_RIGHT_INLINE unsigned short int
1370
stdc_rotate_right_us (unsigned short int v, unsigned int c)
1371
{
1372
  return _gl_stdc_rotate_right (v, c);
1373
}
1374
1375
_GL_STDC_ROTATE_RIGHT_INLINE unsigned int
1376
stdc_rotate_right_ui (unsigned int v, unsigned int c)
1377
{
1378
  return _gl_stdc_rotate_right (v, c);
1379
}
1380
1381
_GL_STDC_ROTATE_RIGHT_INLINE unsigned long int
1382
stdc_rotate_right_ul (unsigned long int v, unsigned int c)
1383
{
1384
  return _gl_stdc_rotate_right (v, c);
1385
}
1386
1387
_GL_STDC_ROTATE_RIGHT_INLINE unsigned long long int
1388
stdc_rotate_right_ull (unsigned long long int v, unsigned int c)
1389
{
1390
  return _gl_stdc_rotate_right (v, c);
1391
}
1392
1393
#  define GNULIB_defined_stdc_rotate_right_functions 1
1394
# endif
1395
1396
# ifndef stdc_rotate_right
1397
#  define stdc_rotate_right(v, c)                                       \
1398
  (_GL_STDBIT_TYPEOF_CAST                                               \
1399
   (v,                                                                  \
1400
    (sizeof (v) == 1 ? stdc_rotate_right_uc (v, c)                      \
1401
     : sizeof (v) == sizeof (unsigned short int) ? stdc_rotate_right_us (v, c) \
1402
     : sizeof (v) == sizeof 0u ? stdc_rotate_right_ui (v, c)            \
1403
     : sizeof (v) == sizeof 0ul ? stdc_rotate_right_ul (v, c)           \
1404
     : stdc_rotate_right_ull (v, c))))
1405
# endif
1406
1407
#endif
1408
1409
1410
/* ISO C2y § 7.18.19 8-bit Memory Reversal  */
1411
1412
#if 0
1413
1414
# if !GNULIB_defined_stdc_memreverse8
1415
1416
_GL_STDC_MEMREVERSE8_INLINE void
1417
stdc_memreverse8 (size_t n, unsigned char *ptr)
1418
{
1419
  if (n > 0)
1420
    {
1421
      /* There is no need to optimize the cases N == 1, N == 2, N == 4
1422
         specially using __builtin_constant_p, because GCC does the possible
1423
         optimizations already, taking into account the alignment of PTR:
1424
         GCC >= 3 for N == 1, GCC >= 8 for N == 2, GCC >= 13 for N == 4.
1425
         (Whereas clang >= 3, <= 22 optimizes only the case N == 1.)  */
1426
      size_t i, j;
1427
      for (i = 0, j = n-1; i < j; i++, j--)
1428
        {
1429
          unsigned char xi = ptr[i];
1430
          unsigned char xj = ptr[j];
1431
          ptr[j] = xi;
1432
          ptr[i] = xj;
1433
        }
1434
    }
1435
}
1436
1437
#  define GNULIB_defined_stdc_memreverse8 1
1438
# endif
1439
1440
#endif
1441
1442
1443
/* ISO C2y § 7.18.20 Exact-width 8-bit Memory Reversal  */
1444
1445
#if 1
1446
1447
/* Note: ISO C defines these functions with argument and return type uintN_t.
1448
   We do it here with argument and return type uint_leastN_t.  This is a
1449
   generalization that does not contradict ISO C: When uintN_t exists, it is
1450
   known that uint_leastN_t is the same type as uintN_t.  */
1451
1452
# if !GNULIB_defined_stdc_memreverse8u_functions
1453
1454
_GL_STDC_MEMREVERSE8U_INLINE uint_least8_t
1455
stdc_memreverse8u8 (uint_least8_t value)
1456
0
{
1457
0
  return value;
1458
0
}
1459
1460
_GL_STDC_MEMREVERSE8U_INLINE uint_least16_t
1461
stdc_memreverse8u16 (uint_least16_t value)
1462
0
{
1463
0
#  ifdef _GL_STDBIT_HAS_BUILTIN_BSWAP16
1464
0
  return __builtin_bswap16 (value);
1465
0
#  else
1466
0
  _GL_STDBIT_UINT_FAST16 mask = 0xFFU;
1467
0
  return (  (value & (mask << (8 * 1))) >> (8 * 1)
1468
0
          | (value & (mask << (8 * 0))) << (8 * 1));
1469
0
#  endif
1470
0
}
1471
1472
_GL_STDC_MEMREVERSE8U_INLINE uint_least32_t
1473
stdc_memreverse8u32 (uint_least32_t value)
1474
0
{
1475
0
#  ifdef _GL_STDBIT_HAS_BUILTIN_BSWAP32
1476
0
  return __builtin_bswap32 (value);
1477
0
#  else
1478
0
  _GL_STDBIT_UINT_FAST32 mask = 0xFFU;
1479
0
  return (  (value & (mask << (8 * 3))) >> (8 * 3)
1480
0
          | (value & (mask << (8 * 2))) >> (8 * 1)
1481
0
          | (value & (mask << (8 * 1))) << (8 * 1)
1482
0
          | (value & (mask << (8 * 0))) << (8 * 3));
1483
0
#  endif
1484
0
}
1485
1486
_GL_STDC_MEMREVERSE8U_INLINE uint_least64_t
1487
stdc_memreverse8u64 (uint_least64_t value)
1488
0
{
1489
0
#  ifdef _GL_STDBIT_HAS_BUILTIN_BSWAP64
1490
0
  return __builtin_bswap64 (value);
1491
0
#  else
1492
0
  _GL_STDBIT_UINT_FAST64 mask = 0xFFU;
1493
0
  return (  (value & (mask << (8 * 7))) >> (8 * 7)
1494
0
          | (value & (mask << (8 * 6))) >> (8 * 5)
1495
0
          | (value & (mask << (8 * 5))) >> (8 * 3)
1496
0
          | (value & (mask << (8 * 4))) >> (8 * 1)
1497
0
          | (value & (mask << (8 * 3))) << (8 * 1)
1498
0
          | (value & (mask << (8 * 2))) << (8 * 3)
1499
0
          | (value & (mask << (8 * 1))) << (8 * 5)
1500
0
          | (value & (mask << (8 * 0))) << (8 * 7));
1501
0
#  endif
1502
0
}
1503
1504
#  define GNULIB_defined_stdc_memreverse8u_functions 1
1505
# endif
1506
1507
#endif
1508
1509
1510
/* ISO C2y § 7.18.21 Endian-Aware 8-Bit Load  */
1511
1512
/* On hosts where _GL_STDBIT_OPTIMIZE_VIA_MEMCPY (see below) might be useful,
1513
   we need to avoid type-punning, because the compiler's aliasing
1514
   analysis would frequently produce incorrect code, and requiring the
1515
   option '-fno-strict-aliasing' is no viable solution.
1516
   So, this definition won't work:
1517
1518
     uint_least16_t
1519
     load16 (const unsigned char ptr[2])
1520
     {
1521
       return *(const uint_least16_t *)ptr;
1522
     }
1523
1524
   Instead, the following definitions are candidates:
1525
1526
     // Trick from Lasse Collin: use memcpy and __builtin_assume_aligned.
1527
     uint_least16_t
1528
     load16_a (const unsigned char ptr[2])
1529
     {
1530
       uint_least16_t value;
1531
       memcpy (&value, __builtin_assume_aligned (ptr, 2), 2);
1532
       return value;
1533
     }
1534
1535
     // Use __builtin_assume_aligned, without memcpy.
1536
     uint_least16_t
1537
     load16_b (const unsigned char ptr[2])
1538
     {
1539
       const unsigned char *aptr =
1540
         (const unsigned char *) __builtin_assume_aligned (ptr, 2);
1541
       return (_GL_STDBIT_BIGENDIAN
1542
               ? ((uint_least16_t) aptr [0] << 8) | (uint_least16_t) aptr [1]
1543
               : (uint_least16_t) aptr [0] | ((uint_least16_t) aptr [1] << 8));
1544
     }
1545
1546
     // Use memcpy and __assume.
1547
     uint_least16_t
1548
     load16_c (const unsigned char ptr[2])
1549
     {
1550
       __assume (((uintptr_t) ptr & (2 - 1)) == 0);
1551
       uint_least16_t value;
1552
       memcpy (&value, __builtin_assume_aligned (ptr, 2), 2);
1553
       return value;
1554
     }
1555
1556
     // Use __assume, without memcpy.
1557
     uint_least16_t
1558
     load16_d (const unsigned char ptr[2])
1559
     {
1560
       __assume (((uintptr_t) ptr & (2 - 1)) == 0);
1561
       return (_GL_STDBIT_BIGENDIAN
1562
               ? ((uint_least16_t) ptr [0] << 8) | (uint_least16_t) ptr [1]
1563
               : (uint_least16_t) ptr [0] | ((uint_least16_t) ptr [1] << 8));
1564
     }
1565
1566
     // Use memcpy, without __builtin_assume_aligned or __assume.
1567
     uint_least16_t
1568
     load16_e (const unsigned char ptr[2])
1569
     {
1570
       uint_least16_t value;
1571
       memcpy (&value, ptr, 2);
1572
       return value;
1573
     }
1574
1575
     // Use the code for the unaligned case.
1576
     uint_least16_t
1577
     load16_f (const unsigned char ptr[2])
1578
     {
1579
       return (_GL_STDBIT_BIGENDIAN
1580
               ? ((uint_least16_t) ptr [0] << 8) | (uint_least16_t) ptr [1]
1581
               : (uint_least16_t) ptr [0] | ((uint_least16_t) ptr [1] << 8));
1582
     }
1583
1584
   Portability constraints:
1585
     - __builtin_assume_aligned works only in GCC >= 4.7 and clang >= 4.
1586
     - __assume works only with MSVC (_MSC_VER >= 1200).
1587
1588
   Which variant produces the best code?
1589
     - memcpy is inlined only in gcc >= 3.4, g++ >= 4.9, clang >= 4.
1590
     - MSVC's __assume has no effect.
1591
     - With gcc 13:
1592
       On armelhf, arm64, i686, powerpc, powerpc64, powerpc64le, s390x, x86_64:
1593
       All of a,b,e,f are equally good.
1594
       On alpha, arm, hppa, mips, mips64, riscv64, sh4, sparc64:
1595
       Only a,b are good; f medium; e worst.
1596
     - With older gcc versions on x86_64:
1597
       gcc >= 10: All of a,b,e,f are equally good.
1598
       gcc < 10: Only a,e are good; b,f medium.
1599
     - With MSVC 14: Only c,e are good; d,f medium.
1600
1601
   So, we use the following heuristic for getting good code:
1602
     - gcc >= 4.7, g++ >= 4.9, clang >= 4, or any other platform
1603
       with __builtin_assume_aligned: Use variant a.
1604
     - MSVC: Use variant e.
1605
     - Otherwise: Use variant f.
1606
 */
1607
#if (defined __clang__ ? __clang_major__ >= 4 : \
1608
     (defined __GNUC__ \
1609
      && (defined __cplusplus \
1610
          ? __GNUC__ + (__GNUC_MINOR__ >= 9) > 4 \
1611
          : __GNUC__ + (__GNUC_MINOR__ >= 7) > 4)))
1612
# define _GL_HAS_BUILTIN_ASSUME_ALIGNED 1
1613
#elif defined __has_builtin
1614
# if __has_builtin (__builtin_assume_aligned)
1615
#  define _GL_HAS_BUILTIN_ASSUME_ALIGNED 1
1616
# endif
1617
#endif
1618
#ifdef _GL_HAS_BUILTIN_ASSUME_ALIGNED
1619
# define _GL_STDBIT_ASSUME_ALIGNED(ptr, align) \
1620
    __builtin_assume_aligned (ptr, align)
1621
#else
1622
# define _GL_STDBIT_ASSUME_ALIGNED(ptr, align) (ptr)
1623
#endif
1624
1625
#if defined _GL_HAS_BUILTIN_ASSUME_ALIGNED || defined _MSC_VER
1626
/* The _GL_STDBIT_OPTIMIZE_VIA_MEMCPY trick works on typical hosts
1627
   where CHAR_BIT == 8 and uint_leastN_t types have minimal sizes.
1628
   Check to be safe and to document the assumption.  */
1629
# define _GL_STDBIT_OPTIMIZE_VIA_MEMCPY \
1630
   ((unsigned char) -1 == 0xFF \
1631
    && sizeof (uint_least16_t) == 2 \
1632
    && sizeof (uint_least32_t) == 4 \
1633
    && sizeof (uint_least64_t) == 8)
1634
#endif
1635
1636
#ifndef _GL_STDBIT_OPTIMIZE_VIA_MEMCPY
1637
# define _GL_STDBIT_OPTIMIZE_VIA_MEMCPY 0
1638
#endif
1639
1640
#define _GL_STDBIT_BIGENDIAN (__STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_BIG__)
1641
1642
#if 0
1643
1644
# if !GNULIB_defined_stdc_load8_functions
1645
1646
_GL_STDC_LOAD8_INLINE uint_least8_t
1647
stdc_load8_beu8 (const unsigned char ptr[1])
1648
{
1649
  return ptr[0];
1650
}
1651
1652
_GL_STDC_LOAD8_INLINE uint_least16_t
1653
stdc_load8_beu16 (const unsigned char ptr[2])
1654
{
1655
  _GL_STDBIT_UINT_FAST16 v0 = ptr[0];
1656
  _GL_STDBIT_UINT_FAST16 v1 = ptr[1];
1657
  return (v0 << (8 * 1)) | (v1 << (8 * 0));
1658
}
1659
1660
_GL_STDC_LOAD8_INLINE uint_least32_t
1661
stdc_load8_beu32 (const unsigned char ptr[4])
1662
{
1663
  _GL_STDBIT_UINT_FAST32 v0 = ptr[0];
1664
  _GL_STDBIT_UINT_FAST32 v1 = ptr[1];
1665
  _GL_STDBIT_UINT_FAST32 v2 = ptr[2];
1666
  _GL_STDBIT_UINT_FAST32 v3 = ptr[3];
1667
  return (v0 << (8 * 3)) | (v1 << (8 * 2)) | (v2 << (8 * 1)) | (v3 << (8 * 0));
1668
}
1669
1670
_GL_STDC_LOAD8_INLINE uint_least64_t
1671
stdc_load8_beu64 (const unsigned char ptr[8])
1672
{
1673
  _GL_STDBIT_UINT_FAST64 v0 = ptr[0];
1674
  _GL_STDBIT_UINT_FAST64 v1 = ptr[1];
1675
  _GL_STDBIT_UINT_FAST64 v2 = ptr[2];
1676
  _GL_STDBIT_UINT_FAST64 v3 = ptr[3];
1677
  _GL_STDBIT_UINT_FAST64 v4 = ptr[4];
1678
  _GL_STDBIT_UINT_FAST64 v5 = ptr[5];
1679
  _GL_STDBIT_UINT_FAST64 v6 = ptr[6];
1680
  _GL_STDBIT_UINT_FAST64 v7 = ptr[7];
1681
  return ((v0 << (8 * 7)) | (v1 << (8 * 6))
1682
          | (v2 << (8 * 5)) | (v3 << (8 * 4))
1683
          | (v4 << (8 * 3)) | (v5 << (8 * 2))
1684
          | (v6 << (8 * 1)) | (v7 << (8 * 0)));
1685
}
1686
1687
_GL_STDC_LOAD8_INLINE uint_least8_t
1688
stdc_load8_leu8 (const unsigned char ptr[1])
1689
{
1690
  return ptr[0];
1691
}
1692
1693
_GL_STDC_LOAD8_INLINE uint_least16_t
1694
stdc_load8_leu16 (const unsigned char ptr[2])
1695
{
1696
  _GL_STDBIT_UINT_FAST16 v0 = ptr[0];
1697
  _GL_STDBIT_UINT_FAST16 v1 = ptr[1];
1698
  return (v0 << (8 * 0)) | (v1 << (8 * 1));
1699
}
1700
1701
_GL_STDC_LOAD8_INLINE uint_least32_t
1702
stdc_load8_leu32 (const unsigned char ptr[4])
1703
{
1704
  _GL_STDBIT_UINT_FAST32 v0 = ptr[0];
1705
  _GL_STDBIT_UINT_FAST32 v1 = ptr[1];
1706
  _GL_STDBIT_UINT_FAST32 v2 = ptr[2];
1707
  _GL_STDBIT_UINT_FAST32 v3 = ptr[3];
1708
  return (v0 << (8 * 0)) | (v1 << (8 * 1)) | (v2 << (8 * 2)) | (v3 << (8 * 3));
1709
}
1710
1711
_GL_STDC_LOAD8_INLINE uint_least64_t
1712
stdc_load8_leu64 (const unsigned char ptr[8])
1713
{
1714
  _GL_STDBIT_UINT_FAST64 v0 = ptr[0];
1715
  _GL_STDBIT_UINT_FAST64 v1 = ptr[1];
1716
  _GL_STDBIT_UINT_FAST64 v2 = ptr[2];
1717
  _GL_STDBIT_UINT_FAST64 v3 = ptr[3];
1718
  _GL_STDBIT_UINT_FAST64 v4 = ptr[4];
1719
  _GL_STDBIT_UINT_FAST64 v5 = ptr[5];
1720
  _GL_STDBIT_UINT_FAST64 v6 = ptr[6];
1721
  _GL_STDBIT_UINT_FAST64 v7 = ptr[7];
1722
  return ((v0 << (8 * 0)) | (v1 << (8 * 1))
1723
          | (v2 << (8 * 2)) | (v3 << (8 * 3))
1724
          | (v4 << (8 * 4)) | (v5 << (8 * 5))
1725
          | (v6 << (8 * 6)) | (v7 << (8 * 7)));
1726
}
1727
1728
_GL_STDC_LOAD8_INLINE int_least8_t
1729
stdc_load8_bes8 (const unsigned char ptr[1])
1730
{
1731
  return stdc_load8_beu8 (ptr);
1732
}
1733
1734
_GL_STDC_LOAD8_INLINE int_least16_t
1735
stdc_load8_bes16 (const unsigned char ptr[2])
1736
{
1737
  return stdc_load8_beu16 (ptr);
1738
}
1739
1740
_GL_STDC_LOAD8_INLINE int_least32_t
1741
stdc_load8_bes32 (const unsigned char ptr[4])
1742
{
1743
  return stdc_load8_beu32 (ptr);
1744
}
1745
1746
_GL_STDC_LOAD8_INLINE int_least64_t
1747
stdc_load8_bes64 (const unsigned char ptr[8])
1748
{
1749
  return stdc_load8_beu64 (ptr);
1750
}
1751
1752
_GL_STDC_LOAD8_INLINE int_least8_t
1753
stdc_load8_les8 (const unsigned char ptr[1])
1754
{
1755
  return stdc_load8_leu8 (ptr);
1756
}
1757
1758
_GL_STDC_LOAD8_INLINE int_least16_t
1759
stdc_load8_les16 (const unsigned char ptr[2])
1760
{
1761
  return stdc_load8_leu16 (ptr);
1762
}
1763
1764
_GL_STDC_LOAD8_INLINE int_least32_t
1765
stdc_load8_les32 (const unsigned char ptr[4])
1766
{
1767
  return stdc_load8_leu32 (ptr);
1768
}
1769
1770
_GL_STDC_LOAD8_INLINE int_least64_t
1771
stdc_load8_les64 (const unsigned char ptr[8])
1772
{
1773
  return stdc_load8_leu64 (ptr);
1774
}
1775
1776
#  define GNULIB_defined_stdc_load8_functions 1
1777
# endif
1778
1779
#endif
1780
1781
#if 0
1782
1783
# if !GNULIB_defined_stdc_load8_aligned_functions
1784
1785
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least8_t
1786
stdc_load8_aligned_beu8 (const unsigned char ptr[1])
1787
{
1788
  return stdc_load8_beu8 (ptr);
1789
}
1790
1791
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least16_t
1792
stdc_load8_aligned_beu16 (const unsigned char ptr[2])
1793
{
1794
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
1795
    {
1796
      uint_least16_t value;
1797
      _GL_STDBIT_MEMCPY (&value, _GL_STDBIT_ASSUME_ALIGNED (ptr, 2), 2);
1798
      if (!_GL_STDBIT_BIGENDIAN)
1799
        value = stdc_memreverse8u16 (value);
1800
      return value;
1801
    }
1802
  else
1803
    return stdc_load8_beu16 (ptr);
1804
}
1805
1806
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least32_t
1807
stdc_load8_aligned_beu32 (const unsigned char ptr[4])
1808
{
1809
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
1810
    {
1811
      uint_least32_t value;
1812
      _GL_STDBIT_MEMCPY (&value, _GL_STDBIT_ASSUME_ALIGNED (ptr, 4), 4);
1813
      if (!_GL_STDBIT_BIGENDIAN)
1814
        value = stdc_memreverse8u32 (value);
1815
      return value;
1816
    }
1817
  else
1818
    return stdc_load8_beu32 (ptr);
1819
}
1820
1821
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least64_t
1822
stdc_load8_aligned_beu64 (const unsigned char ptr[8])
1823
{
1824
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
1825
    {
1826
      uint_least64_t value;
1827
      _GL_STDBIT_MEMCPY (&value, _GL_STDBIT_ASSUME_ALIGNED (ptr, 8), 8);
1828
      if (!_GL_STDBIT_BIGENDIAN)
1829
        value = stdc_memreverse8u64 (value);
1830
      return value;
1831
    }
1832
  else
1833
    return stdc_load8_beu64 (ptr);
1834
}
1835
1836
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least8_t
1837
stdc_load8_aligned_leu8 (const unsigned char ptr[1])
1838
{
1839
  return stdc_load8_leu8 (ptr);
1840
}
1841
1842
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least16_t
1843
stdc_load8_aligned_leu16 (const unsigned char ptr[2])
1844
{
1845
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
1846
    {
1847
      uint_least16_t value;
1848
      _GL_STDBIT_MEMCPY (&value, _GL_STDBIT_ASSUME_ALIGNED (ptr, 2), 2);
1849
      if (_GL_STDBIT_BIGENDIAN)
1850
        value = stdc_memreverse8u16 (value);
1851
      return value;
1852
    }
1853
  else
1854
    return stdc_load8_leu16 (ptr);
1855
}
1856
1857
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least32_t
1858
stdc_load8_aligned_leu32 (const unsigned char ptr[4])
1859
{
1860
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
1861
    {
1862
      uint_least32_t value;
1863
      _GL_STDBIT_MEMCPY (&value, _GL_STDBIT_ASSUME_ALIGNED (ptr, 4), 4);
1864
      if (_GL_STDBIT_BIGENDIAN)
1865
        value = stdc_memreverse8u32 (value);
1866
      return value;
1867
    }
1868
  else
1869
    return stdc_load8_leu32 (ptr);
1870
}
1871
1872
_GL_STDC_LOAD8_ALIGNED_INLINE uint_least64_t
1873
stdc_load8_aligned_leu64 (const unsigned char ptr[8])
1874
{
1875
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
1876
    {
1877
      uint_least64_t value;
1878
      _GL_STDBIT_MEMCPY (&value, _GL_STDBIT_ASSUME_ALIGNED (ptr, 8), 8);
1879
      if (_GL_STDBIT_BIGENDIAN)
1880
        value = stdc_memreverse8u64 (value);
1881
      return value;
1882
    }
1883
  else
1884
    return stdc_load8_leu64 (ptr);
1885
}
1886
1887
_GL_STDC_LOAD8_ALIGNED_INLINE int_least8_t
1888
stdc_load8_aligned_bes8 (const unsigned char ptr[1])
1889
{
1890
  return stdc_load8_aligned_beu8 (ptr);
1891
}
1892
1893
_GL_STDC_LOAD8_ALIGNED_INLINE int_least16_t
1894
stdc_load8_aligned_bes16 (const unsigned char ptr[2])
1895
{
1896
  return stdc_load8_aligned_beu16 (ptr);
1897
}
1898
1899
_GL_STDC_LOAD8_ALIGNED_INLINE int_least32_t
1900
stdc_load8_aligned_bes32 (const unsigned char ptr[4])
1901
{
1902
  return stdc_load8_aligned_beu32 (ptr);
1903
}
1904
1905
_GL_STDC_LOAD8_ALIGNED_INLINE int_least64_t
1906
stdc_load8_aligned_bes64 (const unsigned char ptr[8])
1907
{
1908
  return stdc_load8_aligned_beu64 (ptr);
1909
}
1910
1911
_GL_STDC_LOAD8_ALIGNED_INLINE int_least8_t
1912
stdc_load8_aligned_les8 (const unsigned char ptr[1])
1913
{
1914
  return stdc_load8_aligned_leu8 (ptr);
1915
}
1916
1917
_GL_STDC_LOAD8_ALIGNED_INLINE int_least16_t
1918
stdc_load8_aligned_les16 (const unsigned char ptr[2])
1919
{
1920
  return stdc_load8_aligned_leu16 (ptr);
1921
}
1922
1923
_GL_STDC_LOAD8_ALIGNED_INLINE int_least32_t
1924
stdc_load8_aligned_les32 (const unsigned char ptr[4])
1925
{
1926
  return stdc_load8_aligned_leu32 (ptr);
1927
}
1928
1929
_GL_STDC_LOAD8_ALIGNED_INLINE int_least64_t
1930
stdc_load8_aligned_les64 (const unsigned char ptr[8])
1931
{
1932
  return stdc_load8_aligned_leu64 (ptr);
1933
}
1934
1935
#  define GNULIB_defined_stdc_load8_aligned_functions 1
1936
# endif
1937
1938
#endif
1939
1940
1941
/* ISO C2y § 7.18.22 Endian-Aware 8-Bit Store  */
1942
1943
#if 0
1944
1945
# if !GNULIB_defined_stdc_store8_functions
1946
1947
_GL_STDC_STORE8_INLINE void
1948
stdc_store8_beu8 (uint_least8_t value, unsigned char ptr[1])
1949
{
1950
  ptr[0] = value;
1951
}
1952
1953
_GL_STDC_STORE8_INLINE void
1954
stdc_store8_beu16 (uint_least16_t value, unsigned char ptr[2])
1955
{
1956
  ptr[0] = (value >> 8) & 0xFFU;
1957
  ptr[1] = value & 0xFFU;
1958
}
1959
1960
_GL_STDC_STORE8_INLINE void
1961
stdc_store8_beu32 (uint_least32_t value, unsigned char ptr[4])
1962
{
1963
  ptr[0] = (value >> 24) & 0xFFU;
1964
  ptr[1] = (value >> 16) & 0xFFU;
1965
  ptr[2] = (value >> 8) & 0xFFU;
1966
  ptr[3] = value & 0xFFU;
1967
}
1968
1969
_GL_STDC_STORE8_INLINE void
1970
stdc_store8_beu64 (uint_least64_t value, unsigned char ptr[8])
1971
{
1972
  ptr[0] = (value >> 56) & 0xFFU;
1973
  ptr[1] = (value >> 48) & 0xFFU;
1974
  ptr[2] = (value >> 40) & 0xFFU;
1975
  ptr[3] = (value >> 32) & 0xFFU;
1976
  ptr[4] = (value >> 24) & 0xFFU;
1977
  ptr[5] = (value >> 16) & 0xFFU;
1978
  ptr[6] = (value >> 8) & 0xFFU;
1979
  ptr[7] = value & 0xFFU;
1980
}
1981
1982
_GL_STDC_STORE8_INLINE void
1983
stdc_store8_leu8 (uint_least8_t value, unsigned char ptr[1])
1984
{
1985
  ptr[0] = value;
1986
}
1987
1988
_GL_STDC_STORE8_INLINE void
1989
stdc_store8_leu16 (uint_least16_t value, unsigned char ptr[2])
1990
{
1991
  ptr[0] = value & 0xFFU;
1992
  ptr[1] = (value >> 8) & 0xFFU;
1993
}
1994
1995
_GL_STDC_STORE8_INLINE void
1996
stdc_store8_leu32 (uint_least32_t value, unsigned char ptr[4])
1997
{
1998
  ptr[0] = value & 0xFFU;
1999
  ptr[1] = (value >> 8) & 0xFFU;
2000
  ptr[2] = (value >> 16) & 0xFFU;
2001
  ptr[3] = (value >> 24) & 0xFFU;
2002
}
2003
2004
_GL_STDC_STORE8_INLINE void
2005
stdc_store8_leu64 (uint_least64_t value, unsigned char ptr[8])
2006
{
2007
  ptr[0] = value & 0xFFU;
2008
  ptr[1] = (value >> 8) & 0xFFU;
2009
  ptr[2] = (value >> 16) & 0xFFU;
2010
  ptr[3] = (value >> 24) & 0xFFU;
2011
  ptr[4] = (value >> 32) & 0xFFU;
2012
  ptr[5] = (value >> 40) & 0xFFU;
2013
  ptr[6] = (value >> 48) & 0xFFU;
2014
  ptr[7] = (value >> 56) & 0xFFU;
2015
}
2016
2017
_GL_STDC_STORE8_INLINE void
2018
stdc_store8_bes8 (int_least8_t value, unsigned char ptr[1])
2019
{
2020
  stdc_store8_beu8 (value, ptr);
2021
}
2022
2023
_GL_STDC_STORE8_INLINE void
2024
stdc_store8_bes16 (int_least16_t value, unsigned char ptr[2])
2025
{
2026
  stdc_store8_beu16 (value, ptr);
2027
}
2028
2029
_GL_STDC_STORE8_INLINE void
2030
stdc_store8_bes32 (int_least32_t value, unsigned char ptr[4])
2031
{
2032
  stdc_store8_beu32 (value, ptr);
2033
}
2034
2035
_GL_STDC_STORE8_INLINE void
2036
stdc_store8_bes64 (int_least64_t value, unsigned char ptr[8])
2037
{
2038
  stdc_store8_beu64 (value, ptr);
2039
}
2040
2041
_GL_STDC_STORE8_INLINE void
2042
stdc_store8_les8 (int_least8_t value, unsigned char ptr[1])
2043
{
2044
  stdc_store8_leu8 (value, ptr);
2045
}
2046
2047
_GL_STDC_STORE8_INLINE void
2048
stdc_store8_les16 (int_least16_t value, unsigned char ptr[2])
2049
{
2050
  stdc_store8_leu16 (value, ptr);
2051
}
2052
2053
_GL_STDC_STORE8_INLINE void
2054
stdc_store8_les32 (int_least32_t value, unsigned char ptr[4])
2055
{
2056
  stdc_store8_leu32 (value, ptr);
2057
}
2058
2059
_GL_STDC_STORE8_INLINE void
2060
stdc_store8_les64 (int_least64_t value, unsigned char ptr[8])
2061
{
2062
  stdc_store8_leu64 (value, ptr);
2063
}
2064
2065
#  define GNULIB_defined_stdc_store8_functions 1
2066
# endif
2067
2068
#endif
2069
2070
#if 0
2071
2072
# if !GNULIB_defined_stdc_store8_aligned_functions
2073
2074
_GL_STDC_STORE8_ALIGNED_INLINE void
2075
stdc_store8_aligned_beu8 (uint_least8_t value, unsigned char ptr[1])
2076
{
2077
  stdc_store8_beu8 (value, ptr);
2078
}
2079
2080
_GL_STDC_STORE8_ALIGNED_INLINE void
2081
stdc_store8_aligned_beu16 (uint_least16_t value, unsigned char ptr[2])
2082
{
2083
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
2084
    {
2085
      if (!_GL_STDBIT_BIGENDIAN)
2086
        value = stdc_memreverse8u16 (value);
2087
      _GL_STDBIT_MEMCPY (_GL_STDBIT_ASSUME_ALIGNED (ptr, 2), &value, 2);
2088
    }
2089
  else
2090
    stdc_store8_beu16 (value, ptr);
2091
}
2092
2093
_GL_STDC_STORE8_ALIGNED_INLINE void
2094
stdc_store8_aligned_beu32 (uint_least32_t value, unsigned char ptr[4])
2095
{
2096
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
2097
    {
2098
      if (!_GL_STDBIT_BIGENDIAN)
2099
        value = stdc_memreverse8u32 (value);
2100
      _GL_STDBIT_MEMCPY (_GL_STDBIT_ASSUME_ALIGNED (ptr, 4), &value, 4);
2101
    }
2102
  else
2103
    stdc_store8_beu32 (value, ptr);
2104
}
2105
2106
_GL_STDC_STORE8_ALIGNED_INLINE void
2107
stdc_store8_aligned_beu64 (uint_least64_t value, unsigned char ptr[8])
2108
{
2109
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
2110
    {
2111
      if (!_GL_STDBIT_BIGENDIAN)
2112
        value = stdc_memreverse8u64 (value);
2113
      _GL_STDBIT_MEMCPY (_GL_STDBIT_ASSUME_ALIGNED (ptr, 8), &value, 8);
2114
    }
2115
  else
2116
    stdc_store8_beu64 (value, ptr);
2117
}
2118
2119
_GL_STDC_STORE8_ALIGNED_INLINE void
2120
stdc_store8_aligned_leu8 (uint_least8_t value, unsigned char ptr[1])
2121
{
2122
  stdc_store8_leu8 (value, ptr);
2123
}
2124
2125
_GL_STDC_STORE8_ALIGNED_INLINE void
2126
stdc_store8_aligned_leu16 (uint_least16_t value, unsigned char ptr[2])
2127
{
2128
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
2129
    {
2130
      if (_GL_STDBIT_BIGENDIAN)
2131
        value = stdc_memreverse8u16 (value);
2132
      _GL_STDBIT_MEMCPY (_GL_STDBIT_ASSUME_ALIGNED (ptr, 2), &value, 2);
2133
    }
2134
  else
2135
    stdc_store8_leu16 (value, ptr);
2136
}
2137
2138
_GL_STDC_STORE8_ALIGNED_INLINE void
2139
stdc_store8_aligned_leu32 (uint_least32_t value, unsigned char ptr[4])
2140
{
2141
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
2142
    {
2143
      if (_GL_STDBIT_BIGENDIAN)
2144
        value = stdc_memreverse8u32 (value);
2145
      _GL_STDBIT_MEMCPY (_GL_STDBIT_ASSUME_ALIGNED (ptr, 4), &value, 4);
2146
    }
2147
  else
2148
    stdc_store8_leu32 (value, ptr);
2149
}
2150
2151
_GL_STDC_STORE8_ALIGNED_INLINE void
2152
stdc_store8_aligned_leu64 (uint_least64_t value, unsigned char ptr[8])
2153
{
2154
  if (_GL_STDBIT_OPTIMIZE_VIA_MEMCPY)
2155
    {
2156
      if (_GL_STDBIT_BIGENDIAN)
2157
        value = stdc_memreverse8u64 (value);
2158
      _GL_STDBIT_MEMCPY (_GL_STDBIT_ASSUME_ALIGNED (ptr, 8), &value, 8);
2159
    }
2160
  else
2161
    stdc_store8_leu64 (value, ptr);
2162
}
2163
2164
_GL_STDC_STORE8_ALIGNED_INLINE void
2165
stdc_store8_aligned_bes8 (int_least8_t value, unsigned char ptr[1])
2166
{
2167
  stdc_store8_aligned_beu8 (value, ptr);
2168
}
2169
2170
_GL_STDC_STORE8_ALIGNED_INLINE void
2171
stdc_store8_aligned_bes16 (int_least16_t value, unsigned char ptr[2])
2172
{
2173
  stdc_store8_aligned_beu16 (value, ptr);
2174
}
2175
2176
_GL_STDC_STORE8_ALIGNED_INLINE void
2177
stdc_store8_aligned_bes32 (int_least32_t value, unsigned char ptr[4])
2178
{
2179
  stdc_store8_aligned_beu32 (value, ptr);
2180
}
2181
2182
_GL_STDC_STORE8_ALIGNED_INLINE void
2183
stdc_store8_aligned_bes64 (int_least64_t value, unsigned char ptr[8])
2184
{
2185
  stdc_store8_aligned_beu64 (value, ptr);
2186
}
2187
2188
_GL_STDC_STORE8_ALIGNED_INLINE void
2189
stdc_store8_aligned_les8 (int_least8_t value, unsigned char ptr[1])
2190
{
2191
  stdc_store8_aligned_leu8 (value, ptr);
2192
}
2193
2194
_GL_STDC_STORE8_ALIGNED_INLINE void
2195
stdc_store8_aligned_les16 (int_least16_t value, unsigned char ptr[2])
2196
{
2197
  stdc_store8_aligned_leu16 (value, ptr);
2198
}
2199
2200
_GL_STDC_STORE8_ALIGNED_INLINE void
2201
stdc_store8_aligned_les32 (int_least32_t value, unsigned char ptr[4])
2202
{
2203
  stdc_store8_aligned_leu32 (value, ptr);
2204
}
2205
2206
_GL_STDC_STORE8_ALIGNED_INLINE void
2207
stdc_store8_aligned_les64 (int_least64_t value, unsigned char ptr[8])
2208
{
2209
  stdc_store8_aligned_leu64 (value, ptr);
2210
}
2211
2212
#  define GNULIB_defined_stdc_store8_aligned_functions 1
2213
# endif
2214
2215
#endif
2216
2217
2218
#ifdef __cplusplus
2219
}
2220
#endif
2221
2222
_GL_INLINE_HEADER_END
2223
2224
/* ISO C 23 § 7.18.1 General  */
2225
#ifndef __STDC_VERSION_STDBIT_H__
2226
# define __STDC_VERSION_STDBIT_H__ 202311L
2227
#endif
2228
2229
#endif /* __STDBIT_H */
2230
#endif /* __STDBIT_H */