xref: /netbsd-src/external/gpl3/gcc/dist/libstdc++-v3/include/std/bit (revision b1e838363e3c6fc78a55519254d99869742dd33c)
1181254a7Smrg// <bit> -*- C++ -*-
2181254a7Smrg
3*b1e83836Smrg// Copyright (C) 2018-2022 Free Software Foundation, Inc.
4181254a7Smrg//
5181254a7Smrg// This file is part of the GNU ISO C++ Library.  This library is free
6181254a7Smrg// software; you can redistribute it and/or modify it under the
7181254a7Smrg// terms of the GNU General Public License as published by the
8181254a7Smrg// Free Software Foundation; either version 3, or (at your option)
9181254a7Smrg// any later version.
10181254a7Smrg
11181254a7Smrg// This library is distributed in the hope that it will be useful,
12181254a7Smrg// but WITHOUT ANY WARRANTY; without even the implied warranty of
13181254a7Smrg// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14181254a7Smrg// GNU General Public License for more details.
15181254a7Smrg
16181254a7Smrg// Under Section 7 of GPL version 3, you are granted additional
17181254a7Smrg// permissions described in the GCC Runtime Library Exception, version
18181254a7Smrg// 3.1, as published by the Free Software Foundation.
19181254a7Smrg
20181254a7Smrg// You should have received a copy of the GNU General Public License and
21181254a7Smrg// a copy of the GCC Runtime Library Exception along with this program;
22181254a7Smrg// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23181254a7Smrg// <http://www.gnu.org/licenses/>.
24181254a7Smrg
25181254a7Smrg/** @file include/bit
26181254a7Smrg *  This is a Standard C++ Library header.
27181254a7Smrg */
28181254a7Smrg
29181254a7Smrg#ifndef _GLIBCXX_BIT
30181254a7Smrg#define _GLIBCXX_BIT 1
31181254a7Smrg
32181254a7Smrg#pragma GCC system_header
33181254a7Smrg
34181254a7Smrg#if __cplusplus >= 201402L
35181254a7Smrg
36181254a7Smrg#include <type_traits>
37a448f87cSmrg
38a448f87cSmrg#if _GLIBCXX_HOSTED
39fb8a8121Smrg# include <ext/numeric_traits.h>
40a448f87cSmrg#else
41a448f87cSmrg# include <limits>
42a448f87cSmrg/// @cond undocumented
43a448f87cSmrgnamespace __gnu_cxx
44a448f87cSmrg{
45a448f87cSmrg  template<typename _Tp>
46a448f87cSmrg    struct __int_traits
47a448f87cSmrg    {
48a448f87cSmrg      static constexpr int __digits = std::numeric_limits<_Tp>::digits;
49a448f87cSmrg      static constexpr _Tp __max = std::numeric_limits<_Tp>::max();
50a448f87cSmrg    };
51a448f87cSmrg}
52a448f87cSmrg/// @endcond
53a448f87cSmrg#endif
54181254a7Smrg
55181254a7Smrgnamespace std _GLIBCXX_VISIBILITY(default)
56181254a7Smrg{
57181254a7Smrg_GLIBCXX_BEGIN_NAMESPACE_VERSION
58181254a7Smrg
59fb8a8121Smrg  /**
60fb8a8121Smrg   * @defgroup bit_manip Bit manipulation
61fb8a8121Smrg   * @ingroup numerics
62fb8a8121Smrg   *
63fb8a8121Smrg   * Utilities for examining and manipulating individual bits.
64fb8a8121Smrg   *
65fb8a8121Smrg   * @{
66fb8a8121Smrg   */
67fb8a8121Smrg
68*b1e83836Smrg#if __cplusplus > 201703l && __has_builtin(__builtin_bit_cast)
69*b1e83836Smrg#define __cpp_lib_bit_cast 201806L
70*b1e83836Smrg
71*b1e83836Smrg  /// Create a value of type `To` from the bits of `from`.
72*b1e83836Smrg  /**
73*b1e83836Smrg   * @tparam _To   A trivially-copyable type.
74*b1e83836Smrg   * @param __from A trivially-copyable object of the same size as `_To`.
75*b1e83836Smrg   * @return       An object of type `_To`.
76*b1e83836Smrg   * @since C++20
77*b1e83836Smrg   */
78*b1e83836Smrg  template<typename _To, typename _From>
79*b1e83836Smrg    [[nodiscard]]
80*b1e83836Smrg    constexpr _To
81*b1e83836Smrg    bit_cast(const _From& __from) noexcept
82*b1e83836Smrg#ifdef __cpp_concepts
83*b1e83836Smrg    requires (sizeof(_To) == sizeof(_From))
84*b1e83836Smrg      && __is_trivially_copyable(_To) && __is_trivially_copyable(_From)
85*b1e83836Smrg#endif
86*b1e83836Smrg    {
87*b1e83836Smrg      return __builtin_bit_cast(_To, __from);
88*b1e83836Smrg    }
89*b1e83836Smrg#endif
90*b1e83836Smrg
91*b1e83836Smrg#if __cplusplus > 202002L
92*b1e83836Smrg#define __cpp_lib_byteswap 202110L
93*b1e83836Smrg
94*b1e83836Smrg  /// Reverse order of bytes in the object representation of `value`.
95*b1e83836Smrg  /**
96*b1e83836Smrg   * @tparam _Tp     An integral type.
97*b1e83836Smrg   * @param __value  An object of integer type.
98*b1e83836Smrg   * @return         An object of the same type, with the bytes reversed.
99*b1e83836Smrg   * @since C++23
100*b1e83836Smrg   */
101*b1e83836Smrg  template<typename _Tp>
102*b1e83836Smrg    [[nodiscard]]
103*b1e83836Smrg    constexpr enable_if_t<is_integral<_Tp>::value, _Tp>
104*b1e83836Smrg    byteswap(_Tp __value) noexcept
105*b1e83836Smrg    {
106*b1e83836Smrg      if constexpr (sizeof(_Tp) == 1)
107*b1e83836Smrg	return __value;
108*b1e83836Smrg#if __cpp_if_consteval >= 202106L && __CHAR_BIT__ == 8
109*b1e83836Smrg      if !consteval
110*b1e83836Smrg	{
111*b1e83836Smrg	  if constexpr (sizeof(_Tp) == 2)
112*b1e83836Smrg	    return __builtin_bswap16(__value);
113*b1e83836Smrg	  if constexpr (sizeof(_Tp) == 4)
114*b1e83836Smrg	    return __builtin_bswap32(__value);
115*b1e83836Smrg	  if constexpr (sizeof(_Tp) == 8)
116*b1e83836Smrg	    return __builtin_bswap64(__value);
117*b1e83836Smrg	  if constexpr (sizeof(_Tp) == 16)
118*b1e83836Smrg#if __has_builtin(__builtin_bswap128)
119*b1e83836Smrg	    return __builtin_bswap128(__value);
120*b1e83836Smrg#else
121*b1e83836Smrg	    return (__builtin_bswap64(__value >> 64)
122*b1e83836Smrg		    | (static_cast<_Tp>(__builtin_bswap64(__value)) << 64));
123*b1e83836Smrg#endif
124*b1e83836Smrg	}
125*b1e83836Smrg#endif
126*b1e83836Smrg
127*b1e83836Smrg      // Fallback implementation that handles even __int24 etc.
128*b1e83836Smrg      using _Up = typename __make_unsigned<__remove_cv_t<_Tp>>::__type;
129*b1e83836Smrg      size_t __diff = __CHAR_BIT__ * (sizeof(_Tp) - 1);
130*b1e83836Smrg      _Up __mask1 = static_cast<unsigned char>(~0);
131*b1e83836Smrg      _Up __mask2 = __mask1 << __diff;
132*b1e83836Smrg      _Up __val = __value;
133*b1e83836Smrg      for (size_t __i = 0; __i < sizeof(_Tp) / 2; ++__i)
134*b1e83836Smrg	{
135*b1e83836Smrg	  _Up __byte1 = __val & __mask1;
136*b1e83836Smrg	  _Up __byte2 = __val & __mask2;
137*b1e83836Smrg	  __val = (__val ^ __byte1 ^ __byte2
138*b1e83836Smrg		   ^ (__byte1 << __diff) ^ (__byte2 >> __diff));
139*b1e83836Smrg	  __mask1 <<= __CHAR_BIT__;
140*b1e83836Smrg	  __mask2 >>= __CHAR_BIT__;
141*b1e83836Smrg	  __diff -= 2 * __CHAR_BIT__;
142*b1e83836Smrg	}
143*b1e83836Smrg      return __val;
144*b1e83836Smrg    }
145*b1e83836Smrg#endif
146*b1e83836Smrg
147fb8a8121Smrg  /// @cond undoc
148fb8a8121Smrg
149181254a7Smrg  template<typename _Tp>
150181254a7Smrg    constexpr _Tp
151181254a7Smrg    __rotl(_Tp __x, int __s) noexcept
152181254a7Smrg    {
153fb8a8121Smrg      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
154*b1e83836Smrg      if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
155*b1e83836Smrg	{
156*b1e83836Smrg	  // Variant for power of two _Nd which the compiler can
157*b1e83836Smrg	  // easily pattern match.
158*b1e83836Smrg	  constexpr unsigned __uNd = _Nd;
159*b1e83836Smrg	  const unsigned __r = __s;
160*b1e83836Smrg	  return (__x << (__r % __uNd)) | (__x >> ((-__r) % __uNd));
161*b1e83836Smrg	}
162181254a7Smrg      const int __r = __s % _Nd;
163181254a7Smrg      if (__r == 0)
164181254a7Smrg	return __x;
165181254a7Smrg      else if (__r > 0)
166181254a7Smrg	return (__x << __r) | (__x >> ((_Nd - __r) % _Nd));
167181254a7Smrg      else
168181254a7Smrg	return (__x >> -__r) | (__x << ((_Nd + __r) % _Nd)); // rotr(x, -r)
169181254a7Smrg    }
170181254a7Smrg
171181254a7Smrg  template<typename _Tp>
172181254a7Smrg    constexpr _Tp
173181254a7Smrg    __rotr(_Tp __x, int __s) noexcept
174181254a7Smrg    {
175fb8a8121Smrg      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
176*b1e83836Smrg      if _GLIBCXX17_CONSTEXPR ((_Nd & (_Nd - 1)) == 0)
177*b1e83836Smrg	{
178*b1e83836Smrg	  // Variant for power of two _Nd which the compiler can
179*b1e83836Smrg	  // easily pattern match.
180*b1e83836Smrg	  constexpr unsigned __uNd = _Nd;
181*b1e83836Smrg	  const unsigned __r = __s;
182*b1e83836Smrg	  return (__x >> (__r % __uNd)) | (__x << ((-__r) % __uNd));
183*b1e83836Smrg	}
184181254a7Smrg      const int __r = __s % _Nd;
185181254a7Smrg      if (__r == 0)
186181254a7Smrg	return __x;
187181254a7Smrg      else if (__r > 0)
188181254a7Smrg	return (__x >> __r) | (__x << ((_Nd - __r) % _Nd));
189181254a7Smrg      else
190181254a7Smrg	return (__x << -__r) | (__x >> ((_Nd + __r) % _Nd)); // rotl(x, -r)
191181254a7Smrg    }
192181254a7Smrg
193181254a7Smrg  template<typename _Tp>
194181254a7Smrg    constexpr int
195181254a7Smrg    __countl_zero(_Tp __x) noexcept
196181254a7Smrg    {
197fb8a8121Smrg      using __gnu_cxx::__int_traits;
198fb8a8121Smrg      constexpr auto _Nd = __int_traits<_Tp>::__digits;
199181254a7Smrg
200181254a7Smrg      if (__x == 0)
201181254a7Smrg        return _Nd;
202181254a7Smrg
203fb8a8121Smrg      constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
204fb8a8121Smrg      constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
205fb8a8121Smrg      constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
206181254a7Smrg
207181254a7Smrg      if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
208181254a7Smrg	{
209181254a7Smrg	  constexpr int __diff = _Nd_u - _Nd;
210181254a7Smrg	  return __builtin_clz(__x) - __diff;
211181254a7Smrg	}
212181254a7Smrg      else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
213181254a7Smrg	{
214181254a7Smrg	  constexpr int __diff = _Nd_ul - _Nd;
215181254a7Smrg	  return __builtin_clzl(__x) - __diff;
216181254a7Smrg	}
217181254a7Smrg      else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
218181254a7Smrg	{
219181254a7Smrg	  constexpr int __diff = _Nd_ull - _Nd;
220181254a7Smrg	  return __builtin_clzll(__x) - __diff;
221181254a7Smrg	}
222181254a7Smrg      else // (_Nd > _Nd_ull)
223181254a7Smrg	{
224181254a7Smrg	  static_assert(_Nd <= (2 * _Nd_ull),
225181254a7Smrg			"Maximum supported integer size is 128-bit");
226181254a7Smrg
227181254a7Smrg	  unsigned long long __high = __x >> _Nd_ull;
228181254a7Smrg	  if (__high != 0)
229181254a7Smrg	    {
230181254a7Smrg	      constexpr int __diff = (2 * _Nd_ull) - _Nd;
231181254a7Smrg	      return __builtin_clzll(__high) - __diff;
232181254a7Smrg	    }
233fb8a8121Smrg	  constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
234181254a7Smrg	  unsigned long long __low = __x & __max_ull;
235181254a7Smrg	  return (_Nd - _Nd_ull) + __builtin_clzll(__low);
236181254a7Smrg	}
237181254a7Smrg    }
238181254a7Smrg
239181254a7Smrg  template<typename _Tp>
240181254a7Smrg    constexpr int
241181254a7Smrg    __countl_one(_Tp __x) noexcept
242181254a7Smrg    {
243181254a7Smrg      return std::__countl_zero<_Tp>((_Tp)~__x);
244181254a7Smrg    }
245181254a7Smrg
246181254a7Smrg  template<typename _Tp>
247181254a7Smrg    constexpr int
248181254a7Smrg    __countr_zero(_Tp __x) noexcept
249181254a7Smrg    {
250fb8a8121Smrg      using __gnu_cxx::__int_traits;
251fb8a8121Smrg      constexpr auto _Nd = __int_traits<_Tp>::__digits;
252181254a7Smrg
253181254a7Smrg      if (__x == 0)
254181254a7Smrg        return _Nd;
255181254a7Smrg
256fb8a8121Smrg      constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
257fb8a8121Smrg      constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
258fb8a8121Smrg      constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
259181254a7Smrg
260181254a7Smrg      if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
261181254a7Smrg	return __builtin_ctz(__x);
262181254a7Smrg      else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
263181254a7Smrg	return __builtin_ctzl(__x);
264181254a7Smrg      else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
265181254a7Smrg	return __builtin_ctzll(__x);
266181254a7Smrg      else // (_Nd > _Nd_ull)
267181254a7Smrg	{
268181254a7Smrg	  static_assert(_Nd <= (2 * _Nd_ull),
269181254a7Smrg			"Maximum supported integer size is 128-bit");
270181254a7Smrg
271fb8a8121Smrg	  constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
272181254a7Smrg	  unsigned long long __low = __x & __max_ull;
273181254a7Smrg	  if (__low != 0)
274181254a7Smrg	    return __builtin_ctzll(__low);
275181254a7Smrg	  unsigned long long __high = __x >> _Nd_ull;
276181254a7Smrg	  return __builtin_ctzll(__high) + _Nd_ull;
277181254a7Smrg	}
278181254a7Smrg    }
279181254a7Smrg
280181254a7Smrg  template<typename _Tp>
281181254a7Smrg    constexpr int
282181254a7Smrg    __countr_one(_Tp __x) noexcept
283181254a7Smrg    {
284181254a7Smrg      return std::__countr_zero((_Tp)~__x);
285181254a7Smrg    }
286181254a7Smrg
287181254a7Smrg  template<typename _Tp>
288181254a7Smrg    constexpr int
289181254a7Smrg    __popcount(_Tp __x) noexcept
290181254a7Smrg    {
291fb8a8121Smrg      using __gnu_cxx::__int_traits;
292fb8a8121Smrg      constexpr auto _Nd = __int_traits<_Tp>::__digits;
293181254a7Smrg
294fb8a8121Smrg      constexpr auto _Nd_ull = __int_traits<unsigned long long>::__digits;
295fb8a8121Smrg      constexpr auto _Nd_ul = __int_traits<unsigned long>::__digits;
296fb8a8121Smrg      constexpr auto _Nd_u = __int_traits<unsigned>::__digits;
297181254a7Smrg
298181254a7Smrg      if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_u)
299181254a7Smrg	return __builtin_popcount(__x);
300181254a7Smrg      else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ul)
301181254a7Smrg	return __builtin_popcountl(__x);
302181254a7Smrg      else if _GLIBCXX17_CONSTEXPR (_Nd <= _Nd_ull)
303181254a7Smrg	return __builtin_popcountll(__x);
304181254a7Smrg      else // (_Nd > _Nd_ull)
305181254a7Smrg	{
306181254a7Smrg	  static_assert(_Nd <= (2 * _Nd_ull),
307181254a7Smrg			"Maximum supported integer size is 128-bit");
308181254a7Smrg
309fb8a8121Smrg	  constexpr auto __max_ull = __int_traits<unsigned long long>::__max;
310181254a7Smrg	  unsigned long long __low = __x & __max_ull;
311181254a7Smrg	  unsigned long long __high = __x >> _Nd_ull;
312181254a7Smrg	  return __builtin_popcountll(__low) + __builtin_popcountll(__high);
313181254a7Smrg	}
314181254a7Smrg    }
315181254a7Smrg
316181254a7Smrg  template<typename _Tp>
317181254a7Smrg    constexpr bool
318fb8a8121Smrg    __has_single_bit(_Tp __x) noexcept
319181254a7Smrg    { return std::__popcount(__x) == 1; }
320181254a7Smrg
321181254a7Smrg  template<typename _Tp>
322181254a7Smrg    constexpr _Tp
323fb8a8121Smrg    __bit_ceil(_Tp __x) noexcept
324181254a7Smrg    {
325fb8a8121Smrg      using __gnu_cxx::__int_traits;
326fb8a8121Smrg      constexpr auto _Nd = __int_traits<_Tp>::__digits;
327181254a7Smrg      if (__x == 0 || __x == 1)
328181254a7Smrg        return 1;
329181254a7Smrg      auto __shift_exponent = _Nd - std::__countl_zero((_Tp)(__x - 1u));
330181254a7Smrg      // If the shift exponent equals _Nd then the correct result is not
331181254a7Smrg      // representable as a value of _Tp, and so the result is undefined.
332181254a7Smrg      // Want that undefined behaviour to be detected in constant expressions,
333181254a7Smrg      // by UBSan, and by debug assertions.
334*b1e83836Smrg      if (!std::__is_constant_evaluated())
335fb8a8121Smrg	{
336fb8a8121Smrg	  __glibcxx_assert( __shift_exponent != __int_traits<_Tp>::__digits );
337fb8a8121Smrg	}
338*b1e83836Smrg
339181254a7Smrg      using __promoted_type = decltype(__x << 1);
340181254a7Smrg      if _GLIBCXX17_CONSTEXPR (!is_same<__promoted_type, _Tp>::value)
341181254a7Smrg	{
342181254a7Smrg	  // If __x undergoes integral promotion then shifting by _Nd is
343181254a7Smrg	  // not undefined. In order to make the shift undefined, so that
344181254a7Smrg	  // it is diagnosed in constant expressions and by UBsan, we also
345181254a7Smrg	  // need to "promote" the shift exponent to be too large for the
346181254a7Smrg	  // promoted type.
347181254a7Smrg	  const int __extra_exp = sizeof(__promoted_type) / sizeof(_Tp) / 2;
348181254a7Smrg	  __shift_exponent |= (__shift_exponent & _Nd) << __extra_exp;
349181254a7Smrg	}
350181254a7Smrg      return (_Tp)1u << __shift_exponent;
351181254a7Smrg    }
352181254a7Smrg
353181254a7Smrg  template<typename _Tp>
354181254a7Smrg    constexpr _Tp
355fb8a8121Smrg    __bit_floor(_Tp __x) noexcept
356181254a7Smrg    {
357fb8a8121Smrg      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
358181254a7Smrg      if (__x == 0)
359181254a7Smrg        return 0;
360181254a7Smrg      return (_Tp)1u << (_Nd - std::__countl_zero((_Tp)(__x >> 1)));
361181254a7Smrg    }
362181254a7Smrg
363181254a7Smrg  template<typename _Tp>
364181254a7Smrg    constexpr _Tp
365fb8a8121Smrg    __bit_width(_Tp __x) noexcept
366181254a7Smrg    {
367fb8a8121Smrg      constexpr auto _Nd = __gnu_cxx::__int_traits<_Tp>::__digits;
368181254a7Smrg      return _Nd - std::__countl_zero(__x);
369181254a7Smrg    }
370181254a7Smrg
371fb8a8121Smrg  /// @endcond
372fb8a8121Smrg
373181254a7Smrg#if __cplusplus > 201703L
374181254a7Smrg
375fb8a8121Smrg#define __cpp_lib_bitops 201907L
376181254a7Smrg
377fb8a8121Smrg  /// @cond undoc
378181254a7Smrg  template<typename _Tp, typename _Up = _Tp>
379181254a7Smrg    using _If_is_unsigned_integer
380fb8a8121Smrg      = enable_if_t<__is_unsigned_integer<_Tp>::value, _Up>;
381fb8a8121Smrg  /// @endcond
382181254a7Smrg
383181254a7Smrg  // [bit.rot], rotating
384181254a7Smrg
385fb8a8121Smrg  /// Rotate `x` to the left by `s` bits.
386181254a7Smrg  template<typename _Tp>
387181254a7Smrg    [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
388181254a7Smrg    rotl(_Tp __x, int __s) noexcept
389181254a7Smrg    { return std::__rotl(__x, __s); }
390181254a7Smrg
391fb8a8121Smrg  /// Rotate `x` to the right by `s` bits.
392181254a7Smrg  template<typename _Tp>
393181254a7Smrg    [[nodiscard]] constexpr _If_is_unsigned_integer<_Tp>
394181254a7Smrg    rotr(_Tp __x, int __s) noexcept
395181254a7Smrg    { return std::__rotr(__x, __s); }
396181254a7Smrg
397181254a7Smrg  // [bit.count], counting
398181254a7Smrg
399fb8a8121Smrg  /// The number of contiguous zero bits, starting from the highest bit.
400181254a7Smrg  template<typename _Tp>
401181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp, int>
402181254a7Smrg    countl_zero(_Tp __x) noexcept
403181254a7Smrg    { return std::__countl_zero(__x); }
404181254a7Smrg
405fb8a8121Smrg  /// The number of contiguous one bits, starting from the highest bit.
406181254a7Smrg  template<typename _Tp>
407181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp, int>
408181254a7Smrg    countl_one(_Tp __x) noexcept
409181254a7Smrg    { return std::__countl_one(__x); }
410181254a7Smrg
411fb8a8121Smrg  /// The number of contiguous zero bits, starting from the lowest bit.
412181254a7Smrg  template<typename _Tp>
413181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp, int>
414181254a7Smrg    countr_zero(_Tp __x) noexcept
415181254a7Smrg    { return std::__countr_zero(__x); }
416181254a7Smrg
417fb8a8121Smrg  /// The number of contiguous one bits, starting from the lowest bit.
418181254a7Smrg  template<typename _Tp>
419181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp, int>
420181254a7Smrg    countr_one(_Tp __x) noexcept
421181254a7Smrg    { return std::__countr_one(__x); }
422181254a7Smrg
423fb8a8121Smrg  /// The number of bits set in `x`.
424181254a7Smrg  template<typename _Tp>
425181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp, int>
426181254a7Smrg    popcount(_Tp __x) noexcept
427181254a7Smrg    { return std::__popcount(__x); }
428181254a7Smrg
429181254a7Smrg  // [bit.pow.two], integral powers of 2
430181254a7Smrg
431fb8a8121Smrg#define __cpp_lib_int_pow2 202002L
432fb8a8121Smrg
433fb8a8121Smrg  /// True if `x` is a power of two, false otherwise.
434181254a7Smrg  template<typename _Tp>
435181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp, bool>
436fb8a8121Smrg    has_single_bit(_Tp __x) noexcept
437fb8a8121Smrg    { return std::__has_single_bit(__x); }
438181254a7Smrg
439fb8a8121Smrg  /// The smallest power-of-two not less than `x`.
440181254a7Smrg  template<typename _Tp>
441181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp>
442fb8a8121Smrg    bit_ceil(_Tp __x) noexcept
443fb8a8121Smrg    { return std::__bit_ceil(__x); }
444181254a7Smrg
445fb8a8121Smrg  /// The largest power-of-two not greater than `x`.
446181254a7Smrg  template<typename _Tp>
447181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp>
448fb8a8121Smrg    bit_floor(_Tp __x) noexcept
449fb8a8121Smrg    { return std::__bit_floor(__x); }
450181254a7Smrg
451fb8a8121Smrg  /// The smallest integer greater than the base-2 logarithm of `x`.
452181254a7Smrg  template<typename _Tp>
453181254a7Smrg    constexpr _If_is_unsigned_integer<_Tp>
454fb8a8121Smrg    bit_width(_Tp __x) noexcept
455fb8a8121Smrg    { return std::__bit_width(__x); }
456181254a7Smrg
457181254a7Smrg#define __cpp_lib_endian 201907L
458181254a7Smrg
459*b1e83836Smrg  /// Byte order constants
460*b1e83836Smrg  /**
461*b1e83836Smrg   * The platform endianness can be checked by comparing `std::endian::native`
462*b1e83836Smrg   * to one of `std::endian::big` or `std::endian::little`.
463*b1e83836Smrg   *
464*b1e83836Smrg   * @since C++20
465*b1e83836Smrg   */
466181254a7Smrg  enum class endian
467181254a7Smrg  {
468181254a7Smrg    little = __ORDER_LITTLE_ENDIAN__,
469181254a7Smrg    big    = __ORDER_BIG_ENDIAN__,
470181254a7Smrg    native = __BYTE_ORDER__
471181254a7Smrg  };
472181254a7Smrg#endif // C++2a
473181254a7Smrg
474fb8a8121Smrg  /// @}
475fb8a8121Smrg
476181254a7Smrg_GLIBCXX_END_NAMESPACE_VERSION
477181254a7Smrg} // namespace std
478181254a7Smrg
479181254a7Smrg#endif // C++14
480181254a7Smrg#endif // _GLIBCXX_BIT
481