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