]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/bit
Daily bump.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / bit
CommitLineData
f3e91052
JW
1// <bit> -*- C++ -*-
2
6441eb6d 3// Copyright (C) 2018-2025 Free Software Foundation, Inc.
f3e91052
JW
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/bit
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_BIT
30#define _GLIBCXX_BIT 1
31
63a598de 32#ifdef _GLIBCXX_SYSHDR
f3e91052 33#pragma GCC system_header
63a598de 34#endif
f3e91052
JW
35
36#if __cplusplus >= 201402L
37
fd5a858e 38#include <concepts> // for std::integral
f3e91052 39#include <type_traits>
474cb5a0 40
cf0fded5 41#if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>)
474cb5a0
JW
42# include <ext/numeric_traits.h>
43#else
44# include <limits>
45/// @cond undocumented
46namespace __gnu_cxx
47{
48 template<typename _Tp>
49 struct __int_traits
50 {
51 static constexpr int __digits = std::numeric_limits<_Tp>::digits;
52 static constexpr _Tp __max = std::numeric_limits<_Tp>::max();
53 };
54}
55/// @endcond
56#endif
f3e91052 57
083b7f28
AA
58#define __glibcxx_want_bit_cast
59#define __glibcxx_want_byteswap
60#define __glibcxx_want_bitops
61#define __glibcxx_want_int_pow2
62#define __glibcxx_want_endian
63#include <bits/version.h>
64
f3e91052
JW
65namespace std _GLIBCXX_VISIBILITY(default)
66{
67_GLIBCXX_BEGIN_NAMESPACE_VERSION
68
27e6c1f4
JW
69 /**
70 * @defgroup bit_manip Bit manipulation
71 * @ingroup numerics
72 *
73 * Utilities for examining and manipulating individual bits.
74 *
75 * @{
76 */
77
083b7f28 78#ifdef __cpp_lib_bit_cast // C++ >= 20
9e433b34
JW
79
80 /// Create a value of type `To` from the bits of `from`.
674ec679
JW
81 /**
82 * @tparam _To A trivially-copyable type.
83 * @param __from A trivially-copyable object of the same size as `_To`.
84 * @return An object of type `_To`.
85 * @since C++20
86 */
9e433b34 87 template<typename _To, typename _From>
406f58e1 88 [[nodiscard]]
9e433b34
JW
89 constexpr _To
90 bit_cast(const _From& __from) noexcept
4894d69a
JW
91#ifdef __cpp_concepts
92 requires (sizeof(_To) == sizeof(_From))
c3f281a0 93 && is_trivially_copyable_v<_To> && is_trivially_copyable_v<_From>
4894d69a 94#endif
9e433b34
JW
95 {
96 return __builtin_bit_cast(_To, __from);
97 }
083b7f28 98#endif // __cpp_lib_bit_cast
9e433b34 99
083b7f28 100#ifdef __cpp_lib_byteswap // C++ >= 23
7393fa8b
JJ
101
102 /// Reverse order of bytes in the object representation of `value`.
674ec679
JW
103 /**
104 * @tparam _Tp An integral type.
105 * @param __value An object of integer type.
106 * @return An object of the same type, with the bytes reversed.
107 * @since C++23
108 */
fd5a858e 109 template<integral _Tp>
92084a6d 110 [[nodiscard]]
fd5a858e 111 constexpr _Tp
7393fa8b
JJ
112 byteswap(_Tp __value) noexcept
113 {
114 if constexpr (sizeof(_Tp) == 1)
115 return __value;
116#if __cpp_if_consteval >= 202106L && __CHAR_BIT__ == 8
117 if !consteval
118 {
119 if constexpr (sizeof(_Tp) == 2)
120 return __builtin_bswap16(__value);
121 if constexpr (sizeof(_Tp) == 4)
122 return __builtin_bswap32(__value);
123 if constexpr (sizeof(_Tp) == 8)
124 return __builtin_bswap64(__value);
125 if constexpr (sizeof(_Tp) == 16)
126#if __has_builtin(__builtin_bswap128)
127 return __builtin_bswap128(__value);
128#else
129 return (__builtin_bswap64(__value >> 64)
130 | (static_cast<_Tp>(__builtin_bswap64(__value)) << 64));
131#endif
132 }
133#endif
134
135 // Fallback implementation that handles even __int24 etc.
136 using _Up = typename __make_unsigned<__remove_cv_t<_Tp>>::__type;
137 size_t __diff = __CHAR_BIT__ * (sizeof(_Tp) - 1);
138 _Up __mask1 = static_cast<unsigned char>(~0);
139 _Up __mask2 = __mask1 << __diff;
140 _Up __val = __value;
141 for (size_t __i = 0; __i < sizeof(_Tp) / 2; ++__i)
142 {
143 _Up __byte1 = __val & __mask1;
144 _Up __byte2 = __val & __mask2;
145 __val = (__val ^ __byte1 ^ __byte2
146 ^ (__byte1 << __diff) ^ (__byte2 >> __diff));
147 __mask1 <<= __CHAR_BIT__;
148 __mask2 >>= __CHAR_BIT__;
149 __diff -= 2 * __CHAR_BIT__;
150 }
151 return __val;
152 }
083b7f28 153#endif // __cpp_lib_byteswap
7393fa8b 154
30f6aace 155 /// @cond undocumented
77687bc0
JW
156#pragma GCC diagnostic push
157#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
27e6c1f4 158
f3e91052
JW
159 template<typename _Tp>
160 constexpr _Tp
f35da524 161 __rotl(_Tp __x, int __s) noexcept
f3e91052 162 {
eb04805b 163 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
77687bc0 164 if constexpr ((_Nd & (_Nd - 1)) == 0)
84185598
JJ
165 {
166 // Variant for power of two _Nd which the compiler can
167 // easily pattern match.
168 constexpr unsigned __uNd = _Nd;
f12151e5 169 const auto __r = static_cast<unsigned>(__s);
84185598
JJ
170 return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
171 }
f35da524
JW
172 const int __r = __s % _Nd;
173 if (__r == 0)
174 return __x;
175 else if (__r > 0)
176 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
177 else
178 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
f3e91052
JW
179 }
180
181 template<typename _Tp>
182 constexpr _Tp
f35da524 183 __rotr(_Tp __x, int __s) noexcept
f3e91052 184 {
eb04805b 185 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
77687bc0 186 if constexpr ((_Nd & (_Nd - 1)) == 0)
84185598
JJ
187 {
188 // Variant for power of two _Nd which the compiler can
189 // easily pattern match.
190 constexpr unsigned __uNd = _Nd;
f12151e5 191 const auto __r = static_cast<unsigned>(__s);
84185598
JJ
192 return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
193 }
f35da524
JW
194 const int __r = __s % _Nd;
195 if (__r == 0)
196 return __x;
197 else if (__r > 0)
198 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
199 else
200 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
f3e91052
JW
201 }
202
203 template<typename _Tp>
204 constexpr int
205 __countl_zero(_Tp __x) noexcept
206 {
eb04805b
JW
207 using __gnu_cxx::__int_traits;
208 constexpr auto _Nd = __int_traits<_Tp>::__digits;
f3e91052 209
e8ad697a
JW
210#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clzg)
211 return __builtin_clzg(__x, _Nd);
212#else
f3e91052 213 if (__x == 0)
337dc307 214 return _Nd;
f3e91052 215
eb04805b
JW
216 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
217 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
218 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
f3e91052 219
77687bc0 220 if constexpr (_Nd <= _Nd_u)
f3e91052 221 {
337dc307 222 constexpr int __diff = _Nd_u - _Nd;
f3e91052
JW
223 return __builtin_clz(__x) - __diff;
224 }
77687bc0 225 else if constexpr (_Nd <= _Nd_ul)
f3e91052 226 {
337dc307 227 constexpr int __diff = _Nd_ul - _Nd;
f3e91052
JW
228 return __builtin_clzl(__x) - __diff;
229 }
77687bc0 230 else if constexpr (_Nd <= _Nd_ull)
f3e91052 231 {
337dc307 232 constexpr int __diff = _Nd_ull - _Nd;
f3e91052
JW
233 return __builtin_clzll(__x) - __diff;
234 }
337dc307 235 else // (_Nd > _Nd_ull)
f3e91052 236 {
337dc307 237 static_assert(_Nd <= (2 * _Nd_ull),
f3e91052
JW
238 "Maximum supported integer size is 128-bit");
239
337dc307 240 unsigned long long __high = __x >> _Nd_ull;
f3e91052
JW
241 if (__high != 0)
242 {
337dc307 243 constexpr int __diff = (2 * _Nd_ull) - _Nd;
f3e91052
JW
244 return __builtin_clzll(__high) - __diff;
245 }
eb04805b 246 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
337dc307
JW
247 unsigned long long __low = __x & __max_ull;
248 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
f3e91052 249 }
e8ad697a 250#endif
f3e91052
JW
251 }
252
253 template<typename _Tp>
254 constexpr int
255 __countl_one(_Tp __x) noexcept
256 {
f3e91052
JW
257 return std::__countl_zero<_Tp>((_Tp)~__x);
258 }
259
260 template<typename _Tp>
261 constexpr int
262 __countr_zero(_Tp __x) noexcept
263 {
eb04805b
JW
264 using __gnu_cxx::__int_traits;
265 constexpr auto _Nd = __int_traits<_Tp>::__digits;
f3e91052 266
e8ad697a
JW
267#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_ctzg)
268 return __builtin_ctzg(__x, _Nd);
269#else
f3e91052 270 if (__x == 0)
337dc307 271 return _Nd;
f3e91052 272
eb04805b
JW
273 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
274 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
275 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
f3e91052 276
77687bc0 277 if constexpr (_Nd <= _Nd_u)
f3e91052 278 return __builtin_ctz(__x);
77687bc0 279 else if constexpr (_Nd <= _Nd_ul)
f3e91052 280 return __builtin_ctzl(__x);
77687bc0 281 else if constexpr (_Nd <= _Nd_ull)
f3e91052 282 return __builtin_ctzll(__x);
337dc307 283 else // (_Nd > _Nd_ull)
f3e91052 284 {
337dc307 285 static_assert(_Nd <= (2 * _Nd_ull),
f3e91052
JW
286 "Maximum supported integer size is 128-bit");
287
eb04805b 288 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
337dc307 289 unsigned long long __low = __x & __max_ull;
f3e91052
JW
290 if (__low != 0)
291 return __builtin_ctzll(__low);
337dc307
JW
292 unsigned long long __high = __x >> _Nd_ull;
293 return __builtin_ctzll(__high) + _Nd_ull;
f3e91052 294 }
e8ad697a 295#endif
f3e91052
JW
296 }
297
298 template<typename _Tp>
299 constexpr int
300 __countr_one(_Tp __x) noexcept
301 {
f3e91052
JW
302 return std::__countr_zero((_Tp)~__x);
303 }
304
305 template<typename _Tp>
306 constexpr int
307 __popcount(_Tp __x) noexcept
308 {
e8ad697a
JW
309#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_popcountg)
310 return __builtin_popcountg(__x);
311#else
eb04805b
JW
312 using __gnu_cxx::__int_traits;
313 constexpr auto _Nd = __int_traits<_Tp>::__digits;
f3e91052 314
eb04805b
JW
315 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
316 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
317 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
f3e91052 318
77687bc0 319 if constexpr (_Nd <= _Nd_u)
f3e91052 320 return __builtin_popcount(__x);
77687bc0 321 else if constexpr (_Nd <= _Nd_ul)
f3e91052 322 return __builtin_popcountl(__x);
77687bc0 323 else if constexpr (_Nd <= _Nd_ull)
f3e91052 324 return __builtin_popcountll(__x);
337dc307 325 else // (_Nd > _Nd_ull)
f3e91052 326 {
337dc307 327 static_assert(_Nd <= (2 * _Nd_ull),
f3e91052
JW
328 "Maximum supported integer size is 128-bit");
329
eb04805b 330 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
337dc307
JW
331 unsigned long long __low = __x & __max_ull;
332 unsigned long long __high = __x >> _Nd_ull;
f3e91052
JW
333 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
334 }
e8ad697a 335#endif
f3e91052
JW
336 }
337
338 template<typename _Tp>
339 constexpr bool
9866abe3 340 __has_single_bit(_Tp __x) noexcept
f3e91052
JW
341 { return std::__popcount(__x) == 1; }
342
343 template<typename _Tp>
344 constexpr _Tp
9866abe3 345 __bit_ceil(_Tp __x) noexcept
f3e91052 346 {
eb04805b
JW
347 using __gnu_cxx::__int_traits;
348 constexpr auto _Nd = __int_traits<_Tp>::__digits;
2fb17d2d 349 if (__x == 0 || __x == 1)
f3e91052 350 return 1;
281ab2fb
JW
351 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
352 // If the shift exponent equals _Nd then the correct result is not
353 // representable as a value of _Tp, and so the result is undefined.
354 // Want that undefined behaviour to be detected in constant expressions,
355 // by UBSan, and by debug assertions.
74d14778 356 if (!std::__is_constant_evaluated())
3b39526e 357 {
eb04805b 358 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
3b39526e 359 }
74d14778 360
281ab2fb 361 using __promoted_type = decltype(__x << 1);
77687bc0 362 if constexpr (!is_same<__promoted_type, _Tp>::value)
281ab2fb
JW
363 {
364 // If __x undergoes integral promotion then shifting by _Nd is
365 // not undefined. In order to make the shift undefined, so that
366 // it is diagnosed in constant expressions and by UBsan, we also
367 // need to "promote" the shift exponent to be too large for the
368 // promoted type.
369 const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
370 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
371 }
372 return (_Tp)1u << __shift_exponent;
f3e91052
JW
373 }
374
375 template<typename _Tp>
376 constexpr _Tp
9866abe3 377 __bit_floor(_Tp __x) noexcept
f3e91052 378 {
eb04805b 379 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
f3e91052
JW
380 if (__x == 0)
381 return 0;
382 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
383 }
384
385 template<typename _Tp>
45813280 386 constexpr int
9866abe3 387 __bit_width(_Tp __x) noexcept
f3e91052 388 {
eb04805b 389 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
f3e91052
JW
390 return _Nd - std::__countl_zero(__x);
391 }
392
77687bc0 393#pragma GCC diagnostic pop
27e6c1f4
JW
394 /// @endcond
395
083b7f28 396#ifdef __cpp_lib_bitops // C++ >= 20
0d58d88d 397
30f6aace 398 /// @cond undocumented
fd5a858e
PP
399 template<typename _Tp>
400 concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
27e6c1f4 401 /// @endcond
f3e91052 402
f35da524 403 // [bit.rot], rotating
f3e91052 404
27e6c1f4 405 /// Rotate `x` to the left by `s` bits.
fd5a858e
PP
406 template<__unsigned_integer _Tp>
407 [[nodiscard]] constexpr _Tp
f35da524 408 rotl(_Tp __x, int __s) noexcept
f3e91052
JW
409 { return std::__rotl(__x, __s); }
410
27e6c1f4 411 /// Rotate `x` to the right by `s` bits.
fd5a858e
PP
412 template<__unsigned_integer _Tp>
413 [[nodiscard]] constexpr _Tp
f35da524 414 rotr(_Tp __x, int __s) noexcept
f3e91052
JW
415 { return std::__rotr(__x, __s); }
416
f35da524 417 // [bit.count], counting
f3e91052 418
27e6c1f4 419 /// The number of contiguous zero bits, starting from the highest bit.
fd5a858e
PP
420 template<__unsigned_integer _Tp>
421 constexpr int
f3e91052
JW
422 countl_zero(_Tp __x) noexcept
423 { return std::__countl_zero(__x); }
424
27e6c1f4 425 /// The number of contiguous one bits, starting from the highest bit.
fd5a858e
PP
426 template<__unsigned_integer _Tp>
427 constexpr int
f3e91052
JW
428 countl_one(_Tp __x) noexcept
429 { return std::__countl_one(__x); }
430
27e6c1f4 431 /// The number of contiguous zero bits, starting from the lowest bit.
fd5a858e
PP
432 template<__unsigned_integer _Tp>
433 constexpr int
f3e91052
JW
434 countr_zero(_Tp __x) noexcept
435 { return std::__countr_zero(__x); }
436
27e6c1f4 437 /// The number of contiguous one bits, starting from the lowest bit.
fd5a858e
PP
438 template<__unsigned_integer _Tp>
439 constexpr int
f3e91052
JW
440 countr_one(_Tp __x) noexcept
441 { return std::__countr_one(__x); }
442
27e6c1f4 443 /// The number of bits set in `x`.
fd5a858e
PP
444 template<__unsigned_integer _Tp>
445 constexpr int
f3e91052
JW
446 popcount(_Tp __x) noexcept
447 { return std::__popcount(__x); }
083b7f28 448#endif // __cpp_lib_bitops
f3e91052 449
083b7f28 450#ifdef __cpp_lib_int_pow2 // C++ >= 20
f35da524 451 // [bit.pow.two], integral powers of 2
f3e91052 452
27e6c1f4 453 /// True if `x` is a power of two, false otherwise.
fd5a858e
PP
454 template<__unsigned_integer _Tp>
455 constexpr bool
9866abe3
JW
456 has_single_bit(_Tp __x) noexcept
457 { return std::__has_single_bit(__x); }
f3e91052 458
27e6c1f4 459 /// The smallest power-of-two not less than `x`.
fd5a858e
PP
460 template<__unsigned_integer _Tp>
461 constexpr _Tp
9866abe3
JW
462 bit_ceil(_Tp __x) noexcept
463 { return std::__bit_ceil(__x); }
f3e91052 464
27e6c1f4 465 /// The largest power-of-two not greater than `x`.
fd5a858e
PP
466 template<__unsigned_integer _Tp>
467 constexpr _Tp
9866abe3
JW
468 bit_floor(_Tp __x) noexcept
469 { return std::__bit_floor(__x); }
f3e91052 470
45813280
JW
471 // _GLIBCXX_RESOLVE_LIB_DEFECTS
472 // 3656. Inconsistent bit operations returning a count
27e6c1f4 473 /// The smallest integer greater than the base-2 logarithm of `x`.
fd5a858e
PP
474 template<__unsigned_integer _Tp>
475 constexpr int
9866abe3
JW
476 bit_width(_Tp __x) noexcept
477 { return std::__bit_width(__x); }
083b7f28 478#endif // defined (__cpp_lib_int_pow2)
f3e91052 479
083b7f28 480#ifdef __cpp_lib_endian // C++ >= 20
a5378f9b 481
674ec679
JW
482 /// Byte order constants
483 /**
484 * The platform endianness can be checked by comparing `std::endian::native`
485 * to one of `std::endian::big` or `std::endian::little`.
486 *
487 * @since C++20
488 */
45c7215c
JW
489 enum class endian
490 {
491 little = __ORDER_LITTLE_ENDIAN__,
492 big = __ORDER_BIG_ENDIAN__,
493 native = __BYTE_ORDER__
494 };
083b7f28 495#endif // __cpp_lib_endian
f3e91052 496
27e6c1f4
JW
497 /// @}
498
f3e91052
JW
499_GLIBCXX_END_NAMESPACE_VERSION
500} // namespace std
501
502#endif // C++14
503#endif // _GLIBCXX_BIT