Debian Codegraph @

libstdc++-v3/include/c_compatibility/stdbit.h

raw
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.
47 namespace __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 */
57 template<typename _Tp>
58 inline unsigned int
59 stdc_leading_zeros(_Tp __value)
60 {
61 static_assert(std::__unsigned_integer<_Tp>);
62 return std::countl_zero(__value);
63 }
64
65 inline unsigned int
66 stdc_leading_zeros_uc(unsigned char __value)
67 { return stdc_leading_zeros(__value); }
68
69 inline unsigned int
70 stdc_leading_zeros_us(unsigned short __value)
71 { return stdc_leading_zeros(__value); }
72
73 inline unsigned int
74 stdc_leading_zeros_ui(unsigned int __value)
75 { return stdc_leading_zeros(__value); }
76
77 inline unsigned int
78 stdc_leading_zeros_ul(unsigned long int __value)
79 { return stdc_leading_zeros(__value); }
80
81 inline unsigned int
82 stdc_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 */
92 template<typename _Tp>
93 inline unsigned int
94 stdc_leading_ones(_Tp __value)
95 {
96 static_assert(std::__unsigned_integer<_Tp>);
97 return std::countl_one(__value);
98 }
99
100 inline unsigned int
101 stdc_leading_ones_uc(unsigned char __value)
102 { return stdc_leading_ones(__value); }
103
104 inline unsigned int
105 stdc_leading_ones_us(unsigned short __value)
106 { return stdc_leading_ones(__value); }
107
108 inline unsigned int
109 stdc_leading_ones_ui(unsigned int __value)
110 { return stdc_leading_ones(__value); }
111
112 inline unsigned int
113 stdc_leading_ones_ul(unsigned long int __value)
114 { return stdc_leading_ones(__value); }
115
116 inline unsigned int
117 stdc_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 */
127 template<typename _Tp>
128 inline unsigned int
129 stdc_trailing_zeros(_Tp __value)
130 {
131 static_assert(std::__unsigned_integer<_Tp>);
132 return std::countr_zero(__value);
133 }
134
135 inline unsigned int
136 stdc_trailing_zeros_uc(unsigned char __value)
137 { return stdc_trailing_zeros(__value); }
138
139 inline unsigned int
140 stdc_trailing_zeros_us(unsigned short __value)
141 { return stdc_trailing_zeros(__value); }
142
143 inline unsigned int
144 stdc_trailing_zeros_ui(unsigned int __value)
145 { return stdc_trailing_zeros(__value); }
146
147 inline unsigned int
148 stdc_trailing_zeros_ul(unsigned long int __value)
149 { return stdc_trailing_zeros(__value); }
150
151 inline unsigned int
152 stdc_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 */
162 template<typename _Tp>
163 inline unsigned int
164 stdc_trailing_ones(_Tp __value)
165 {
166 static_assert(std::__unsigned_integer<_Tp>);
167 return std::countr_one(__value);
168 }
169
170 inline unsigned int
171 stdc_trailing_ones_uc(unsigned char __value)
172 { return stdc_trailing_ones(__value); }
173
174 inline unsigned int
175 stdc_trailing_ones_us(unsigned short __value)
176 { return stdc_trailing_ones(__value); }
177
178 inline unsigned int
179 stdc_trailing_ones_ui(unsigned int __value)
180 { return stdc_trailing_ones(__value); }
181
182 inline unsigned int
183 stdc_trailing_ones_ul(unsigned long int __value)
184 { return stdc_trailing_ones(__value); }
185
186 inline unsigned int
187 stdc_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 */
199 template<typename _Tp>
200 inline unsigned int
201 stdc_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
207 inline unsigned int
208 stdc_first_leading_zero_uc(unsigned char __value)
209 { return stdc_first_leading_zero(__value); }
210
211 inline unsigned int
212 stdc_first_leading_zero_us(unsigned short __value)
213 { return stdc_first_leading_zero(__value); }
214
215 inline unsigned int
216 stdc_first_leading_zero_ui(unsigned int __value)
217 { return stdc_first_leading_zero(__value); }
218
219 inline unsigned int
220 stdc_first_leading_zero_ul(unsigned long int __value)
221 { return stdc_first_leading_zero(__value); }
222
223 inline unsigned int
224 stdc_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 */
236 template<typename _Tp>
237 inline unsigned int
238 stdc_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
244 inline unsigned int
245 stdc_first_leading_one_uc(unsigned char __value)
246 { return stdc_first_leading_one(__value); }
247
248 inline unsigned int
249 stdc_first_leading_one_us(unsigned short __value)
250 { return stdc_first_leading_one(__value); }
251
252 inline unsigned int
253 stdc_first_leading_one_ui(unsigned int __value)
254 { return stdc_first_leading_one(__value); }
255
256 inline unsigned int
257 stdc_first_leading_one_ul(unsigned long int __value)
258 { return stdc_first_leading_one(__value); }
259
260 inline unsigned int
261 stdc_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 */
273 template<typename _Tp>
274 inline unsigned int
275 stdc_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
281 inline unsigned int
282 stdc_first_trailing_zero_uc(unsigned char __value)
283 { return stdc_first_trailing_zero(__value); }
284
285 inline unsigned int
286 stdc_first_trailing_zero_us(unsigned short __value)
287 { return stdc_first_trailing_zero(__value); }
288
289 inline unsigned int
290 stdc_first_trailing_zero_ui(unsigned int __value)
291 { return stdc_first_trailing_zero(__value); }
292
293 inline unsigned int
294 stdc_first_trailing_zero_ul(unsigned long int __value)
295 { return stdc_first_trailing_zero(__value); }
296
297 inline unsigned int
298 stdc_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 */
310 template<typename _Tp>
311 inline unsigned int
312 stdc_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
318 inline unsigned int
319 stdc_first_trailing_one_uc(unsigned char __value)
320 { return stdc_first_trailing_one(__value); }
321
322 inline unsigned int
323 stdc_first_trailing_one_us(unsigned short __value)
324 { return stdc_first_trailing_one(__value); }
325
326 inline unsigned int
327 stdc_first_trailing_one_ui(unsigned int __value)
328 { return stdc_first_trailing_one(__value); }
329
330 inline unsigned int
331 stdc_first_trailing_one_ul(unsigned long int __value)
332 { return stdc_first_trailing_one(__value); }
333
334 inline unsigned int
335 stdc_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 */
346 template<typename _Tp>
347 inline unsigned int
348 stdc_count_zeros(_Tp __value)
349 {
350 static_assert(std::__unsigned_integer<_Tp>);
351 return std::popcount(_Tp(~__value));
352 }
353
354 inline unsigned int
355 stdc_count_zeros_uc(unsigned char __value)
356 { return stdc_count_zeros(__value); }
357
358 inline unsigned int
359 stdc_count_zeros_us(unsigned short __value)
360 { return stdc_count_zeros(__value); }
361
362 inline unsigned int
363 stdc_count_zeros_ui(unsigned int __value)
364 { return stdc_count_zeros(__value); }
365
366 inline unsigned int
367 stdc_count_zeros_ul(unsigned long int __value)
368 { return stdc_count_zeros(__value); }
369
370 inline unsigned int
371 stdc_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 */
382 template<typename _Tp>
383 inline unsigned int
384 stdc_count_ones(_Tp __value)
385 {
386 static_assert(std::__unsigned_integer<_Tp>);
387 return std::popcount(__value);
388 }
389
390 inline unsigned int
391 stdc_count_ones_uc(unsigned char __value)
392 { return stdc_count_ones(__value); }
393
394 inline unsigned int
395 stdc_count_ones_us(unsigned short __value)
396 { return stdc_count_ones(__value); }
397
398 inline unsigned int
399 stdc_count_ones_ui(unsigned int __value)
400 { return stdc_count_ones(__value); }
401
402 inline unsigned int
403 stdc_count_ones_ul(unsigned long int __value)
404 { return stdc_count_ones(__value); }
405
406 inline unsigned int
407 stdc_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 */
418 template<typename _Tp>
419 inline bool
420 stdc_has_single_bit(_Tp __value)
421 {
422 static_assert(std::__unsigned_integer<_Tp>);
423 return std::has_single_bit(__value);
424 }
425
426 inline bool
427 stdc_has_single_bit_uc(unsigned char __value)
428 { return stdc_has_single_bit(__value); }
429
430 inline bool
431 stdc_has_single_bit_us(unsigned short __value)
432 { return stdc_has_single_bit(__value); }
433
434 inline bool
435 stdc_has_single_bit_ui(unsigned int __value)
436 { return stdc_has_single_bit(__value); }
437
438 inline bool
439 stdc_has_single_bit_ul(unsigned long int __value)
440 { return stdc_has_single_bit(__value); }
441
442 inline bool
443 stdc_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 */
454 template<typename _Tp>
455 inline unsigned int
456 stdc_bit_width(_Tp __value)
457 {
458 static_assert(std::__unsigned_integer<_Tp>);
459 return std::bit_width(__value);
460 }
461
462 inline unsigned int
463 stdc_bit_width_uc(unsigned char __value)
464 { return stdc_bit_width(__value); }
465
466 inline unsigned int
467 stdc_bit_width_us(unsigned short __value)
468 { return stdc_bit_width(__value); }
469
470 inline unsigned int
471 stdc_bit_width_ui(unsigned int __value)
472 { return stdc_bit_width(__value); }
473
474 inline unsigned int
475 stdc_bit_width_ul(unsigned long int __value)
476 { return stdc_bit_width(__value); }
477
478 inline unsigned int
479 stdc_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 */
490 template<typename _Tp>
491 inline _Tp
492 stdc_bit_floor(_Tp __value)
493 {
494 static_assert(std::__unsigned_integer<_Tp>);
495 return std::bit_floor(__value);
496 }
497
498 inline unsigned char
499 stdc_bit_floor_uc(unsigned char __value)
500 { return stdc_bit_floor(__value); }
501
502 inline unsigned short
503 stdc_bit_floor_us(unsigned short __value)
504 { return stdc_bit_floor(__value); }
505
506 inline unsigned int
507 stdc_bit_floor_ui(unsigned int __value)
508 { return stdc_bit_floor(__value); }
509
510 inline unsigned long int
511 stdc_bit_floor_ul(unsigned long int __value)
512 { return stdc_bit_floor(__value); }
513
514 inline unsigned long long int
515 stdc_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 */
529 template<typename _Tp>
530 inline _Tp
531 stdc_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
538 inline unsigned char
539 stdc_bit_ceil_uc(unsigned char __value)
540 { return stdc_bit_ceil(__value); }
541
542 inline unsigned short
543 stdc_bit_ceil_us(unsigned short __value)
544 { return stdc_bit_ceil(__value); }
545
546 inline unsigned int
547 stdc_bit_ceil_ui(unsigned int __value)
548 { return stdc_bit_ceil(__value); }
549
550 inline unsigned long int
551 stdc_bit_ceil_ul(unsigned long int __value)
552 { return stdc_bit_ceil(__value); }
553
554 inline unsigned long long int
555 stdc_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