32#pragma GCC system_header
34#if __cplusplus >= 201402L
39#if _GLIBCXX_HOSTED || __has_include(<ext/numeric_traits.h>)
46 template<
typename _Tp>
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
63namespace std _GLIBCXX_VISIBILITY(default)
65_GLIBCXX_BEGIN_NAMESPACE_VERSION
76#ifdef __cpp_lib_bit_cast
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);
98#ifdef __cpp_lib_byteswap
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));
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__;
155 template<
typename _Tp>
157 __rotl(_Tp __x,
int __s)
noexcept
160 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
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));
177 template<
typename _Tp>
179 __rotr(_Tp __x,
int __s)
noexcept
182 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
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));
199 template<
typename _Tp>
201 __countl_zero(_Tp __x)
noexcept
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;
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
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);
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
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);
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
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));
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)
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
366 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
369 template<
typename _Tp>
371 __bit_width(_Tp __x)
noexcept
374 return _Nd - std::__countl_zero(__x);
379#ifdef __cpp_lib_bitops
382 template<
typename _Tp>
383 concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
389 template<__
unsigned_
integer _Tp>
390 [[nodiscard]]
constexpr _Tp
391 rotl(_Tp __x,
int __s)
noexcept
392 {
return std::__rotl(__x, __s); }
395 template<__
unsigned_
integer _Tp>
396 [[nodiscard]]
constexpr _Tp
397 rotr(_Tp __x,
int __s)
noexcept
398 {
return std::__rotr(__x, __s); }
403 template<__
unsigned_
integer _Tp>
405 countl_zero(_Tp __x)
noexcept
406 {
return std::__countl_zero(__x); }
409 template<__
unsigned_
integer _Tp>
411 countl_one(_Tp __x)
noexcept
412 {
return std::__countl_one(__x); }
415 template<__
unsigned_
integer _Tp>
417 countr_zero(_Tp __x)
noexcept
418 {
return std::__countr_zero(__x); }
421 template<__
unsigned_
integer _Tp>
423 countr_one(_Tp __x)
noexcept
424 {
return std::__countr_one(__x); }
427 template<__
unsigned_
integer _Tp>
429 popcount(_Tp __x)
noexcept
430 {
return std::__popcount(__x); }
433#ifdef __cpp_lib_int_pow2
437 template<__
unsigned_
integer _Tp>
439 has_single_bit(_Tp __x)
noexcept
440 {
return std::__has_single_bit(__x); }
443 template<__
unsigned_
integer _Tp>
445 bit_ceil(_Tp __x)
noexcept
446 {
return std::__bit_ceil(__x); }
449 template<__
unsigned_
integer _Tp>
451 bit_floor(_Tp __x)
noexcept
452 {
return std::__bit_floor(__x); }
457 template<__
unsigned_
integer _Tp>
459 bit_width(_Tp __x)
noexcept
460 {
return std::__bit_width(__x); }
463#ifdef __cpp_lib_endian
474 little = __ORDER_LITTLE_ENDIAN__,
475 big = __ORDER_BIG_ENDIAN__,
476 native = __BYTE_ORDER__
482_GLIBCXX_END_NAMESPACE_VERSION
ISO C++ entities toplevel namespace is std.
GNU extensions for public use.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
Properties of fundamental types.
static constexpr _Tp max() noexcept