3// Copyright (C) 2018-2025 Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 * This is a Standard C++ Library header.
33#pragma GCC system_header
36#if __cplusplus >= 201402L
38#include <concepts> // for std::integral
41#if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>)
42# include <ext/numeric_traits.h>
48 template<typename _Tp>
51 static constexpr int __digits = std::numeric_limits<_Tp>::digits;
52 static constexpr _Tp __max = std::numeric_limits<_Tp>::max();
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>
65namespace std _GLIBCXX_VISIBILITY(default)
67_GLIBCXX_BEGIN_NAMESPACE_VERSION
70 * @defgroup bit_manip Bit manipulation
73 * Utilities for examining and manipulating individual bits.
78#ifdef __cpp_lib_bit_cast // C++ >= 20
80 /// Create a value of type `To` from the bits of `from`.
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`.
87 template<typename _To, typename _From>
90 bit_cast(const _From& __from) noexcept
92 requires (sizeof(_To) == sizeof(_From))
93 && is_trivially_copyable_v<_To> && is_trivially_copyable_v<_From>
96 return __builtin_bit_cast(_To, __from);
98#endif // __cpp_lib_bit_cast
100#ifdef __cpp_lib_byteswap // C++ >= 23
102 /// Reverse order of bytes in the object representation of `value`.
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.
109 template<integral _Tp>
112 byteswap(_Tp __value) noexcept
114 if constexpr (sizeof(_Tp) == 1)
116#if __cpp_if_consteval >= 202106L && __CHAR_BIT__ == 8
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);
129 return (__builtin_bswap64(__value >> 64)
130 | (static_cast<_Tp>(__builtin_bswap64(__value)) << 64));
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;
141 for (size_t __i = 0; __i < sizeof(_Tp) / 2; ++__i)
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__;
153#endif // __cpp_lib_byteswap
155 /// @cond undocumented
157 template<typename _Tp>
159 __rotl(_Tp __x, int __s) noexcept
161 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
162 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
164 // Variant for power of two _Nd which the compiler can
165 // easily pattern match.
166 constexpr unsigned __uNd = _Nd;
167 const unsigned __r = __s;
168 return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
170 const int __r = __s % _Nd;
174 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
176 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
179 template<typename _Tp>
181 __rotr(_Tp __x, int __s) noexcept
183 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
184 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
186 // Variant for power of two _Nd which the compiler can
187 // easily pattern match.
188 constexpr unsigned __uNd = _Nd;
189 const unsigned __r = __s;
190 return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
192 const int __r = __s % _Nd;
196 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
198 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
201 template<typename _Tp>
203 __countl_zero(_Tp __x) noexcept
205 using __gnu_cxx::__int_traits;
206 constexpr auto _Nd = __int_traits<_Tp>::__digits;
211 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
212 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
213 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
215 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
217 constexpr int __diff = _Nd_u - _Nd;
218 return __builtin_clz(__x) - __diff;
220 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
222 constexpr int __diff = _Nd_ul - _Nd;
223 return __builtin_clzl(__x) - __diff;
225 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
227 constexpr int __diff = _Nd_ull - _Nd;
228 return __builtin_clzll(__x) - __diff;
230 else // (_Nd > _Nd_ull)
232 static_assert(_Nd <= (2 * _Nd_ull),
233 "Maximum supported integer size is 128-bit");
235 unsigned long long __high = __x >> _Nd_ull;
238 constexpr int __diff = (2 * _Nd_ull) - _Nd;
239 return __builtin_clzll(__high) - __diff;
241 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
242 unsigned long long __low = __x & __max_ull;
243 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
247 template<typename _Tp>
249 __countl_one(_Tp __x) noexcept
251 return std::__countl_zero<_Tp>((_Tp)~__x);
254 template<typename _Tp>
256 __countr_zero(_Tp __x) noexcept
258 using __gnu_cxx::__int_traits;
259 constexpr auto _Nd = __int_traits<_Tp>::__digits;
264 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
265 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
266 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
268 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
269 return __builtin_ctz(__x);
270 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
271 return __builtin_ctzl(__x);
272 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
273 return __builtin_ctzll(__x);
274 else // (_Nd > _Nd_ull)
276 static_assert(_Nd <= (2 * _Nd_ull),
277 "Maximum supported integer size is 128-bit");
279 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
280 unsigned long long __low = __x & __max_ull;
282 return __builtin_ctzll(__low);
283 unsigned long long __high = __x >> _Nd_ull;
284 return __builtin_ctzll(__high) + _Nd_ull;
288 template<typename _Tp>
290 __countr_one(_Tp __x) noexcept
292 return std::__countr_zero((_Tp)~__x);
295 template<typename _Tp>
297 __popcount(_Tp __x) noexcept
299 using __gnu_cxx::__int_traits;
300 constexpr auto _Nd = __int_traits<_Tp>::__digits;
302 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
303 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
304 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
306 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
307 return __builtin_popcount(__x);
308 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
309 return __builtin_popcountl(__x);
310 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
311 return __builtin_popcountll(__x);
312 else // (_Nd > _Nd_ull)
314 static_assert(_Nd <= (2 * _Nd_ull),
315 "Maximum supported integer size is 128-bit");
317 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
318 unsigned long long __low = __x & __max_ull;
319 unsigned long long __high = __x >> _Nd_ull;
320 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
324 template<typename _Tp>
326 __has_single_bit(_Tp __x) noexcept
327 { return std::__popcount(__x) == 1; }
329 template<typename _Tp>
331 __bit_ceil(_Tp __x) noexcept
333 using __gnu_cxx::__int_traits;
334 constexpr auto _Nd = __int_traits<_Tp>::__digits;
335 if (__x == 0 || __x == 1)
337 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
338 // If the shift exponent equals _Nd then the correct result is not
339 // representable as a value of _Tp, and so the result is undefined.
340 // Want that undefined behaviour to be detected in constant expressions,
341 // by UBSan, and by debug assertions.
342 if (!std::__is_constant_evaluated())
344 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
347 using __promoted_type = decltype(__x << 1);
348 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
350 // If __x undergoes integral promotion then shifting by _Nd is
351 // not undefined. In order to make the shift undefined, so that
352 // it is diagnosed in constant expressions and by UBsan, we also
353 // need to "promote" the shift exponent to be too large for the
355 const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
356 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
358 return (_Tp)1u << __shift_exponent;
361 template<typename _Tp>
363 __bit_floor(_Tp __x) noexcept
365 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
368 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
371 template<typename _Tp>
373 __bit_width(_Tp __x) noexcept
375 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
376 return _Nd - std::__countl_zero(__x);
381#ifdef __cpp_lib_bitops // C++ >= 20
383 /// @cond undocumented
384 template<typename _Tp>
385 concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
388 // [bit.rot], rotating
390 /// Rotate `x` to the left by `s` bits.
391 template<__unsigned_integer _Tp>
392 [[nodiscard]] constexpr _Tp
393 rotl(_Tp __x, int __s) noexcept
394 { return std::__rotl(__x, __s); }
396 /// Rotate `x` to the right by `s` bits.
397 template<__unsigned_integer _Tp>
398 [[nodiscard]] constexpr _Tp
399 rotr(_Tp __x, int __s) noexcept
400 { return std::__rotr(__x, __s); }
402 // [bit.count], counting
404 /// The number of contiguous zero bits, starting from the highest bit.
405 template<__unsigned_integer _Tp>
407 countl_zero(_Tp __x) noexcept
408 { return std::__countl_zero(__x); }
410 /// The number of contiguous one bits, starting from the highest bit.
411 template<__unsigned_integer _Tp>
413 countl_one(_Tp __x) noexcept
414 { return std::__countl_one(__x); }
416 /// The number of contiguous zero bits, starting from the lowest bit.
417 template<__unsigned_integer _Tp>
419 countr_zero(_Tp __x) noexcept
420 { return std::__countr_zero(__x); }
422 /// The number of contiguous one bits, starting from the lowest bit.
423 template<__unsigned_integer _Tp>
425 countr_one(_Tp __x) noexcept
426 { return std::__countr_one(__x); }
428 /// The number of bits set in `x`.
429 template<__unsigned_integer _Tp>
431 popcount(_Tp __x) noexcept
432 { return std::__popcount(__x); }
433#endif // __cpp_lib_bitops
435#ifdef __cpp_lib_int_pow2 // C++ >= 20
436 // [bit.pow.two], integral powers of 2
438 /// True if `x` is a power of two, false otherwise.
439 template<__unsigned_integer _Tp>
441 has_single_bit(_Tp __x) noexcept
442 { return std::__has_single_bit(__x); }
444 /// The smallest power-of-two not less than `x`.
445 template<__unsigned_integer _Tp>
447 bit_ceil(_Tp __x) noexcept
448 { return std::__bit_ceil(__x); }
450 /// The largest power-of-two not greater than `x`.
451 template<__unsigned_integer _Tp>
453 bit_floor(_Tp __x) noexcept
454 { return std::__bit_floor(__x); }
456 // _GLIBCXX_RESOLVE_LIB_DEFECTS
457 // 3656. Inconsistent bit operations returning a count
458 /// The smallest integer greater than the base-2 logarithm of `x`.
459 template<__unsigned_integer _Tp>
461 bit_width(_Tp __x) noexcept
462 { return std::__bit_width(__x); }
463#endif // defined (__cpp_lib_int_pow2)
465#ifdef __cpp_lib_endian // C++ >= 20
467 /// Byte order constants
469 * The platform endianness can be checked by comparing `std::endian::native`
470 * to one of `std::endian::big` or `std::endian::little`.
476 little = __ORDER_LITTLE_ENDIAN__,
477 big = __ORDER_BIG_ENDIAN__,
478 native = __BYTE_ORDER__
480#endif // __cpp_lib_endian
484_GLIBCXX_END_NAMESPACE_VERSION
488#endif // _GLIBCXX_BIT