blob: 1c0e8ad3ba8bda860c2ed9877b85b042f0556e53 [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
Marshall Clowbf32ec92018-08-17 16:07:48 +000045} // namespace std
46
47*/
48
49#include <__config>
Marshall Clowd852d5d2019-07-01 23:00:32 +000050#include <limits>
51#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000052#include <version>
Marshall Clowd852d5d2019-07-01 23:00:32 +000053#include <__debug>
Marshall Clowbf32ec92018-08-17 16:07:48 +000054
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 Clowd852d5d2019-07-01 23:00:32 +000066_LIBCPP_PUSH_MACROS
67#include <__undef_macros>
68
Marshall Clowbf32ec92018-08-17 16:07:48 +000069_LIBCPP_BEGIN_NAMESPACE_STD
70
Marshall Clowea687e22018-08-17 17:27:25 +000071#ifndef _LIBCPP_COMPILER_MSVC
72
Marshall Clowd852d5d2019-07-01 23:00:32 +000073inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000074int __libcpp_ctz(unsigned __x) _NOEXCEPT { return __builtin_ctz(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000075
Marshall Clowd852d5d2019-07-01 23:00:32 +000076inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000077int __libcpp_ctz(unsigned long __x) _NOEXCEPT { return __builtin_ctzl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000078
Marshall Clowd852d5d2019-07-01 23:00:32 +000079inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000080int __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000081
82
Marshall Clowd852d5d2019-07-01 23:00:32 +000083inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000084int __libcpp_clz(unsigned __x) _NOEXCEPT { return __builtin_clz(__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_clz(unsigned long __x) _NOEXCEPT { return __builtin_clzl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000088
Marshall Clowd852d5d2019-07-01 23:00:32 +000089inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000090int __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000091
92
Marshall Clowd852d5d2019-07-01 23:00:32 +000093inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +000094int __libcpp_popcount(unsigned __x) _NOEXCEPT { return __builtin_popcount(__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_popcount(unsigned long __x) _NOEXCEPT { return __builtin_popcountl(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +000098
Marshall Clowd852d5d2019-07-01 23:00:32 +000099inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow370375e2019-07-12 01:01:55 +0000100int __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
Marshall Clowea687e22018-08-17 17:27:25 +0000101
102#else // _LIBCPP_COMPILER_MSVC
103
Marshall Clowbf32ec92018-08-17 16:07:48 +0000104// Precondition: __x != 0
105inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000106int __libcpp_ctz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000107 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
108 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +0000109 unsigned long __where;
110 if (_BitScanForward(&__where, __x))
111 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000112 return 32;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000113}
114
115inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000116int __libcpp_ctz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000117 static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
118 return __ctz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000119}
120
121inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000122int __libcpp_ctz(unsigned long long __x) {
Marshall Clowea687e22018-08-17 17:27:25 +0000123 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000124#if defined(_LIBCPP_HAS_BITSCAN64)
125 (defined(_M_AMD64) || defined(__x86_64__))
Marshall Clowea687e22018-08-17 17:27:25 +0000126 if (_BitScanForward64(&__where, __x))
127 return static_cast<int>(__where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000128#else
129 // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
Marshall Clowea687e22018-08-17 17:27:25 +0000130 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 Clowbf32ec92018-08-17 16:07:48 +0000134#endif
135 return 64;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000136}
137
138// Precondition: __x != 0
139inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000140int __libcpp_clz(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000141 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
142 static_assert(sizeof(unsigned long) == 4, "");
Marshall Clowea687e22018-08-17 17:27:25 +0000143 unsigned long __where;
144 if (_BitScanReverse(&__where, __x))
145 return static_cast<int>(31 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000146 return 32; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000147}
148
149inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000150int __libcpp_clz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000151 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
Marshall Clow370375e2019-07-12 01:01:55 +0000152 return __libcpp_clz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000153}
154
155inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow370375e2019-07-12 01:01:55 +0000156int __libcpp_clz(unsigned long long __x) {
Marshall Clowea687e22018-08-17 17:27:25 +0000157 unsigned long __where;
Marshall Clowbf32ec92018-08-17 16:07:48 +0000158#if defined(_LIBCPP_HAS_BITSCAN64)
Marshall Clowea687e22018-08-17 17:27:25 +0000159 if (_BitScanReverse64(&__where, __x))
160 return static_cast<int>(63 - __where);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000161#else
Marshall Clowea687e22018-08-17 17:27:25 +0000162 // 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 Clowbf32ec92018-08-17 16:07:48 +0000167#endif
168 return 64; // Undefined Behavior.
Marshall Clowbf32ec92018-08-17 16:07:48 +0000169}
170
Marshall Clow370375e2019-07-12 01:01:55 +0000171inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000172 static_assert(sizeof(unsigned) == 4, "");
173 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000174}
175
Marshall Clow370375e2019-07-12 01:01:55 +0000176inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000177 static_assert(sizeof(unsigned long) == 4, "");
178 return __popcnt(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000179}
180
Marshall Clow370375e2019-07-12 01:01:55 +0000181inline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000182 static_assert(sizeof(unsigned long long) == 8, "");
183 return __popcnt64(__x);
Marshall Clowbf32ec92018-08-17 16:07:48 +0000184}
185
Marshall Clowea687e22018-08-17 17:27:25 +0000186#endif // _LIBCPP_COMPILER_MSVC
187
Marshall Clowd852d5d2019-07-01 23:00:32 +0000188template <class _Tp>
189using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Marshall Clow370375e2019-07-12 01:01:55 +0000190 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 Clowd852d5d2019-07-01 23:00:32 +0000197 >;
198
199
Marshall Clowd852d5d2019-07-01 23:00:32 +0000200template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000201_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
202_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000203{
Marshall Clow370375e2019-07-12 01:01:55 +0000204 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000205 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 Clowd852d5d2019-07-01 23:00:32 +0000212template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000213_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
214_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000215{
Marshall Clow370375e2019-07-12 01:01:55 +0000216 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000217 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 Clow370375e2019-07-12 01:01:55 +0000224
Marshall Clowd852d5d2019-07-01 23:00:32 +0000225template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000226_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
227int __countr_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000228{
Marshall Clow370375e2019-07-12 01:01:55 +0000229 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000230 if (__t == 0)
231 return numeric_limits<_Tp>::digits;
232
Marshall Clow370375e2019-07-12 01:01:55 +0000233 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 Clowd852d5d2019-07-01 23:00:32 +0000239 else
240 {
241 int __ret = 0;
242 int __iter = 0;
243 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
Marshall Clow370375e2019-07-12 01:01:55 +0000244 while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
Marshall Clowd852d5d2019-07-01 23:00:32 +0000245 {
246 __ret += __iter;
247 __t >>= __ulldigits;
248 }
249 return __ret + __iter;
250 }
251}
252
Marshall Clowd852d5d2019-07-01 23:00:32 +0000253template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000254_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
255int __countl_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000256{
Marshall Clow370375e2019-07-12 01:01:55 +0000257 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
285template<class _Tp>
286_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
287int __countl_one(_Tp __t) _NOEXCEPT
288{
289 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000290 return __t != numeric_limits<_Tp>::max()
Marshall Clow370375e2019-07-12 01:01:55 +0000291 ? __countl_zero(static_cast<_Tp>(~__t))
Marshall Clowd852d5d2019-07-01 23:00:32 +0000292 : numeric_limits<_Tp>::digits;
293}
294
295
Marshall Clowd852d5d2019-07-01 23:00:32 +0000296template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000297_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
298int __countr_one(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000299{
Marshall Clow370375e2019-07-12 01:01:55 +0000300 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
307template<class _Tp>
308_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
309int
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 Clowd852d5d2019-07-01 23:00:32 +0000319 else
320 {
321 int __ret = 0;
322 while (__t != 0)
323 {
Marshall Clow370375e2019-07-12 01:01:55 +0000324 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000325 __t >>= numeric_limits<unsigned long long>::digits;
326 }
327 return __ret;
328 }
329}
330
331
332// integral log base 2
333template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000334_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
335unsigned __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
341template <class _Tp>
342_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
343bool __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
352template<class _Tp>
Marshall Clowd852d5d2019-07-01 23:00:32 +0000353_LIBCPP_INLINE_VISIBILITY constexpr
Marshall Clow370375e2019-07-12 01:01:55 +0000354enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
355rotl(_Tp __t, unsigned int __cnt) noexcept
356{
357 return __rotl(__t, __cnt);
358}
359
360
361// rotr
362template<class _Tp>
363_LIBCPP_INLINE_VISIBILITY constexpr
364enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
365rotr(_Tp __t, unsigned int __cnt) noexcept
366{
367 return __rotr(__t, __cnt);
368}
369
370
371template<class _Tp>
372_LIBCPP_INLINE_VISIBILITY constexpr
373enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
374countl_zero(_Tp __t) noexcept
375{
376 return __countl_zero(__t);
377}
378
379
380template<class _Tp>
381_LIBCPP_INLINE_VISIBILITY constexpr
382enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
383countl_one(_Tp __t) noexcept
384{
385 return __countl_one(__t);
386}
387
388
389template<class _Tp>
390_LIBCPP_INLINE_VISIBILITY constexpr
391enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
392countr_zero(_Tp __t) noexcept
393{
394 return __countr_zero(__t);
395}
396
397
398template<class _Tp>
399_LIBCPP_INLINE_VISIBILITY constexpr
400enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
401countr_one(_Tp __t) noexcept
402{
403 return __countr_one(__t);
404}
405
406
407template<class _Tp>
408_LIBCPP_INLINE_VISIBILITY constexpr
409enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
410popcount(_Tp __t) noexcept
411{
412 return __popcount(__t);
413}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000414
415
416template <class _Tp>
417_LIBCPP_INLINE_VISIBILITY constexpr
418enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
Marshall Clow370375e2019-07-12 01:01:55 +0000419ispow2(_Tp __t) noexcept
420{
421 return __ispow2(__t);
422}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000423
424template <class _Tp>
425_LIBCPP_INLINE_VISIBILITY constexpr
426enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000427floor2(_Tp __t) noexcept
428{
429 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
430}
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>
435ceil2(_Tp __t) noexcept
436{
437 if (__t < 2) return 1;
438 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
Marshall Clowa2851c72019-07-02 03:21:16 +0000439 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to ceil2");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000440
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
451template <class _Tp>
452_LIBCPP_INLINE_VISIBILITY constexpr
453enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
454log2p1(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000455{
456 return __t == 0 ? 0 : __bit_log2(__t) + 1;
457}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000458
459#endif // _LIBCPP_STD_VER > 17
460
Marshall Clowbf32ec92018-08-17 16:07:48 +0000461_LIBCPP_END_NAMESPACE_STD
462
Marshall Clowd852d5d2019-07-01 23:00:32 +0000463_LIBCPP_POP_MACROS
464
Marshall Clowbf32ec92018-08-17 16:07:48 +0000465#endif // _LIBCPP_BIT