3// Copyright (C) 2018-2024 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.
32#pragma GCC system_header
34#if __cplusplus >= 201402L
36#include <concepts> // for std::integral
39#if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>)
40# include <ext/numeric_traits.h>
46 template<typename _Tp>
49 static constexpr int __digits = std::numeric_limits<_Tp>::digits;
50 static constexpr _Tp __max = std::numeric_limits<_Tp>::max();
56#define __glibcxx_want_bit_cast
57#define __glibcxx_want_byteswap
58#define __glibcxx_want_bitops
59#define __glibcxx_want_int_pow2
60#define __glibcxx_want_endian
61#include <bits/version.h>
63namespace std _GLIBCXX_VISIBILITY(default)
65_GLIBCXX_BEGIN_NAMESPACE_VERSION
68 * @defgroup bit_manip Bit manipulation
71 * Utilities for examining and manipulating individual bits.
76#ifdef __cpp_lib_bit_cast // C++ >= 20
78 /// Create a value of type `To` from the bits of `from`.
80 * @tparam _To A trivially-copyable type.
81 * @param __from A trivially-copyable object of the same size as `_To`.
82 * @return An object of type `_To`.
85 template<typename _To, typename _From>
88 bit_cast(const _From& __from) noexcept
90 requires (sizeof(_To) == sizeof(_From))
91 && is_trivially_copyable_v<_To> && is_trivially_copyable_v<_From>
94 return __builtin_bit_cast(_To, __from);
96#endif // __cpp_lib_bit_cast
98#ifdef __cpp_lib_byteswap // C++ >= 23
100 /// Reverse order of bytes in the object representation of `value`.
102 * @tparam _Tp An integral type.
103 * @param __value An object of integer type.
104 * @return An object of the same type, with the bytes reversed.
107 template<integral _Tp>
110 byteswap(_Tp __value) noexcept
112 if constexpr (sizeof(_Tp) == 1)
114#if __cpp_if_consteval >= 202106L && __CHAR_BIT__ == 8
117 if constexpr (sizeof(_Tp) == 2)
118 return __builtin_bswap16(__value);
119 if constexpr (sizeof(_Tp) == 4)
120 return __builtin_bswap32(__value);
121 if constexpr (sizeof(_Tp) == 8)
122 return __builtin_bswap64(__value);
123 if constexpr (sizeof(_Tp) == 16)
124#if __has_builtin(__builtin_bswap128)
125 return __builtin_bswap128(__value);
127 return (__builtin_bswap64(__value >> 64)
128 | (static_cast<_Tp>(__builtin_bswap64(__value)) << 64));
133 // Fallback implementation that handles even __int24 etc.
134 using _Up = typename __make_unsigned<__remove_cv_t<_Tp>>::__type;
135 size_t __diff = __CHAR_BIT__ * (sizeof(_Tp) - 1);
136 _Up __mask1 = static_cast<unsigned char>(~0);
137 _Up __mask2 = __mask1 << __diff;
139 for (size_t __i = 0; __i < sizeof(_Tp) / 2; ++__i)
141 _Up __byte1 = __val & __mask1;
142 _Up __byte2 = __val & __mask2;
143 __val = (__val ^ __byte1 ^ __byte2
144 ^ (__byte1 << __diff) ^ (__byte2 >> __diff));
145 __mask1 <<= __CHAR_BIT__;
146 __mask2 >>= __CHAR_BIT__;
147 __diff -= 2 * __CHAR_BIT__;
151#endif // __cpp_lib_byteswap
153 /// @cond undocumented
155 template<typename _Tp>
157 __rotl(_Tp __x, int __s) noexcept
159 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
160 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
162 // Variant for power of two _Nd which the compiler can
163 // easily pattern match.
164 constexpr unsigned __uNd = _Nd;
165 const unsigned __r = __s;
166 return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
168 const int __r = __s % _Nd;
172 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
174 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
177 template<typename _Tp>
179 __rotr(_Tp __x, int __s) noexcept
181 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
182 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
184 // Variant for power of two _Nd which the compiler can
185 // easily pattern match.
186 constexpr unsigned __uNd = _Nd;
187 const unsigned __r = __s;
188 return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
190 const int __r = __s % _Nd;
194 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
196 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
199 template<typename _Tp>
201 __countl_zero(_Tp __x) noexcept
203 using __gnu_cxx::__int_traits;
204 constexpr auto _Nd = __int_traits<_Tp>::__digits;
209 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
210 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
211 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
213 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
215 constexpr int __diff = _Nd_u - _Nd;
216 return __builtin_clz(__x) - __diff;
218 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
220 constexpr int __diff = _Nd_ul - _Nd;
221 return __builtin_clzl(__x) - __diff;
223 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
225 constexpr int __diff = _Nd_ull - _Nd;
226 return __builtin_clzll(__x) - __diff;
228 else // (_Nd > _Nd_ull)
230 static_assert(_Nd <= (2 * _Nd_ull),
231 "Maximum supported integer size is 128-bit");
233 unsigned long long __high = __x >> _Nd_ull;
236 constexpr int __diff = (2 * _Nd_ull) - _Nd;
237 return __builtin_clzll(__high) - __diff;
239 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
240 unsigned long long __low = __x & __max_ull;
241 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
245 template<typename _Tp>
247 __countl_one(_Tp __x) noexcept
249 return std::__countl_zero<_Tp>((_Tp)~__x);
252 template<typename _Tp>
254 __countr_zero(_Tp __x) noexcept
256 using __gnu_cxx::__int_traits;
257 constexpr auto _Nd = __int_traits<_Tp>::__digits;
262 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
263 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
264 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
266 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
267 return __builtin_ctz(__x);
268 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
269 return __builtin_ctzl(__x);
270 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
271 return __builtin_ctzll(__x);
272 else // (_Nd > _Nd_ull)
274 static_assert(_Nd <= (2 * _Nd_ull),
275 "Maximum supported integer size is 128-bit");
277 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
278 unsigned long long __low = __x & __max_ull;
280 return __builtin_ctzll(__low);
281 unsigned long long __high = __x >> _Nd_ull;
282 return __builtin_ctzll(__high) + _Nd_ull;
286 template<typename _Tp>
288 __countr_one(_Tp __x) noexcept
290 return std::__countr_zero((_Tp)~__x);
293 template<typename _Tp>
295 __popcount(_Tp __x) noexcept
297 using __gnu_cxx::__int_traits;
298 constexpr auto _Nd = __int_traits<_Tp>::__digits;
300 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
301 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
302 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
304 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
305 return __builtin_popcount(__x);
306 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
307 return __builtin_popcountl(__x);
308 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
309 return __builtin_popcountll(__x);
310 else // (_Nd > _Nd_ull)
312 static_assert(_Nd <= (2 * _Nd_ull),
313 "Maximum supported integer size is 128-bit");
315 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
316 unsigned long long __low = __x & __max_ull;
317 unsigned long long __high = __x >> _Nd_ull;
318 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
322 template<typename _Tp>
324 __has_single_bit(_Tp __x) noexcept
325 { return std::__popcount(__x) == 1; }
327 template<typename _Tp>
329 __bit_ceil(_Tp __x) noexcept
331 using __gnu_cxx::__int_traits;
332 constexpr auto _Nd = __int_traits<_Tp>::__digits;
333 if (__x == 0 || __x == 1)
335 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
336 // If the shift exponent equals _Nd then the correct result is not
337 // representable as a value of _Tp, and so the result is undefined.
338 // Want that undefined behaviour to be detected in constant expressions,
339 // by UBSan, and by debug assertions.
340 if (!std::__is_constant_evaluated())
342 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
345 using __promoted_type = decltype(__x << 1);
346 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
348 // If __x undergoes integral promotion then shifting by _Nd is
349 // not undefined. In order to make the shift undefined, so that
350 // it is diagnosed in constant expressions and by UBsan, we also
351 // need to "promote" the shift exponent to be too large for the
353 const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
354 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
356 return (_Tp)1u << __shift_exponent;
359 template<typename _Tp>
361 __bit_floor(_Tp __x) noexcept
363 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
366 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
369 template<typename _Tp>
371 __bit_width(_Tp __x) noexcept
373 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
374 return _Nd - std::__countl_zero(__x);
379#ifdef __cpp_lib_bitops // C++ >= 20
381 /// @cond undocumented
382 template<typename _Tp>
383 concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
386 // [bit.rot], rotating
388 /// Rotate `x` to the left by `s` bits.
389 template<__unsigned_integer _Tp>
390 [[nodiscard]] constexpr _Tp
391 rotl(_Tp __x, int __s) noexcept
392 { return std::__rotl(__x, __s); }
394 /// Rotate `x` to the right by `s` bits.
395 template<__unsigned_integer _Tp>
396 [[nodiscard]] constexpr _Tp
397 rotr(_Tp __x, int __s) noexcept
398 { return std::__rotr(__x, __s); }
400 // [bit.count], counting
402 /// The number of contiguous zero bits, starting from the highest bit.
403 template<__unsigned_integer _Tp>
405 countl_zero(_Tp __x) noexcept
406 { return std::__countl_zero(__x); }
408 /// The number of contiguous one bits, starting from the highest bit.
409 template<__unsigned_integer _Tp>
411 countl_one(_Tp __x) noexcept
412 { return std::__countl_one(__x); }
414 /// The number of contiguous zero bits, starting from the lowest bit.
415 template<__unsigned_integer _Tp>
417 countr_zero(_Tp __x) noexcept
418 { return std::__countr_zero(__x); }
420 /// The number of contiguous one bits, starting from the lowest bit.
421 template<__unsigned_integer _Tp>
423 countr_one(_Tp __x) noexcept
424 { return std::__countr_one(__x); }
426 /// The number of bits set in `x`.
427 template<__unsigned_integer _Tp>
429 popcount(_Tp __x) noexcept
430 { return std::__popcount(__x); }
431#endif // __cpp_lib_bitops
433#ifdef __cpp_lib_int_pow2 // C++ >= 20
434 // [bit.pow.two], integral powers of 2
436 /// True if `x` is a power of two, false otherwise.
437 template<__unsigned_integer _Tp>
439 has_single_bit(_Tp __x) noexcept
440 { return std::__has_single_bit(__x); }
442 /// The smallest power-of-two not less than `x`.
443 template<__unsigned_integer _Tp>
445 bit_ceil(_Tp __x) noexcept
446 { return std::__bit_ceil(__x); }
448 /// The largest power-of-two not greater than `x`.
449 template<__unsigned_integer _Tp>
451 bit_floor(_Tp __x) noexcept
452 { return std::__bit_floor(__x); }
454 // _GLIBCXX_RESOLVE_LIB_DEFECTS
455 // 3656. Inconsistent bit operations returning a count
456 /// The smallest integer greater than the base-2 logarithm of `x`.
457 template<__unsigned_integer _Tp>
459 bit_width(_Tp __x) noexcept
460 { return std::__bit_width(__x); }
461#endif // defined (__cpp_lib_int_pow2)
463#ifdef __cpp_lib_endian // C++ >= 20
465 /// Byte order constants
467 * The platform endianness can be checked by comparing `std::endian::native`
468 * to one of `std::endian::big` or `std::endian::little`.
474 little = __ORDER_LITTLE_ENDIAN__,
475 big = __ORDER_BIG_ENDIAN__,
476 native = __BYTE_ORDER__
478#endif // __cpp_lib_endian
482_GLIBCXX_END_NAMESPACE_VERSION
486#endif // _GLIBCXX_BIT