]>
Commit | Line | Data |
---|---|---|
f3e91052 JW |
1 | // <bit> -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 2018-2021 Free Software Foundation, Inc. |
f3e91052 JW |
4 | // |
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) | |
9 | // any later version. | |
10 | ||
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. | |
15 | ||
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. | |
19 | ||
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/>. | |
24 | ||
25 | /** @file include/bit | |
26 | * This is a Standard C++ Library header. | |
27 | */ | |
28 | ||
29 | #ifndef _GLIBCXX_BIT | |
30 | #define _GLIBCXX_BIT 1 | |
31 | ||
32 | #pragma GCC system_header | |
33 | ||
34 | #if __cplusplus >= 201402L | |
35 | ||
36 | #include <type_traits> | |
eb04805b | 37 | #include <ext/numeric_traits.h> |
f3e91052 JW |
38 | |
39 | namespace std _GLIBCXX_VISIBILITY(default) | |
40 | { | |
41 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
42 | ||
27e6c1f4 JW |
43 | /** |
44 | * @defgroup bit_manip Bit manipulation | |
45 | * @ingroup numerics | |
46 | * | |
47 | * Utilities for examining and manipulating individual bits. | |
48 | * | |
49 | * @{ | |
50 | */ | |
51 | ||
9e433b34 JW |
52 | #if __cplusplus > 201703l && __has_builtin(__builtin_bit_cast) |
53 | #define __cpp_lib_bit_cast 201806L | |
54 | ||
55 | /// Create a value of type `To` from the bits of `from`. | |
56 | template<typename _To, typename _From> | |
57 | constexpr _To | |
58 | bit_cast(const _From& __from) noexcept | |
59 | { | |
60 | return __builtin_bit_cast(_To, __from); | |
61 | } | |
62 | #endif | |
63 | ||
27e6c1f4 JW |
64 | /// @cond undoc |
65 | ||
f3e91052 JW |
66 | template<typename _Tp> |
67 | constexpr _Tp | |
f35da524 | 68 | __rotl(_Tp __x, int __s) noexcept |
f3e91052 | 69 | { |
eb04805b | 70 | constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits; |
84185598 JJ |
71 | if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0) |
72 | { | |
73 | // Variant for power of two _Nd which the compiler can | |
74 | // easily pattern match. | |
75 | constexpr unsigned __uNd = _Nd; | |
76 | const unsigned __r = __s; | |
77 | return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd)); | |
78 | } | |
f35da524 JW |
79 | const int __r = __s % _Nd; |
80 | if (__r == 0) | |
81 | return __x; | |
82 | else if (__r > 0) | |
83 | return (__x << __r) | (__x >> ((_Nd - __r) % _Nd)); | |
84 | else | |
85 | return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r) | |
f3e91052 JW |
86 | } |
87 | ||
88 | template<typename _Tp> | |
89 | constexpr _Tp | |
f35da524 | 90 | __rotr(_Tp __x, int __s) noexcept |
f3e91052 | 91 | { |
eb04805b | 92 | constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits; |
84185598 JJ |
93 | if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0) |
94 | { | |
95 | // Variant for power of two _Nd which the compiler can | |
96 | // easily pattern match. | |
97 | constexpr unsigned __uNd = _Nd; | |
98 | const unsigned __r = __s; | |
99 | return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd)); | |
100 | } | |
f35da524 JW |
101 | const int __r = __s % _Nd; |
102 | if (__r == 0) | |
103 | return __x; | |
104 | else if (__r > 0) | |
105 | return (__x >> __r) | (__x << ((_Nd - __r) % _Nd)); | |
106 | else | |
107 | return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r) | |
f3e91052 JW |
108 | } |
109 | ||
110 | template<typename _Tp> | |
111 | constexpr int | |
112 | __countl_zero(_Tp __x) noexcept | |
113 | { | |
eb04805b JW |
114 | using __gnu_cxx::__int_traits; |
115 | constexpr auto _Nd = __int_traits<_Tp>::__digits; | |
f3e91052 JW |
116 | |
117 | if (__x == 0) | |
337dc307 | 118 | return _Nd; |
f3e91052 | 119 | |
eb04805b JW |
120 | constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits; |
121 | constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits; | |
122 | constexpr auto _Nd_u = __int_traits<unsigned>::__digits; | |
f3e91052 | 123 | |
337dc307 | 124 | if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u) |
f3e91052 | 125 | { |
337dc307 | 126 | constexpr int __diff = _Nd_u - _Nd; |
f3e91052 JW |
127 | return __builtin_clz(__x) - __diff; |
128 | } | |
337dc307 | 129 | else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul) |
f3e91052 | 130 | { |
337dc307 | 131 | constexpr int __diff = _Nd_ul - _Nd; |
f3e91052 JW |
132 | return __builtin_clzl(__x) - __diff; |
133 | } | |
337dc307 | 134 | else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull) |
f3e91052 | 135 | { |
337dc307 | 136 | constexpr int __diff = _Nd_ull - _Nd; |
f3e91052 JW |
137 | return __builtin_clzll(__x) - __diff; |
138 | } | |
337dc307 | 139 | else // (_Nd > _Nd_ull) |
f3e91052 | 140 | { |
337dc307 | 141 | static_assert(_Nd <= (2 * _Nd_ull), |
f3e91052 JW |
142 | "Maximum supported integer size is 128-bit"); |
143 | ||
337dc307 | 144 | unsigned long long __high = __x >> _Nd_ull; |
f3e91052 JW |
145 | if (__high != 0) |
146 | { | |
337dc307 | 147 | constexpr int __diff = (2 * _Nd_ull) - _Nd; |
f3e91052 JW |
148 | return __builtin_clzll(__high) - __diff; |
149 | } | |
eb04805b | 150 | constexpr auto __max_ull = __int_traits<unsigned long long>::__max; |
337dc307 JW |
151 | unsigned long long __low = __x & __max_ull; |
152 | return (_Nd - _Nd_ull) + __builtin_clzll(__low); | |
f3e91052 JW |
153 | } |
154 | } | |
155 | ||
156 | template<typename _Tp> | |
157 | constexpr int | |
158 | __countl_one(_Tp __x) noexcept | |
159 | { | |
f3e91052 JW |
160 | return std::__countl_zero<_Tp>((_Tp)~__x); |
161 | } | |
162 | ||
163 | template<typename _Tp> | |
164 | constexpr int | |
165 | __countr_zero(_Tp __x) noexcept | |
166 | { | |
eb04805b JW |
167 | using __gnu_cxx::__int_traits; |
168 | constexpr auto _Nd = __int_traits<_Tp>::__digits; | |
f3e91052 JW |
169 | |
170 | if (__x == 0) | |
337dc307 | 171 | return _Nd; |
f3e91052 | 172 | |
eb04805b JW |
173 | constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits; |
174 | constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits; | |
175 | constexpr auto _Nd_u = __int_traits<unsigned>::__digits; | |
f3e91052 | 176 | |
337dc307 | 177 | if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u) |
f3e91052 | 178 | return __builtin_ctz(__x); |
337dc307 | 179 | else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul) |
f3e91052 | 180 | return __builtin_ctzl(__x); |
337dc307 | 181 | else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull) |
f3e91052 | 182 | return __builtin_ctzll(__x); |
337dc307 | 183 | else // (_Nd > _Nd_ull) |
f3e91052 | 184 | { |
337dc307 | 185 | static_assert(_Nd <= (2 * _Nd_ull), |
f3e91052 JW |
186 | "Maximum supported integer size is 128-bit"); |
187 | ||
eb04805b | 188 | constexpr auto __max_ull = __int_traits<unsigned long long>::__max; |
337dc307 | 189 | unsigned long long __low = __x & __max_ull; |
f3e91052 JW |
190 | if (__low != 0) |
191 | return __builtin_ctzll(__low); | |
337dc307 JW |
192 | unsigned long long __high = __x >> _Nd_ull; |
193 | return __builtin_ctzll(__high) + _Nd_ull; | |
f3e91052 JW |
194 | } |
195 | } | |
196 | ||
197 | template<typename _Tp> | |
198 | constexpr int | |
199 | __countr_one(_Tp __x) noexcept | |
200 | { | |
f3e91052 JW |
201 | return std::__countr_zero((_Tp)~__x); |
202 | } | |
203 | ||
204 | template<typename _Tp> | |
205 | constexpr int | |
206 | __popcount(_Tp __x) noexcept | |
207 | { | |
eb04805b JW |
208 | using __gnu_cxx::__int_traits; |
209 | constexpr auto _Nd = __int_traits<_Tp>::__digits; | |
f3e91052 | 210 | |
eb04805b JW |
211 | constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits; |
212 | constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits; | |
213 | constexpr auto _Nd_u = __int_traits<unsigned>::__digits; | |
f3e91052 | 214 | |
337dc307 | 215 | if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u) |
f3e91052 | 216 | return __builtin_popcount(__x); |
337dc307 | 217 | else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul) |
f3e91052 | 218 | return __builtin_popcountl(__x); |
337dc307 | 219 | else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull) |
f3e91052 | 220 | return __builtin_popcountll(__x); |
337dc307 | 221 | else // (_Nd > _Nd_ull) |
f3e91052 | 222 | { |
337dc307 | 223 | static_assert(_Nd <= (2 * _Nd_ull), |
f3e91052 JW |
224 | "Maximum supported integer size is 128-bit"); |
225 | ||
eb04805b | 226 | constexpr auto __max_ull = __int_traits<unsigned long long>::__max; |
337dc307 JW |
227 | unsigned long long __low = __x & __max_ull; |
228 | unsigned long long __high = __x >> _Nd_ull; | |
f3e91052 JW |
229 | return __builtin_popcountll(__low) + __builtin_popcountll(__high); |
230 | } | |
231 | } | |
232 | ||
233 | template<typename _Tp> | |
234 | constexpr bool | |
9866abe3 | 235 | __has_single_bit(_Tp __x) noexcept |
f3e91052 JW |
236 | { return std::__popcount(__x) == 1; } |
237 | ||
238 | template<typename _Tp> | |
239 | constexpr _Tp | |
9866abe3 | 240 | __bit_ceil(_Tp __x) noexcept |
f3e91052 | 241 | { |
eb04805b JW |
242 | using __gnu_cxx::__int_traits; |
243 | constexpr auto _Nd = __int_traits<_Tp>::__digits; | |
2fb17d2d | 244 | if (__x == 0 || __x == 1) |
f3e91052 | 245 | return 1; |
281ab2fb JW |
246 | auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u)); |
247 | // If the shift exponent equals _Nd then the correct result is not | |
248 | // representable as a value of _Tp, and so the result is undefined. | |
249 | // Want that undefined behaviour to be detected in constant expressions, | |
250 | // by UBSan, and by debug assertions. | |
251 | #ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED | |
252 | if (!__builtin_is_constant_evaluated()) | |
3b39526e | 253 | { |
eb04805b | 254 | __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits ); |
3b39526e | 255 | } |
281ab2fb JW |
256 | #endif |
257 | using __promoted_type = decltype(__x << 1); | |
258 | if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value) | |
259 | { | |
260 | // If __x undergoes integral promotion then shifting by _Nd is | |
261 | // not undefined. In order to make the shift undefined, so that | |
262 | // it is diagnosed in constant expressions and by UBsan, we also | |
263 | // need to "promote" the shift exponent to be too large for the | |
264 | // promoted type. | |
265 | const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2; | |
266 | __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp; | |
267 | } | |
268 | return (_Tp)1u << __shift_exponent; | |
f3e91052 JW |
269 | } |
270 | ||
271 | template<typename _Tp> | |
272 | constexpr _Tp | |
9866abe3 | 273 | __bit_floor(_Tp __x) noexcept |
f3e91052 | 274 | { |
eb04805b | 275 | constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits; |
f3e91052 JW |
276 | if (__x == 0) |
277 | return 0; | |
278 | return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1))); | |
279 | } | |
280 | ||
281 | template<typename _Tp> | |
282 | constexpr _Tp | |
9866abe3 | 283 | __bit_width(_Tp __x) noexcept |
f3e91052 | 284 | { |
eb04805b | 285 | constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits; |
f3e91052 JW |
286 | return _Nd - std::__countl_zero(__x); |
287 | } | |
288 | ||
27e6c1f4 JW |
289 | /// @endcond |
290 | ||
f3e91052 JW |
291 | #if __cplusplus > 201703L |
292 | ||
0d58d88d JW |
293 | #define __cpp_lib_bitops 201907L |
294 | ||
27e6c1f4 | 295 | /// @cond undoc |
f3e91052 JW |
296 | template<typename _Tp, typename _Up = _Tp> |
297 | using _If_is_unsigned_integer | |
47f79054 | 298 | = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>; |
27e6c1f4 | 299 | /// @endcond |
f3e91052 | 300 | |
f35da524 | 301 | // [bit.rot], rotating |
f3e91052 | 302 | |
27e6c1f4 | 303 | /// Rotate `x` to the left by `s` bits. |
f3e91052 | 304 | template<typename _Tp> |
f35da524 JW |
305 | [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp> |
306 | rotl(_Tp __x, int __s) noexcept | |
f3e91052 JW |
307 | { return std::__rotl(__x, __s); } |
308 | ||
27e6c1f4 | 309 | /// Rotate `x` to the right by `s` bits. |
f3e91052 | 310 | template<typename _Tp> |
f35da524 JW |
311 | [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp> |
312 | rotr(_Tp __x, int __s) noexcept | |
f3e91052 JW |
313 | { return std::__rotr(__x, __s); } |
314 | ||
f35da524 | 315 | // [bit.count], counting |
f3e91052 | 316 | |
27e6c1f4 | 317 | /// The number of contiguous zero bits, starting from the highest bit. |
f3e91052 JW |
318 | template<typename _Tp> |
319 | constexpr _If_is_unsigned_integer<_Tp, int> | |
320 | countl_zero(_Tp __x) noexcept | |
321 | { return std::__countl_zero(__x); } | |
322 | ||
27e6c1f4 | 323 | /// The number of contiguous one bits, starting from the highest bit. |
f3e91052 JW |
324 | template<typename _Tp> |
325 | constexpr _If_is_unsigned_integer<_Tp, int> | |
326 | countl_one(_Tp __x) noexcept | |
327 | { return std::__countl_one(__x); } | |
328 | ||
27e6c1f4 | 329 | /// The number of contiguous zero bits, starting from the lowest bit. |
f3e91052 JW |
330 | template<typename _Tp> |
331 | constexpr _If_is_unsigned_integer<_Tp, int> | |
332 | countr_zero(_Tp __x) noexcept | |
333 | { return std::__countr_zero(__x); } | |
334 | ||
27e6c1f4 | 335 | /// The number of contiguous one bits, starting from the lowest bit. |
f3e91052 JW |
336 | template<typename _Tp> |
337 | constexpr _If_is_unsigned_integer<_Tp, int> | |
338 | countr_one(_Tp __x) noexcept | |
339 | { return std::__countr_one(__x); } | |
340 | ||
27e6c1f4 | 341 | /// The number of bits set in `x`. |
f3e91052 JW |
342 | template<typename _Tp> |
343 | constexpr _If_is_unsigned_integer<_Tp, int> | |
344 | popcount(_Tp __x) noexcept | |
345 | { return std::__popcount(__x); } | |
f3e91052 | 346 | |
f35da524 | 347 | // [bit.pow.two], integral powers of 2 |
f3e91052 | 348 | |
9866abe3 JW |
349 | #define __cpp_lib_int_pow2 202002L |
350 | ||
27e6c1f4 | 351 | /// True if `x` is a power of two, false otherwise. |
f3e91052 JW |
352 | template<typename _Tp> |
353 | constexpr _If_is_unsigned_integer<_Tp, bool> | |
9866abe3 JW |
354 | has_single_bit(_Tp __x) noexcept |
355 | { return std::__has_single_bit(__x); } | |
f3e91052 | 356 | |
27e6c1f4 | 357 | /// The smallest power-of-two not less than `x`. |
f3e91052 JW |
358 | template<typename _Tp> |
359 | constexpr _If_is_unsigned_integer<_Tp> | |
9866abe3 JW |
360 | bit_ceil(_Tp __x) noexcept |
361 | { return std::__bit_ceil(__x); } | |
f3e91052 | 362 | |
27e6c1f4 | 363 | /// The largest power-of-two not greater than `x`. |
f3e91052 JW |
364 | template<typename _Tp> |
365 | constexpr _If_is_unsigned_integer<_Tp> | |
9866abe3 JW |
366 | bit_floor(_Tp __x) noexcept |
367 | { return std::__bit_floor(__x); } | |
f3e91052 | 368 | |
27e6c1f4 | 369 | /// The smallest integer greater than the base-2 logarithm of `x`. |
f3e91052 JW |
370 | template<typename _Tp> |
371 | constexpr _If_is_unsigned_integer<_Tp> | |
9866abe3 JW |
372 | bit_width(_Tp __x) noexcept |
373 | { return std::__bit_width(__x); } | |
f3e91052 | 374 | |
a5378f9b JW |
375 | #define __cpp_lib_endian 201907L |
376 | ||
45c7215c JW |
377 | /// Byte order |
378 | enum class endian | |
379 | { | |
380 | little = __ORDER_LITTLE_ENDIAN__, | |
381 | big = __ORDER_BIG_ENDIAN__, | |
382 | native = __BYTE_ORDER__ | |
383 | }; | |
f3e91052 JW |
384 | #endif // C++2a |
385 | ||
27e6c1f4 JW |
386 | /// @} |
387 | ||
f3e91052 JW |
388 | _GLIBCXX_END_NAMESPACE_VERSION |
389 | } // namespace std | |
390 | ||
391 | #endif // C++14 | |
392 | #endif // _GLIBCXX_BIT |