forked from OSchip/llvm-project
[libc++][NFC] Use concepts in <bit>.
All supported compilers have concepts support so use that in the C++20 functions in <bit>. s/_LIBCPP_INLINE_VISIBILITY/_LIBCPP_HIDE_FROM_ABI/ as drive-by fix. Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D127594
This commit is contained in:
parent
aaeb958ced
commit
23b10a4a66
|
@ -21,14 +21,12 @@ _LIBCPP_BEGIN_NAMESPACE_STD
|
||||||
|
|
||||||
#if _LIBCPP_STD_VER > 17
|
#if _LIBCPP_STD_VER > 17
|
||||||
|
|
||||||
template<class _ToType, class _FromType, class = enable_if_t<
|
template <class _ToType, class _FromType>
|
||||||
sizeof(_ToType) == sizeof(_FromType) &&
|
requires(sizeof(_ToType) == sizeof(_FromType) &&
|
||||||
is_trivially_copyable_v<_ToType> &&
|
is_trivially_copyable_v<_ToType> &&
|
||||||
is_trivially_copyable_v<_FromType>
|
is_trivially_copyable_v<_FromType>)
|
||||||
>>
|
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _ToType bit_cast(const _FromType& __from) noexcept {
|
||||||
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
|
return __builtin_bit_cast(_ToType, __from);
|
||||||
constexpr _ToType bit_cast(_FromType const& __from) noexcept {
|
|
||||||
return __builtin_bit_cast(_ToType, __from);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // _LIBCPP_STD_VER > 17
|
#endif // _LIBCPP_STD_VER > 17
|
||||||
|
|
|
@ -65,6 +65,7 @@ namespace std {
|
||||||
#include <__bit/bit_cast.h>
|
#include <__bit/bit_cast.h>
|
||||||
#include <__bit/byteswap.h>
|
#include <__bit/byteswap.h>
|
||||||
#include <__bits> // __libcpp_clz
|
#include <__bits> // __libcpp_clz
|
||||||
|
#include <__concepts/arithmetic.h>
|
||||||
#include <__config>
|
#include <__config>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
@ -87,7 +88,7 @@ _LIBCPP_PUSH_MACROS
|
||||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
|
||||||
|
@ -98,7 +99,7 @@ _Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
|
||||||
|
@ -109,7 +110,7 @@ _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
int __countr_zero(_Tp __t) _NOEXCEPT
|
int __countr_zero(_Tp __t) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
|
||||||
|
@ -136,7 +137,7 @@ int __countr_zero(_Tp __t) _NOEXCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
int __countl_zero(_Tp __t) _NOEXCEPT
|
int __countl_zero(_Tp __t) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
|
||||||
|
@ -168,7 +169,7 @@ int __countl_zero(_Tp __t) _NOEXCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
int __countl_one(_Tp __t) _NOEXCEPT
|
int __countl_one(_Tp __t) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
|
||||||
|
@ -178,7 +179,7 @@ int __countl_one(_Tp __t) _NOEXCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
int __countr_one(_Tp __t) _NOEXCEPT
|
int __countr_one(_Tp __t) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
|
||||||
|
@ -188,7 +189,7 @@ int __countr_one(_Tp __t) _NOEXCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
int __popcount(_Tp __t) _NOEXCEPT
|
int __popcount(_Tp __t) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
|
||||||
|
@ -212,7 +213,7 @@ int __popcount(_Tp __t) _NOEXCEPT
|
||||||
|
|
||||||
// integral log base 2
|
// integral log base 2
|
||||||
template<class _Tp>
|
template<class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
unsigned __bit_log2(_Tp __t) _NOEXCEPT
|
unsigned __bit_log2(_Tp __t) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
|
||||||
|
@ -220,7 +221,7 @@ unsigned __bit_log2(_Tp __t) _NOEXCEPT
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tp>
|
template <class _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
|
||||||
bool __has_single_bit(_Tp __t) _NOEXCEPT
|
bool __has_single_bit(_Tp __t) _NOEXCEPT
|
||||||
{
|
{
|
||||||
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
|
||||||
|
@ -229,103 +230,70 @@ bool __has_single_bit(_Tp __t) _NOEXCEPT
|
||||||
|
|
||||||
#if _LIBCPP_STD_VER > 17
|
#if _LIBCPP_STD_VER > 17
|
||||||
|
|
||||||
template<class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, unsigned int __cnt) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
return __rotl(__t, __cnt);
|
||||||
rotl(_Tp __t, unsigned int __cnt) noexcept
|
|
||||||
{
|
|
||||||
return __rotl(__t, __cnt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, unsigned int __cnt) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
return __rotr(__t, __cnt);
|
||||||
rotr(_Tp __t, unsigned int __cnt) noexcept
|
|
||||||
{
|
|
||||||
return __rotr(__t, __cnt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
return __countl_zero(__t);
|
||||||
countl_zero(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __countl_zero(__t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
return __countl_one(__t);
|
||||||
countl_one(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __countl_one(__t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
return __countr_zero(__t);
|
||||||
countr_zero(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __countr_zero(__t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
return __countr_one(__t);
|
||||||
countr_one(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __countr_one(__t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
return __popcount(__t);
|
||||||
popcount(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __popcount(__t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool>
|
return __has_single_bit(__t);
|
||||||
has_single_bit(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __has_single_bit(__t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_floor(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
|
||||||
bit_floor(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
if (__t < 2)
|
||||||
bit_ceil(_Tp __t) noexcept
|
return 1;
|
||||||
{
|
const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
|
||||||
if (__t < 2) return 1;
|
_LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
|
||||||
const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
|
|
||||||
_LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
|
|
||||||
|
|
||||||
if constexpr (sizeof(_Tp) >= sizeof(unsigned))
|
if constexpr (sizeof(_Tp) >= sizeof(unsigned))
|
||||||
return _Tp{1} << __n;
|
return _Tp{1} << __n;
|
||||||
else
|
else {
|
||||||
{
|
const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
|
||||||
const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
|
const unsigned __retVal = 1u << (__n + __extra);
|
||||||
const unsigned __retVal = 1u << (__n + __extra);
|
return (_Tp)(__retVal >> __extra);
|
||||||
return (_Tp) (__retVal >> __extra);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tp>
|
template <__libcpp_unsigned_integer _Tp>
|
||||||
_LIBCPP_INLINE_VISIBILITY constexpr
|
_LIBCPP_HIDE_FROM_ABI constexpr int bit_width(_Tp __t) noexcept {
|
||||||
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
return __t == 0 ? 0 : __bit_log2(__t) + 1;
|
||||||
bit_width(_Tp __t) noexcept
|
|
||||||
{
|
|
||||||
return __t == 0 ? 0 : __bit_log2(__t) + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum class endian
|
enum class endian
|
||||||
|
|
Loading…
Reference in New Issue