3// Copyright (C) 2018-2021 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
26 * This is a Standard C++ Library header.
32#pragma GCC system_header
34#if __cplusplus >= 201402L
39# include <ext/numeric_traits.h>
45 template<typename _Tp>
48 static constexpr int __digits = std::numeric_limits<_Tp>::digits;
49 static constexpr _Tp __max = std::numeric_limits<_Tp>::max();
55namespace std _GLIBCXX_VISIBILITY(default)
57_GLIBCXX_BEGIN_NAMESPACE_VERSION
60 * @defgroup bit_manip Bit manipulation
63 * Utilities for examining and manipulating individual bits.
68#if __cplusplus > 201703l && __has_builtin(__builtin_bit_cast)
69#define __cpp_lib_bit_cast 201806L
71 /// Create a value of type `To` from the bits of `from`.
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
91 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
92 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
94 // Variant for power of two _Nd which the compiler can
95 // easily pattern match.
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)); // rotr(x, -r)
109 template<typename _Tp>
111 __rotr(_Tp __x, int __s) noexcept
113 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
114 if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
116 // Variant for power of two _Nd which the compiler can
117 // easily pattern match.
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)); // rotl(x, -r)
131 template<typename _Tp>
133 __countl_zero(_Tp __x) noexcept
135 using __gnu_cxx::__int_traits;
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;
160 else // (_Nd > _Nd_ull)
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
188 using __gnu_cxx::__int_traits;
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);
204 else // (_Nd > _Nd_ull)
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
229 using __gnu_cxx::__int_traits;
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);
242 else // (_Nd > _Nd_ull)
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
263 using __gnu_cxx::__int_traits;
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));
268 // If the shift exponent equals _Nd then the correct result is not
269 // representable as a value of _Tp, and so the result is undefined.
270 // Want that undefined behaviour to be detected in constant expressions,
271 // by UBSan, and by debug assertions.
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)
281 // If __x undergoes integral promotion then shifting by _Nd is
282 // not undefined. In order to make the shift undefined, so that
283 // it is diagnosed in constant expressions and by UBsan, we also
284 // need to "promote" the shift exponent to be too large for the
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
296 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
299 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
302 template<typename _Tp>
304 __bit_width(_Tp __x) noexcept
306 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
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>;
322 // [bit.rot], rotating
324 /// Rotate `x` to the left by `s` bits.
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); }
330 /// Rotate `x` to the right by `s` bits.
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); }
336 // [bit.count], counting
338 /// The number of contiguous zero bits, starting from the highest bit.
339 template<typename _Tp>
340 constexpr _If_is_unsigned_integer<_Tp, int>
341 countl_zero(_Tp __x) noexcept
342 { return std::__countl_zero(__x); }
344 /// The number of contiguous one bits, starting from the highest bit.
345 template<typename _Tp>
346 constexpr _If_is_unsigned_integer<_Tp, int>
347 countl_one(_Tp __x) noexcept
348 { return std::__countl_one(__x); }
350 /// The number of contiguous zero bits, starting from the lowest bit.
351 template<typename _Tp>
352 constexpr _If_is_unsigned_integer<_Tp, int>
353 countr_zero(_Tp __x) noexcept
354 { return std::__countr_zero(__x); }
356 /// The number of contiguous one bits, starting from the lowest bit.
357 template<typename _Tp>
358 constexpr _If_is_unsigned_integer<_Tp, int>
359 countr_one(_Tp __x) noexcept
360 { return std::__countr_one(__x); }
362 /// The number of bits set in `x`.
363 template<typename _Tp>
364 constexpr _If_is_unsigned_integer<_Tp, int>
365 popcount(_Tp __x) noexcept
366 { return std::__popcount(__x); }
368 // [bit.pow.two], integral powers of 2
370#define __cpp_lib_int_pow2 202002L
372 /// True if `x` is a power of two, false otherwise.
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); }
378 /// The smallest power-of-two not less than `x`.
379 template<typename _Tp>
380 constexpr _If_is_unsigned_integer<_Tp>
381 bit_ceil(_Tp __x) noexcept
382 { return std::__bit_ceil(__x); }
384 /// The largest power-of-two not greater than `x`.
385 template<typename _Tp>
386 constexpr _If_is_unsigned_integer<_Tp>
387 bit_floor(_Tp __x) noexcept
388 { return std::__bit_floor(__x); }
390 /// The smallest integer greater than the base-2 logarithm of `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
413#endif // _GLIBCXX_BIT