3 // Copyright (C) 2018-2020 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.
30 #define _GLIBCXX_BIT 1
32 #pragma GCC system_header
34 #if __cplusplus >= 201402L
36 #include <type_traits>
37 #include <ext/numeric_traits.h>
39 namespace std _GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @defgroup bit_manip Bit manipulation
47 * Utilities for examining and manipulating individual bits.
54 template<typename _Tp>
56 __rotl(_Tp __x, int __s) noexcept
58 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
59 const int __r = __s % _Nd;
63 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
65 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
68 template<typename _Tp>
70 __rotr(_Tp __x, int __s) noexcept
72 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
73 const int __r = __s % _Nd;
77 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
79 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
82 template<typename _Tp>
84 __countl_zero(_Tp __x) noexcept
86 using __gnu_cxx::__int_traits;
87 constexpr auto _Nd = __int_traits<_Tp>::__digits;
92 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
93 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
94 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
96 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
98 constexpr int __diff = _Nd_u - _Nd;
99 return __builtin_clz(__x) - __diff;
101 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
103 constexpr int __diff = _Nd_ul - _Nd;
104 return __builtin_clzl(__x) - __diff;
106 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
108 constexpr int __diff = _Nd_ull - _Nd;
109 return __builtin_clzll(__x) - __diff;
111 else // (_Nd > _Nd_ull)
113 static_assert(_Nd <= (2 * _Nd_ull),
114 "Maximum supported integer size is 128-bit");
116 unsigned long long __high = __x >> _Nd_ull;
119 constexpr int __diff = (2 * _Nd_ull) - _Nd;
120 return __builtin_clzll(__high) - __diff;
122 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
123 unsigned long long __low = __x & __max_ull;
124 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
128 template<typename _Tp>
130 __countl_one(_Tp __x) noexcept
132 return std::__countl_zero<_Tp>((_Tp)~__x);
135 template<typename _Tp>
137 __countr_zero(_Tp __x) noexcept
139 using __gnu_cxx::__int_traits;
140 constexpr auto _Nd = __int_traits<_Tp>::__digits;
145 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
146 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
147 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
149 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
150 return __builtin_ctz(__x);
151 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
152 return __builtin_ctzl(__x);
153 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
154 return __builtin_ctzll(__x);
155 else // (_Nd > _Nd_ull)
157 static_assert(_Nd <= (2 * _Nd_ull),
158 "Maximum supported integer size is 128-bit");
160 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
161 unsigned long long __low = __x & __max_ull;
163 return __builtin_ctzll(__low);
164 unsigned long long __high = __x >> _Nd_ull;
165 return __builtin_ctzll(__high) + _Nd_ull;
169 template<typename _Tp>
171 __countr_one(_Tp __x) noexcept
173 return std::__countr_zero((_Tp)~__x);
176 template<typename _Tp>
178 __popcount(_Tp __x) noexcept
180 using __gnu_cxx::__int_traits;
181 constexpr auto _Nd = __int_traits<_Tp>::__digits;
183 constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
184 constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
185 constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
187 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
188 return __builtin_popcount(__x);
189 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
190 return __builtin_popcountl(__x);
191 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
192 return __builtin_popcountll(__x);
193 else // (_Nd > _Nd_ull)
195 static_assert(_Nd <= (2 * _Nd_ull),
196 "Maximum supported integer size is 128-bit");
198 constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
199 unsigned long long __low = __x & __max_ull;
200 unsigned long long __high = __x >> _Nd_ull;
201 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
205 template<typename _Tp>
207 __has_single_bit(_Tp __x) noexcept
208 { return std::__popcount(__x) == 1; }
210 template<typename _Tp>
212 __bit_ceil(_Tp __x) noexcept
214 using __gnu_cxx::__int_traits;
215 constexpr auto _Nd = __int_traits<_Tp>::__digits;
216 if (__x == 0 || __x == 1)
218 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
219 // If the shift exponent equals _Nd then the correct result is not
220 // representable as a value of _Tp, and so the result is undefined.
221 // Want that undefined behaviour to be detected in constant expressions,
222 // by UBSan, and by debug assertions.
223 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
224 if (!__builtin_is_constant_evaluated())
226 __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
229 using __promoted_type = decltype(__x << 1);
230 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
232 // If __x undergoes integral promotion then shifting by _Nd is
233 // not undefined. In order to make the shift undefined, so that
234 // it is diagnosed in constant expressions and by UBsan, we also
235 // need to "promote" the shift exponent to be too large for the
237 const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
238 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
240 return (_Tp)1u << __shift_exponent;
243 template<typename _Tp>
245 __bit_floor(_Tp __x) noexcept
247 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
250 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
253 template<typename _Tp>
255 __bit_width(_Tp __x) noexcept
257 constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
258 return _Nd - std::__countl_zero(__x);
263 #if __cplusplus > 201703L
265 #define __cpp_lib_bitops 201907L
268 template<typename _Tp, typename _Up = _Tp>
269 using _If_is_unsigned_integer
270 = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
273 // [bit.rot], rotating
275 /// Rotate `x` to the left by `s` bits.
276 template<typename _Tp>
277 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
278 rotl(_Tp __x, int __s) noexcept
279 { return std::__rotl(__x, __s); }
281 /// Rotate `x` to the right by `s` bits.
282 template<typename _Tp>
283 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
284 rotr(_Tp __x, int __s) noexcept
285 { return std::__rotr(__x, __s); }
287 // [bit.count], counting
289 /// The number of contiguous zero bits, starting from the highest bit.
290 template<typename _Tp>
291 constexpr _If_is_unsigned_integer<_Tp, int>
292 countl_zero(_Tp __x) noexcept
293 { return std::__countl_zero(__x); }
295 /// The number of contiguous one bits, starting from the highest bit.
296 template<typename _Tp>
297 constexpr _If_is_unsigned_integer<_Tp, int>
298 countl_one(_Tp __x) noexcept
299 { return std::__countl_one(__x); }
301 /// The number of contiguous zero bits, starting from the lowest bit.
302 template<typename _Tp>
303 constexpr _If_is_unsigned_integer<_Tp, int>
304 countr_zero(_Tp __x) noexcept
305 { return std::__countr_zero(__x); }
307 /// The number of contiguous one bits, starting from the lowest bit.
308 template<typename _Tp>
309 constexpr _If_is_unsigned_integer<_Tp, int>
310 countr_one(_Tp __x) noexcept
311 { return std::__countr_one(__x); }
313 /// The number of bits set in `x`.
314 template<typename _Tp>
315 constexpr _If_is_unsigned_integer<_Tp, int>
316 popcount(_Tp __x) noexcept
317 { return std::__popcount(__x); }
319 // [bit.pow.two], integral powers of 2
321 #define __cpp_lib_int_pow2 202002L
323 /// True if `x` is a power of two, false otherwise.
324 template<typename _Tp>
325 constexpr _If_is_unsigned_integer<_Tp, bool>
326 has_single_bit(_Tp __x) noexcept
327 { return std::__has_single_bit(__x); }
329 /// The smallest power-of-two not less than `x`.
330 template<typename _Tp>
331 constexpr _If_is_unsigned_integer<_Tp>
332 bit_ceil(_Tp __x) noexcept
333 { return std::__bit_ceil(__x); }
335 /// The largest power-of-two not greater than `x`.
336 template<typename _Tp>
337 constexpr _If_is_unsigned_integer<_Tp>
338 bit_floor(_Tp __x) noexcept
339 { return std::__bit_floor(__x); }
341 /// The smallest integer greater than the base-2 logarithm of `x`.
342 template<typename _Tp>
343 constexpr _If_is_unsigned_integer<_Tp>
344 bit_width(_Tp __x) noexcept
345 { return std::__bit_width(__x); }
347 #define __cpp_lib_endian 201907L
352 little = __ORDER_LITTLE_ENDIAN__,
353 big = __ORDER_BIG_ENDIAN__,
354 native = __BYTE_ORDER__
360 _GLIBCXX_END_NAMESPACE_VERSION
364 #endif // _GLIBCXX_BIT