Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 1 | // -*- C++ -*- |
| 2 | //===------------------------------ bit ----------------------------------===// |
| 3 | // |
Chandler Carruth | d201210 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===---------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_BIT |
| 11 | #define _LIBCPP_BIT |
| 12 | |
| 13 | /* |
| 14 | bit synopsis |
| 15 | |
| 16 | namespace std { |
Louis Dionne | 1462d4d | 2020-05-28 14:28:38 -0400 | [diff] [blame] | 17 | // [bit.cast], bit_cast |
| 18 | template<class To, class From> |
| 19 | constexpr To bit_cast(const From& from) noexcept; // C++20 |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 20 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 21 | // [bit.pow.two], integral powers of 2 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 22 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 23 | constexpr bool has_single_bit(T x) noexcept; // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 24 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 25 | constexpr T bit_ceil(T x); // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 26 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 27 | constexpr T bit_floor(T x) noexcept; // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 28 | template <class T> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 29 | constexpr T bit_width(T x) noexcept; // C++20 |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 30 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 31 | // [bit.rotate], rotating |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 32 | template<class T> |
| 33 | constexpr T rotl(T x, unsigned int s) noexcept; // C++20 |
| 34 | template<class T> |
| 35 | constexpr T rotr(T x, unsigned int s) noexcept; // C++20 |
| 36 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 37 | // [bit.count], counting |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 38 | template<class T> |
| 39 | constexpr int countl_zero(T x) noexcept; // C++20 |
| 40 | template<class T> |
| 41 | constexpr int countl_one(T x) noexcept; // C++20 |
| 42 | template<class T> |
| 43 | constexpr int countr_zero(T x) noexcept; // C++20 |
| 44 | template<class T> |
| 45 | constexpr int countr_one(T x) noexcept; // C++20 |
| 46 | template<class T> |
| 47 | constexpr int popcount(T x) noexcept; // C++20 |
| 48 | |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 49 | // [bit.endian], endian |
Marshall Clow | e8a18f3 | 2019-07-23 04:20:19 +0000 | [diff] [blame] | 50 | enum class endian { |
| 51 | little = see below, // C++20 |
| 52 | big = see below, // C++20 |
| 53 | native = see below // C++20 |
| 54 | }; |
| 55 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 56 | } // namespace std |
| 57 | |
| 58 | */ |
| 59 | |
Louis Dionne | 1462d4d | 2020-05-28 14:28:38 -0400 | [diff] [blame] | 60 | #include <__bit/bit_cast.h> |
Arthur O'Dwyer | ef18160 | 2021-05-19 11:57:04 -0400 | [diff] [blame] | 61 | #include <__bits> // __libcpp_clz |
Louis Dionne | 1462d4d | 2020-05-28 14:28:38 -0400 | [diff] [blame] | 62 | #include <__config> |
Arthur O'Dwyer | 597cac4 | 2021-05-12 23:04:03 -0400 | [diff] [blame] | 63 | #include <__debug> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 64 | #include <limits> |
| 65 | #include <type_traits> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 66 | #include <version> |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 67 | |
| 68 | #if defined(__IBMCPP__) |
Louis Dionne | 90a0449 | 2021-02-02 16:58:38 -0500 | [diff] [blame] | 69 | #include "__support/ibm/support.h" |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 70 | #endif |
| 71 | #if defined(_LIBCPP_COMPILER_MSVC) |
| 72 | #include <intrin.h> |
| 73 | #endif |
| 74 | |
| 75 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 76 | #pragma GCC system_header |
| 77 | #endif |
| 78 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 79 | _LIBCPP_PUSH_MACROS |
| 80 | #include <__undef_macros> |
| 81 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 82 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 83 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 84 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 85 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 86 | _Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 87 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 88 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 89 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 90 | if ((__cnt % __dig) == 0) |
| 91 | return __t; |
| 92 | return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig))); |
| 93 | } |
| 94 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 95 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 96 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 97 | _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 98 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 99 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 100 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 101 | if ((__cnt % __dig) == 0) |
| 102 | return __t; |
| 103 | return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig))); |
| 104 | } |
| 105 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 106 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 107 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 108 | int __countr_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 109 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 110 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 111 | if (__t == 0) |
| 112 | return numeric_limits<_Tp>::digits; |
| 113 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 114 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 115 | return __libcpp_ctz(static_cast<unsigned int>(__t)); |
| 116 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 117 | return __libcpp_ctz(static_cast<unsigned long>(__t)); |
| 118 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 119 | return __libcpp_ctz(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 120 | else |
| 121 | { |
| 122 | int __ret = 0; |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 123 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 124 | while (static_cast<unsigned long long>(__t) == 0uLL) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 125 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 126 | __ret += __ulldigits; |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 127 | __t >>= __ulldigits; |
| 128 | } |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 129 | return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 133 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 134 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 135 | int __countl_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 136 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 137 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type"); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 138 | if (__t == 0) |
| 139 | return numeric_limits<_Tp>::digits; |
| 140 | |
| 141 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 142 | return __libcpp_clz(static_cast<unsigned int>(__t)) |
| 143 | - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits); |
| 144 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 145 | return __libcpp_clz(static_cast<unsigned long>(__t)) |
| 146 | - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits); |
| 147 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 148 | return __libcpp_clz(static_cast<unsigned long long>(__t)) |
| 149 | - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits); |
| 150 | else |
| 151 | { |
| 152 | int __ret = 0; |
| 153 | int __iter = 0; |
| 154 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
| 155 | while (true) { |
| 156 | __t = __rotr(__t, __ulldigits); |
| 157 | if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits) |
| 158 | break; |
| 159 | __ret += __iter; |
| 160 | } |
| 161 | return __ret + __iter; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | template<class _Tp> |
| 166 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 167 | int __countl_one(_Tp __t) _NOEXCEPT |
| 168 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 169 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 170 | return __t != numeric_limits<_Tp>::max() |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 171 | ? __countl_zero(static_cast<_Tp>(~__t)) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 172 | : numeric_limits<_Tp>::digits; |
| 173 | } |
| 174 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 175 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 176 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 177 | int __countr_one(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 178 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 179 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type"); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 180 | return __t != numeric_limits<_Tp>::max() |
| 181 | ? __countr_zero(static_cast<_Tp>(~__t)) |
| 182 | : numeric_limits<_Tp>::digits; |
| 183 | } |
| 184 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 185 | template<class _Tp> |
| 186 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 187 | int __popcount(_Tp __t) _NOEXCEPT |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 188 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 189 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type"); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 190 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 191 | return __libcpp_popcount(static_cast<unsigned int>(__t)); |
| 192 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 193 | return __libcpp_popcount(static_cast<unsigned long>(__t)); |
| 194 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 195 | return __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 196 | else |
| 197 | { |
| 198 | int __ret = 0; |
| 199 | while (__t != 0) |
| 200 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 201 | __ret += __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 202 | __t >>= numeric_limits<unsigned long long>::digits; |
| 203 | } |
| 204 | return __ret; |
| 205 | } |
| 206 | } |
| 207 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 208 | // integral log base 2 |
| 209 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 210 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 211 | unsigned __bit_log2(_Tp __t) _NOEXCEPT |
| 212 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 213 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type"); |
Arthur O'Dwyer | 07b2249 | 2020-11-27 11:02:06 -0500 | [diff] [blame] | 214 | return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | template <class _Tp> |
| 218 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 219 | bool __has_single_bit(_Tp __t) _NOEXCEPT |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 220 | { |
Arthur O'Dwyer | 515615e | 2021-05-12 13:09:26 -0400 | [diff] [blame] | 221 | static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type"); |
Louis Dionne | a1e3b7d | 2020-05-28 14:28:09 -0400 | [diff] [blame] | 222 | return __t != 0 && (((__t & (__t - 1)) == 0)); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 225 | #if _LIBCPP_STD_VER > 17 |
| 226 | |
| 227 | template<class _Tp> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 228 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 229 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 230 | rotl(_Tp __t, unsigned int __cnt) noexcept |
| 231 | { |
| 232 | return __rotl(__t, __cnt); |
| 233 | } |
| 234 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 235 | template<class _Tp> |
| 236 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 237 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 238 | rotr(_Tp __t, unsigned int __cnt) noexcept |
| 239 | { |
| 240 | return __rotr(__t, __cnt); |
| 241 | } |
| 242 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 243 | template<class _Tp> |
| 244 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 245 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 246 | countl_zero(_Tp __t) noexcept |
| 247 | { |
| 248 | return __countl_zero(__t); |
| 249 | } |
| 250 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 251 | template<class _Tp> |
| 252 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 253 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 254 | countl_one(_Tp __t) noexcept |
| 255 | { |
| 256 | return __countl_one(__t); |
| 257 | } |
| 258 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 259 | template<class _Tp> |
| 260 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 261 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 262 | countr_zero(_Tp __t) noexcept |
| 263 | { |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 264 | return __countr_zero(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 267 | template<class _Tp> |
| 268 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 269 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 270 | countr_one(_Tp __t) noexcept |
| 271 | { |
| 272 | return __countr_one(__t); |
| 273 | } |
| 274 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 275 | template<class _Tp> |
| 276 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 277 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 278 | popcount(_Tp __t) noexcept |
| 279 | { |
| 280 | return __popcount(__t); |
| 281 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 282 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 283 | template <class _Tp> |
| 284 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 285 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 286 | has_single_bit(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 287 | { |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 288 | return __has_single_bit(__t); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 289 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 290 | |
| 291 | template <class _Tp> |
| 292 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 293 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 294 | bit_floor(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 295 | { |
| 296 | return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); |
| 297 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 298 | |
| 299 | template <class _Tp> |
| 300 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 301 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 302 | bit_ceil(_Tp __t) noexcept |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 303 | { |
| 304 | if (__t < 2) return 1; |
| 305 | const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u)); |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 306 | _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 307 | |
| 308 | if constexpr (sizeof(_Tp) >= sizeof(unsigned)) |
| 309 | return _Tp{1} << __n; |
| 310 | else |
| 311 | { |
| 312 | const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; |
| 313 | const unsigned __retVal = 1u << (__n + __extra); |
| 314 | return (_Tp) (__retVal >> __extra); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | template <class _Tp> |
| 319 | _LIBCPP_INLINE_VISIBILITY constexpr |
Louis Dionne | 2554716 | 2021-08-17 12:26:09 -0400 | [diff] [blame] | 320 | enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp> |
Mark de Wever | 896e074 | 2020-11-24 14:50:49 +0100 | [diff] [blame] | 321 | bit_width(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 322 | { |
| 323 | return __t == 0 ? 0 : __bit_log2(__t) + 1; |
| 324 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 325 | |
Marshall Clow | e8a18f3 | 2019-07-23 04:20:19 +0000 | [diff] [blame] | 326 | enum class endian |
| 327 | { |
| 328 | little = 0xDEAD, |
| 329 | big = 0xFACE, |
| 330 | #if defined(_LIBCPP_LITTLE_ENDIAN) |
| 331 | native = little |
| 332 | #elif defined(_LIBCPP_BIG_ENDIAN) |
| 333 | native = big |
| 334 | #else |
| 335 | native = 0xCAFE |
| 336 | #endif |
| 337 | }; |
| 338 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 339 | #endif // _LIBCPP_STD_VER > 17 |
| 340 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 341 | _LIBCPP_END_NAMESPACE_STD |
| 342 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 343 | _LIBCPP_POP_MACROS |
| 344 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 345 | #endif // _LIBCPP_BIT |