diff options
Diffstat (limited to 'contrib/llvm-project/libcxx/include/bit')
-rw-r--r-- | contrib/llvm-project/libcxx/include/bit | 343 |
1 files changed, 32 insertions, 311 deletions
diff --git a/contrib/llvm-project/libcxx/include/bit b/contrib/llvm-project/libcxx/include/bit index f8c37c3d6bbf..84e2080377e4 100644 --- a/contrib/llvm-project/libcxx/include/bit +++ b/contrib/llvm-project/libcxx/include/bit @@ -1,5 +1,5 @@ // -*- C++ -*- -//===------------------------------ bit ----------------------------------===// +//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -14,6 +14,13 @@ bit synopsis namespace std { + // [bit.cast], bit_cast + template<class To, class From> + constexpr To bit_cast(const From& from) noexcept; // C++20 + + // [bit.byteswap], byteswap + template<class T> + constexpr T byteswap(T value) noexcept; // C++23 // [bit.pow.two], integral powers of 2 template <class T> @@ -23,13 +30,13 @@ namespace std { template <class T> constexpr T bit_floor(T x) noexcept; // C++20 template <class T> - constexpr T bit_width(T x) noexcept; // C++20 + constexpr int bit_width(T x) noexcept; // C++20 // [bit.rotate], rotating template<class T> - constexpr T rotl(T x, unsigned int s) noexcept; // C++20 + constexpr T rotl(T x, int s) noexcept; // C++20 template<class T> - constexpr T rotr(T x, unsigned int s) noexcept; // C++20 + constexpr T rotr(T x, int s) noexcept; // C++20 // [bit.count], counting template<class T> @@ -48,324 +55,38 @@ namespace std { little = see below, // C++20 big = see below, // C++20 native = see below // C++20 -}; + }; } // namespace std */ +#include <__assert> // all public C++ headers provide the assertion handler +#include <__bit/bit_cast.h> +#include <__bit/bit_ceil.h> +#include <__bit/bit_floor.h> +#include <__bit/bit_log2.h> +#include <__bit/bit_width.h> +#include <__bit/blsr.h> +#include <__bit/byteswap.h> +#include <__bit/countl.h> +#include <__bit/countr.h> +#include <__bit/endian.h> +#include <__bit/has_single_bit.h> +#include <__bit/popcount.h> +#include <__bit/rotate.h> #include <__config> -#include <__bits> -#include <limits> -#include <type_traits> #include <version> -#include <__debug> - -#if defined(__IBMCPP__) -#include "__support/ibm/support.h" -#endif -#if defined(_LIBCPP_COMPILER_MSVC) -#include <intrin.h> -#endif #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) -#pragma GCC system_header +# pragma GCC system_header #endif -_LIBCPP_PUSH_MACROS -#include <__undef_macros> - -_LIBCPP_BEGIN_NAMESPACE_STD - - -template <class _Tp> -using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool, - is_integral<_Tp>::value && - is_unsigned<_Tp>::value && - _IsNotSame<typename remove_cv<_Tp>::type, bool>::value && - _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value && - _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value && - _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value && - _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value - >; - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned"); - const unsigned int __dig = numeric_limits<_Tp>::digits; - if ((__cnt % __dig) == 0) - return __t; - return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig))); -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned"); - const unsigned int __dig = numeric_limits<_Tp>::digits; - if ((__cnt % __dig) == 0) - return __t; - return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig))); -} - - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -int __countr_zero(_Tp __t) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned"); - if (__t == 0) - return numeric_limits<_Tp>::digits; - - if (sizeof(_Tp) <= sizeof(unsigned int)) - return __libcpp_ctz(static_cast<unsigned int>(__t)); - else if (sizeof(_Tp) <= sizeof(unsigned long)) - return __libcpp_ctz(static_cast<unsigned long>(__t)); - else if (sizeof(_Tp) <= sizeof(unsigned long long)) - return __libcpp_ctz(static_cast<unsigned long long>(__t)); - else - { - int __ret = 0; - int __iter = 0; - const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; - while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits) - { - __ret += __iter; - __t >>= __ulldigits; - } - return __ret + __iter; - } -} - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -int __countl_zero(_Tp __t) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned"); - if (__t == 0) - return numeric_limits<_Tp>::digits; - - if (sizeof(_Tp) <= sizeof(unsigned int)) - return __libcpp_clz(static_cast<unsigned int>(__t)) - - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits); - else if (sizeof(_Tp) <= sizeof(unsigned long)) - return __libcpp_clz(static_cast<unsigned long>(__t)) - - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits); - else if (sizeof(_Tp) <= sizeof(unsigned long long)) - return __libcpp_clz(static_cast<unsigned long long>(__t)) - - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits); - else - { - int __ret = 0; - int __iter = 0; - const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; - while (true) { - __t = __rotr(__t, __ulldigits); - if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits) - break; - __ret += __iter; - } - return __ret + __iter; - } -} - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -int __countl_one(_Tp __t) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned"); - return __t != numeric_limits<_Tp>::max() - ? __countl_zero(static_cast<_Tp>(~__t)) - : numeric_limits<_Tp>::digits; -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -int __countr_one(_Tp __t) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned"); - return __t != numeric_limits<_Tp>::max() - ? __countr_zero(static_cast<_Tp>(~__t)) - : numeric_limits<_Tp>::digits; -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -int -__popcount(_Tp __t) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned"); - if (sizeof(_Tp) <= sizeof(unsigned int)) - return __libcpp_popcount(static_cast<unsigned int>(__t)); - else if (sizeof(_Tp) <= sizeof(unsigned long)) - return __libcpp_popcount(static_cast<unsigned long>(__t)); - else if (sizeof(_Tp) <= sizeof(unsigned long long)) - return __libcpp_popcount(static_cast<unsigned long long>(__t)); - else - { - int __ret = 0; - while (__t != 0) - { - __ret += __libcpp_popcount(static_cast<unsigned long long>(__t)); - __t >>= numeric_limits<unsigned long long>::digits; - } - return __ret; - } -} - - -// integral log base 2 -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 -unsigned __bit_log2(_Tp __t) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned"); - return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t); -} - -template <class _Tp> -_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR -bool __has_single_bit(_Tp __t) _NOEXCEPT -{ - static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned"); - return __t != 0 && (((__t & (__t - 1)) == 0)); -} - - -#if _LIBCPP_STD_VER > 17 - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> -rotl(_Tp __t, unsigned int __cnt) noexcept -{ - return __rotl(__t, __cnt); -} - - -// rotr -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> -rotr(_Tp __t, unsigned int __cnt) noexcept -{ - return __rotr(__t, __cnt); -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> -countl_zero(_Tp __t) noexcept -{ - return __countl_zero(__t); -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> -countl_one(_Tp __t) noexcept -{ - return __countl_one(__t); -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> -countr_zero(_Tp __t) noexcept -{ - return __countr_zero(__t); -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> -countr_one(_Tp __t) noexcept -{ - return __countr_one(__t); -} - - -template<class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> -popcount(_Tp __t) noexcept -{ - return __popcount(__t); -} - - -template <class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool> -has_single_bit(_Tp __t) noexcept -{ - return __has_single_bit(__t); -} - -template <class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> -bit_floor(_Tp __t) noexcept -{ - return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); -} - -template <class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> -bit_ceil(_Tp __t) noexcept -{ - if (__t < 2) return 1; - const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u)); - _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil"); - - if constexpr (sizeof(_Tp) >= sizeof(unsigned)) - return _Tp{1} << __n; - else - { - const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; - const unsigned __retVal = 1u << (__n + __extra); - return (_Tp) (__retVal >> __extra); - } -} - -template <class _Tp> -_LIBCPP_INLINE_VISIBILITY constexpr -enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> -bit_width(_Tp __t) noexcept -{ - return __t == 0 ? 0 : __bit_log2(__t) + 1; -} - -enum class endian -{ - little = 0xDEAD, - big = 0xFACE, -#if defined(_LIBCPP_LITTLE_ENDIAN) - native = little -#elif defined(_LIBCPP_BIG_ENDIAN) - native = big -#else - native = 0xCAFE +#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 +# include <cstdlib> +# include <iosfwd> +# include <limits> +# include <type_traits> #endif -}; - -#endif // _LIBCPP_STD_VER > 17 - -_LIBCPP_END_NAMESPACE_STD - -_LIBCPP_POP_MACROS #endif // _LIBCPP_BIT |