blob: 6dc85b5d01fb27d646571cb405c2ffcb09a6955d [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
Marshall Clowd852d5d2019-07-01 23:00:32 +000018 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
Louis Dionnefd77e4f2019-10-23 10:40:15 -070045 // 20.15.9, endian
Marshall Clowe8a18f32019-07-23 04:20:19 +000046 enum class endian {
47 little = see below, // C++20
48 big = see below, // C++20
49 native = see below // C++20
50};
51
Marshall Clowbf32ec92018-08-17 16:07:48 +000052} // namespace std
53
54*/
55
56#include <__config>
Marshall Clowd852d5d2019-07-01 23:00:32 +000057#include <limits>
58#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000059#include <version>
Marshall Clowd852d5d2019-07-01 23:00:32 +000060#include <__debug>
Marshall Clowbf32ec92018-08-17 16:07:48 +000061
62#if defined(__IBMCPP__)
63#include "support/ibm/support.h"
64#endif
65#if defined(_LIBCPP_COMPILER_MSVC)
66#include <intrin.h>
67#endif
68
69#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
70#pragma GCC system_header
71#endif
72
Marshall Clowd852d5d2019-07-01 23:00:32 +000073_LIBCPP_PUSH_MACROS
74#include <__undef_macros>
75
Marshall Clowbf32ec92018-08-17 16:07:48 +000076_LIBCPP_BEGIN_NAMESPACE_STD
77
Marshall Clowea687e22018-08-17 17:27:25 +000078#ifndef _LIBCPP_COMPILER_MSVC
79
Marshall Clowd852d5d2019-07-01 23:00:32 +000080inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000081int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000082
Marshall Clowd852d5d2019-07-01 23:00:32 +000083inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000084int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000085
Marshall Clowd852d5d2019-07-01 23:00:32 +000086inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000087int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000088
89
Marshall Clowd852d5d2019-07-01 23:00:32 +000090inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000091int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000092
Marshall Clowd852d5d2019-07-01 23:00:32 +000093inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000094int __libcpp_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000095
Marshall Clowd852d5d2019-07-01 23:00:32 +000096inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000097int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000098
99
Marshall Clowd852d5d2019-07-01 23:00:32 +0000100inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +0000101int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +0000102
Marshall Clowd852d5d2019-07-01 23:00:32 +0000103inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +0000104int __libcpp_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +0000105
Marshall Clowd852d5d2019-07-01 23:00:32 +0000106inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +0000107int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +0000108
109#else // _LIBCPP_COMPILER_MSVC
110
Marshall Clowbf32ec92018-08-17 16:07:48 +0000111// Precondition: __x != 0
112inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000113int __libcpp_ctz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000114 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
115 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +0000116 unsigned long __where;
117 if (_BitScanForward(&__where, __x))
118 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000119 return 32;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000120}
121
122inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000123int __libcpp_ctz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000124 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
125 return __ctz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000126}
127
128inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000129int __libcpp_ctz(unsigned long long __x) {
Marshall Clowea687e22018-08-17 17:27:25 +0000130 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000131#if defined(_LIBCPP_HAS_BITSCAN64)
132 (defined(_M_AMD64) || defined(__x86_64__))
Marshall Clowea687e22018-08-17 17:27:25 +0000133 if (_BitScanForward64(&__where, __x))
134 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000135#else
136 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
Marshall Clowea687e22018-08-17 17:27:25 +0000137 if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
138 return static_cast<int>(__where);
139 if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
140 return static_cast<int>(__where + 32);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000141#endif
142 return 64;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000143}
144
145// Precondition: __x != 0
146inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000147int __libcpp_clz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000148 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
149 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +0000150 unsigned long __where;
151 if (_BitScanReverse(&__where, __x))
152 return static_cast<int>(31 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000153 return 32; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000154}
155
156inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000157int __libcpp_clz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000158 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
Marshall Clow370375e2019-07-12 01:01:55 +0000159 return __libcpp_clz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000160}
161
162inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000163int __libcpp_clz(unsigned long long __x) {
Marshall Clowea687e22018-08-17 17:27:25 +0000164 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000165#if defined(_LIBCPP_HAS_BITSCAN64)
Marshall Clowea687e22018-08-17 17:27:25 +0000166 if (_BitScanReverse64(&__where, __x))
167 return static_cast<int>(63 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000168#else
Marshall Clowea687e22018-08-17 17:27:25 +0000169 // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
170 if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
171 return static_cast<int>(63 - (__where + 32));
172 if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
173 return static_cast<int>(63 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000174#endif
175 return 64; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000176}
177
Marshall Clow370375e2019-07-12 01:01:55 +0000178inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000179 static_assert(sizeof(unsigned) == 4, "");
180 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000181}
182
Marshall Clow370375e2019-07-12 01:01:55 +0000183inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000184 static_assert(sizeof(unsigned long) == 4, "");
185 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000186}
187
Marshall Clow370375e2019-07-12 01:01:55 +0000188inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000189 static_assert(sizeof(unsigned long long) == 8, "");
190 return __popcnt64(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000191}
192
Marshall Clowea687e22018-08-17 17:27:25 +0000193#endif // _LIBCPP_COMPILER_MSVC
194
Marshall Clowd852d5d2019-07-01 23:00:32 +0000195template <class _Tp>
196using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Marshall Clow370375e2019-07-12 01:01:55 +0000197 is_integral<_Tp>::value &&
198 is_unsigned<_Tp>::value &&
199 _IsNotSame<typename remove_cv<_Tp>::type, bool>::value &&
200 _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value &&
201 _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value &&
202 _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value &&
203 _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value
Marshall Clowd852d5d2019-07-01 23:00:32 +0000204 >;
205
206
Marshall Clowd852d5d2019-07-01 23:00:32 +0000207template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000208_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
209_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000210{
Marshall Clow370375e2019-07-12 01:01:55 +0000211 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000212 const unsigned int __dig = numeric_limits<_Tp>::digits;
213 if ((__cnt % __dig) == 0)
214 return __t;
215 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
216}
217
218
Marshall Clowd852d5d2019-07-01 23:00:32 +0000219template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000220_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
221_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000222{
Marshall Clow370375e2019-07-12 01:01:55 +0000223 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000224 const unsigned int __dig = numeric_limits<_Tp>::digits;
225 if ((__cnt % __dig) == 0)
226 return __t;
227 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
228}
229
230
Marshall Clow370375e2019-07-12 01:01:55 +0000231
Marshall Clowd852d5d2019-07-01 23:00:32 +0000232template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000233_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
234int __countr_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000235{
Marshall Clow370375e2019-07-12 01:01:55 +0000236 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000237 if (__t == 0)
238 return numeric_limits<_Tp>::digits;
239
Marshall Clow370375e2019-07-12 01:01:55 +0000240 if (sizeof(_Tp) <= sizeof(unsigned int))
241 return __libcpp_ctz(static_cast<unsigned int>(__t));
242 else if (sizeof(_Tp) <= sizeof(unsigned long))
243 return __libcpp_ctz(static_cast<unsigned long>(__t));
244 else if (sizeof(_Tp) <= sizeof(unsigned long long))
245 return __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000246 else
247 {
248 int __ret = 0;
249 int __iter = 0;
250 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
Marshall Clow370375e2019-07-12 01:01:55 +0000251 while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
Marshall Clowd852d5d2019-07-01 23:00:32 +0000252 {
253 __ret += __iter;
254 __t >>= __ulldigits;
255 }
256 return __ret + __iter;
257 }
258}
259
Marshall Clowd852d5d2019-07-01 23:00:32 +0000260template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000261_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
262int __countl_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000263{
Marshall Clow370375e2019-07-12 01:01:55 +0000264 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned");
265 if (__t == 0)
266 return numeric_limits<_Tp>::digits;
267
268 if (sizeof(_Tp) <= sizeof(unsigned int))
269 return __libcpp_clz(static_cast<unsigned int>(__t))
270 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
271 else if (sizeof(_Tp) <= sizeof(unsigned long))
272 return __libcpp_clz(static_cast<unsigned long>(__t))
273 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
274 else if (sizeof(_Tp) <= sizeof(unsigned long long))
275 return __libcpp_clz(static_cast<unsigned long long>(__t))
276 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
277 else
278 {
279 int __ret = 0;
280 int __iter = 0;
281 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
282 while (true) {
283 __t = __rotr(__t, __ulldigits);
284 if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
285 break;
286 __ret += __iter;
287 }
288 return __ret + __iter;
289 }
290}
291
292template<class _Tp>
293_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
294int __countl_one(_Tp __t) _NOEXCEPT
295{
296 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000297 return __t != numeric_limits<_Tp>::max()
Marshall Clow370375e2019-07-12 01:01:55 +0000298 ? __countl_zero(static_cast<_Tp>(~__t))
Marshall Clowd852d5d2019-07-01 23:00:32 +0000299 : numeric_limits<_Tp>::digits;
300}
301
302
Marshall Clowd852d5d2019-07-01 23:00:32 +0000303template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000304_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
305int __countr_one(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000306{
Marshall Clow370375e2019-07-12 01:01:55 +0000307 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned");
308 return __t != numeric_limits<_Tp>::max()
309 ? __countr_zero(static_cast<_Tp>(~__t))
310 : numeric_limits<_Tp>::digits;
311}
312
313
314template<class _Tp>
315_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
316int
317__popcount(_Tp __t) _NOEXCEPT
318{
319 static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned");
320 if (sizeof(_Tp) <= sizeof(unsigned int))
321 return __libcpp_popcount(static_cast<unsigned int>(__t));
322 else if (sizeof(_Tp) <= sizeof(unsigned long))
323 return __libcpp_popcount(static_cast<unsigned long>(__t));
324 else if (sizeof(_Tp) <= sizeof(unsigned long long))
325 return __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000326 else
327 {
328 int __ret = 0;
329 while (__t != 0)
330 {
Marshall Clow370375e2019-07-12 01:01:55 +0000331 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000332 __t >>= numeric_limits<unsigned long long>::digits;
333 }
334 return __ret;
335 }
336}
337
338
339// integral log base 2
340template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000341_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
342unsigned __bit_log2(_Tp __t) _NOEXCEPT
343{
344 static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned");
345 return std::numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
346}
347
348template <class _Tp>
349_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
350bool __ispow2(_Tp __t) _NOEXCEPT
351{
352 static_assert(__bitop_unsigned_integer<_Tp>::value, "__ispow2 requires unsigned");
Louis Dionnefd77e4f2019-10-23 10:40:15 -0700353 return __t != 0 && (((__t & (__t - 1)) == 0));
Marshall Clow370375e2019-07-12 01:01:55 +0000354}
355
356
357#if _LIBCPP_STD_VER > 17
358
359template<class _Tp>
Marshall Clowd852d5d2019-07-01 23:00:32 +0000360_LIBCPP_INLINE_VISIBILITY constexpr
Marshall Clow370375e2019-07-12 01:01:55 +0000361enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
362rotl(_Tp __t, unsigned int __cnt) noexcept
363{
364 return __rotl(__t, __cnt);
365}
366
367
368// rotr
369template<class _Tp>
370_LIBCPP_INLINE_VISIBILITY constexpr
371enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
372rotr(_Tp __t, unsigned int __cnt) noexcept
373{
374 return __rotr(__t, __cnt);
375}
376
377
378template<class _Tp>
379_LIBCPP_INLINE_VISIBILITY constexpr
380enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
381countl_zero(_Tp __t) noexcept
382{
383 return __countl_zero(__t);
384}
385
386
387template<class _Tp>
388_LIBCPP_INLINE_VISIBILITY constexpr
389enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
390countl_one(_Tp __t) noexcept
391{
392 return __countl_one(__t);
393}
394
395
396template<class _Tp>
397_LIBCPP_INLINE_VISIBILITY constexpr
398enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
399countr_zero(_Tp __t) noexcept
400{
401 return __countr_zero(__t);
402}
403
404
405template<class _Tp>
406_LIBCPP_INLINE_VISIBILITY constexpr
407enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
408countr_one(_Tp __t) noexcept
409{
410 return __countr_one(__t);
411}
412
413
414template<class _Tp>
415_LIBCPP_INLINE_VISIBILITY constexpr
416enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
417popcount(_Tp __t) noexcept
418{
419 return __popcount(__t);
420}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000421
422
423template <class _Tp>
424_LIBCPP_INLINE_VISIBILITY constexpr
425enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
Marshall Clow370375e2019-07-12 01:01:55 +0000426ispow2(_Tp __t) noexcept
427{
428 return __ispow2(__t);
429}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000430
431template <class _Tp>
432_LIBCPP_INLINE_VISIBILITY constexpr
433enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000434floor2(_Tp __t) noexcept
435{
436 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
437}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000438
439template <class _Tp>
440_LIBCPP_INLINE_VISIBILITY constexpr
441enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
442ceil2(_Tp __t) noexcept
443{
444 if (__t < 2) return 1;
445 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
Marshall Clowa2851c72019-07-02 03:21:16 +0000446 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to ceil2");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000447
448 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
449 return _Tp{1} << __n;
450 else
451 {
452 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
453 const unsigned __retVal = 1u << (__n + __extra);
454 return (_Tp) (__retVal >> __extra);
455 }
456}
457
458template <class _Tp>
459_LIBCPP_INLINE_VISIBILITY constexpr
460enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
461log2p1(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000462{
463 return __t == 0 ? 0 : __bit_log2(__t) + 1;
464}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000465
Marshall Clowe8a18f32019-07-23 04:20:19 +0000466
467enum 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