2018-08-18 00:07:48 +08:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===------------------------------ bit ----------------------------------===//
|
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-08-18 00:07:48 +08:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_BIT
|
|
|
|
#define _LIBCPP_BIT
|
|
|
|
|
|
|
|
/*
|
|
|
|
bit synopsis
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
template <class T>
|
|
|
|
constexpr bool ispow2(T x) noexcept; // C++20
|
|
|
|
template <class T>
|
|
|
|
constexpr T ceil2(T x); // C++20
|
|
|
|
template <class T>
|
|
|
|
constexpr T floor2(T x) noexcept; // C++20
|
|
|
|
template <class T>
|
|
|
|
constexpr T log2p1(T x) noexcept; // C++20
|
|
|
|
|
|
|
|
// 23.20.2, rotating
|
|
|
|
template<class T>
|
|
|
|
constexpr T rotl(T x, unsigned int s) noexcept; // C++20
|
|
|
|
template<class T>
|
|
|
|
constexpr T rotr(T x, unsigned int s) noexcept; // C++20
|
|
|
|
|
|
|
|
// 23.20.3, counting
|
|
|
|
template<class T>
|
|
|
|
constexpr int countl_zero(T x) noexcept; // C++20
|
|
|
|
template<class T>
|
|
|
|
constexpr int countl_one(T x) noexcept; // C++20
|
|
|
|
template<class T>
|
|
|
|
constexpr int countr_zero(T x) noexcept; // C++20
|
|
|
|
template<class T>
|
|
|
|
constexpr int countr_one(T x) noexcept; // C++20
|
|
|
|
template<class T>
|
|
|
|
constexpr int popcount(T x) noexcept; // C++20
|
|
|
|
|
2019-10-24 01:40:15 +08:00
|
|
|
// 20.15.9, endian
|
2019-07-23 12:20:19 +08:00
|
|
|
enum class endian {
|
|
|
|
little = see below, // C++20
|
|
|
|
big = see below, // C++20
|
|
|
|
native = see below // C++20
|
|
|
|
};
|
|
|
|
|
2018-08-18 00:07:48 +08:00
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <__config>
|
2019-07-02 07:00:32 +08:00
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
2018-09-13 03:41:40 +08:00
|
|
|
#include <version>
|
2019-07-02 07:00:32 +08:00
|
|
|
#include <__debug>
|
2018-08-18 00:07:48 +08:00
|
|
|
|
|
|
|
#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
|
|
|
|
#endif
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2018-08-18 00:07:48 +08:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2018-08-18 01:27:25 +08:00
|
|
|
#ifndef _LIBCPP_COMPILER_MSVC
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
|
2018-08-18 01:27:25 +08:00
|
|
|
|
|
|
|
#else // _LIBCPP_COMPILER_MSVC
|
|
|
|
|
2018-08-18 00:07:48 +08:00
|
|
|
// Precondition: __x != 0
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_ctz(unsigned __x) {
|
2018-08-18 00:07:48 +08:00
|
|
|
static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
|
|
|
|
static_assert(sizeof(unsigned long) == 4, "");
|
2018-08-18 01:27:25 +08:00
|
|
|
unsigned long __where;
|
|
|
|
if (_BitScanForward(&__where, __x))
|
|
|
|
return static_cast<int>(__where);
|
2018-08-18 00:07:48 +08:00
|
|
|
return 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_ctz(unsigned long __x) {
|
2018-08-18 00:07:48 +08:00
|
|
|
static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
|
|
|
|
return __ctz(static_cast<unsigned>(__x));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_ctz(unsigned long long __x) {
|
2018-08-18 01:27:25 +08:00
|
|
|
unsigned long __where;
|
2018-08-18 00:07:48 +08:00
|
|
|
#if defined(_LIBCPP_HAS_BITSCAN64)
|
|
|
|
(defined(_M_AMD64) || defined(__x86_64__))
|
2018-08-18 01:27:25 +08:00
|
|
|
if (_BitScanForward64(&__where, __x))
|
|
|
|
return static_cast<int>(__where);
|
2018-08-18 00:07:48 +08:00
|
|
|
#else
|
|
|
|
// Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
|
2018-08-18 01:27:25 +08:00
|
|
|
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);
|
2018-08-18 00:07:48 +08:00
|
|
|
#endif
|
|
|
|
return 64;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precondition: __x != 0
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_clz(unsigned __x) {
|
2018-08-18 00:07:48 +08:00
|
|
|
static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
|
|
|
|
static_assert(sizeof(unsigned long) == 4, "");
|
2018-08-18 01:27:25 +08:00
|
|
|
unsigned long __where;
|
|
|
|
if (_BitScanReverse(&__where, __x))
|
|
|
|
return static_cast<int>(31 - __where);
|
2018-08-18 00:07:48 +08:00
|
|
|
return 32; // Undefined Behavior.
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_clz(unsigned long __x) {
|
2018-08-18 00:07:48 +08:00
|
|
|
static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
|
2019-07-12 09:01:55 +08:00
|
|
|
return __libcpp_clz(static_cast<unsigned>(__x));
|
2018-08-18 00:07:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_INLINE_VISIBILITY
|
2019-07-12 09:01:55 +08:00
|
|
|
int __libcpp_clz(unsigned long long __x) {
|
2018-08-18 01:27:25 +08:00
|
|
|
unsigned long __where;
|
2018-08-18 00:07:48 +08:00
|
|
|
#if defined(_LIBCPP_HAS_BITSCAN64)
|
2018-08-18 01:27:25 +08:00
|
|
|
if (_BitScanReverse64(&__where, __x))
|
|
|
|
return static_cast<int>(63 - __where);
|
2018-08-18 00:07:48 +08:00
|
|
|
#else
|
2018-08-18 01:27:25 +08:00
|
|
|
// 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);
|
2018-08-18 00:07:48 +08:00
|
|
|
#endif
|
|
|
|
return 64; // Undefined Behavior.
|
|
|
|
}
|
|
|
|
|
2019-07-12 09:01:55 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
|
2018-08-18 00:07:48 +08:00
|
|
|
static_assert(sizeof(unsigned) == 4, "");
|
|
|
|
return __popcnt(__x);
|
|
|
|
}
|
|
|
|
|
2019-07-12 09:01:55 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
|
2018-08-18 00:07:48 +08:00
|
|
|
static_assert(sizeof(unsigned long) == 4, "");
|
|
|
|
return __popcnt(__x);
|
|
|
|
}
|
|
|
|
|
2019-07-12 09:01:55 +08:00
|
|
|
inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
|
2018-08-18 00:07:48 +08:00
|
|
|
static_assert(sizeof(unsigned long long) == 8, "");
|
|
|
|
return __popcnt64(__x);
|
|
|
|
}
|
|
|
|
|
2018-08-18 01:27:25 +08:00
|
|
|
#endif // _LIBCPP_COMPILER_MSVC
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
template <class _Tp>
|
|
|
|
using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
|
2019-07-12 09:01:55 +08:00
|
|
|
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
|
2019-07-02 07:00:32 +08:00
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
|
|
template<class _Tp>
|
2019-07-12 09:01:55 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
|
|
|
_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
|
2019-07-02 07:00:32 +08:00
|
|
|
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>
|
2019-07-12 09:01:55 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
|
|
|
_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
|
2019-07-02 07:00:32 +08:00
|
|
|
const unsigned int __dig = numeric_limits<_Tp>::digits;
|
|
|
|
if ((__cnt % __dig) == 0)
|
|
|
|
return __t;
|
|
|
|
return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-12 09:01:55 +08:00
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
template<class _Tp>
|
2019-07-12 09:01:55 +08:00
|
|
|
_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
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned");
|
2019-07-02 07:00:32 +08:00
|
|
|
if (__t == 0)
|
|
|
|
return numeric_limits<_Tp>::digits;
|
|
|
|
|
2019-07-12 09:01:55 +08:00
|
|
|
if (sizeof(_Tp) <= sizeof(unsigned int))
|
|
|
|
return __libcpp_clz(static_cast<unsigned int>(__t))
|
2019-07-02 07:00:32 +08:00
|
|
|
- (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
|
2019-07-12 09:01:55 +08:00
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long))
|
|
|
|
return __libcpp_clz(static_cast<unsigned long>(__t))
|
2019-07-02 07:00:32 +08:00
|
|
|
- (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
|
2019-07-12 09:01:55 +08:00
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long long))
|
|
|
|
return __libcpp_clz(static_cast<unsigned long long>(__t))
|
2019-07-02 07:00:32 +08:00
|
|
|
- (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) {
|
2019-07-12 09:01:55 +08:00
|
|
|
__t = __rotr(__t, __ulldigits);
|
|
|
|
if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
|
2019-07-02 07:00:32 +08:00
|
|
|
break;
|
|
|
|
__ret += __iter;
|
|
|
|
}
|
|
|
|
return __ret + __iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _Tp>
|
2019-07-12 09:01:55 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
|
|
|
int __countl_one(_Tp __t) _NOEXCEPT
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
|
2019-07-02 07:00:32 +08:00
|
|
|
return __t != numeric_limits<_Tp>::max()
|
2019-07-12 09:01:55 +08:00
|
|
|
? __countl_zero(static_cast<_Tp>(~__t))
|
2019-07-02 07:00:32 +08:00
|
|
|
: numeric_limits<_Tp>::digits;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class _Tp>
|
2019-07-12 09:01:55 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
|
|
|
int __countr_one(_Tp __t) _NOEXCEPT
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
|
2019-07-12 09:01:55 +08:00
|
|
|
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));
|
2019-07-02 07:00:32 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int __ret = 0;
|
2019-07-12 09:01:55 +08:00
|
|
|
while (__t != 0)
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
__ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
|
|
|
|
__t >>= numeric_limits<unsigned long long>::digits;
|
2019-07-02 07:00:32 +08:00
|
|
|
}
|
2019-07-12 09:01:55 +08:00
|
|
|
return __ret;
|
2019-07-02 07:00:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-12 09:01:55 +08:00
|
|
|
// 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 std::numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
|
|
|
bool __ispow2(_Tp __t) _NOEXCEPT
|
|
|
|
{
|
|
|
|
static_assert(__bitop_unsigned_integer<_Tp>::value, "__ispow2 requires unsigned");
|
2019-10-24 01:40:15 +08:00
|
|
|
return __t != 0 && (((__t & (__t - 1)) == 0));
|
2019-07-12 09:01:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
template<class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
|
|
|
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
|
2019-07-12 09:01:55 +08:00
|
|
|
countl_zero(_Tp __t) noexcept
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
return __countl_zero(__t);
|
2019-07-02 07:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
|
|
|
enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
|
2019-07-12 09:01:55 +08:00
|
|
|
countl_one(_Tp __t) noexcept
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2019-07-12 09:01:55 +08:00
|
|
|
return __countl_one(__t);
|
2019-07-02 07:00:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
2019-07-12 09:01:55 +08:00
|
|
|
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);
|
|
|
|
}
|
2019-07-02 07:00:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
|
|
|
enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
|
2019-07-12 09:01:55 +08:00
|
|
|
ispow2(_Tp __t) noexcept
|
|
|
|
{
|
|
|
|
return __ispow2(__t);
|
|
|
|
}
|
2019-07-02 07:00:32 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
|
|
|
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
|
2019-07-12 09:01:55 +08:00
|
|
|
floor2(_Tp __t) noexcept
|
|
|
|
{
|
|
|
|
return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
|
|
|
|
}
|
2019-07-02 07:00:32 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
|
|
|
enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
|
|
|
|
ceil2(_Tp __t) noexcept
|
|
|
|
{
|
|
|
|
if (__t < 2) return 1;
|
|
|
|
const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
|
2019-07-02 11:21:16 +08:00
|
|
|
_LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to ceil2");
|
2019-07-02 07:00:32 +08:00
|
|
|
|
|
|
|
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>
|
|
|
|
log2p1(_Tp __t) noexcept
|
2019-07-12 09:01:55 +08:00
|
|
|
{
|
|
|
|
return __t == 0 ? 0 : __bit_log2(__t) + 1;
|
|
|
|
}
|
2019-07-02 07:00:32 +08:00
|
|
|
|
2019-07-23 12:20:19 +08:00
|
|
|
|
|
|
|
enum class endian
|
|
|
|
{
|
|
|
|
little = 0xDEAD,
|
|
|
|
big = 0xFACE,
|
|
|
|
#if defined(_LIBCPP_LITTLE_ENDIAN)
|
|
|
|
native = little
|
|
|
|
#elif defined(_LIBCPP_BIG_ENDIAN)
|
|
|
|
native = big
|
|
|
|
#else
|
|
|
|
native = 0xCAFE
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
#endif // _LIBCPP_STD_VER > 17
|
|
|
|
|
2018-08-18 00:07:48 +08:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2019-07-02 07:00:32 +08:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2018-08-18 00:07:48 +08:00
|
|
|
#endif // _LIBCPP_BIT
|