xref: /llvm-project-15.0.7/libcxx/include/bit (revision d3cbcc4e)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_BIT
11#define _LIBCPP_BIT
12
13/*
14    bit synopsis
15
16namespace std {
17  // [bit.cast], bit_cast
18  template<class To, class From>
19    constexpr To bit_cast(const From& from) noexcept; // C++20
20
21  // [bit.byteswap], byteswap
22  template<class T>
23    constexpr T byteswap(T value) noexcept;      // C++23
24
25  // [bit.pow.two], integral powers of 2
26  template <class T>
27    constexpr bool has_single_bit(T x) noexcept; // C++20
28  template <class T>
29    constexpr T bit_ceil(T x);                   // C++20
30  template <class T>
31    constexpr T bit_floor(T x) noexcept;         // C++20
32  template <class T>
33    constexpr int bit_width(T x) noexcept;       // C++20
34
35  // [bit.rotate], rotating
36  template<class T>
37    constexpr T rotl(T x, unsigned int s) noexcept; // C++20
38  template<class T>
39    constexpr T rotr(T x, unsigned int s) noexcept; // C++20
40
41  // [bit.count], counting
42  template<class T>
43    constexpr int countl_zero(T x) noexcept;  // C++20
44  template<class T>
45    constexpr int countl_one(T x) noexcept;   // C++20
46  template<class T>
47    constexpr int countr_zero(T x) noexcept;  // C++20
48  template<class T>
49    constexpr int countr_one(T x) noexcept;   // C++20
50  template<class T>
51    constexpr int popcount(T x) noexcept;     // C++20
52
53  // [bit.endian], endian
54  enum class endian {
55    little = see below,        // C++20
56    big = see below,           // C++20
57    native = see below         // C++20
58  };
59
60} // namespace std
61
62*/
63
64#include <__assert> // all public C++ headers provide the assertion handler
65#include <__bit/bit_cast.h>
66#include <__bit/byteswap.h>
67#include <__bits> // __libcpp_clz
68#include <__concepts/arithmetic.h>
69#include <__config>
70#include <limits>
71#include <type_traits>
72#include <version>
73
74#if defined(__IBMCPP__)
75#  include "__support/ibm/support.h"
76#endif
77#if defined(_LIBCPP_COMPILER_MSVC)
78#  include <intrin.h>
79#endif
80
81#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
82#  pragma GCC system_header
83#endif
84
85_LIBCPP_PUSH_MACROS
86#include <__undef_macros>
87
88_LIBCPP_BEGIN_NAMESPACE_STD
89
90template<class _Tp>
91_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
92_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
93{
94    static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
95    const unsigned int __dig = numeric_limits<_Tp>::digits;
96    if ((__cnt % __dig) == 0)
97        return __t;
98    return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
99}
100
101template<class _Tp>
102_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
103int __countl_zero(_Tp __t) _NOEXCEPT
104{
105    static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
106    if (__t == 0)
107        return numeric_limits<_Tp>::digits;
108
109    if      (sizeof(_Tp) <= sizeof(unsigned int))
110        return std::__libcpp_clz(static_cast<unsigned int>(__t))
111              - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
112    else if (sizeof(_Tp) <= sizeof(unsigned long))
113        return std::__libcpp_clz(static_cast<unsigned long>(__t))
114              - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
115    else if (sizeof(_Tp) <= sizeof(unsigned long long))
116        return std::__libcpp_clz(static_cast<unsigned long long>(__t))
117              - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
118    else
119    {
120        int __ret = 0;
121        int __iter = 0;
122        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
123        while (true) {
124            __t = std::__rotr(__t, __ulldigits);
125            if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
126                break;
127            __ret += __iter;
128            }
129        return __ret + __iter;
130    }
131}
132
133#if _LIBCPP_STD_VER > 17
134
135template <__libcpp_unsigned_integer _Tp>
136_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, unsigned int __cnt) noexcept {
137  const unsigned int __dig = numeric_limits<_Tp>::digits;
138  if ((__cnt % __dig) == 0)
139    return __t;
140  return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
141}
142
143template <__libcpp_unsigned_integer _Tp>
144_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, unsigned int __cnt) noexcept {
145  return std::__rotr(__t, __cnt);
146}
147
148template <__libcpp_unsigned_integer _Tp>
149_LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {
150  return std::__countl_zero(__t);
151}
152
153template <__libcpp_unsigned_integer _Tp>
154_LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
155  return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
156}
157
158template <__libcpp_unsigned_integer _Tp>
159_LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
160  if (__t == 0)
161    return numeric_limits<_Tp>::digits;
162
163  if (sizeof(_Tp) <= sizeof(unsigned int))
164    return std::__libcpp_ctz(static_cast<unsigned int>(__t));
165  else if (sizeof(_Tp) <= sizeof(unsigned long))
166    return std::__libcpp_ctz(static_cast<unsigned long>(__t));
167  else if (sizeof(_Tp) <= sizeof(unsigned long long))
168    return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
169  else {
170    int __ret = 0;
171    const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
172    while (static_cast<unsigned long long>(__t) == 0uLL) {
173      __ret += __ulldigits;
174      __t >>= __ulldigits;
175    }
176    return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
177  }
178}
179
180template <__libcpp_unsigned_integer _Tp>
181_LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
182  return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
183}
184
185template <__libcpp_unsigned_integer _Tp>
186_LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
187  if (sizeof(_Tp) <= sizeof(unsigned int))
188    return std::__libcpp_popcount(static_cast<unsigned int>(__t));
189  else if (sizeof(_Tp) <= sizeof(unsigned long))
190    return std::__libcpp_popcount(static_cast<unsigned long>(__t));
191  else if (sizeof(_Tp) <= sizeof(unsigned long long))
192    return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
193  else {
194    int __ret = 0;
195    while (__t != 0) {
196      __ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
197      __t >>= numeric_limits<unsigned long long>::digits;
198    }
199    return __ret;
200  }
201}
202
203template <__libcpp_unsigned_integer _Tp>
204_LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
205  return __t != 0 && (((__t & (__t - 1)) == 0));
206}
207
208// integral log base 2
209template <__libcpp_unsigned_integer _Tp>
210_LIBCPP_HIDE_FROM_ABI constexpr _Tp __bit_log2(_Tp __t) noexcept {
211  return numeric_limits<_Tp>::digits - 1 - std::countl_zero(__t);
212}
213
214template <__libcpp_unsigned_integer _Tp>
215_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_floor(_Tp __t) noexcept {
216  return __t == 0 ? 0 : _Tp{1} << std::__bit_log2(__t);
217}
218
219template <__libcpp_unsigned_integer _Tp>
220_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept {
221  if (__t < 2)
222    return 1;
223  const unsigned __n = numeric_limits<_Tp>::digits - std::countl_zero((_Tp)(__t - 1u));
224  _LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
225
226  if constexpr (sizeof(_Tp) >= sizeof(unsigned))
227    return _Tp{1} << __n;
228  else {
229    const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
230    const unsigned __retVal = 1u << (__n + __extra);
231    return (_Tp)(__retVal >> __extra);
232  }
233}
234
235template <__libcpp_unsigned_integer _Tp>
236_LIBCPP_HIDE_FROM_ABI constexpr int bit_width(_Tp __t) noexcept {
237  return __t == 0 ? 0 : std::__bit_log2(__t) + 1;
238}
239
240enum class endian {
241  little = 0xDEAD,
242  big = 0xFACE,
243#  if defined(_LIBCPP_LITTLE_ENDIAN)
244  native = little
245#  elif defined(_LIBCPP_BIG_ENDIAN)
246  native = big
247#  else
248  native = 0xCAFE
249#  endif
250};
251
252#endif // _LIBCPP_STD_VER > 17
253
254_LIBCPP_END_NAMESPACE_STD
255
256_LIBCPP_POP_MACROS
257
258#endif // _LIBCPP_BIT
259