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
65namespace std _GLIBCXX_VISIBILITY(default)
67_GLIBCXX_BEGIN_NAMESPACE_VERSION
78#ifdef __cpp_lib_bit_cast
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);
100#ifdef __cpp_lib_byteswap
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__;
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)
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));
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)
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));
201 template<
typename _Tp>
203 __countl_zero(_Tp __x)
noexcept
206 constexpr auto _Nd = __int_traits<_Tp>::__digits;
208#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_clzg)
209 return __builtin_clzg(__x, _Nd);
214 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
215 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
216 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
218 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
220 constexpr int __diff = _Nd_u - _Nd;
221 return __builtin_clz(__x) - __diff;
223 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
225 constexpr int __diff = _Nd_ul - _Nd;
226 return __builtin_clzl(__x) - __diff;
228 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
230 constexpr int __diff = _Nd_ull - _Nd;
231 return __builtin_clzll(__x) - __diff;
235 static_assert(_Nd <= (2 * _Nd_ull),
236 "Maximum supported integer size is 128-bit");
238 unsigned long long __high = __x >> _Nd_ull;
241 constexpr int __diff = (2 * _Nd_ull) - _Nd;
242 return __builtin_clzll(__high) - __diff;
244 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
245 unsigned long long __low = __x & __max_ull;
246 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
251 template<
typename _Tp>
253 __countl_one(_Tp __x)
noexcept
255 return std::__countl_zero<_Tp>((_Tp)~__x);
258 template<
typename _Tp>
260 __countr_zero(_Tp __x)
noexcept
263 constexpr auto _Nd = __int_traits<_Tp>::__digits;
265#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_ctzg)
266 return __builtin_ctzg(__x, _Nd);
271 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
272 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
273 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
275 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
276 return __builtin_ctz(__x);
277 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
278 return __builtin_ctzl(__x);
279 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
280 return __builtin_ctzll(__x);
283 static_assert(_Nd <= (2 * _Nd_ull),
284 "Maximum supported integer size is 128-bit");
286 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
287 unsigned long long __low = __x & __max_ull;
289 return __builtin_ctzll(__low);
290 unsigned long long __high = __x >> _Nd_ull;
291 return __builtin_ctzll(__high) + _Nd_ull;
296 template<
typename _Tp>
298 __countr_one(_Tp __x)
noexcept
300 return std::__countr_zero((_Tp)~__x);
303 template<
typename _Tp>
305 __popcount(_Tp __x)
noexcept
307#if _GLIBCXX_USE_BUILTIN_TRAIT(__builtin_popcountg)
308 return __builtin_popcountg(__x);
311 constexpr auto _Nd = __int_traits<_Tp>::__digits;
313 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
314 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
315 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
317 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
318 return __builtin_popcount(__x);
319 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
320 return __builtin_popcountl(__x);
321 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
322 return __builtin_popcountll(__x);
325 static_assert(_Nd <= (2 * _Nd_ull),
326 "Maximum supported integer size is 128-bit");
328 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
329 unsigned long long __low = __x & __max_ull;
330 unsigned long long __high = __x >> _Nd_ull;
331 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
336 template<
typename _Tp>
338 __has_single_bit(_Tp __x)
noexcept
339 {
return std::__popcount(__x) == 1; }
341 template<
typename _Tp>
343 __bit_ceil(_Tp __x)
noexcept
346 constexpr auto _Nd = __int_traits<_Tp>::__digits;
347 if (__x == 0 || __x == 1)
349 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
354 if (!std::__is_constant_evaluated())
356 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
359 using __promoted_type =
decltype(__x << 1);
367 const int __extra_exp =
sizeof(__promoted_type) /
sizeof(_Tp) / 2;
368 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
370 return (_Tp)1u << __shift_exponent;
373 template<
typename _Tp>
375 __bit_floor(_Tp __x)
noexcept
377 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
380 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
383 template<
typename _Tp>
385 __bit_width(_Tp __x)
noexcept
387 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
388 return _Nd - std::__countl_zero(__x);
393#ifdef __cpp_lib_bitops
396 template<
typename _Tp>
397 concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
403 template<__
unsigned_
integer _Tp>
404 [[nodiscard]]
constexpr _Tp
405 rotl(_Tp __x,
int __s)
noexcept
406 {
return std::__rotl(__x, __s); }
409 template<__
unsigned_
integer _Tp>
410 [[nodiscard]]
constexpr _Tp
411 rotr(_Tp __x,
int __s)
noexcept
412 {
return std::__rotr(__x, __s); }
417 template<__
unsigned_
integer _Tp>
419 countl_zero(_Tp __x)
noexcept
420 {
return std::__countl_zero(__x); }
423 template<__
unsigned_
integer _Tp>
425 countl_one(_Tp __x)
noexcept
426 {
return std::__countl_one(__x); }
429 template<__
unsigned_
integer _Tp>
431 countr_zero(_Tp __x)
noexcept
432 {
return std::__countr_zero(__x); }
435 template<__
unsigned_
integer _Tp>
437 countr_one(_Tp __x)
noexcept
438 {
return std::__countr_one(__x); }
441 template<__
unsigned_
integer _Tp>
443 popcount(_Tp __x)
noexcept
444 {
return std::__popcount(__x); }
447#ifdef __cpp_lib_int_pow2
451 template<__
unsigned_
integer _Tp>
453 has_single_bit(_Tp __x)
noexcept
454 {
return std::__has_single_bit(__x); }
457 template<__
unsigned_
integer _Tp>
459 bit_ceil(_Tp __x)
noexcept
460 {
return std::__bit_ceil(__x); }
463 template<__
unsigned_
integer _Tp>
465 bit_floor(_Tp __x)
noexcept
466 {
return std::__bit_floor(__x); }
471 template<__
unsigned_
integer _Tp>
473 bit_width(_Tp __x)
noexcept
474 {
return std::__bit_width(__x); }
477#ifdef __cpp_lib_endian
488 little = __ORDER_LITTLE_ENDIAN__,
489 big = __ORDER_BIG_ENDIAN__,
490 native = __BYTE_ORDER__
496_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>.
static constexpr int digits
static constexpr _Tp max() noexcept