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