2018-08-18 00:07:48 +08:00
|
|
|
// -*- C++ -*-
|
2021-11-18 05:25:01 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-08-18 00:07:48 +08:00
|
|
|
//
|
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 {
|
2020-05-29 02:28:38 +08:00
|
|
|
// [bit.cast], bit_cast
|
|
|
|
template<class To, class From>
|
|
|
|
constexpr To bit_cast(const From& from) noexcept; // C++20
|
2018-08-18 00:07:48 +08:00
|
|
|
|
2020-05-29 02:28:09 +08:00
|
|
|
// [bit.pow.two], integral powers of 2
|
2019-07-02 07:00:32 +08:00
|
|
|
template <class T>
|
2020-11-24 21:50:49 +08:00
|
|
|
constexpr bool has_single_bit(T x) noexcept; // C++20
|
2019-07-02 07:00:32 +08:00
|
|
|
template <class T>
|
2020-11-24 21:50:49 +08:00
|
|
|
constexpr T bit_ceil(T x); // C++20
|
2019-07-02 07:00:32 +08:00
|
|
|
template <class T>
|
2020-11-24 21:50:49 +08:00
|
|
|
constexpr T bit_floor(T x) noexcept; // C++20
|
2019-07-02 07:00:32 +08:00
|
|
|
template <class T>
|
2020-11-24 21:50:49 +08:00
|
|
|
constexpr T bit_width(T x) noexcept; // C++20
|
2019-07-02 07:00:32 +08:00
|
|
|
|
2020-05-29 02:28:09 +08:00
|
|
|
// [bit.rotate], rotating
|
2019-07-02 07:00:32 +08:00
|
|
|
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
|
|
|
|
|
2020-05-29 02:28:09 +08:00
|
|
|
// [bit.count], counting
|
2019-07-02 07:00:32 +08:00
|
|
|
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
|
|
|
|
|
2020-05-29 02:28:09 +08:00
|
|
|
// [bit.endian], 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
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-05-29 02:28:38 +08:00
|
|
|
#include <__bit/bit_cast.h>
|
2021-05-19 23:57:04 +08:00
|
|
|
#include <__bits> // __libcpp_clz
|
2020-05-29 02:28:38 +08:00
|
|
|
#include <__config>
|
2021-05-13 11:04:03 +08:00
|
|
|
#include <__debug>
|
2019-07-02 07:00:32 +08:00
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
2018-09-13 03:41:40 +08:00
|
|
|
#include <version>
|
2018-08-18 00:07:48 +08:00
|
|
|
|
|
|
|
#if defined(__IBMCPP__)
|
2021-02-03 05:58:38 +08:00
|
|
|
#include "__support/ibm/support.h"
|
2018-08-18 00:07:48 +08:00
|
|
|
#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
|
|
|
|
|
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
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
|
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
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
|
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
|
|
|
|
int __countr_zero(_Tp __t) _NOEXCEPT
|
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
|
2019-07-12 09:01:55 +08:00
|
|
|
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;
|
|
|
|
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
|
2021-05-13 01:09:26 +08:00
|
|
|
while (static_cast<unsigned long long>(__t) == 0uLL)
|
2019-07-12 09:01:55 +08:00
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
__ret += __ulldigits;
|
2019-07-12 09:01:55 +08:00
|
|
|
__t >>= __ulldigits;
|
|
|
|
}
|
2021-05-13 01:09:26 +08:00
|
|
|
return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t));
|
2019-07-12 09:01:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
|
|
|
int __countl_zero(_Tp __t) _NOEXCEPT
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
|
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
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
|
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
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
|
2019-07-12 09:01:55 +08:00
|
|
|
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
|
2021-05-13 01:09:26 +08:00
|
|
|
int __popcount(_Tp __t) _NOEXCEPT
|
2019-07-12 09:01:55 +08:00
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
|
2019-07-12 09:01:55 +08:00
|
|
|
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
|
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-28 00:02:06 +08:00
|
|
|
return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
|
2019-07-12 09:01:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
|
2020-11-24 21:50:49 +08:00
|
|
|
bool __has_single_bit(_Tp __t) _NOEXCEPT
|
2019-07-12 09:01:55 +08:00
|
|
|
{
|
2021-05-13 01:09:26 +08:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
|
2020-05-29 02:28:09 +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
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
2019-07-12 09:01:55 +08:00
|
|
|
rotl(_Tp __t, unsigned int __cnt) noexcept
|
|
|
|
{
|
|
|
|
return __rotl(__t, __cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
2019-07-12 09:01:55 +08:00
|
|
|
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
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_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
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_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
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
2019-07-12 09:01:55 +08:00
|
|
|
countr_zero(_Tp __t) noexcept
|
|
|
|
{
|
2020-11-24 21:50:49 +08:00
|
|
|
return __countr_zero(__t);
|
2019-07-12 09:01:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
2019-07-12 09:01:55 +08:00
|
|
|
countr_one(_Tp __t) noexcept
|
|
|
|
{
|
|
|
|
return __countr_one(__t);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
|
2019-07-12 09:01:55 +08:00
|
|
|
popcount(_Tp __t) noexcept
|
|
|
|
{
|
|
|
|
return __popcount(__t);
|
|
|
|
}
|
2019-07-02 07:00:32 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool>
|
2020-11-24 21:50:49 +08:00
|
|
|
has_single_bit(_Tp __t) noexcept
|
2019-07-12 09:01:55 +08:00
|
|
|
{
|
2020-11-24 21:50:49 +08:00
|
|
|
return __has_single_bit(__t);
|
2019-07-12 09:01:55 +08:00
|
|
|
}
|
2019-07-02 07:00:32 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
2020-11-24 21:50:49 +08:00
|
|
|
bit_floor(_Tp __t) noexcept
|
2019-07-12 09:01:55 +08:00
|
|
|
{
|
|
|
|
return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
|
|
|
|
}
|
2019-07-02 07:00:32 +08:00
|
|
|
|
|
|
|
template <class _Tp>
|
|
|
|
_LIBCPP_INLINE_VISIBILITY constexpr
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
2020-11-24 21:50:49 +08:00
|
|
|
bit_ceil(_Tp __t) noexcept
|
2019-07-02 07:00:32 +08:00
|
|
|
{
|
|
|
|
if (__t < 2) return 1;
|
|
|
|
const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
|
2020-11-24 21:50:49 +08:00
|
|
|
_LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
|
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
|
[libc++] Use enable_if_t instead of _EnableIf
I just ran into a compiler error involving __bind_back and some overloads
that were being disabled with _EnableIf. I noticed that the error message
was quite bad and did not mention the reason for the overload being
excluded. Specifically, the error looked like this:
candidate template ignored: substitution failure [with _Args =
<ContiguousView>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
Instead, when using enable_if or enable_if_t, the compiler is clever and
can produce better diagnostics, like so:
candidate template ignored: requirement 'is_invocable_v<
std::__bind_back_op<1, std::integer_sequence<unsigned long, 0>>,
std::ranges::views::__transform::__fn &, std::tuple<PlusOne> &,
ContiguousView>' was not satisfied [with _Args = <ContiguousView>]
Basically, it tries to do a poor man's implementation of concepts, which
is already a lot better than simply complaining about substitution failure.
Hence, this commit uses enable_if_t instead of _EnableIf whenever
possible. That is both more straightforward than using the internal
helper, and also leads to better error messages in those cases.
I understand the motivation for _EnableIf's implementation was to improve
compile-time performance, however I believe striving to improve error
messages is even more important for our QOI, hence this patch. Furthermore,
it is unclear that _EnableIf actually improved compile-time performance
in any noticeable way (see discussion in the review for details).
Differential Revision: https://reviews.llvm.org/D108216
2021-08-18 00:26:09 +08:00
|
|
|
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
|
2020-11-24 21:50:49 +08:00
|
|
|
bit_width(_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
|