blob: a720b2e6513f94e6c41ceed3e9089fe6e1961f58 [file] [log] [blame]
Marshall Clowbf32ec92018-08-17 16:07:48 +00001// -*- C++ -*-
2//===------------------------------ bit ----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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 Clowbf32ec92018-08-17 16:07:48 +00007//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_BIT
11#define _LIBCPP_BIT
12
13/*
14 bit synopsis
15
16namespace std {
17
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040018 // [bit.pow.two], integral powers of 2
Marshall Clowd852d5d2019-07-01 23:00:32 +000019 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010020 constexpr bool has_single_bit(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000021 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010022 constexpr T bit_ceil(T x); // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000023 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010024 constexpr T bit_floor(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000025 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010026 constexpr T bit_width(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000027
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040028 // [bit.rotate], rotating
Marshall Clowd852d5d2019-07-01 23:00:32 +000029 template<class T>
30 constexpr T rotl(T x, unsigned int s) noexcept; // C++20
31 template<class T>
32 constexpr T rotr(T x, unsigned int s) noexcept; // C++20
33
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040034 // [bit.count], counting
Marshall Clowd852d5d2019-07-01 23:00:32 +000035 template<class T>
36 constexpr int countl_zero(T x) noexcept; // C++20
37 template<class T>
38 constexpr int countl_one(T x) noexcept; // C++20
39 template<class T>
40 constexpr int countr_zero(T x) noexcept; // C++20
41 template<class T>
42 constexpr int countr_one(T x) noexcept; // C++20
43 template<class T>
44 constexpr int popcount(T x) noexcept; // C++20
45
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040046 // [bit.endian], endian
Marshall Clowe8a18f32019-07-23 04:20:19 +000047 enum class endian {
48 little = see below, // C++20
49 big = see below, // C++20
50 native = see below // C++20
51};
52
Marshall Clowbf32ec92018-08-17 16:07:48 +000053} // namespace std
54
55*/
56
57#include <__config>
Marshall Clowd852d5d2019-07-01 23:00:32 +000058#include <limits>
59#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000060#include <version>
Marshall Clowd852d5d2019-07-01 23:00:32 +000061#include <__debug>
Marshall Clowbf32ec92018-08-17 16:07:48 +000062
63#if defined(__IBMCPP__)
64#include "support/ibm/support.h"
65#endif
66#if defined(_LIBCPP_COMPILER_MSVC)
67#include <intrin.h>
68#endif
69
70#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
71#pragma GCC system_header
72#endif
73
Marshall Clowd852d5d2019-07-01 23:00:32 +000074_LIBCPP_PUSH_MACROS
75#include <__undef_macros>
76
Marshall Clowbf32ec92018-08-17 16:07:48 +000077_LIBCPP_BEGIN_NAMESPACE_STD
78
Marshall Clowea687e22018-08-17 17:27:25 +000079#ifndef _LIBCPP_COMPILER_MSVC
80
Marshall Clowd852d5d2019-07-01 23:00:32 +000081inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000082int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000083
Marshall Clowd852d5d2019-07-01 23:00:32 +000084inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000085int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000086
Marshall Clowd852d5d2019-07-01 23:00:32 +000087inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000088int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000089
90
Marshall Clowd852d5d2019-07-01 23:00:32 +000091inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000092int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000093
Marshall Clowd852d5d2019-07-01 23:00:32 +000094inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000095int __libcpp_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000096
Marshall Clowd852d5d2019-07-01 23:00:32 +000097inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000098int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000099
100
Marshall Clowd852d5d2019-07-01 23:00:32 +0000101inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +0000102int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +0000103
Marshall Clowd852d5d2019-07-01 23:00:32 +0000104inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +0000105int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +0000106
Marshall Clowd852d5d2019-07-01 23:00:32 +0000107inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +0000108int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +0000109
110#else // _LIBCPP_COMPILER_MSVC
111
Marshall Clowbf32ec92018-08-17 16:07:48 +0000112// Precondition: __x != 0
113inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000114int __libcpp_ctz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000115 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
116 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +0000117 unsigned long __where;
118 if (_BitScanForward(&__where, __x))
119 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000120 return 32;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000121}
122
123inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000124int __libcpp_ctz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000125 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
126 return __ctz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000127}
128
129inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000130int __libcpp_ctz(unsigned long long __x) {
Marshall Clowea687e22018-08-17 17:27:25 +0000131 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000132#if defined(_LIBCPP_HAS_BITSCAN64)
133 (defined(_M_AMD64) || defined(__x86_64__))
Marshall Clowea687e22018-08-17 17:27:25 +0000134 if (_BitScanForward64(&__where, __x))
135 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000136#else
137 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
Marshall Clowea687e22018-08-17 17:27:25 +0000138 if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
139 return static_cast<int>(__where);
140 if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
141 return static_cast<int>(__where + 32);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000142#endif
143 return 64;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000144}
145
146// Precondition: __x != 0
147inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000148int __libcpp_clz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000149 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
150 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +0000151 unsigned long __where;
152 if (_BitScanReverse(&__where, __x))
153 return static_cast<int>(31 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000154 return 32; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000155}
156
157inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000158int __libcpp_clz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000159 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
Marshall Clow370375e2019-07-12 01:01:55 +0000160 return __libcpp_clz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000161}
162
163inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000164int __libcpp_clz(unsigned long long __x) {
Marshall Clowea687e22018-08-17 17:27:25 +0000165 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000166#if defined(_LIBCPP_HAS_BITSCAN64)
Marshall Clowea687e22018-08-17 17:27:25 +0000167 if (_BitScanReverse64(&__where, __x))
168 return static_cast<int>(63 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000169#else
Marshall Clowea687e22018-08-17 17:27:25 +0000170 // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
171 if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
172 return static_cast<int>(63 - (__where + 32));
173 if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
174 return static_cast<int>(63 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000175#endif
176 return 64; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000177}
178
Marshall Clow370375e2019-07-12 01:01:55 +0000179inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000180 static_assert(sizeof(unsigned) == 4, "");
181 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000182}
183
Marshall Clow370375e2019-07-12 01:01:55 +0000184inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000185 static_assert(sizeof(unsigned long) == 4, "");
186 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000187}
188
Marshall Clow370375e2019-07-12 01:01:55 +0000189inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000190 static_assert(sizeof(unsigned long long) == 8, "");
191 return __popcnt64(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000192}
193
Marshall Clowea687e22018-08-17 17:27:25 +0000194#endif // _LIBCPP_COMPILER_MSVC
195
Marshall Clowd852d5d2019-07-01 23:00:32 +0000196template <class _Tp>
197using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Marshall Clow370375e2019-07-12 01:01:55 +0000198 is_integral<_Tp>::value &&
199 is_unsigned<_Tp>::value &&
200 _IsNotSame<typename remove_cv<_Tp>::type, bool>::value &&
201 _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value &&
202 _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value &&
203 _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value &&
204 _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value
Marshall Clowd852d5d2019-07-01 23:00:32 +0000205 >;
206
207
Marshall Clowd852d5d2019-07-01 23:00:32 +0000208template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000209_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
210_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000211{
Marshall Clow370375e2019-07-12 01:01:55 +0000212 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000213 const unsigned int __dig = numeric_limits<_Tp>::digits;
214 if ((__cnt % __dig) == 0)
215 return __t;
216 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
217}
218
219
Marshall Clowd852d5d2019-07-01 23:00:32 +0000220template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000221_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
222_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000223{
Marshall Clow370375e2019-07-12 01:01:55 +0000224 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000225 const unsigned int __dig = numeric_limits<_Tp>::digits;
226 if ((__cnt % __dig) == 0)
227 return __t;
228 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
229}
230
231
Marshall Clow370375e2019-07-12 01:01:55 +0000232
Marshall Clowd852d5d2019-07-01 23:00:32 +0000233template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000234_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
235int __countr_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000236{
Marshall Clow370375e2019-07-12 01:01:55 +0000237 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000238 if (__t == 0)
239 return numeric_limits<_Tp>::digits;
240
Marshall Clow370375e2019-07-12 01:01:55 +0000241 if (sizeof(_Tp) <= sizeof(unsigned int))
242 return __libcpp_ctz(static_cast<unsigned int>(__t));
243 else if (sizeof(_Tp) <= sizeof(unsigned long))
244 return __libcpp_ctz(static_cast<unsigned long>(__t));
245 else if (sizeof(_Tp) <= sizeof(unsigned long long))
246 return __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000247 else
248 {
249 int __ret = 0;
250 int __iter = 0;
251 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
Marshall Clow370375e2019-07-12 01:01:55 +0000252 while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
Marshall Clowd852d5d2019-07-01 23:00:32 +0000253 {
254 __ret += __iter;
255 __t >>= __ulldigits;
256 }
257 return __ret + __iter;
258 }
259}
260
Marshall Clowd852d5d2019-07-01 23:00:32 +0000261template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000262_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
263int __countl_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000264{
Marshall Clow370375e2019-07-12 01:01:55 +0000265 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned");
266 if (__t == 0)
267 return numeric_limits<_Tp>::digits;
268
269 if (sizeof(_Tp) <= sizeof(unsigned int))
270 return __libcpp_clz(static_cast<unsigned int>(__t))
271 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
272 else if (sizeof(_Tp) <= sizeof(unsigned long))
273 return __libcpp_clz(static_cast<unsigned long>(__t))
274 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
275 else if (sizeof(_Tp) <= sizeof(unsigned long long))
276 return __libcpp_clz(static_cast<unsigned long long>(__t))
277 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
278 else
279 {
280 int __ret = 0;
281 int __iter = 0;
282 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
283 while (true) {
284 __t = __rotr(__t, __ulldigits);
285 if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
286 break;
287 __ret += __iter;
288 }
289 return __ret + __iter;
290 }
291}
292
293template<class _Tp>
294_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
295int __countl_one(_Tp __t) _NOEXCEPT
296{
297 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000298 return __t != numeric_limits<_Tp>::max()
Marshall Clow370375e2019-07-12 01:01:55 +0000299 ? __countl_zero(static_cast<_Tp>(~__t))
Marshall Clowd852d5d2019-07-01 23:00:32 +0000300 : numeric_limits<_Tp>::digits;
301}
302
303
Marshall Clowd852d5d2019-07-01 23:00:32 +0000304template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000305_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
306int __countr_one(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000307{
Marshall Clow370375e2019-07-12 01:01:55 +0000308 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned");
309 return __t != numeric_limits<_Tp>::max()
310 ? __countr_zero(static_cast<_Tp>(~__t))
311 : numeric_limits<_Tp>::digits;
312}
313
314
315template<class _Tp>
316_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
317int
318__popcount(_Tp __t) _NOEXCEPT
319{
320 static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned");
321 if (sizeof(_Tp) <= sizeof(unsigned int))
322 return __libcpp_popcount(static_cast<unsigned int>(__t));
323 else if (sizeof(_Tp) <= sizeof(unsigned long))
324 return __libcpp_popcount(static_cast<unsigned long>(__t));
325 else if (sizeof(_Tp) <= sizeof(unsigned long long))
326 return __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000327 else
328 {
329 int __ret = 0;
330 while (__t != 0)
331 {
Marshall Clow370375e2019-07-12 01:01:55 +0000332 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000333 __t >>= numeric_limits<unsigned long long>::digits;
334 }
335 return __ret;
336 }
337}
338
339
340// integral log base 2
341template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000342_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
343unsigned __bit_log2(_Tp __t) _NOEXCEPT
344{
345 static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned");
346 return std::numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
347}
348
349template <class _Tp>
350_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Mark de Wever896e0742020-11-24 14:50:49 +0100351bool __has_single_bit(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000352{
Mark de Wever896e0742020-11-24 14:50:49 +0100353 static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned");
Louis Dionnea1e3b7d2020-05-28 14:28:09 -0400354 return __t != 0 && (((__t & (__t - 1)) == 0));
Marshall Clow370375e2019-07-12 01:01:55 +0000355}
356
357
358#if _LIBCPP_STD_VER > 17
359
360template<class _Tp>
Marshall Clowd852d5d2019-07-01 23:00:32 +0000361_LIBCPP_INLINE_VISIBILITY constexpr
Marshall Clow370375e2019-07-12 01:01:55 +0000362enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
363rotl(_Tp __t, unsigned int __cnt) noexcept
364{
365 return __rotl(__t, __cnt);
366}
367
368
369// rotr
370template<class _Tp>
371_LIBCPP_INLINE_VISIBILITY constexpr
372enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
373rotr(_Tp __t, unsigned int __cnt) noexcept
374{
375 return __rotr(__t, __cnt);
376}
377
378
379template<class _Tp>
380_LIBCPP_INLINE_VISIBILITY constexpr
381enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
382countl_zero(_Tp __t) noexcept
383{
384 return __countl_zero(__t);
385}
386
387
388template<class _Tp>
389_LIBCPP_INLINE_VISIBILITY constexpr
390enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
391countl_one(_Tp __t) noexcept
392{
393 return __countl_one(__t);
394}
395
396
397template<class _Tp>
398_LIBCPP_INLINE_VISIBILITY constexpr
399enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
400countr_zero(_Tp __t) noexcept
401{
Mark de Wever896e0742020-11-24 14:50:49 +0100402 return __countr_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000403}
404
405
406template<class _Tp>
407_LIBCPP_INLINE_VISIBILITY constexpr
408enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
409countr_one(_Tp __t) noexcept
410{
411 return __countr_one(__t);
412}
413
414
415template<class _Tp>
416_LIBCPP_INLINE_VISIBILITY constexpr
417enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
418popcount(_Tp __t) noexcept
419{
420 return __popcount(__t);
421}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000422
423
424template <class _Tp>
425_LIBCPP_INLINE_VISIBILITY constexpr
426enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
Mark de Wever896e0742020-11-24 14:50:49 +0100427has_single_bit(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000428{
Mark de Wever896e0742020-11-24 14:50:49 +0100429 return __has_single_bit(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000430}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000431
432template <class _Tp>
433_LIBCPP_INLINE_VISIBILITY constexpr
434enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100435bit_floor(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000436{
437 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
438}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000439
440template <class _Tp>
441_LIBCPP_INLINE_VISIBILITY constexpr
442enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100443bit_ceil(_Tp __t) noexcept
Marshall Clowd852d5d2019-07-01 23:00:32 +0000444{
445 if (__t < 2) return 1;
446 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
Mark de Wever896e0742020-11-24 14:50:49 +0100447 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000448
449 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
450 return _Tp{1} << __n;
451 else
452 {
453 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
454 const unsigned __retVal = 1u << (__n + __extra);
455 return (_Tp) (__retVal >> __extra);
456 }
457}
458
459template <class _Tp>
460_LIBCPP_INLINE_VISIBILITY constexpr
461enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100462bit_width(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000463{
464 return __t == 0 ? 0 : __bit_log2(__t) + 1;
465}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000466
Marshall Clowe8a18f32019-07-23 04:20:19 +0000467enum class endian
468{
469 little = 0xDEAD,
470 big = 0xFACE,
471#if defined(_LIBCPP_LITTLE_ENDIAN)
472 native = little
473#elif defined(_LIBCPP_BIG_ENDIAN)
474 native = big
475#else
476 native = 0xCAFE
477#endif
478};
479
Marshall Clowd852d5d2019-07-01 23:00:32 +0000480#endif // _LIBCPP_STD_VER > 17
481
Marshall Clowbf32ec92018-08-17 16:07:48 +0000482_LIBCPP_END_NAMESPACE_STD
483
Marshall Clowd852d5d2019-07-01 23:00:32 +0000484_LIBCPP_POP_MACROS
485
Marshall Clowbf32ec92018-08-17 16:07:48 +0000486#endif // _LIBCPP_BIT