aboutsummaryrefslogtreecommitdiff
path: root/libcxx/include/bit
diff options
context:
space:
mode:
Diffstat (limited to 'libcxx/include/bit')
-rw-r--r--libcxx/include/bit146
1 files changed, 15 insertions, 131 deletions
diff --git a/libcxx/include/bit b/libcxx/include/bit
index ae4605b19166..fe360179c5ca 100644
--- a/libcxx/include/bit
+++ b/libcxx/include/bit
@@ -17,13 +17,13 @@ namespace std {
// [bit.pow.two], integral powers of 2
template <class T>
- constexpr bool ispow2(T x) noexcept; // C++20
+ constexpr bool has_single_bit(T x) noexcept; // C++20
template <class T>
- constexpr T ceil2(T x); // C++20
+ constexpr T bit_ceil(T x); // C++20
template <class T>
- constexpr T floor2(T x) noexcept; // C++20
+ constexpr T bit_floor(T x) noexcept; // C++20
template <class T>
- constexpr T log2p1(T x) noexcept; // C++20
+ constexpr T bit_width(T x) noexcept; // C++20
// [bit.rotate], rotating
template<class T>
@@ -55,6 +55,7 @@ namespace std {
*/
#include <__config>
+#include <__bits>
#include <limits>
#include <type_traits>
#include <version>
@@ -76,122 +77,6 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
-#ifndef _LIBCPP_COMPILER_MSVC
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); }
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); }
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
-
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); }
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); }
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
-
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); }
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); }
-
-inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
-
-#else // _LIBCPP_COMPILER_MSVC
-
-// Precondition: __x != 0
-inline _LIBCPP_INLINE_VISIBILITY
-int __libcpp_ctz(unsigned __x) {
- static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
- static_assert(sizeof(unsigned long) == 4, "");
- unsigned long __where;
- if (_BitScanForward(&__where, __x))
- return static_cast<int>(__where);
- return 32;
-}
-
-inline _LIBCPP_INLINE_VISIBILITY
-int __libcpp_ctz(unsigned long __x) {
- static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
- return __ctz(static_cast<unsigned>(__x));
-}
-
-inline _LIBCPP_INLINE_VISIBILITY
-int __libcpp_ctz(unsigned long long __x) {
- unsigned long __where;
-#if defined(_LIBCPP_HAS_BITSCAN64)
- (defined(_M_AMD64) || defined(__x86_64__))
- if (_BitScanForward64(&__where, __x))
- return static_cast<int>(__where);
-#else
- // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
- if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
- return static_cast<int>(__where);
- if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
- return static_cast<int>(__where + 32);
-#endif
- return 64;
-}
-
-// Precondition: __x != 0
-inline _LIBCPP_INLINE_VISIBILITY
-int __libcpp_clz(unsigned __x) {
- static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
- static_assert(sizeof(unsigned long) == 4, "");
- unsigned long __where;
- if (_BitScanReverse(&__where, __x))
- return static_cast<int>(31 - __where);
- return 32; // Undefined Behavior.
-}
-
-inline _LIBCPP_INLINE_VISIBILITY
-int __libcpp_clz(unsigned long __x) {
- static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
- return __libcpp_clz(static_cast<unsigned>(__x));
-}
-
-inline _LIBCPP_INLINE_VISIBILITY
-int __libcpp_clz(unsigned long long __x) {
- unsigned long __where;
-#if defined(_LIBCPP_HAS_BITSCAN64)
- if (_BitScanReverse64(&__where, __x))
- return static_cast<int>(63 - __where);
-#else
- // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
- if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
- return static_cast<int>(63 - (__where + 32));
- if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
- return static_cast<int>(63 - __where);
-#endif
- return 64; // Undefined Behavior.
-}
-
-inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
- static_assert(sizeof(unsigned) == 4, "");
- return __popcnt(__x);
-}
-
-inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
- static_assert(sizeof(unsigned long) == 4, "");
- return __popcnt(__x);
-}
-
-inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
- static_assert(sizeof(unsigned long long) == 8, "");
- return __popcnt64(__x);
-}
-
-#endif // _LIBCPP_COMPILER_MSVC
template <class _Tp>
using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
@@ -343,14 +228,14 @@ _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 std::numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
+ return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
-bool __ispow2(_Tp __t) _NOEXCEPT
+bool __has_single_bit(_Tp __t) _NOEXCEPT
{
- static_assert(__bitop_unsigned_integer<_Tp>::value, "__ispow2 requires unsigned");
+ static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned");
return __t != 0 && (((__t & (__t - 1)) == 0));
}
@@ -399,7 +284,7 @@ _LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
countr_zero(_Tp __t) noexcept
{
- return __countr_zero(__t);
+ return __countr_zero(__t);
}
@@ -424,15 +309,15 @@ popcount(_Tp __t) noexcept
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
-ispow2(_Tp __t) noexcept
+has_single_bit(_Tp __t) noexcept
{
- return __ispow2(__t);
+ return __has_single_bit(__t);
}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
-floor2(_Tp __t) noexcept
+bit_floor(_Tp __t) noexcept
{
return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
}
@@ -440,11 +325,11 @@ floor2(_Tp __t) noexcept
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
-ceil2(_Tp __t) noexcept
+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 ceil2");
+ _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;
@@ -459,12 +344,11 @@ ceil2(_Tp __t) noexcept
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
-log2p1(_Tp __t) noexcept
+bit_width(_Tp __t) noexcept
{
return __t == 0 ? 0 : __bit_log2(__t) + 1;
}
-
enum class endian
{
little = 0xDEAD,