30 #define _GLIBCXX_BIT 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
39 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 #if __cplusplus > 201703l && __has_builtin(__builtin_bit_cast)
53 #define __cpp_lib_bit_cast 201806L
56 template<
typename _To,
typename _From>
59 bit_cast(
const _From& __from) noexcept
61 return __builtin_bit_cast(_To, __from);
67 template<
typename _Tp>
69 __rotl(_Tp __x,
int __s) noexcept
72 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
76 constexpr
unsigned __uNd = _Nd;
77 const unsigned __r = __s;
78 return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
80 const int __r = __s % _Nd;
84 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
86 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd));
89 template<
typename _Tp>
91 __rotr(_Tp __x,
int __s) noexcept
94 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
98 constexpr
unsigned __uNd = _Nd;
99 const unsigned __r = __s;
100 return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
102 const int __r = __s % _Nd;
106 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
108 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd));
111 template<
typename _Tp>
113 __countl_zero(_Tp __x) noexcept
116 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
121 constexpr
auto _Nd_ull = __int_traits<unsigned long long>::__digits;
122 constexpr
auto _Nd_ul = __int_traits<unsigned long>::__digits;
123 constexpr
auto _Nd_u = __int_traits<unsigned>::__digits;
125 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
127 constexpr
int __diff = _Nd_u - _Nd;
128 return __builtin_clz(__x) - __diff;
130 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
132 constexpr
int __diff = _Nd_ul - _Nd;
133 return __builtin_clzl(__x) - __diff;
135 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
137 constexpr
int __diff = _Nd_ull - _Nd;
138 return __builtin_clzll(__x) - __diff;
142 static_assert(_Nd <= (2 * _Nd_ull),
143 "Maximum supported integer size is 128-bit");
145 unsigned long long __high = __x >> _Nd_ull;
148 constexpr
int __diff = (2 * _Nd_ull) - _Nd;
149 return __builtin_clzll(__high) - __diff;
151 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
152 unsigned long long __low = __x & __max_ull;
153 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
157 template<
typename _Tp>
159 __countl_one(_Tp __x) noexcept
161 return std::__countl_zero<_Tp>((_Tp)~__x);
164 template<
typename _Tp>
166 __countr_zero(_Tp __x) noexcept
169 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
174 constexpr
auto _Nd_ull = __int_traits<unsigned long long>::__digits;
175 constexpr
auto _Nd_ul = __int_traits<unsigned long>::__digits;
176 constexpr
auto _Nd_u = __int_traits<unsigned>::__digits;
178 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
179 return __builtin_ctz(__x);
180 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
181 return __builtin_ctzl(__x);
182 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
183 return __builtin_ctzll(__x);
186 static_assert(_Nd <= (2 * _Nd_ull),
187 "Maximum supported integer size is 128-bit");
189 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
190 unsigned long long __low = __x & __max_ull;
192 return __builtin_ctzll(__low);
193 unsigned long long __high = __x >> _Nd_ull;
194 return __builtin_ctzll(__high) + _Nd_ull;
198 template<
typename _Tp>
200 __countr_one(_Tp __x) noexcept
202 return std::__countr_zero((_Tp)~__x);
205 template<
typename _Tp>
207 __popcount(_Tp __x) noexcept
210 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
212 constexpr
auto _Nd_ull = __int_traits<unsigned long long>::__digits;
213 constexpr
auto _Nd_ul = __int_traits<unsigned long>::__digits;
214 constexpr
auto _Nd_u = __int_traits<unsigned>::__digits;
216 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
217 return __builtin_popcount(__x);
218 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
219 return __builtin_popcountl(__x);
220 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
221 return __builtin_popcountll(__x);
224 static_assert(_Nd <= (2 * _Nd_ull),
225 "Maximum supported integer size is 128-bit");
227 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
228 unsigned long long __low = __x & __max_ull;
229 unsigned long long __high = __x >> _Nd_ull;
230 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
234 template<
typename _Tp>
236 __has_single_bit(_Tp __x) noexcept
237 {
return std::__popcount(__x) == 1; }
239 template<
typename _Tp>
241 __bit_ceil(_Tp __x) noexcept
244 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
245 if (__x == 0 || __x == 1)
247 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
252 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
253 if (!__builtin_is_constant_evaluated())
255 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
258 using __promoted_type = decltype(__x << 1);
259 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
266 const int __extra_exp =
sizeof(__promoted_type) /
sizeof(_Tp) / 2;
267 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
269 return (_Tp)1u << __shift_exponent;
272 template<
typename _Tp>
274 __bit_floor(_Tp __x) noexcept
279 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
282 template<
typename _Tp>
284 __bit_width(_Tp __x) noexcept
287 return _Nd - std::__countl_zero(__x);
292 #if __cplusplus > 201703L
294 #define __cpp_lib_bitops 201907L
297 template<
typename _Tp,
typename _Up = _Tp>
298 using _If_is_unsigned_integer
299 = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
305 template<
typename _Tp>
306 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
307 rotl(_Tp __x,
int __s) noexcept
308 {
return std::__rotl(__x, __s); }
311 template<
typename _Tp>
312 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
313 rotr(_Tp __x,
int __s) noexcept
314 {
return std::__rotr(__x, __s); }
319 template<
typename _Tp>
320 constexpr _If_is_unsigned_integer<_Tp, int>
321 countl_zero(_Tp __x) noexcept
322 {
return std::__countl_zero(__x); }
325 template<
typename _Tp>
326 constexpr _If_is_unsigned_integer<_Tp, int>
327 countl_one(_Tp __x) noexcept
328 {
return std::__countl_one(__x); }
331 template<
typename _Tp>
332 constexpr _If_is_unsigned_integer<_Tp, int>
333 countr_zero(_Tp __x) noexcept
334 {
return std::__countr_zero(__x); }
337 template<
typename _Tp>
338 constexpr _If_is_unsigned_integer<_Tp, int>
339 countr_one(_Tp __x) noexcept
340 {
return std::__countr_one(__x); }
343 template<
typename _Tp>
344 constexpr _If_is_unsigned_integer<_Tp, int>
345 popcount(_Tp __x) noexcept
346 {
return std::__popcount(__x); }
350 #define __cpp_lib_int_pow2 202002L
353 template<
typename _Tp>
354 constexpr _If_is_unsigned_integer<_Tp, bool>
355 has_single_bit(_Tp __x) noexcept
356 {
return std::__has_single_bit(__x); }
359 template<
typename _Tp>
360 constexpr _If_is_unsigned_integer<_Tp>
361 bit_ceil(_Tp __x) noexcept
362 {
return std::__bit_ceil(__x); }
365 template<
typename _Tp>
366 constexpr _If_is_unsigned_integer<_Tp>
367 bit_floor(_Tp __x) noexcept
368 {
return std::__bit_floor(__x); }
371 template<
typename _Tp>
372 constexpr _If_is_unsigned_integer<_Tp>
373 bit_width(_Tp __x) noexcept
374 {
return std::__bit_width(__x); }
376 #define __cpp_lib_endian 201907L
381 little = __ORDER_LITTLE_ENDIAN__,
382 big = __ORDER_BIG_ENDIAN__,
383 native = __BYTE_ORDER__
389 _GLIBCXX_END_NAMESPACE_VERSION
ISO C++ entities toplevel namespace is std.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.