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
|
// C compatibility header <stdbit.h> -*- C++ -*-
// Copyright The GNU Toolchain Authors.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library 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 General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file include/stdbit.h
* This is a Standard C++ Library header.
*/
#ifndef _GLIBCXX_STDBIT_H
#define _GLIBCXX_STDBIT_H
#if __cplusplus > 202302L
#include <bit>
#define __STDC_VERSION_STDBIT_H__ 202311L
#define __STDC_ENDIAN_BIG__ __ORDER_BIG_ENDIAN__
#define __STDC_ENDIAN_LITTLE__ __ORDER_LITTLE_ENDIAN__
#define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER__
#ifndef _GLIBCXX_DOXYGEN
// We define these in our own namespace, but let Doxygen think otherwise.
namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
{
#endif
/** Count the number of leading zero bits
*
* @param __value An unsigned integer.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_leading_zeros(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::countl_zero(__value);
}
inline unsigned int
stdc_leading_zeros_uc(unsigned char __value)
{ return stdc_leading_zeros(__value); }
inline unsigned int
stdc_leading_zeros_us(unsigned short __value)
{ return stdc_leading_zeros(__value); }
inline unsigned int
stdc_leading_zeros_ui(unsigned int __value)
{ return stdc_leading_zeros(__value); }
inline unsigned int
stdc_leading_zeros_ul(unsigned long int __value)
{ return stdc_leading_zeros(__value); }
inline unsigned int
stdc_leading_zeros_ull(unsigned long long int __value)
{ return stdc_leading_zeros(__value); }
/// @}
/** Count the number of leading one bits
*
* @param __value An unsigned integer.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_leading_ones(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::countl_one(__value);
}
inline unsigned int
stdc_leading_ones_uc(unsigned char __value)
{ return stdc_leading_ones(__value); }
inline unsigned int
stdc_leading_ones_us(unsigned short __value)
{ return stdc_leading_ones(__value); }
inline unsigned int
stdc_leading_ones_ui(unsigned int __value)
{ return stdc_leading_ones(__value); }
inline unsigned int
stdc_leading_ones_ul(unsigned long int __value)
{ return stdc_leading_ones(__value); }
inline unsigned int
stdc_leading_ones_ull(unsigned long long int __value)
{ return stdc_leading_ones(__value); }
/// @}
/** Count the number of trailing zero bits
*
* @param __value An unsigned integer.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_trailing_zeros(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::countr_zero(__value);
}
inline unsigned int
stdc_trailing_zeros_uc(unsigned char __value)
{ return stdc_trailing_zeros(__value); }
inline unsigned int
stdc_trailing_zeros_us(unsigned short __value)
{ return stdc_trailing_zeros(__value); }
inline unsigned int
stdc_trailing_zeros_ui(unsigned int __value)
{ return stdc_trailing_zeros(__value); }
inline unsigned int
stdc_trailing_zeros_ul(unsigned long int __value)
{ return stdc_trailing_zeros(__value); }
inline unsigned int
stdc_trailing_zeros_ull(unsigned long long int __value)
{ return stdc_trailing_zeros(__value); }
/// @}
/** Count the number of trailing one bits
*
* @param __value An unsigned integer.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_trailing_ones(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::countr_one(__value);
}
inline unsigned int
stdc_trailing_ones_uc(unsigned char __value)
{ return stdc_trailing_ones(__value); }
inline unsigned int
stdc_trailing_ones_us(unsigned short __value)
{ return stdc_trailing_ones(__value); }
inline unsigned int
stdc_trailing_ones_ui(unsigned int __value)
{ return stdc_trailing_ones(__value); }
inline unsigned int
stdc_trailing_ones_ul(unsigned long int __value)
{ return stdc_trailing_ones(__value); }
inline unsigned int
stdc_trailing_ones_ull(unsigned long long int __value)
{ return stdc_trailing_ones(__value); }
/// @}
/** Find the leftmost (i.e. most significant) zero bit
*
* @param __value An unsigned integer.
* @return The one-based index of the first zero bit counting from the left,
* or zero if there are no zero bits.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_first_leading_zero(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return __value == _Tp(-1) ? 0 : 1 + std::countl_one(__value);
}
inline unsigned int
stdc_first_leading_zero_uc(unsigned char __value)
{ return stdc_first_leading_zero(__value); }
inline unsigned int
stdc_first_leading_zero_us(unsigned short __value)
{ return stdc_first_leading_zero(__value); }
inline unsigned int
stdc_first_leading_zero_ui(unsigned int __value)
{ return stdc_first_leading_zero(__value); }
inline unsigned int
stdc_first_leading_zero_ul(unsigned long int __value)
{ return stdc_first_leading_zero(__value); }
inline unsigned int
stdc_first_leading_zero_ull(unsigned long long int __value)
{ return stdc_first_leading_zero(__value); }
/// @}
/** Find the leftmost (i.e. most significant) one bit
*
* @param __value An unsigned integer.
* @return The one-based index of the first one bit counting from the left,
* or zero if there are no one bits.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_first_leading_one(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return __value == 0 ? 0 : 1 + std::countl_zero(__value);
}
inline unsigned int
stdc_first_leading_one_uc(unsigned char __value)
{ return stdc_first_leading_one(__value); }
inline unsigned int
stdc_first_leading_one_us(unsigned short __value)
{ return stdc_first_leading_one(__value); }
inline unsigned int
stdc_first_leading_one_ui(unsigned int __value)
{ return stdc_first_leading_one(__value); }
inline unsigned int
stdc_first_leading_one_ul(unsigned long int __value)
{ return stdc_first_leading_one(__value); }
inline unsigned int
stdc_first_leading_one_ull(unsigned long long int __value)
{ return stdc_first_leading_one(__value); }
/// @}
/** Find the rightmost (i.e. least significant) zero bit
*
* @param __value An unsigned integer.
* @return The one-based index of the first zero bit counting from the right,
* or zero if there are no zero bits.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_first_trailing_zero(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return __value == _Tp(-1) ? 0 : 1 + std::countr_one(__value);
}
inline unsigned int
stdc_first_trailing_zero_uc(unsigned char __value)
{ return stdc_first_trailing_zero(__value); }
inline unsigned int
stdc_first_trailing_zero_us(unsigned short __value)
{ return stdc_first_trailing_zero(__value); }
inline unsigned int
stdc_first_trailing_zero_ui(unsigned int __value)
{ return stdc_first_trailing_zero(__value); }
inline unsigned int
stdc_first_trailing_zero_ul(unsigned long int __value)
{ return stdc_first_trailing_zero(__value); }
inline unsigned int
stdc_first_trailing_zero_ull(unsigned long long int __value)
{ return stdc_first_trailing_zero(__value); }
/// @}
/** Find the rightmost (i.e. least significant) one bit
*
* @param __value An unsigned integer.
* @return The one-based index of the first one bit counting from the right,
* or zero if there are no one bits.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_first_trailing_one(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return __value == 0 ? 0 : 1 + std::countr_zero(__value);
}
inline unsigned int
stdc_first_trailing_one_uc(unsigned char __value)
{ return stdc_first_trailing_one(__value); }
inline unsigned int
stdc_first_trailing_one_us(unsigned short __value)
{ return stdc_first_trailing_one(__value); }
inline unsigned int
stdc_first_trailing_one_ui(unsigned int __value)
{ return stdc_first_trailing_one(__value); }
inline unsigned int
stdc_first_trailing_one_ul(unsigned long int __value)
{ return stdc_first_trailing_one(__value); }
inline unsigned int
stdc_first_trailing_one_ull(unsigned long long int __value)
{ return stdc_first_trailing_one(__value); }
/// @}
/** Count zeros
*
* @param __value An unsigned integer.
* @return The total number of zero bits in `__value`.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_count_zeros(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::popcount(_Tp(~__value));
}
inline unsigned int
stdc_count_zeros_uc(unsigned char __value)
{ return stdc_count_zeros(__value); }
inline unsigned int
stdc_count_zeros_us(unsigned short __value)
{ return stdc_count_zeros(__value); }
inline unsigned int
stdc_count_zeros_ui(unsigned int __value)
{ return stdc_count_zeros(__value); }
inline unsigned int
stdc_count_zeros_ul(unsigned long int __value)
{ return stdc_count_zeros(__value); }
inline unsigned int
stdc_count_zeros_ull(unsigned long long int __value)
{ return stdc_count_zeros(__value); }
/// @}
/** Count ones
*
* @param __value An unsigned integer.
* @return The total number of one bits in `__value`.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_count_ones(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::popcount(__value);
}
inline unsigned int
stdc_count_ones_uc(unsigned char __value)
{ return stdc_count_ones(__value); }
inline unsigned int
stdc_count_ones_us(unsigned short __value)
{ return stdc_count_ones(__value); }
inline unsigned int
stdc_count_ones_ui(unsigned int __value)
{ return stdc_count_ones(__value); }
inline unsigned int
stdc_count_ones_ul(unsigned long int __value)
{ return stdc_count_ones(__value); }
inline unsigned int
stdc_count_ones_ull(unsigned long long int __value)
{ return stdc_count_ones(__value); }
/// @}
/** Power of two check
*
* @param __value An unsigned integer.
* @return True if the value has a single bit set, false otherwise.
* @since C++26
* @{
*/
template<typename _Tp>
inline bool
stdc_has_single_bit(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::has_single_bit(__value);
}
inline bool
stdc_has_single_bit_uc(unsigned char __value)
{ return stdc_has_single_bit(__value); }
inline bool
stdc_has_single_bit_us(unsigned short __value)
{ return stdc_has_single_bit(__value); }
inline bool
stdc_has_single_bit_ui(unsigned int __value)
{ return stdc_has_single_bit(__value); }
inline bool
stdc_has_single_bit_ul(unsigned long int __value)
{ return stdc_has_single_bit(__value); }
inline bool
stdc_has_single_bit_ull(unsigned long long int __value)
{ return stdc_has_single_bit(__value); }
/// @}
/** Bit width
*
* @param __value An unsigned integer.
* @return The minimum number of bits needed to represent `__value`.
* @since C++26
* @{
*/
template<typename _Tp>
inline unsigned int
stdc_bit_width(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::bit_width(__value);
}
inline unsigned int
stdc_bit_width_uc(unsigned char __value)
{ return stdc_bit_width(__value); }
inline unsigned int
stdc_bit_width_us(unsigned short __value)
{ return stdc_bit_width(__value); }
inline unsigned int
stdc_bit_width_ui(unsigned int __value)
{ return stdc_bit_width(__value); }
inline unsigned int
stdc_bit_width_ul(unsigned long int __value)
{ return stdc_bit_width(__value); }
inline unsigned int
stdc_bit_width_ull(unsigned long long int __value)
{ return stdc_bit_width(__value); }
/// @}
/** Bit floor
*
* @param __value An unsigned integer.
* @return The largest power of two that is not greater than `__value`.
* @since C++26
* @{
*/
template<typename _Tp>
inline _Tp
stdc_bit_floor(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
return std::bit_floor(__value);
}
inline unsigned char
stdc_bit_floor_uc(unsigned char __value)
{ return stdc_bit_floor(__value); }
inline unsigned short
stdc_bit_floor_us(unsigned short __value)
{ return stdc_bit_floor(__value); }
inline unsigned int
stdc_bit_floor_ui(unsigned int __value)
{ return stdc_bit_floor(__value); }
inline unsigned long int
stdc_bit_floor_ul(unsigned long int __value)
{ return stdc_bit_floor(__value); }
inline unsigned long long int
stdc_bit_floor_ull(unsigned long long int __value)
{ return stdc_bit_floor(__value); }
/// @}
/** Bit ceiling
*
* Unlike `std::bit_ceil`, this is defined to return zero for values which
* are not representable in the return type.
*
* @param __value An unsigned integer.
* @return The smallest power of two that is not less than `__value`.
* @since C++26
* @{
*/
template<typename _Tp>
inline _Tp
stdc_bit_ceil(_Tp __value)
{
static_assert(std::__unsigned_integer<_Tp>);
constexpr _Tp __msb = _Tp(1) << (__gnu_cxx::__int_traits<_Tp>::__digits - 1);
return (__value & __msb) ? 0 : std::bit_ceil(__value);
}
inline unsigned char
stdc_bit_ceil_uc(unsigned char __value)
{ return stdc_bit_ceil(__value); }
inline unsigned short
stdc_bit_ceil_us(unsigned short __value)
{ return stdc_bit_ceil(__value); }
inline unsigned int
stdc_bit_ceil_ui(unsigned int __value)
{ return stdc_bit_ceil(__value); }
inline unsigned long int
stdc_bit_ceil_ul(unsigned long int __value)
{ return stdc_bit_ceil(__value); }
inline unsigned long long int
stdc_bit_ceil_ull(unsigned long long int __value)
{ return stdc_bit_ceil(__value); }
/// @}
#ifndef _GLIBCXX_DOXYGEN
} // namespace __gnu_cxx
#define _GLIBCXX_STDBIT_FUNC(F) \
using __gnu_cxx::F ## _uc; \
using __gnu_cxx::F ## _us; \
using __gnu_cxx::F ## _ui; \
using __gnu_cxx::F ## _ul; \
using __gnu_cxx::F ## _ull; \
using __gnu_cxx::F
_GLIBCXX_STDBIT_FUNC(stdc_leading_zeros);
_GLIBCXX_STDBIT_FUNC(stdc_leading_ones);
_GLIBCXX_STDBIT_FUNC(stdc_trailing_zeros);
_GLIBCXX_STDBIT_FUNC(stdc_trailing_ones);
_GLIBCXX_STDBIT_FUNC(stdc_first_leading_zero);
_GLIBCXX_STDBIT_FUNC(stdc_first_leading_one);
_GLIBCXX_STDBIT_FUNC(stdc_first_trailing_zero);
_GLIBCXX_STDBIT_FUNC(stdc_first_trailing_one);
_GLIBCXX_STDBIT_FUNC(stdc_count_zeros);
_GLIBCXX_STDBIT_FUNC(stdc_count_ones);
_GLIBCXX_STDBIT_FUNC(stdc_has_single_bit);
_GLIBCXX_STDBIT_FUNC(stdc_bit_width);
_GLIBCXX_STDBIT_FUNC(stdc_bit_floor);
_GLIBCXX_STDBIT_FUNC(stdc_bit_ceil);
#undef _GLIBCXX_STDBIT_FUNC
#endif // !DOXYGEN
#endif // C++26
#endif // _GLIBCXX_STDBIT_H
|