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
43 template<typename _Tp>
45 __rotl(_Tp __x, int __s) noexcept
47 constexpr auto _Nd = numeric_limits<_Tp>::digits;
48 const int __r = __s % _Nd;
52 return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
54 return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
57 template<typename _Tp>
59 __rotr(_Tp __x, int __s) noexcept
61 constexpr auto _Nd = numeric_limits<_Tp>::digits;
62 const int __r = __s % _Nd;
66 return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
68 return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
71 template<typename _Tp>
73 __countl_zero(_Tp __x) noexcept
75 constexpr auto _Nd = numeric_limits<_Tp>::digits;
80 constexpr auto _Nd_ull = numeric_limits<unsigned long long>::digits;
81 constexpr auto _Nd_ul = numeric_limits<unsigned long>::digits;
82 constexpr auto _Nd_u = numeric_limits<unsigned>::digits;
84 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
86 constexpr int __diff = _Nd_u - _Nd;
87 return __builtin_clz(__x) - __diff;
89 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
91 constexpr int __diff = _Nd_ul - _Nd;
92 return __builtin_clzl(__x) - __diff;
94 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
96 constexpr int __diff = _Nd_ull - _Nd;
97 return __builtin_clzll(__x) - __diff;
99 else // (_Nd > _Nd_ull)
101 static_assert(_Nd <= (2 * _Nd_ull),
102 "Maximum supported integer size is 128-bit");
104 unsigned long long __high = __x >> _Nd_ull;
107 constexpr int __diff = (2 * _Nd_ull) - _Nd;
108 return __builtin_clzll(__high) - __diff;
110 constexpr auto __max_ull = numeric_limits<unsigned long long>::max();
111 unsigned long long __low = __x & __max_ull;
112 return (_Nd - _Nd_ull) + __builtin_clzll(__low);
116 template<typename _Tp>
118 __countl_one(_Tp __x) noexcept
120 if (__x == numeric_limits<_Tp>::max())
121 return numeric_limits<_Tp>::digits;
122 return std::__countl_zero<_Tp>((_Tp)~__x);
125 template<typename _Tp>
127 __countr_zero(_Tp __x) noexcept
129 constexpr auto _Nd = numeric_limits<_Tp>::digits;
134 constexpr auto _Nd_ull = numeric_limits<unsigned long long>::digits;
135 constexpr auto _Nd_ul = numeric_limits<unsigned long>::digits;
136 constexpr auto _Nd_u = numeric_limits<unsigned>::digits;
138 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
139 return __builtin_ctz(__x);
140 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
141 return __builtin_ctzl(__x);
142 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
143 return __builtin_ctzll(__x);
144 else // (_Nd > _Nd_ull)
146 static_assert(_Nd <= (2 * _Nd_ull),
147 "Maximum supported integer size is 128-bit");
149 constexpr auto __max_ull = numeric_limits<unsigned long long>::max();
150 unsigned long long __low = __x & __max_ull;
152 return __builtin_ctzll(__low);
153 unsigned long long __high = __x >> _Nd_ull;
154 return __builtin_ctzll(__high) + _Nd_ull;
158 template<typename _Tp>
160 __countr_one(_Tp __x) noexcept
162 if (__x == numeric_limits<_Tp>::max())
163 return numeric_limits<_Tp>::digits;
164 return std::__countr_zero((_Tp)~__x);
167 template<typename _Tp>
169 __popcount(_Tp __x) noexcept
171 constexpr auto _Nd = numeric_limits<_Tp>::digits;
176 constexpr auto _Nd_ull = numeric_limits<unsigned long long>::digits;
177 constexpr auto _Nd_ul = numeric_limits<unsigned long>::digits;
178 constexpr auto _Nd_u = numeric_limits<unsigned>::digits;
180 if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
181 return __builtin_popcount(__x);
182 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
183 return __builtin_popcountl(__x);
184 else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
185 return __builtin_popcountll(__x);
186 else // (_Nd > _Nd_ull)
188 static_assert(_Nd <= (2 * _Nd_ull),
189 "Maximum supported integer size is 128-bit");
191 constexpr auto __max_ull = numeric_limits<unsigned long long>::max();
192 unsigned long long __low = __x & __max_ull;
193 unsigned long long __high = __x >> _Nd_ull;
194 return __builtin_popcountll(__low) + __builtin_popcountll(__high);
198 template<typename _Tp>
200 __ispow2(_Tp __x) noexcept
201 { return std::__popcount(__x) == 1; }
203 template<typename _Tp>
205 __ceil2(_Tp __x) noexcept
207 constexpr auto _Nd = numeric_limits<_Tp>::digits;
208 if (__x == 0 || __x == 1)
210 auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
211 // If the shift exponent equals _Nd then the correct result is not
212 // representable as a value of _Tp, and so the result is undefined.
213 // Want that undefined behaviour to be detected in constant expressions,
214 // by UBSan, and by debug assertions.
215 #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
216 if (!__builtin_is_constant_evaluated())
217 __glibcxx_assert( __shift_exponent != numeric_limits<_Tp>::digits );
219 using __promoted_type = decltype(__x << 1);
220 if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
222 // If __x undergoes integral promotion then shifting by _Nd is
223 // not undefined. In order to make the shift undefined, so that
224 // it is diagnosed in constant expressions and by UBsan, we also
225 // need to "promote" the shift exponent to be too large for the
227 const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
228 __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
230 return (_Tp)1u << __shift_exponent;
233 template<typename _Tp>
235 __floor2(_Tp __x) noexcept
237 constexpr auto _Nd = numeric_limits<_Tp>::digits;
240 return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
243 template<typename _Tp>
245 __log2p1(_Tp __x) noexcept
247 constexpr auto _Nd = numeric_limits<_Tp>::digits;
248 return _Nd - std::__countl_zero(__x);
251 #if __cplusplus > 201703L
253 template<typename _Tp, typename _Up = _Tp>
254 using _If_is_unsigned_integer
255 = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
257 // [bit.rot], rotating
259 template<typename _Tp>
260 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
261 rotl(_Tp __x, int __s) noexcept
262 { return std::__rotl(__x, __s); }
264 template<typename _Tp>
265 [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
266 rotr(_Tp __x, int __s) noexcept
267 { return std::__rotr(__x, __s); }
269 // [bit.count], counting
271 template<typename _Tp>
272 constexpr _If_is_unsigned_integer<_Tp, int>
273 countl_zero(_Tp __x) noexcept
274 { return std::__countl_zero(__x); }
276 template<typename _Tp>
277 constexpr _If_is_unsigned_integer<_Tp, int>
278 countl_one(_Tp __x) noexcept
279 { return std::__countl_one(__x); }
281 template<typename _Tp>
282 constexpr _If_is_unsigned_integer<_Tp, int>
283 countr_zero(_Tp __x) noexcept
284 { return std::__countr_zero(__x); }
286 template<typename _Tp>
287 constexpr _If_is_unsigned_integer<_Tp, int>
288 countr_one(_Tp __x) noexcept
289 { return std::__countr_one(__x); }
291 template<typename _Tp>
292 constexpr _If_is_unsigned_integer<_Tp, int>
293 popcount(_Tp __x) noexcept
294 { return std::__popcount(__x); }
296 // [bit.pow.two], integral powers of 2
298 template<typename _Tp>
299 constexpr _If_is_unsigned_integer<_Tp, bool>
300 ispow2(_Tp __x) noexcept
301 { return std::__ispow2(__x); }
303 template<typename _Tp>
304 constexpr _If_is_unsigned_integer<_Tp>
305 ceil2(_Tp __x) noexcept
306 { return std::__ceil2(__x); }
308 template<typename _Tp>
309 constexpr _If_is_unsigned_integer<_Tp>
310 floor2(_Tp __x) noexcept
311 { return std::__floor2(__x); }
313 template<typename _Tp>
314 constexpr _If_is_unsigned_integer<_Tp>
315 log2p1(_Tp __x) noexcept
316 { return std::__log2p1(__x); }
318 #define __cpp_lib_endian 201907L
323 little = __ORDER_LITTLE_ENDIAN__,
324 big = __ORDER_BIG_ENDIAN__,
325 native = __BYTE_ORDER__
329 _GLIBCXX_END_NAMESPACE_VERSION
333 #endif // _GLIBCXX_BIT