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 { |
| 17 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 18 | template <class T> |
| 19 | constexpr bool ispow2(T x) noexcept; // C++20 |
| 20 | template <class T> |
| 21 | constexpr T ceil2(T x); // C++20 |
| 22 | template <class T> |
| 23 | constexpr T floor2(T x) noexcept; // C++20 |
| 24 | template <class T> |
| 25 | constexpr T log2p1(T x) noexcept; // C++20 |
| 26 | |
| 27 | // 23.20.2, rotating |
| 28 | template<class T> |
| 29 | constexpr T rotl(T x, unsigned int s) noexcept; // C++20 |
| 30 | template<class T> |
| 31 | constexpr T rotr(T x, unsigned int s) noexcept; // C++20 |
| 32 | |
| 33 | // 23.20.3, counting |
| 34 | template<class T> |
| 35 | constexpr int countl_zero(T x) noexcept; // C++20 |
| 36 | template<class T> |
| 37 | constexpr int countl_one(T x) noexcept; // C++20 |
| 38 | template<class T> |
| 39 | constexpr int countr_zero(T x) noexcept; // C++20 |
| 40 | template<class T> |
| 41 | constexpr int countr_one(T x) noexcept; // C++20 |
| 42 | template<class T> |
| 43 | constexpr int popcount(T x) noexcept; // C++20 |
| 44 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 45 | } // namespace std |
| 46 | |
| 47 | */ |
| 48 | |
| 49 | #include <__config> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 50 | #include <limits> |
| 51 | #include <type_traits> |
Marshall Clow | 0a1e750 | 2018-09-12 19:41:40 +0000 | [diff] [blame] | 52 | #include <version> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 53 | #include <__debug> |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 54 | |
| 55 | #if defined(__IBMCPP__) |
| 56 | #include "support/ibm/support.h" |
| 57 | #endif |
| 58 | #if defined(_LIBCPP_COMPILER_MSVC) |
| 59 | #include <intrin.h> |
| 60 | #endif |
| 61 | |
| 62 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 63 | #pragma GCC system_header |
| 64 | #endif |
| 65 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 66 | _LIBCPP_PUSH_MACROS |
| 67 | #include <__undef_macros> |
| 68 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 69 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 70 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 71 | #ifndef _LIBCPP_COMPILER_MSVC |
| 72 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 73 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 74 | int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 75 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 76 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 77 | int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 78 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 79 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 80 | int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 81 | |
| 82 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 83 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 84 | int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 85 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 86 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 87 | int __libcpp_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 88 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 89 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 90 | int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 91 | |
| 92 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 93 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 94 | int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 95 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 96 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 97 | int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 98 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 99 | inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 100 | int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); } |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 101 | |
| 102 | #else // _LIBCPP_COMPILER_MSVC |
| 103 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 104 | // Precondition: __x != 0 |
| 105 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 106 | int __libcpp_ctz(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 107 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 108 | static_assert(sizeof(unsigned long) == 4, ""); |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 109 | unsigned long __where; |
| 110 | if (_BitScanForward(&__where, __x)) |
| 111 | return static_cast<int>(__where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 112 | return 32; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 116 | int __libcpp_ctz(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 117 | static_assert(sizeof(unsigned long) == sizeof(unsigned), ""); |
| 118 | return __ctz(static_cast<unsigned>(__x)); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 122 | int __libcpp_ctz(unsigned long long __x) { |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 123 | unsigned long __where; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 124 | #if defined(_LIBCPP_HAS_BITSCAN64) |
| 125 | (defined(_M_AMD64) || defined(__x86_64__)) |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 126 | if (_BitScanForward64(&__where, __x)) |
| 127 | return static_cast<int>(__where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 128 | #else |
| 129 | // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls. |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 130 | if (_BitScanForward(&__where, static_cast<unsigned long>(__x))) |
| 131 | return static_cast<int>(__where); |
| 132 | if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32))) |
| 133 | return static_cast<int>(__where + 32); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 134 | #endif |
| 135 | return 64; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // Precondition: __x != 0 |
| 139 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 140 | int __libcpp_clz(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 141 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
| 142 | static_assert(sizeof(unsigned long) == 4, ""); |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 143 | unsigned long __where; |
| 144 | if (_BitScanReverse(&__where, __x)) |
| 145 | return static_cast<int>(31 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 146 | return 32; // Undefined Behavior. |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 150 | int __libcpp_clz(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 151 | static_assert(sizeof(unsigned) == sizeof(unsigned long), ""); |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 152 | return __libcpp_clz(static_cast<unsigned>(__x)); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | inline _LIBCPP_INLINE_VISIBILITY |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 156 | int __libcpp_clz(unsigned long long __x) { |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 157 | unsigned long __where; |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 158 | #if defined(_LIBCPP_HAS_BITSCAN64) |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 159 | if (_BitScanReverse64(&__where, __x)) |
| 160 | return static_cast<int>(63 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 161 | #else |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 162 | // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls. |
| 163 | if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32))) |
| 164 | return static_cast<int>(63 - (__where + 32)); |
| 165 | if (_BitScanReverse(&__where, static_cast<unsigned long>(__x))) |
| 166 | return static_cast<int>(63 - __where); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 167 | #endif |
| 168 | return 64; // Undefined Behavior. |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 171 | inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 172 | static_assert(sizeof(unsigned) == 4, ""); |
| 173 | return __popcnt(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 176 | inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 177 | static_assert(sizeof(unsigned long) == 4, ""); |
| 178 | return __popcnt(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 181 | inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) { |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 182 | static_assert(sizeof(unsigned long long) == 8, ""); |
| 183 | return __popcnt64(__x); |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Marshall Clow | ea687e2 | 2018-08-17 17:27:25 +0000 | [diff] [blame] | 186 | #endif // _LIBCPP_COMPILER_MSVC |
| 187 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 188 | template <class _Tp> |
| 189 | using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool, |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 190 | is_integral<_Tp>::value && |
| 191 | is_unsigned<_Tp>::value && |
| 192 | _IsNotSame<typename remove_cv<_Tp>::type, bool>::value && |
| 193 | _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value && |
| 194 | _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value && |
| 195 | _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value && |
| 196 | _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 197 | >; |
| 198 | |
| 199 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 200 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 201 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 202 | _Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 203 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 204 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 205 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 206 | if ((__cnt % __dig) == 0) |
| 207 | return __t; |
| 208 | return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig))); |
| 209 | } |
| 210 | |
| 211 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 212 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 213 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 214 | _Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 215 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 216 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 217 | const unsigned int __dig = numeric_limits<_Tp>::digits; |
| 218 | if ((__cnt % __dig) == 0) |
| 219 | return __t; |
| 220 | return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig))); |
| 221 | } |
| 222 | |
| 223 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 224 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 225 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 226 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 227 | int __countr_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 228 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 229 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 230 | if (__t == 0) |
| 231 | return numeric_limits<_Tp>::digits; |
| 232 | |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 233 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 234 | return __libcpp_ctz(static_cast<unsigned int>(__t)); |
| 235 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 236 | return __libcpp_ctz(static_cast<unsigned long>(__t)); |
| 237 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 238 | return __libcpp_ctz(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 239 | else |
| 240 | { |
| 241 | int __ret = 0; |
| 242 | int __iter = 0; |
| 243 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 244 | while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 245 | { |
| 246 | __ret += __iter; |
| 247 | __t >>= __ulldigits; |
| 248 | } |
| 249 | return __ret + __iter; |
| 250 | } |
| 251 | } |
| 252 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 253 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 254 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 255 | int __countl_zero(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 256 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 257 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned"); |
| 258 | if (__t == 0) |
| 259 | return numeric_limits<_Tp>::digits; |
| 260 | |
| 261 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 262 | return __libcpp_clz(static_cast<unsigned int>(__t)) |
| 263 | - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits); |
| 264 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 265 | return __libcpp_clz(static_cast<unsigned long>(__t)) |
| 266 | - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits); |
| 267 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 268 | return __libcpp_clz(static_cast<unsigned long long>(__t)) |
| 269 | - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits); |
| 270 | else |
| 271 | { |
| 272 | int __ret = 0; |
| 273 | int __iter = 0; |
| 274 | const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits; |
| 275 | while (true) { |
| 276 | __t = __rotr(__t, __ulldigits); |
| 277 | if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits) |
| 278 | break; |
| 279 | __ret += __iter; |
| 280 | } |
| 281 | return __ret + __iter; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | template<class _Tp> |
| 286 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 287 | int __countl_one(_Tp __t) _NOEXCEPT |
| 288 | { |
| 289 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 290 | return __t != numeric_limits<_Tp>::max() |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 291 | ? __countl_zero(static_cast<_Tp>(~__t)) |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 292 | : numeric_limits<_Tp>::digits; |
| 293 | } |
| 294 | |
| 295 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 296 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 297 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 298 | int __countr_one(_Tp __t) _NOEXCEPT |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 299 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 300 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned"); |
| 301 | return __t != numeric_limits<_Tp>::max() |
| 302 | ? __countr_zero(static_cast<_Tp>(~__t)) |
| 303 | : numeric_limits<_Tp>::digits; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | template<class _Tp> |
| 308 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 309 | int |
| 310 | __popcount(_Tp __t) _NOEXCEPT |
| 311 | { |
| 312 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned"); |
| 313 | if (sizeof(_Tp) <= sizeof(unsigned int)) |
| 314 | return __libcpp_popcount(static_cast<unsigned int>(__t)); |
| 315 | else if (sizeof(_Tp) <= sizeof(unsigned long)) |
| 316 | return __libcpp_popcount(static_cast<unsigned long>(__t)); |
| 317 | else if (sizeof(_Tp) <= sizeof(unsigned long long)) |
| 318 | return __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 319 | else |
| 320 | { |
| 321 | int __ret = 0; |
| 322 | while (__t != 0) |
| 323 | { |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 324 | __ret += __libcpp_popcount(static_cast<unsigned long long>(__t)); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 325 | __t >>= numeric_limits<unsigned long long>::digits; |
| 326 | } |
| 327 | return __ret; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | // integral log base 2 |
| 333 | template<class _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 334 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 |
| 335 | unsigned __bit_log2(_Tp __t) _NOEXCEPT |
| 336 | { |
| 337 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned"); |
| 338 | return std::numeric_limits<_Tp>::digits - 1 - __countl_zero(__t); |
| 339 | } |
| 340 | |
| 341 | template <class _Tp> |
| 342 | _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR |
| 343 | bool __ispow2(_Tp __t) _NOEXCEPT |
| 344 | { |
| 345 | static_assert(__bitop_unsigned_integer<_Tp>::value, "__ispow2 requires unsigned"); |
| 346 | return __t != 0 && (((__t & (__t - 1)) == 0)); |
| 347 | } |
| 348 | |
| 349 | |
| 350 | #if _LIBCPP_STD_VER > 17 |
| 351 | |
| 352 | template<class _Tp> |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 353 | _LIBCPP_INLINE_VISIBILITY constexpr |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 354 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 355 | rotl(_Tp __t, unsigned int __cnt) noexcept |
| 356 | { |
| 357 | return __rotl(__t, __cnt); |
| 358 | } |
| 359 | |
| 360 | |
| 361 | // rotr |
| 362 | template<class _Tp> |
| 363 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 364 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 365 | rotr(_Tp __t, unsigned int __cnt) noexcept |
| 366 | { |
| 367 | return __rotr(__t, __cnt); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | template<class _Tp> |
| 372 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 373 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 374 | countl_zero(_Tp __t) noexcept |
| 375 | { |
| 376 | return __countl_zero(__t); |
| 377 | } |
| 378 | |
| 379 | |
| 380 | template<class _Tp> |
| 381 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 382 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 383 | countl_one(_Tp __t) noexcept |
| 384 | { |
| 385 | return __countl_one(__t); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | template<class _Tp> |
| 390 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 391 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 392 | countr_zero(_Tp __t) noexcept |
| 393 | { |
| 394 | return __countr_zero(__t); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | template<class _Tp> |
| 399 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 400 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 401 | countr_one(_Tp __t) noexcept |
| 402 | { |
| 403 | return __countr_one(__t); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | template<class _Tp> |
| 408 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 409 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, int> |
| 410 | popcount(_Tp __t) noexcept |
| 411 | { |
| 412 | return __popcount(__t); |
| 413 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 414 | |
| 415 | |
| 416 | template <class _Tp> |
| 417 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 418 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 419 | ispow2(_Tp __t) noexcept |
| 420 | { |
| 421 | return __ispow2(__t); |
| 422 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 423 | |
| 424 | template <class _Tp> |
| 425 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 426 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 427 | floor2(_Tp __t) noexcept |
| 428 | { |
| 429 | return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); |
| 430 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 431 | |
| 432 | template <class _Tp> |
| 433 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 434 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 435 | ceil2(_Tp __t) noexcept |
| 436 | { |
| 437 | if (__t < 2) return 1; |
| 438 | const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u)); |
Marshall Clow | a2851c7 | 2019-07-02 03:21:16 +0000 | [diff] [blame] | 439 | _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to ceil2"); |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 440 | |
| 441 | if constexpr (sizeof(_Tp) >= sizeof(unsigned)) |
| 442 | return _Tp{1} << __n; |
| 443 | else |
| 444 | { |
| 445 | const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; |
| 446 | const unsigned __retVal = 1u << (__n + __extra); |
| 447 | return (_Tp) (__retVal >> __extra); |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | template <class _Tp> |
| 452 | _LIBCPP_INLINE_VISIBILITY constexpr |
| 453 | enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp> |
| 454 | log2p1(_Tp __t) noexcept |
Marshall Clow | 370375e | 2019-07-12 01:01:55 +0000 | [diff] [blame] | 455 | { |
| 456 | return __t == 0 ? 0 : __bit_log2(__t) + 1; |
| 457 | } |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 458 | |
| 459 | #endif // _LIBCPP_STD_VER > 17 |
| 460 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 461 | _LIBCPP_END_NAMESPACE_STD |
| 462 | |
Marshall Clow | d852d5d | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 463 | _LIBCPP_POP_MACROS |
| 464 | |
Marshall Clow | bf32ec9 | 2018-08-17 16:07:48 +0000 | [diff] [blame] | 465 | #endif // _LIBCPP_BIT |