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
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
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;
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;
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
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);
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
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);
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
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));
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)
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
368 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
371 template<
typename _Tp>
373 __bit_width(_Tp __x)
noexcept
376 return _Nd - std::__countl_zero(__x);
381#ifdef __cpp_lib_bitops
384 template<
typename _Tp>
385 concept __unsigned_integer = __is_unsigned_integer<_Tp>::value;
391 template<__
unsigned_
integer _Tp>
392 [[nodiscard]]
constexpr _Tp
393 rotl(_Tp __x,
int __s)
noexcept
394 {
return std::__rotl(__x, __s); }
397 template<__
unsigned_
integer _Tp>
398 [[nodiscard]]
constexpr _Tp
399 rotr(_Tp __x,
int __s)
noexcept
400 {
return std::__rotr(__x, __s); }
405 template<__
unsigned_
integer _Tp>
407 countl_zero(_Tp __x)
noexcept
408 {
return std::__countl_zero(__x); }
411 template<__
unsigned_
integer _Tp>
413 countl_one(_Tp __x)
noexcept
414 {
return std::__countl_one(__x); }
417 template<__
unsigned_
integer _Tp>
419 countr_zero(_Tp __x)
noexcept
420 {
return std::__countr_zero(__x); }
423 template<__
unsigned_
integer _Tp>
425 countr_one(_Tp __x)
noexcept
426 {
return std::__countr_one(__x); }
429 template<__
unsigned_
integer _Tp>
431 popcount(_Tp __x)
noexcept
432 {
return std::__popcount(__x); }
435#ifdef __cpp_lib_int_pow2
439 template<__
unsigned_
integer _Tp>
441 has_single_bit(_Tp __x)
noexcept
442 {
return std::__has_single_bit(__x); }
445 template<__
unsigned_
integer _Tp>
447 bit_ceil(_Tp __x)
noexcept
448 {
return std::__bit_ceil(__x); }
451 template<__
unsigned_
integer _Tp>
453 bit_floor(_Tp __x)
noexcept
454 {
return std::__bit_floor(__x); }
459 template<__
unsigned_
integer _Tp>
461 bit_width(_Tp __x)
noexcept
462 {
return std::__bit_width(__x); }
465#ifdef __cpp_lib_endian
476 little = __ORDER_LITTLE_ENDIAN__,
477 big = __ORDER_BIG_ENDIAN__,
478 native = __BYTE_ORDER__
484_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