30 #define _GLIBCXX_BIT 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
45 template<
typename _Tp>
55 namespace std _GLIBCXX_VISIBILITY(default)
57 _GLIBCXX_BEGIN_NAMESPACE_VERSION
70 template<
typename _Tp>
72 __rotl(_Tp __x,
int __s) noexcept
75 const int __r = __s % _Nd;
79 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
81 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd));
84 template<
typename _Tp>
86 __rotr(_Tp __x,
int __s) noexcept
89 const int __r = __s % _Nd;
93 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
95 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd));
98 template<
typename _Tp>
100 __countl_zero(_Tp __x) noexcept
103 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
108 constexpr
auto _Nd_ull = __int_traits<unsigned long long>::__digits;
109 constexpr
auto _Nd_ul = __int_traits<unsigned long>::__digits;
110 constexpr
auto _Nd_u = __int_traits<unsigned>::__digits;
112 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
114 constexpr
int __diff = _Nd_u - _Nd;
115 return __builtin_clz(__x) - __diff;
117 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
119 constexpr
int __diff = _Nd_ul - _Nd;
120 return __builtin_clzl(__x) - __diff;
122 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
124 constexpr
int __diff = _Nd_ull - _Nd;
125 return __builtin_clzll(__x) - __diff;
129 static_assert(_Nd <= (2 * _Nd_ull),
130 "Maximum supported integer size is 128-bit");
132 unsigned long long __high = __x >> _Nd_ull;
135 constexpr
int __diff = (2 * _Nd_ull) - _Nd;
136 return __builtin_clzll(__high) - __diff;
138 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
139 unsigned long long __low = __x & __max_ull;
140 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
144 template<
typename _Tp>
146 __countl_one(_Tp __x) noexcept
148 return std::__countl_zero<_Tp>((_Tp)~__x);
151 template<
typename _Tp>
153 __countr_zero(_Tp __x) noexcept
156 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
161 constexpr
auto _Nd_ull = __int_traits<unsigned long long>::__digits;
162 constexpr
auto _Nd_ul = __int_traits<unsigned long>::__digits;
163 constexpr
auto _Nd_u = __int_traits<unsigned>::__digits;
165 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
166 return __builtin_ctz(__x);
167 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
168 return __builtin_ctzl(__x);
169 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
170 return __builtin_ctzll(__x);
173 static_assert(_Nd <= (2 * _Nd_ull),
174 "Maximum supported integer size is 128-bit");
176 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
177 unsigned long long __low = __x & __max_ull;
179 return __builtin_ctzll(__low);
180 unsigned long long __high = __x >> _Nd_ull;
181 return __builtin_ctzll(__high) + _Nd_ull;
185 template<
typename _Tp>
187 __countr_one(_Tp __x) noexcept
189 return std::__countr_zero((_Tp)~__x);
192 template<
typename _Tp>
194 __popcount(_Tp __x) noexcept
197 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
199 constexpr
auto _Nd_ull = __int_traits<unsigned long long>::__digits;
200 constexpr
auto _Nd_ul = __int_traits<unsigned long>::__digits;
201 constexpr
auto _Nd_u = __int_traits<unsigned>::__digits;
203 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
204 return __builtin_popcount(__x);
205 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
206 return __builtin_popcountl(__x);
207 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
208 return __builtin_popcountll(__x);
211 static_assert(_Nd <= (2 * _Nd_ull),
212 "Maximum supported integer size is 128-bit");
214 constexpr
auto __max_ull = __int_traits<unsigned long long>::__max;
215 unsigned long long __low = __x & __max_ull;
216 unsigned long long __high = __x >> _Nd_ull;
217 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
221 template<
typename _Tp>
223 __has_single_bit(_Tp __x) noexcept
224 {
return std::__popcount(__x) == 1; }
226 template<
typename _Tp>
228 __bit_ceil(_Tp __x) noexcept
231 constexpr
auto _Nd = __int_traits<_Tp>::__digits;
232 if (__x == 0 || __x == 1)
234 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
239 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
240 if (!__builtin_is_constant_evaluated())
242 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
245 using __promoted_type = decltype(__x << 1);
246 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
253 const int __extra_exp =
sizeof(__promoted_type) /
sizeof(_Tp) / 2;
254 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
256 return (_Tp)1u << __shift_exponent;
259 template<
typename _Tp>
261 __bit_floor(_Tp __x) noexcept
266 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
269 template<
typename _Tp>
271 __bit_width(_Tp __x) noexcept
274 return _Nd - std::__countl_zero(__x);
279 #if __cplusplus > 201703L
281 #define __cpp_lib_bitops 201907L
284 template<
typename _Tp,
typename _Up = _Tp>
285 using _If_is_unsigned_integer
286 = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
292 template<
typename _Tp>
293 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
294 rotl(_Tp __x,
int __s) noexcept
295 {
return std::__rotl(__x, __s); }
298 template<
typename _Tp>
299 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
300 rotr(_Tp __x,
int __s) noexcept
301 {
return std::__rotr(__x, __s); }
306 template<
typename _Tp>
307 constexpr _If_is_unsigned_integer<_Tp, int>
308 countl_zero(_Tp __x) noexcept
309 {
return std::__countl_zero(__x); }
312 template<
typename _Tp>
313 constexpr _If_is_unsigned_integer<_Tp, int>
314 countl_one(_Tp __x) noexcept
315 {
return std::__countl_one(__x); }
318 template<
typename _Tp>
319 constexpr _If_is_unsigned_integer<_Tp, int>
320 countr_zero(_Tp __x) noexcept
321 {
return std::__countr_zero(__x); }
324 template<
typename _Tp>
325 constexpr _If_is_unsigned_integer<_Tp, int>
326 countr_one(_Tp __x) noexcept
327 {
return std::__countr_one(__x); }
330 template<
typename _Tp>
331 constexpr _If_is_unsigned_integer<_Tp, int>
332 popcount(_Tp __x) noexcept
333 {
return std::__popcount(__x); }
337 #define __cpp_lib_int_pow2 202002L
340 template<
typename _Tp>
341 constexpr _If_is_unsigned_integer<_Tp, bool>
342 has_single_bit(_Tp __x) noexcept
343 {
return std::__has_single_bit(__x); }
346 template<
typename _Tp>
347 constexpr _If_is_unsigned_integer<_Tp>
348 bit_ceil(_Tp __x) noexcept
349 {
return std::__bit_ceil(__x); }
352 template<
typename _Tp>
353 constexpr _If_is_unsigned_integer<_Tp>
354 bit_floor(_Tp __x) noexcept
355 {
return std::__bit_floor(__x); }
358 template<
typename _Tp>
359 constexpr _If_is_unsigned_integer<_Tp>
360 bit_width(_Tp __x) noexcept
361 {
return std::__bit_width(__x); }
363 #define __cpp_lib_endian 201907L
368 little = __ORDER_LITTLE_ENDIAN__,
369 big = __ORDER_BIG_ENDIAN__,
370 native = __BYTE_ORDER__
376 _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