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>
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())
229 __glibcxx_assert( __shift_exponent != numeric_limits<_Tp>::digits );
232 using __promoted_type = decltype(__x << 1);
233 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
235 // If __x undergoes integral promotion then shifting by _Nd is
236 // not undefined. In order to make the shift undefined, so that
237 // it is diagnosed in constant expressions and by UBsan, we also
238 // need to "promote" the shift exponent to be too large for the
240 const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
241 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
243 return (_Tp)1u << __shift_exponent;
246 template<typename _Tp>
248 __floor2(_Tp __x) noexcept
250 constexpr auto _Nd = numeric_limits<_Tp>::digits;
253 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
256 template<typename _Tp>
258 __log2p1(_Tp __x) noexcept
260 constexpr auto _Nd = numeric_limits<_Tp>::digits;
261 return _Nd - std::__countl_zero(__x);
266 #if __cplusplus > 201703L
268 #define __cpp_lib_bitops 201907L
271 template<typename _Tp, typename _Up = _Tp>
272 using _If_is_unsigned_integer
273 = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
276 // [bit.rot], rotating
278 /// Rotate `x` to the left by `s` bits.
279 template<typename _Tp>
280 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
281 rotl(_Tp __x, int __s) noexcept
282 { return std::__rotl(__x, __s); }
284 /// Rotate `x` to the right by `s` bits.
285 template<typename _Tp>
286 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
287 rotr(_Tp __x, int __s) noexcept
288 { return std::__rotr(__x, __s); }
290 // [bit.count], counting
292 /// The number of contiguous zero bits, starting from the highest bit.
293 template<typename _Tp>
294 constexpr _If_is_unsigned_integer<_Tp, int>
295 countl_zero(_Tp __x) noexcept
296 { return std::__countl_zero(__x); }
298 /// The number of contiguous one bits, starting from the highest bit.
299 template<typename _Tp>
300 constexpr _If_is_unsigned_integer<_Tp, int>
301 countl_one(_Tp __x) noexcept
302 { return std::__countl_one(__x); }
304 /// The number of contiguous zero bits, starting from the lowest bit.
305 template<typename _Tp>
306 constexpr _If_is_unsigned_integer<_Tp, int>
307 countr_zero(_Tp __x) noexcept
308 { return std::__countr_zero(__x); }
310 /// The number of contiguous one bits, starting from the lowest bit.
311 template<typename _Tp>
312 constexpr _If_is_unsigned_integer<_Tp, int>
313 countr_one(_Tp __x) noexcept
314 { return std::__countr_one(__x); }
316 /// The number of bits set in `x`.
317 template<typename _Tp>
318 constexpr _If_is_unsigned_integer<_Tp, int>
319 popcount(_Tp __x) noexcept
320 { return std::__popcount(__x); }
322 // [bit.pow.two], integral powers of 2
324 /// True if `x` is a power of two, false otherwise.
325 template<typename _Tp>
326 constexpr _If_is_unsigned_integer<_Tp, bool>
327 ispow2(_Tp __x) noexcept
328 { return std::__ispow2(__x); }
330 /// The smallest power-of-two not less than `x`.
331 template<typename _Tp>
332 constexpr _If_is_unsigned_integer<_Tp>
333 ceil2(_Tp __x) noexcept
334 { return std::__ceil2(__x); }
336 /// The largest power-of-two not greater than `x`.
337 template<typename _Tp>
338 constexpr _If_is_unsigned_integer<_Tp>
339 floor2(_Tp __x) noexcept
340 { return std::__floor2(__x); }
342 /// The smallest integer greater than the base-2 logarithm of `x`.
343 template<typename _Tp>
344 constexpr _If_is_unsigned_integer<_Tp>
345 log2p1(_Tp __x) noexcept
346 { return std::__log2p1(__x); }
348 #define __cpp_lib_endian 201907L
353 little = __ORDER_LITTLE_ENDIAN__,
354 big = __ORDER_BIG_ENDIAN__,
355 native = __BYTE_ORDER__
361 _GLIBCXX_END_NAMESPACE_VERSION
365 #endif // _GLIBCXX_BIT