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