2 //===----------------------------------------------------------------------===//
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
8 //===---------------------------------------------------------------------===//
17 // [bit.cast], bit_cast
18 template<class To, class From>
19 constexpr To bit_cast(const From& from) noexcept; // C++20
21 // [bit.byteswap], byteswap
23 constexpr T byteswap(T value) noexcept; // C++23
25 // [bit.pow.two], integral powers of 2
27 constexpr bool has_single_bit(T x) noexcept; // C++20
29 constexpr T bit_ceil(T x); // C++20
31 constexpr T bit_floor(T x) noexcept; // C++20
33 constexpr int bit_width(T x) noexcept; // C++20
35 // [bit.rotate], rotating
37 constexpr T rotl(T x, unsigned int s) noexcept; // C++20
39 constexpr T rotr(T x, unsigned int s) noexcept; // C++20
41 // [bit.count], counting
43 constexpr int countl_zero(T x) noexcept; // C++20
45 constexpr int countl_one(T x) noexcept; // C++20
47 constexpr int countr_zero(T x) noexcept; // C++20
49 constexpr int countr_one(T x) noexcept; // C++20
51 constexpr int popcount(T x) noexcept; // C++20
53 // [bit.endian], endian
55 little = see below, // C++20
56 big = see below, // C++20
57 native = see below // C++20
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>
71 #include <type_traits>
74 #ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
78 #if defined(_LIBCPP_COMPILER_MSVC)
82 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
83 # pragma GCC system_header
87 #include <__undef_macros>
89 _LIBCPP_BEGIN_NAMESPACE_STD
92 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
93 _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
95 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
96 const unsigned int __dig = numeric_limits<_Tp>::digits;
97 if ((__cnt % __dig) == 0)
99 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
103 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
104 int __countl_zero(_Tp __t) _NOEXCEPT
106 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
108 return numeric_limits<_Tp>::digits;
110 if (sizeof(_Tp) <= sizeof(unsigned int))
111 return std::__libcpp_clz(static_cast<unsigned int>(__t))
112 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
113 else if (sizeof(_Tp) <= sizeof(unsigned long))
114 return std::__libcpp_clz(static_cast<unsigned long>(__t))
115 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
116 else if (sizeof(_Tp) <= sizeof(unsigned long long))
117 return std::__libcpp_clz(static_cast<unsigned long long>(__t))
118 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
123 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
125 __t = std::__rotr(__t, __ulldigits);
126 if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
130 return __ret + __iter;
134 #if _LIBCPP_STD_VER > 17
136 template <__libcpp_unsigned_integer _Tp>
137 _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, unsigned int __cnt) noexcept {
138 const unsigned int __dig = numeric_limits<_Tp>::digits;
139 if ((__cnt % __dig) == 0)
141 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
144 template <__libcpp_unsigned_integer _Tp>
145 _LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, unsigned int __cnt) noexcept {
146 return std::__rotr(__t, __cnt);
149 template <__libcpp_unsigned_integer _Tp>
150 _LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {
151 return std::__countl_zero(__t);
154 template <__libcpp_unsigned_integer _Tp>
155 _LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
156 return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
159 template <__libcpp_unsigned_integer _Tp>
160 _LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
162 return numeric_limits<_Tp>::digits;
164 if (sizeof(_Tp) <= sizeof(unsigned int))
165 return std::__libcpp_ctz(static_cast<unsigned int>(__t));
166 else if (sizeof(_Tp) <= sizeof(unsigned long))
167 return std::__libcpp_ctz(static_cast<unsigned long>(__t));
168 else if (sizeof(_Tp) <= sizeof(unsigned long long))
169 return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
172 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
173 while (static_cast<unsigned long long>(__t) == 0uLL) {
174 __ret += __ulldigits;
177 return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
181 template <__libcpp_unsigned_integer _Tp>
182 _LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
183 return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
186 template <__libcpp_unsigned_integer _Tp>
187 _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
188 if (sizeof(_Tp) <= sizeof(unsigned int))
189 return std::__libcpp_popcount(static_cast<unsigned int>(__t));
190 else if (sizeof(_Tp) <= sizeof(unsigned long))
191 return std::__libcpp_popcount(static_cast<unsigned long>(__t));
192 else if (sizeof(_Tp) <= sizeof(unsigned long long))
193 return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
197 __ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
198 __t >>= numeric_limits<unsigned long long>::digits;
204 template <__libcpp_unsigned_integer _Tp>
205 _LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
206 return __t != 0 && (((__t & (__t - 1)) == 0));
209 // integral log base 2
210 template <__libcpp_unsigned_integer _Tp>
211 _LIBCPP_HIDE_FROM_ABI constexpr _Tp __bit_log2(_Tp __t) noexcept {
212 return numeric_limits<_Tp>::digits - 1 - std::countl_zero(__t);
215 template <__libcpp_unsigned_integer _Tp>
216 _LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_floor(_Tp __t) noexcept {
217 return __t == 0 ? 0 : _Tp{1} << std::__bit_log2(__t);
220 template <__libcpp_unsigned_integer _Tp>
221 _LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept {
224 const unsigned __n = numeric_limits<_Tp>::digits - std::countl_zero((_Tp)(__t - 1u));
225 _LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
227 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
228 return _Tp{1} << __n;
230 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
231 const unsigned __retVal = 1u << (__n + __extra);
232 return (_Tp)(__retVal >> __extra);
236 template <__libcpp_unsigned_integer _Tp>
237 _LIBCPP_HIDE_FROM_ABI constexpr int bit_width(_Tp __t) noexcept {
238 return __t == 0 ? 0 : std::__bit_log2(__t) + 1;
244 # if defined(_LIBCPP_LITTLE_ENDIAN)
246 # elif defined(_LIBCPP_BIG_ENDIAN)
253 #endif // _LIBCPP_STD_VER > 17
255 _LIBCPP_END_NAMESPACE_STD
259 #endif // _LIBCPP_BIT