32#pragma GCC system_header
34#if __cplusplus >= 201402L
45 template<
typename _Tp>
55namespace std _GLIBCXX_VISIBILITY(default)
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
68#if __cplusplus > 201703l && __has_builtin(__builtin_bit_cast)
69#define __cpp_lib_bit_cast 201806L
72 template<
typename _To,
typename _From>
75 bit_cast(
const _From& __from)
noexcept
77 requires (
sizeof(_To) ==
sizeof(_From))
78 && __is_trivially_copyable(_To) && __is_trivially_copyable(_From)
81 return __builtin_bit_cast(_To, __from);
87 template<
typename _Tp>
89 __rotl(_Tp __x,
int __s)
noexcept
92 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
96 constexpr unsigned __uNd = _Nd;
97 const unsigned __r = __s;
98 return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
100 const int __r = __s % _Nd;
104 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
106 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd));
109 template<
typename _Tp>
111 __rotr(_Tp __x,
int __s)
noexcept
114 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
118 constexpr unsigned __uNd = _Nd;
119 const unsigned __r = __s;
120 return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
122 const int __r = __s % _Nd;
126 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
128 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd));
131 template<
typename _Tp>
133 __countl_zero(_Tp __x)
noexcept
136 constexpr auto _Nd = __int_traits<_Tp>::__digits;
141 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
142 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
143 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
145 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
147 constexpr int __diff = _Nd_u - _Nd;
148 return __builtin_clz(__x) - __diff;
150 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
152 constexpr int __diff = _Nd_ul - _Nd;
153 return __builtin_clzl(__x) - __diff;
155 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
157 constexpr int __diff = _Nd_ull - _Nd;
158 return __builtin_clzll(__x) - __diff;
162 static_assert(_Nd <= (2 * _Nd_ull),
163 "Maximum supported integer size is 128-bit");
165 unsigned long long __high = __x >> _Nd_ull;
168 constexpr int __diff = (2 * _Nd_ull) - _Nd;
169 return __builtin_clzll(__high) - __diff;
171 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
172 unsigned long long __low = __x & __max_ull;
173 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
177 template<
typename _Tp>
179 __countl_one(_Tp __x)
noexcept
181 return std::__countl_zero<_Tp>((_Tp)~__x);
184 template<
typename _Tp>
186 __countr_zero(_Tp __x)
noexcept
189 constexpr auto _Nd = __int_traits<_Tp>::__digits;
194 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
195 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
196 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
198 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
199 return __builtin_ctz(__x);
200 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
201 return __builtin_ctzl(__x);
202 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
203 return __builtin_ctzll(__x);
206 static_assert(_Nd <= (2 * _Nd_ull),
207 "Maximum supported integer size is 128-bit");
209 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
210 unsigned long long __low = __x & __max_ull;
212 return __builtin_ctzll(__low);
213 unsigned long long __high = __x >> _Nd_ull;
214 return __builtin_ctzll(__high) + _Nd_ull;
218 template<
typename _Tp>
220 __countr_one(_Tp __x)
noexcept
222 return std::__countr_zero((_Tp)~__x);
225 template<
typename _Tp>
227 __popcount(_Tp __x)
noexcept
230 constexpr auto _Nd = __int_traits<_Tp>::__digits;
232 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
233 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
234 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
236 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
237 return __builtin_popcount(__x);
238 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
239 return __builtin_popcountl(__x);
240 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
241 return __builtin_popcountll(__x);
244 static_assert(_Nd <= (2 * _Nd_ull),
245 "Maximum supported integer size is 128-bit");
247 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
248 unsigned long long __low = __x & __max_ull;
249 unsigned long long __high = __x >> _Nd_ull;
250 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
254 template<
typename _Tp>
256 __has_single_bit(_Tp __x)
noexcept
257 {
return std::__popcount(__x) == 1; }
259 template<
typename _Tp>
261 __bit_ceil(_Tp __x)
noexcept
264 constexpr auto _Nd = __int_traits<_Tp>::__digits;
265 if (__x == 0 || __x == 1)
267 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
272#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
273 if (!__builtin_is_constant_evaluated())
275 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
278 using __promoted_type =
decltype(__x << 1);
279 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
286 const int __extra_exp =
sizeof(__promoted_type) /
sizeof(_Tp) / 2;
287 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
289 return (_Tp)1u << __shift_exponent;
292 template<
typename _Tp>
294 __bit_floor(_Tp __x)
noexcept
299 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
302 template<
typename _Tp>
304 __bit_width(_Tp __x)
noexcept
307 return _Nd - std::__countl_zero(__x);
312#if __cplusplus > 201703L
314#define __cpp_lib_bitops 201907L
317 template<
typename _Tp,
typename _Up = _Tp>
318 using _If_is_unsigned_integer
319 = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
325 template<
typename _Tp>
326 [[nodiscard]]
constexpr _If_is_unsigned_integer<_Tp>
327 rotl(_Tp __x,
int __s)
noexcept
328 {
return std::__rotl(__x, __s); }
331 template<
typename _Tp>
332 [[nodiscard]]
constexpr _If_is_unsigned_integer<_Tp>
333 rotr(_Tp __x,
int __s)
noexcept
334 {
return std::__rotr(__x, __s); }
339 template<
typename _Tp>
340 constexpr _If_is_unsigned_integer<_Tp, int>
341 countl_zero(_Tp __x)
noexcept
342 {
return std::__countl_zero(__x); }
345 template<
typename _Tp>
346 constexpr _If_is_unsigned_integer<_Tp, int>
347 countl_one(_Tp __x)
noexcept
348 {
return std::__countl_one(__x); }
351 template<
typename _Tp>
352 constexpr _If_is_unsigned_integer<_Tp, int>
353 countr_zero(_Tp __x)
noexcept
354 {
return std::__countr_zero(__x); }
357 template<
typename _Tp>
358 constexpr _If_is_unsigned_integer<_Tp, int>
359 countr_one(_Tp __x)
noexcept
360 {
return std::__countr_one(__x); }
363 template<
typename _Tp>
364 constexpr _If_is_unsigned_integer<_Tp, int>
365 popcount(_Tp __x)
noexcept
366 {
return std::__popcount(__x); }
370#define __cpp_lib_int_pow2 202002L
373 template<
typename _Tp>
374 constexpr _If_is_unsigned_integer<_Tp, bool>
375 has_single_bit(_Tp __x)
noexcept
376 {
return std::__has_single_bit(__x); }
379 template<
typename _Tp>
380 constexpr _If_is_unsigned_integer<_Tp>
381 bit_ceil(_Tp __x)
noexcept
382 {
return std::__bit_ceil(__x); }
385 template<
typename _Tp>
386 constexpr _If_is_unsigned_integer<_Tp>
387 bit_floor(_Tp __x)
noexcept
388 {
return std::__bit_floor(__x); }
391 template<
typename _Tp>
392 constexpr _If_is_unsigned_integer<_Tp>
393 bit_width(_Tp __x)
noexcept
394 {
return std::__bit_width(__x); }
396#define __cpp_lib_endian 201907L
401 little = __ORDER_LITTLE_ENDIAN__,
402 big = __ORDER_BIG_ENDIAN__,
403 native = __BYTE_ORDER__
409_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