libstdc++: stdbit.h Source File

libstdc++

stdbit.h
Go to the documentation of this file.
1// C compatibility header <stdbit.h> -*- C++ -*-
2
3// Copyright The GNU Toolchain Authors.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library 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 General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/stdbit.h
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_STDBIT_H
30#define _GLIBCXX_STDBIT_H
31
32#define __glibcxx_want_stdbit_h
33#include <bits/version.h>
34
35#ifdef __cpp_lib_stdbit_h // C++ >= 26
36
37#include <bit>
38
39#define __STDC_VERSION_STDBIT_H__ 202311L
40
41#define __STDC_ENDIAN_BIG__ __ORDER_BIG_ENDIAN__
42#define __STDC_ENDIAN_LITTLE__ __ORDER_LITTLE_ENDIAN__
43#define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER__
44
45#ifndef _GLIBCXX_DOXYGEN
46// We define these in our own namespace, but let Doxygen think otherwise.
47namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
48{
49#endif
50
51/** Count the number of leading zero bits
52 *
53 * @param __value An unsigned integer.
54 * @since C++26
55 * @{
56 */
57template<typename _Tp>
58inline unsigned int
59stdc_leading_zeros(_Tp __value)
60{
61 static_assert(std::__unsigned_integer<_Tp>);
62 return std::countl_zero(__value);
63}
64
65inline unsigned int
66stdc_leading_zeros_uc(unsigned char __value)
67{ return stdc_leading_zeros(__value); }
68
69inline unsigned int
70stdc_leading_zeros_us(unsigned short __value)
71{ return stdc_leading_zeros(__value); }
72
73inline unsigned int
74stdc_leading_zeros_ui(unsigned int __value)
75{ return stdc_leading_zeros(__value); }
76
77inline unsigned int
78stdc_leading_zeros_ul(unsigned long int __value)
79{ return stdc_leading_zeros(__value); }
80
81inline unsigned int
82stdc_leading_zeros_ull(unsigned long long int __value)
83{ return stdc_leading_zeros(__value); }
84/// @}
85
86/** Count the number of leading one bits
87 *
88 * @param __value An unsigned integer.
89 * @since C++26
90 * @{
91 */
92template<typename _Tp>
93inline unsigned int
94stdc_leading_ones(_Tp __value)
95{
96 static_assert(std::__unsigned_integer<_Tp>);
97 return std::countl_one(__value);
98}
99
100inline unsigned int
101stdc_leading_ones_uc(unsigned char __value)
102{ return stdc_leading_ones(__value); }
103
104inline unsigned int
105stdc_leading_ones_us(unsigned short __value)
106{ return stdc_leading_ones(__value); }
107
108inline unsigned int
109stdc_leading_ones_ui(unsigned int __value)
110{ return stdc_leading_ones(__value); }
111
112inline unsigned int
113stdc_leading_ones_ul(unsigned long int __value)
114{ return stdc_leading_ones(__value); }
115
116inline unsigned int
117stdc_leading_ones_ull(unsigned long long int __value)
118{ return stdc_leading_ones(__value); }
119/// @}
120
121/** Count the number of trailing zero bits
122 *
123 * @param __value An unsigned integer.
124 * @since C++26
125 * @{
126 */
127template<typename _Tp>
128inline unsigned int
129stdc_trailing_zeros(_Tp __value)
130{
131 static_assert(std::__unsigned_integer<_Tp>);
132 return std::countr_zero(__value);
133}
134
135inline unsigned int
136stdc_trailing_zeros_uc(unsigned char __value)
137{ return stdc_trailing_zeros(__value); }
138
139inline unsigned int
140stdc_trailing_zeros_us(unsigned short __value)
141{ return stdc_trailing_zeros(__value); }
142
143inline unsigned int
144stdc_trailing_zeros_ui(unsigned int __value)
145{ return stdc_trailing_zeros(__value); }
146
147inline unsigned int
148stdc_trailing_zeros_ul(unsigned long int __value)
149{ return stdc_trailing_zeros(__value); }
150
151inline unsigned int
152stdc_trailing_zeros_ull(unsigned long long int __value)
153{ return stdc_trailing_zeros(__value); }
154/// @}
155
156/** Count the number of trailing one bits
157 *
158 * @param __value An unsigned integer.
159 * @since C++26
160 * @{
161 */
162template<typename _Tp>
163inline unsigned int
164stdc_trailing_ones(_Tp __value)
165{
166 static_assert(std::__unsigned_integer<_Tp>);
167 return std::countr_one(__value);
168}
169
170inline unsigned int
171stdc_trailing_ones_uc(unsigned char __value)
172{ return stdc_trailing_ones(__value); }
173
174inline unsigned int
175stdc_trailing_ones_us(unsigned short __value)
176{ return stdc_trailing_ones(__value); }
177
178inline unsigned int
179stdc_trailing_ones_ui(unsigned int __value)
180{ return stdc_trailing_ones(__value); }
181
182inline unsigned int
183stdc_trailing_ones_ul(unsigned long int __value)
184{ return stdc_trailing_ones(__value); }
185
186inline unsigned int
187stdc_trailing_ones_ull(unsigned long long int __value)
188{ return stdc_trailing_ones(__value); }
189/// @}
190
191/** Find the leftmost (i.e. most significant) zero bit
192 *
193 * @param __value An unsigned integer.
194 * @return The one-based index of the first zero bit counting from the left,
195 * or zero if there are no zero bits.
196 * @since C++26
197 * @{
198 */
199template<typename _Tp>
200inline unsigned int
201stdc_first_leading_zero(_Tp __value)
202{
203 static_assert(std::__unsigned_integer<_Tp>);
204 return __value == _Tp(-1) ? 0 : 1 + std::countl_one(__value);
205}
206
207inline unsigned int
208stdc_first_leading_zero_uc(unsigned char __value)
209{ return stdc_first_leading_zero(__value); }
210
211inline unsigned int
212stdc_first_leading_zero_us(unsigned short __value)
213{ return stdc_first_leading_zero(__value); }
214
215inline unsigned int
216stdc_first_leading_zero_ui(unsigned int __value)
217{ return stdc_first_leading_zero(__value); }
218
219inline unsigned int
220stdc_first_leading_zero_ul(unsigned long int __value)
221{ return stdc_first_leading_zero(__value); }
222
223inline unsigned int
224stdc_first_leading_zero_ull(unsigned long long int __value)
225{ return stdc_first_leading_zero(__value); }
226/// @}
227
228/** Find the leftmost (i.e. most significant) one bit
229 *
230 * @param __value An unsigned integer.
231 * @return The one-based index of the first one bit counting from the left,
232 * or zero if there are no one bits.
233 * @since C++26
234 * @{
235 */
236template<typename _Tp>
237inline unsigned int
238stdc_first_leading_one(_Tp __value)
239{
240 static_assert(std::__unsigned_integer<_Tp>);
241 return __value == 0 ? 0 : 1 + std::countl_zero(__value);
242}
243
244inline unsigned int
245stdc_first_leading_one_uc(unsigned char __value)
246{ return stdc_first_leading_one(__value); }
247
248inline unsigned int
249stdc_first_leading_one_us(unsigned short __value)
250{ return stdc_first_leading_one(__value); }
251
252inline unsigned int
253stdc_first_leading_one_ui(unsigned int __value)
254{ return stdc_first_leading_one(__value); }
255
256inline unsigned int
257stdc_first_leading_one_ul(unsigned long int __value)
258{ return stdc_first_leading_one(__value); }
259
260inline unsigned int
261stdc_first_leading_one_ull(unsigned long long int __value)
262{ return stdc_first_leading_one(__value); }
263/// @}
264
265/** Find the rightmost (i.e. least significant) zero bit
266 *
267 * @param __value An unsigned integer.
268 * @return The one-based index of the first zero bit counting from the right,
269 * or zero if there are no zero bits.
270 * @since C++26
271 * @{
272 */
273template<typename _Tp>
274inline unsigned int
275stdc_first_trailing_zero(_Tp __value)
276{
277 static_assert(std::__unsigned_integer<_Tp>);
278 return __value == _Tp(-1) ? 0 : 1 + std::countr_one(__value);
279}
280
281inline unsigned int
282stdc_first_trailing_zero_uc(unsigned char __value)
283{ return stdc_first_trailing_zero(__value); }
284
285inline unsigned int
286stdc_first_trailing_zero_us(unsigned short __value)
287{ return stdc_first_trailing_zero(__value); }
288
289inline unsigned int
290stdc_first_trailing_zero_ui(unsigned int __value)
291{ return stdc_first_trailing_zero(__value); }
292
293inline unsigned int
294stdc_first_trailing_zero_ul(unsigned long int __value)
295{ return stdc_first_trailing_zero(__value); }
296
297inline unsigned int
298stdc_first_trailing_zero_ull(unsigned long long int __value)
299{ return stdc_first_trailing_zero(__value); }
300/// @}
301
302/** Find the rightmost (i.e. least significant) one bit
303 *
304 * @param __value An unsigned integer.
305 * @return The one-based index of the first one bit counting from the right,
306 * or zero if there are no one bits.
307 * @since C++26
308 * @{
309 */
310template<typename _Tp>
311inline unsigned int
312stdc_first_trailing_one(_Tp __value)
313{
314 static_assert(std::__unsigned_integer<_Tp>);
315 return __value == 0 ? 0 : 1 + std::countr_zero(__value);
316}
317
318inline unsigned int
319stdc_first_trailing_one_uc(unsigned char __value)
320{ return stdc_first_trailing_one(__value); }
321
322inline unsigned int
323stdc_first_trailing_one_us(unsigned short __value)
324{ return stdc_first_trailing_one(__value); }
325
326inline unsigned int
327stdc_first_trailing_one_ui(unsigned int __value)
328{ return stdc_first_trailing_one(__value); }
329
330inline unsigned int
331stdc_first_trailing_one_ul(unsigned long int __value)
332{ return stdc_first_trailing_one(__value); }
333
334inline unsigned int
335stdc_first_trailing_one_ull(unsigned long long int __value)
336{ return stdc_first_trailing_one(__value); }
337/// @}
338
339/** Count zeros
340 *
341 * @param __value An unsigned integer.
342 * @return The total number of zero bits in `__value`.
343 * @since C++26
344 * @{
345 */
346template<typename _Tp>
347inline unsigned int
348stdc_count_zeros(_Tp __value)
349{
350 static_assert(std::__unsigned_integer<_Tp>);
351 return std::popcount(_Tp(~__value));
352}
353
354inline unsigned int
355stdc_count_zeros_uc(unsigned char __value)
356{ return stdc_count_zeros(__value); }
357
358inline unsigned int
359stdc_count_zeros_us(unsigned short __value)
360{ return stdc_count_zeros(__value); }
361
362inline unsigned int
363stdc_count_zeros_ui(unsigned int __value)
364{ return stdc_count_zeros(__value); }
365
366inline unsigned int
367stdc_count_zeros_ul(unsigned long int __value)
368{ return stdc_count_zeros(__value); }
369
370inline unsigned int
371stdc_count_zeros_ull(unsigned long long int __value)
372{ return stdc_count_zeros(__value); }
373/// @}
374
375/** Count ones
376 *
377 * @param __value An unsigned integer.
378 * @return The total number of one bits in `__value`.
379 * @since C++26
380 * @{
381 */
382template<typename _Tp>
383inline unsigned int
384stdc_count_ones(_Tp __value)
385{
386 static_assert(std::__unsigned_integer<_Tp>);
387 return std::popcount(__value);
388}
389
390inline unsigned int
391stdc_count_ones_uc(unsigned char __value)
392{ return stdc_count_ones(__value); }
393
394inline unsigned int
395stdc_count_ones_us(unsigned short __value)
396{ return stdc_count_ones(__value); }
397
398inline unsigned int
399stdc_count_ones_ui(unsigned int __value)
400{ return stdc_count_ones(__value); }
401
402inline unsigned int
403stdc_count_ones_ul(unsigned long int __value)
404{ return stdc_count_ones(__value); }
405
406inline unsigned int
407stdc_count_ones_ull(unsigned long long int __value)
408{ return stdc_count_ones(__value); }
409/// @}
410
411/** Power of two check
412 *
413 * @param __value An unsigned integer.
414 * @return True if the value has a single bit set, false otherwise.
415 * @since C++26
416 * @{
417 */
418template<typename _Tp>
419inline bool
420stdc_has_single_bit(_Tp __value)
421{
422 static_assert(std::__unsigned_integer<_Tp>);
423 return std::has_single_bit(__value);
424}
425
426inline bool
427stdc_has_single_bit_uc(unsigned char __value)
428{ return stdc_has_single_bit(__value); }
429
430inline bool
431stdc_has_single_bit_us(unsigned short __value)
432{ return stdc_has_single_bit(__value); }
433
434inline bool
435stdc_has_single_bit_ui(unsigned int __value)
436{ return stdc_has_single_bit(__value); }
437
438inline bool
439stdc_has_single_bit_ul(unsigned long int __value)
440{ return stdc_has_single_bit(__value); }
441
442inline bool
443stdc_has_single_bit_ull(unsigned long long int __value)
444{ return stdc_has_single_bit(__value); }
445/// @}
446
447/** Bit width
448 *
449 * @param __value An unsigned integer.
450 * @return The minimum number of bits needed to represent `__value`.
451 * @since C++26
452 * @{
453 */
454template<typename _Tp>
455inline unsigned int
456stdc_bit_width(_Tp __value)
457{
458 static_assert(std::__unsigned_integer<_Tp>);
459 return std::bit_width(__value);
460}
461
462inline unsigned int
463stdc_bit_width_uc(unsigned char __value)
464{ return stdc_bit_width(__value); }
465
466inline unsigned int
467stdc_bit_width_us(unsigned short __value)
468{ return stdc_bit_width(__value); }
469
470inline unsigned int
471stdc_bit_width_ui(unsigned int __value)
472{ return stdc_bit_width(__value); }
473
474inline unsigned int
475stdc_bit_width_ul(unsigned long int __value)
476{ return stdc_bit_width(__value); }
477
478inline unsigned int
479stdc_bit_width_ull(unsigned long long int __value)
480{ return stdc_bit_width(__value); }
481/// @}
482
483/** Bit floor
484 *
485 * @param __value An unsigned integer.
486 * @return The largest power of two that is not greater than `__value`.
487 * @since C++26
488 * @{
489 */
490template<typename _Tp>
491inline _Tp
492stdc_bit_floor(_Tp __value)
493{
494 static_assert(std::__unsigned_integer<_Tp>);
495 return std::bit_floor(__value);
496}
497
498inline unsigned char
499stdc_bit_floor_uc(unsigned char __value)
500{ return stdc_bit_floor(__value); }
501
502inline unsigned short
503stdc_bit_floor_us(unsigned short __value)
504{ return stdc_bit_floor(__value); }
505
506inline unsigned int
507stdc_bit_floor_ui(unsigned int __value)
508{ return stdc_bit_floor(__value); }
509
510inline unsigned long int
511stdc_bit_floor_ul(unsigned long int __value)
512{ return stdc_bit_floor(__value); }
513
514inline unsigned long long int
515stdc_bit_floor_ull(unsigned long long int __value)
516{ return stdc_bit_floor(__value); }
517/// @}
518
519/** Bit ceiling
520 *
521 * Unlike `std::bit_ceil`, this is defined to return zero for values which
522 * are not representable in the return type.
523 *
524 * @param __value An unsigned integer.
525 * @return The smallest power of two that is not less than `__value`.
526 * @since C++26
527 * @{
528 */
529template<typename _Tp>
530inline _Tp
531stdc_bit_ceil(_Tp __value)
532{
533 static_assert(std::__unsigned_integer<_Tp>);
534 constexpr _Tp __msb = _Tp(1) << (__gnu_cxx::__int_traits<_Tp>::__digits - 1);
535 return (__value & __msb) ? 0 : std::bit_ceil(__value);
536}
537
538inline unsigned char
539stdc_bit_ceil_uc(unsigned char __value)
540{ return stdc_bit_ceil(__value); }
541
542inline unsigned short
543stdc_bit_ceil_us(unsigned short __value)
544{ return stdc_bit_ceil(__value); }
545
546inline unsigned int
547stdc_bit_ceil_ui(unsigned int __value)
548{ return stdc_bit_ceil(__value); }
549
550inline unsigned long int
551stdc_bit_ceil_ul(unsigned long int __value)
552{ return stdc_bit_ceil(__value); }
553
554inline unsigned long long int
555stdc_bit_ceil_ull(unsigned long long int __value)
556{ return stdc_bit_ceil(__value); }
557/// @}
558
559#ifndef _GLIBCXX_DOXYGEN
560} // namespace __gnu_cxx
561#define _GLIBCXX_STDBIT_FUNC(F) \
562 using __gnu_cxx::F ## _uc; \
563 using __gnu_cxx::F ## _us; \
564 using __gnu_cxx::F ## _ui; \
565 using __gnu_cxx::F ## _ul; \
566 using __gnu_cxx::F ## _ull; \
567 using __gnu_cxx::F
568_GLIBCXX_STDBIT_FUNC(stdc_leading_zeros);
569_GLIBCXX_STDBIT_FUNC(stdc_leading_ones);
570_GLIBCXX_STDBIT_FUNC(stdc_trailing_zeros);
571_GLIBCXX_STDBIT_FUNC(stdc_trailing_ones);
572_GLIBCXX_STDBIT_FUNC(stdc_first_leading_zero);
573_GLIBCXX_STDBIT_FUNC(stdc_first_leading_one);
574_GLIBCXX_STDBIT_FUNC(stdc_first_trailing_zero);
575_GLIBCXX_STDBIT_FUNC(stdc_first_trailing_one);
576_GLIBCXX_STDBIT_FUNC(stdc_count_zeros);
577_GLIBCXX_STDBIT_FUNC(stdc_count_ones);
578_GLIBCXX_STDBIT_FUNC(stdc_has_single_bit);
579_GLIBCXX_STDBIT_FUNC(stdc_bit_width);
580_GLIBCXX_STDBIT_FUNC(stdc_bit_floor);
581_GLIBCXX_STDBIT_FUNC(stdc_bit_ceil);
582#undef _GLIBCXX_STDBIT_FUNC
583#endif // !DOXYGEN
584#endif // __cpp_lib_stdbit_h
585
586#endif // _GLIBCXX_STDBIT_H
bit
version.h
__gnu_cxx
GNU extensions for public use.