30 #define _GLIBCXX_BIT 1 32 #ifdef _GLIBCXX_SYSHDR 33 #pragma GCC system_header 36 #if __cplusplus >= 201402L 41 #if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>) 48 template<
typename _Tp>
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 65 namespace std _GLIBCXX_VISIBILITY(default)
67 _GLIBCXX_BEGIN_NAMESPACE_VERSION
78 #ifdef __cpp_lib_bit_cast // C++ >= 20 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 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));
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 156 #pragma GCC diagnostic push 157 #pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr 159 template<
typename _Tp>
161 __rotl(_Tp __x,
int __s) noexcept
164 if constexpr ((_Nd & (_Nd - 1)) == 0)
168 constexpr
unsigned __uNd = _Nd;
169 const auto __r =
static_cast<unsigned>(__s);
170 return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
172 const int __r = __s % _Nd;
176 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
178 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd));
181 template<
typename _Tp>
183 __rotr(_Tp __x,
int __s) noexcept
186 if constexpr ((_Nd & (_Nd - 1)) == 0)
190 constexpr
unsigned __uNd = _Nd;
191 const auto __r =
static_cast<unsigned>(__s);
192 return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
194 const int __r = __s % _Nd;
198 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
200 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd));
203 template<
typename _Tp>
205 __countl_zero(_Tp __x) noexcept
208 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
210 #if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clzg) 211 return __builtin_clzg(__x, _Nd);
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;
220 if constexpr (_Nd <= _Nd_u)
222 constexpr
int __diff = _Nd_u - _Nd;
223 return __builtin_clz(__x) - __diff;
225 else if constexpr (_Nd <= _Nd_ul)
227 constexpr
int __diff = _Nd_ul - _Nd;
228 return __builtin_clzl(__x) - __diff;
230 else if constexpr (_Nd <= _Nd_ull)
232 constexpr
int __diff = _Nd_ull - _Nd;
233 return __builtin_clzll(__x) - __diff;
237 static_assert(_Nd <= (2 * _Nd_ull),
238 "Maximum supported integer size is 128-bit");
240 unsigned long long __high = __x >> _Nd_ull;
243 constexpr
int __diff = (2 * _Nd_ull) - _Nd;
244 return __builtin_clzll(__high) - __diff;
246 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
247 unsigned long long __low = __x & __max_ull;
248 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
253 template<
typename _Tp>
255 __countl_one(_Tp __x) noexcept
257 return std::__countl_zero<_Tp>((_Tp)~__x);
260 template<
typename _Tp>
262 __countr_zero(_Tp __x) noexcept
265 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
267 #if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_ctzg) 268 return __builtin_ctzg(__x, _Nd);
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;
277 if constexpr (_Nd <= _Nd_u)
278 return __builtin_ctz(__x);
279 else if constexpr (_Nd <= _Nd_ul)
280 return __builtin_ctzl(__x);
281 else if constexpr (_Nd <= _Nd_ull)
282 return __builtin_ctzll(__x);
285 static_assert(_Nd <= (2 * _Nd_ull),
286 "Maximum supported integer size is 128-bit");
288 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
289 unsigned long long __low = __x & __max_ull;
291 return __builtin_ctzll(__low);
292 unsigned long long __high = __x >> _Nd_ull;
293 return __builtin_ctzll(__high) + _Nd_ull;
298 template<
typename _Tp>
300 __countr_one(_Tp __x) noexcept
302 return std::__countr_zero((_Tp)~__x);
305 template<
typename _Tp>
307 __popcount(_Tp __x) noexcept
309 #if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_popcountg) 310 return __builtin_popcountg(__x);
313 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
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;
319 if constexpr (_Nd <= _Nd_u)
320 return __builtin_popcount(__x);
321 else if constexpr (_Nd <= _Nd_ul)
322 return __builtin_popcountl(__x);
323 else if constexpr (_Nd <= _Nd_ull)
324 return __builtin_popcountll(__x);
327 static_assert(_Nd <= (2 * _Nd_ull),
328 "Maximum supported integer size is 128-bit");
330 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
331 unsigned long long __low = __x & __max_ull;
332 unsigned long long __high = __x >> _Nd_ull;
333 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
338 template<
typename _Tp>
340 __has_single_bit(_Tp __x) noexcept
341 {
return std::__popcount(__x) == 1; }
343 template<
typename _Tp>
345 __bit_ceil(_Tp __x) noexcept
348 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
349 if (__x == 0 || __x == 1)
351 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
356 if (!std::__is_constant_evaluated())
358 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
361 using __promoted_type = decltype(__x << 1);
362 if constexpr (!is_same<__promoted_type, _Tp>::value)
369 const int __extra_exp =
sizeof(__promoted_type) /
sizeof(_Tp) / 2;
370 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
372 return (_Tp)1u << __shift_exponent;
375 template<
typename _Tp>
377 __bit_floor(_Tp __x) noexcept
382 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
385 template<
typename _Tp>
387 __bit_width(_Tp __x) noexcept
390 return _Nd - std::__countl_zero(__x);
393 #pragma GCC diagnostic pop 396 #ifdef __cpp_lib_bitops // C++ >= 20 399 template<
typename _Tp>
400 concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
406 template<__
unsigned_
integer _Tp>
407 [[nodiscard]] constexpr _Tp
408 rotl(_Tp __x,
int __s) noexcept
409 {
return std::__rotl(__x, __s); }
412 template<__
unsigned_
integer _Tp>
413 [[nodiscard]] constexpr _Tp
414 rotr(_Tp __x,
int __s) noexcept
415 {
return std::__rotr(__x, __s); }
420 template<__
unsigned_
integer _Tp>
422 countl_zero(_Tp __x) noexcept
423 {
return std::__countl_zero(__x); }
426 template<__
unsigned_
integer _Tp>
428 countl_one(_Tp __x) noexcept
429 {
return std::__countl_one(__x); }
432 template<__
unsigned_
integer _Tp>
434 countr_zero(_Tp __x) noexcept
435 {
return std::__countr_zero(__x); }
438 template<__
unsigned_
integer _Tp>
440 countr_one(_Tp __x) noexcept
441 {
return std::__countr_one(__x); }
444 template<__
unsigned_
integer _Tp>
446 popcount(_Tp __x) noexcept
447 {
return std::__popcount(__x); }
448 #endif // __cpp_lib_bitops 450 #ifdef __cpp_lib_int_pow2 // C++ >= 20 454 template<__
unsigned_
integer _Tp>
456 has_single_bit(_Tp __x) noexcept
457 {
return std::__has_single_bit(__x); }
460 template<__
unsigned_
integer _Tp>
462 bit_ceil(_Tp __x) noexcept
463 {
return std::__bit_ceil(__x); }
466 template<__
unsigned_
integer _Tp>
468 bit_floor(_Tp __x) noexcept
469 {
return std::__bit_floor(__x); }
474 template<__
unsigned_
integer _Tp>
476 bit_width(_Tp __x) noexcept
477 {
return std::__bit_width(__x); }
478 #endif // defined (__cpp_lib_int_pow2) 480 #ifdef __cpp_lib_endian // C++ >= 20 491 little = __ORDER_LITTLE_ENDIAN__,
492 big = __ORDER_BIG_ENDIAN__,
493 native = __BYTE_ORDER__
495 #endif // __cpp_lib_endian 499 _GLIBCXX_END_NAMESPACE_VERSION
503 #endif // _GLIBCXX_BIT
GNU extensions for public use.
ISO C++ entities toplevel namespace is std.
Properties of fundamental types.
static constexpr _Tp max() noexcept
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.