2018-08-17 16:07:48 +00:00
|
|
|
// -*- C++ -*-
|
2021-11-17 16:25:01 -05:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-08-17 16:07:48 +00:00
|
|
|
//
|
2019-01-19 10:56:40 +00: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-17 16:07:48 +00:00
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_BIT
|
|
|
|
#define _LIBCPP_BIT
|
|
|
|
|
|
|
|
/*
|
|
|
|
bit synopsis
|
|
|
|
|
|
|
|
namespace std {
|
2021-11-22 00:22:55 +01:00
|
|
|
// [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
|
2018-08-17 16:07:48 +00:00
|
|
|
|
2020-05-28 14:28:09 -04:00
|
|
|
// [bit.pow.two], integral powers of 2
|
2019-07-01 23:00:32 +00:00
|
|
|
template <class T>
|
2020-11-24 14:50:49 +01:00
|
|
|
constexpr bool has_single_bit(T x) noexcept; // C++20
|
2019-07-01 23:00:32 +00:00
|
|
|
template <class T>
|
2020-11-24 14:50:49 +01:00
|
|
|
constexpr T bit_ceil(T x); // C++20
|
2019-07-01 23:00:32 +00:00
|
|
|
template <class T>
|
2020-11-24 14:50:49 +01:00
|
|
|
constexpr T bit_floor(T x) noexcept; // C++20
|
2019-07-01 23:00:32 +00:00
|
|
|
template <class T>
|
2022-02-23 20:08:28 -05:00
|
|
|
constexpr int bit_width(T x) noexcept; // C++20
|
2019-07-01 23:00:32 +00:00
|
|
|
|
2020-05-28 14:28:09 -04:00
|
|
|
// [bit.rotate], rotating
|
2019-07-01 23:00:32 +00: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-28 14:28:09 -04:00
|
|
|
// [bit.count], counting
|
2019-07-01 23:00:32 +00: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-28 14:28:09 -04:00
|
|
|
// [bit.endian], endian
|
2019-07-23 04:20:19 +00:00
|
|
|
enum class endian {
|
|
|
|
little = see below, // C++20
|
|
|
|
big = see below, // C++20
|
|
|
|
native = see below // C++20
|
2021-11-22 00:22:55 +01:00
|
|
|
};
|
2019-07-23 04:20:19 +00:00
|
|
|
|
2018-08-17 16:07:48 +00:00
|
|
|
} // namespace std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-03-25 12:55:36 -04:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2020-05-28 14:28:38 -04:00
|
|
|
#include <__bit/bit_cast.h>
|
2021-11-22 00:22:55 +01:00
|
|
|
#include <__bit/byteswap.h>
|
2021-05-19 11:57:04 -04:00
|
|
|
#include <__bits> // __libcpp_clz
|
2022-06-12 12:24:21 +02:00
|
|
|
#include <__concepts/arithmetic.h>
|
2020-05-28 14:28:38 -04:00
|
|
|
#include <__config>
|
2019-07-01 23:00:32 +00:00
|
|
|
#include <limits>
|
|
|
|
#include <type_traits>
|
2018-09-12 19:41:40 +00:00
|
|
|
#include <version>
|
2018-08-17 16:07:48 +00:00
|
|
|
|
[libc++] Re-add transitive includes that had been removed since LLVM 14
This commit re-adds transitive includes that had been removed by
4cd04d1687f1, c36870c8e79c, a83f4b9cda57, 1458458b558d, 2e2f3158c604,
and 489637e66dd3. This should cover almost all the includes that had
been removed since LLVM 14 and that would contribute to breaking user
code when releasing LLVM 15.
It is possible to disable the inclusion of these headers by defining
_LIBCPP_REMOVE_TRANSITIVE_INCLUDES. The intent is that vendors will
enable that macro and start fixing downstream issues immediately. We
can then remove the macro (and the transitive includes) by default in
a future release. That way, we will break users only once by removing
transitive includes in bulk instead of doing it bit by bit a every
release, which is more disruptive for users.
Note 1: The set of headers to re-add was found by re-generating the
transitive include test on a checkout of release/14.x, which
provided the list of all transitive includes we used to provide.
Note 2: Several includes of <vector>, <optional>, <array> and <unordered_map>
have been added in this commit. These transitive inclusions were
added when we implemented boyer_moore_searcher in <functional>.
Note 3: This is a best effort patch to try and resolve downstream breakage
caused since branching LLVM 14. I wasn't able to perfectly mirror
transitive includes in LLVM 14 for a few headers, so I added a
release note explaining it. To summarize, adding boyer_moore_searcher
created a bunch of circular dependencies, so we have to break
backwards compatibility in a few cases.
Differential Revision: https://reviews.llvm.org/D128661
2022-06-27 15:53:41 -04:00
|
|
|
#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
|
|
|
|
# include <iosfwd>
|
|
|
|
#endif
|
|
|
|
|
2018-08-17 16:07:48 +00:00
|
|
|
#if defined(_LIBCPP_COMPILER_MSVC)
|
2022-06-14 17:47:51 +02:00
|
|
|
# include <intrin.h>
|
2018-08-17 16:07:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-01 20:16:40 -05:00
|
|
|
# pragma GCC system_header
|
2018-08-17 16:07:48 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-01 23:00:32 +00:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2018-08-17 16:07:48 +00:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2019-07-01 23:00:32 +00:00
|
|
|
template<class _Tp>
|
2022-06-12 12:24:21 +02:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
2019-07-12 01:01:55 +00:00
|
|
|
_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
|
2019-07-01 23:00:32 +00:00
|
|
|
{
|
2021-05-12 13:09:26 -04:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
|
2019-07-01 23:00:32 +00: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 01:01:55 +00:00
|
|
|
template<class _Tp>
|
2022-06-12 12:24:21 +02:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
|
2019-07-12 01:01:55 +00:00
|
|
|
int __countl_zero(_Tp __t) _NOEXCEPT
|
2019-07-01 23:00:32 +00:00
|
|
|
{
|
2021-05-12 13:09:26 -04:00
|
|
|
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
|
2019-07-01 23:00:32 +00:00
|
|
|
if (__t == 0)
|
|
|
|
return numeric_limits<_Tp>::digits;
|
|
|
|
|
2019-07-12 01:01:55 +00:00
|
|
|
if (sizeof(_Tp) <= sizeof(unsigned int))
|
2022-06-14 17:47:51 +02:00
|
|
|
return std::__libcpp_clz(static_cast<unsigned int>(__t))
|
2019-07-01 23:00:32 +00:00
|
|
|
- (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
|
2019-07-12 01:01:55 +00:00
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long))
|
2022-06-14 17:47:51 +02:00
|
|
|
return std::__libcpp_clz(static_cast<unsigned long>(__t))
|
2019-07-01 23:00:32 +00:00
|
|
|
- (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
|
2019-07-12 01:01:55 +00:00
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long long))
|
2022-06-14 17:47:51 +02:00
|
|
|
return std::__libcpp_clz(static_cast<unsigned long long>(__t))
|
2019-07-01 23:00:32 +00: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) {
|
2022-06-14 17:47:51 +02:00
|
|
|
__t = std::__rotr(__t, __ulldigits);
|
|
|
|
if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
|
2019-07-01 23:00:32 +00:00
|
|
|
break;
|
|
|
|
__ret += __iter;
|
|
|
|
}
|
|
|
|
return __ret + __iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-12 01:01:55 +00:00
|
|
|
#if _LIBCPP_STD_VER > 17
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, unsigned int __cnt) noexcept {
|
2022-06-14 17:47:51 +02: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 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, unsigned int __cnt) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
return std::__rotr(__t, __cnt);
|
2019-07-12 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
return std::__countl_zero(__t);
|
2019-07-01 23:00:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
|
2019-07-01 23:00:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
if (__t == 0)
|
|
|
|
return numeric_limits<_Tp>::digits;
|
|
|
|
|
|
|
|
if (sizeof(_Tp) <= sizeof(unsigned int))
|
|
|
|
return std::__libcpp_ctz(static_cast<unsigned int>(__t));
|
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long))
|
|
|
|
return std::__libcpp_ctz(static_cast<unsigned long>(__t));
|
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long long))
|
|
|
|
return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
|
|
|
|
else {
|
|
|
|
int __ret = 0;
|
|
|
|
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
|
|
|
|
while (static_cast<unsigned long long>(__t) == 0uLL) {
|
|
|
|
__ret += __ulldigits;
|
|
|
|
__t >>= __ulldigits;
|
|
|
|
}
|
|
|
|
return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
|
|
|
|
}
|
2019-07-12 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
|
2019-07-12 01:01:55 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
if (sizeof(_Tp) <= sizeof(unsigned int))
|
|
|
|
return std::__libcpp_popcount(static_cast<unsigned int>(__t));
|
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long))
|
|
|
|
return std::__libcpp_popcount(static_cast<unsigned long>(__t));
|
|
|
|
else if (sizeof(_Tp) <= sizeof(unsigned long long))
|
|
|
|
return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
|
|
|
|
else {
|
|
|
|
int __ret = 0;
|
|
|
|
while (__t != 0) {
|
|
|
|
__ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
|
|
|
|
__t >>= numeric_limits<unsigned long long>::digits;
|
|
|
|
}
|
|
|
|
return __ret;
|
|
|
|
}
|
2019-07-12 01:01:55 +00:00
|
|
|
}
|
2019-07-01 23:00:32 +00:00
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
return __t != 0 && (((__t & (__t - 1)) == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// integral log base 2
|
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp __bit_log2(_Tp __t) noexcept {
|
|
|
|
return numeric_limits<_Tp>::digits - 1 - std::countl_zero(__t);
|
2019-07-12 01:01:55 +00:00
|
|
|
}
|
2019-07-01 23:00:32 +00:00
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_floor(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
return __t == 0 ? 0 : _Tp{1} << std::__bit_log2(__t);
|
2019-07-12 01:01:55 +00:00
|
|
|
}
|
2019-07-01 23:00:32 +00:00
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept {
|
|
|
|
if (__t < 2)
|
|
|
|
return 1;
|
2022-06-14 17:47:51 +02:00
|
|
|
const unsigned __n = numeric_limits<_Tp>::digits - std::countl_zero((_Tp)(__t - 1u));
|
2022-06-12 12:24:21 +02:00
|
|
|
_LIBCPP_ASSERT(__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);
|
|
|
|
}
|
2019-07-01 23:00:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-12 12:24:21 +02:00
|
|
|
template <__libcpp_unsigned_integer _Tp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI constexpr int bit_width(_Tp __t) noexcept {
|
2022-06-14 17:47:51 +02:00
|
|
|
return __t == 0 ? 0 : std::__bit_log2(__t) + 1;
|
2019-07-12 01:01:55 +00:00
|
|
|
}
|
2019-07-01 23:00:32 +00:00
|
|
|
|
2022-06-14 17:47:51 +02: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-23 04:20:19 +00:00
|
|
|
};
|
|
|
|
|
2019-07-01 23:00:32 +00:00
|
|
|
#endif // _LIBCPP_STD_VER > 17
|
|
|
|
|
2018-08-17 16:07:48 +00:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2019-07-01 23:00:32 +00:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2018-08-17 16:07:48 +00:00
|
|
|
#endif // _LIBCPP_BIT
|