3 // Copyright (C) 2018-2019 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>
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 = numeric_limits<_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 = numeric_limits<_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 constexpr auto _Nd = numeric_limits<_Tp>::digits;
91 constexpr auto _Nd_ull = numeric_limits<unsigned long long>::digits;
92 constexpr auto _Nd_ul = numeric_limits<unsigned long>::digits;
93 constexpr auto _Nd_u = numeric_limits<unsigned>::digits;
95 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
97 constexpr int __diff = _Nd_u - _Nd;
98 return __builtin_clz(__x) - __diff;
100 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
102 constexpr int __diff = _Nd_ul - _Nd;
103 return __builtin_clzl(__x) - __diff;
105 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
107 constexpr int __diff = _Nd_ull - _Nd;
108 return __builtin_clzll(__x) - __diff;
110 else // (_Nd > _Nd_ull)
112 static_assert(_Nd <= (2 * _Nd_ull),
113 "Maximum supported integer size is 128-bit");
115 unsigned long long __high = __x >> _Nd_ull;
118 constexpr int __diff = (2 * _Nd_ull) - _Nd;
119 return __builtin_clzll(__high) - __diff;
121 constexpr auto __max_ull = numeric_limits<unsigned long long>::max();
122 unsigned long long __low = __x & __max_ull;
123 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
127 template<typename _Tp>
129 __countl_one(_Tp __x) noexcept
131 if (__x == numeric_limits<_Tp>::max())
132 return numeric_limits<_Tp>::digits;
133 return std::__countl_zero<_Tp>((_Tp)~__x);
136 template<typename _Tp>
138 __countr_zero(_Tp __x) noexcept
140 constexpr auto _Nd = numeric_limits<_Tp>::digits;
145 constexpr auto _Nd_ull = numeric_limits<unsigned long long>::digits;
146 constexpr auto _Nd_ul = numeric_limits<unsigned long>::digits;
147 constexpr auto _Nd_u = numeric_limits<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 = numeric_limits<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 if (__x == numeric_limits<_Tp>::max())
174 return numeric_limits<_Tp>::digits;
175 return std::__countr_zero((_Tp)~__x);
178 template<typename _Tp>
180 __popcount(_Tp __x) noexcept
182 constexpr auto _Nd = numeric_limits<_Tp>::digits;
187 constexpr auto _Nd_ull = numeric_limits<unsigned long long>::digits;
188 constexpr auto _Nd_ul = numeric_limits<unsigned long>::digits;
189 constexpr auto _Nd_u = numeric_limits<unsigned>::digits;
191 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
192 return __builtin_popcount(__x);
193 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
194 return __builtin_popcountl(__x);
195 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
196 return __builtin_popcountll(__x);
197 else // (_Nd > _Nd_ull)
199 static_assert(_Nd <= (2 * _Nd_ull),
200 "Maximum supported integer size is 128-bit");
202 constexpr auto __max_ull = numeric_limits<unsigned long long>::max();
203 unsigned long long __low = __x & __max_ull;
204 unsigned long long __high = __x >> _Nd_ull;
205 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
209 template<typename _Tp>
211 __ispow2(_Tp __x) noexcept
212 { return std::__popcount(__x) == 1; }
214 template<typename _Tp>
216 __ceil2(_Tp __x) noexcept
218 constexpr auto _Nd = numeric_limits<_Tp>::digits;
219 if (__x == 0 || __x == 1)
221 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
222 // If the shift exponent equals _Nd then the correct result is not
223 // representable as a value of _Tp, and so the result is undefined.
224 // Want that undefined behaviour to be detected in constant expressions,
225 // by UBSan, and by debug assertions.
226 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
227 if (!__builtin_is_constant_evaluated())
228 __glibcxx_assert( __shift_exponent != numeric_limits<_Tp>::digits );
230 using __promoted_type = decltype(__x << 1);
231 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
233 // If __x undergoes integral promotion then shifting by _Nd is
234 // not undefined. In order to make the shift undefined, so that
235 // it is diagnosed in constant expressions and by UBsan, we also
236 // need to "promote" the shift exponent to be too large for the
238 const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
239 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
241 return (_Tp)1u << __shift_exponent;
244 template<typename _Tp>
246 __floor2(_Tp __x) noexcept
248 constexpr auto _Nd = numeric_limits<_Tp>::digits;
251 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
254 template<typename _Tp>
256 __log2p1(_Tp __x) noexcept
258 constexpr auto _Nd = numeric_limits<_Tp>::digits;
259 return _Nd - std::__countl_zero(__x);
264 #if __cplusplus > 201703L
267 template<typename _Tp, typename _Up = _Tp>
268 using _If_is_unsigned_integer
269 = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
272 // [bit.rot], rotating
274 /// Rotate `x` to the left by `s` bits.
275 template<typename _Tp>
276 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
277 rotl(_Tp __x, int __s) noexcept
278 { return std::__rotl(__x, __s); }
280 /// Rotate `x` to the right by `s` bits.
281 template<typename _Tp>
282 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
283 rotr(_Tp __x, int __s) noexcept
284 { return std::__rotr(__x, __s); }
286 // [bit.count], counting
288 /// The number of contiguous zero bits, starting from the highest bit.
289 template<typename _Tp>
290 constexpr _If_is_unsigned_integer<_Tp, int>
291 countl_zero(_Tp __x) noexcept
292 { return std::__countl_zero(__x); }
294 /// The number of contiguous one bits, starting from the highest bit.
295 template<typename _Tp>
296 constexpr _If_is_unsigned_integer<_Tp, int>
297 countl_one(_Tp __x) noexcept
298 { return std::__countl_one(__x); }
300 /// The number of contiguous zero bits, starting from the lowest bit.
301 template<typename _Tp>
302 constexpr _If_is_unsigned_integer<_Tp, int>
303 countr_zero(_Tp __x) noexcept
304 { return std::__countr_zero(__x); }
306 /// The number of contiguous one bits, starting from the lowest bit.
307 template<typename _Tp>
308 constexpr _If_is_unsigned_integer<_Tp, int>
309 countr_one(_Tp __x) noexcept
310 { return std::__countr_one(__x); }
312 /// The number of bits set in `x`.
313 template<typename _Tp>
314 constexpr _If_is_unsigned_integer<_Tp, int>
315 popcount(_Tp __x) noexcept
316 { return std::__popcount(__x); }
318 // [bit.pow.two], integral powers of 2
320 /// True if `x` is a power of two, false otherwise.
321 template<typename _Tp>
322 constexpr _If_is_unsigned_integer<_Tp, bool>
323 ispow2(_Tp __x) noexcept
324 { return std::__ispow2(__x); }
326 /// The smallest power-of-two not less than `x`.
327 template<typename _Tp>
328 constexpr _If_is_unsigned_integer<_Tp>
329 ceil2(_Tp __x) noexcept
330 { return std::__ceil2(__x); }
332 /// The largest power-of-two not greater than `x`.
333 template<typename _Tp>
334 constexpr _If_is_unsigned_integer<_Tp>
335 floor2(_Tp __x) noexcept
336 { return std::__floor2(__x); }
338 /// The smallest integer greater than the base-2 logarithm of `x`.
339 template<typename _Tp>
340 constexpr _If_is_unsigned_integer<_Tp>
341 log2p1(_Tp __x) noexcept
342 { return std::__log2p1(__x); }
344 #define __cpp_lib_endian 201907L
349 little = __ORDER_LITTLE_ENDIAN__,
350 big = __ORDER_BIG_ENDIAN__,
351 native = __BYTE_ORDER__
357 _GLIBCXX_END_NAMESPACE_VERSION
361 #endif // _GLIBCXX_BIT