blob: 9c45edd47ee6297bdd62d25e639056d8f89d7052 [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
74int __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
77int __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
80int __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
84int __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
87int __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
90int __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
94int __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
97int __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
100int __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 Clowea687e22018-08-17 17:27:25 +0000106int __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 Clowea687e22018-08-17 17:27:25 +0000116int __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 Clowea687e22018-08-17 17:27:25 +0000122int __ctz(unsigned long long __x) {
123 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 Clowea687e22018-08-17 17:27:25 +0000140int __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 Clowea687e22018-08-17 17:27:25 +0000150int __clz(unsigned long __x) {
Marshall Clowbf32ec92018-08-17 16:07:48 +0000151 static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
152 return __clz(static_cast<unsigned>(__x));
Marshall Clowbf32ec92018-08-17 16:07:48 +0000153}
154
155inline _LIBCPP_INLINE_VISIBILITY
Marshall Clowea687e22018-08-17 17:27:25 +0000156int __clz(unsigned long long __x) {
157 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 Clowea687e22018-08-17 17:27:25 +0000171inline _LIBCPP_INLINE_VISIBILITY int __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 Clowea687e22018-08-17 17:27:25 +0000176inline _LIBCPP_INLINE_VISIBILITY int __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 Clowea687e22018-08-17 17:27:25 +0000181inline _LIBCPP_INLINE_VISIBILITY int __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 +0000188#if _LIBCPP_STD_VER > 17
189
190template <class _Tp>
191using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
192 is_integral_v<_Tp> &&
193 is_unsigned_v<_Tp> &&
194 _IsNotSame<remove_cv_t<_Tp>, bool>::value &&
195 _IsNotSame<remove_cv_t<_Tp>, signed char>::value &&
196 _IsNotSame<remove_cv_t<_Tp>, wchar_t>::value &&
197 _IsNotSame<remove_cv_t<_Tp>, char16_t>::value &&
198 _IsNotSame<remove_cv_t<_Tp>, char32_t>::value
199 >;
200
201
202// rotl
203template<class _Tp>
204inline _LIBCPP_INLINE_VISIBILITY constexpr
205enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
206rotl(_Tp __t, unsigned int __cnt) noexcept
207{
208 const unsigned int __dig = numeric_limits<_Tp>::digits;
209 if ((__cnt % __dig) == 0)
210 return __t;
211 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
212}
213
214
215// rotr
216template<class _Tp>
217_LIBCPP_INLINE_VISIBILITY constexpr
218enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
219rotr(_Tp __t, unsigned int __cnt) noexcept
220{
221 const unsigned int __dig = numeric_limits<_Tp>::digits;
222 if ((__cnt % __dig) == 0)
223 return __t;
224 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
225}
226
227
228// countl_zero
229template<class _Tp>
230_LIBCPP_INLINE_VISIBILITY constexpr
231enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
232countl_zero(_Tp __t) noexcept
233{
234 if (__t == 0)
235 return numeric_limits<_Tp>::digits;
236
237 if constexpr (sizeof(_Tp) <= sizeof(unsigned int))
238 return __clz(static_cast<unsigned int>(__t))
239 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
240 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
241 return __clz(static_cast<unsigned long>(__t))
242 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
243 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
244 return __clz(static_cast<unsigned long long>(__t))
245 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
246 else
247 {
248 int __ret = 0;
249 int __iter = 0;
250 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
251 while (true) {
252 __t = rotr(__t, __ulldigits);
253 if ((__iter = countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
254 break;
255 __ret += __iter;
256 }
257 return __ret + __iter;
258 }
259}
260
261
262// countl_one
263template<class _Tp>
264_LIBCPP_INLINE_VISIBILITY constexpr
265enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
266countl_one(_Tp __t) noexcept
267{
268 return __t != numeric_limits<_Tp>::max()
269 ? countl_zero(static_cast<_Tp>(~__t))
270 : numeric_limits<_Tp>::digits;
271}
272
273
274// countr_zero
275template<class _Tp>
276_LIBCPP_INLINE_VISIBILITY constexpr
277enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
278countr_zero(_Tp __t) noexcept
279{
280 if (__t == 0)
281 return numeric_limits<_Tp>::digits;
282
283 if constexpr (sizeof(_Tp) <= sizeof(unsigned int))
284 return __ctz(static_cast<unsigned int>(__t));
285 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
286 return __ctz(static_cast<unsigned long>(__t));
287 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
288 return __ctz(static_cast<unsigned long long>(__t));
289 else
290 {
291 int __ret = 0;
292 int __iter = 0;
293 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
294 while ((__iter = countr_zero(static_cast<unsigned long long>(__t))) == __ulldigits)
295 {
296 __ret += __iter;
297 __t >>= __ulldigits;
298 }
299 return __ret + __iter;
300 }
301}
302
303
304// countr_one
305template<class _Tp>
306_LIBCPP_INLINE_VISIBILITY constexpr
307enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
308countr_one(_Tp __t) noexcept
309{
310 return __t != numeric_limits<_Tp>::max()
311 ? countr_zero(static_cast<_Tp>(~__t))
312 : numeric_limits<_Tp>::digits;
313}
314
315
316// popcount
317template<class _Tp>
318_LIBCPP_INLINE_VISIBILITY constexpr
319enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
320popcount(_Tp __t) noexcept
321{
322 if constexpr (sizeof(_Tp) <= sizeof(unsigned int))
323 return __popcount(static_cast<unsigned int>(__t));
324 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long))
325 return __popcount(static_cast<unsigned long>(__t));
326 else if constexpr (sizeof(_Tp) <= sizeof(unsigned long long))
327 return __popcount(static_cast<unsigned long long>(__t));
328 else
329 {
330 int __ret = 0;
331 while (__t != 0)
332 {
333 __ret += __popcount(static_cast<unsigned long long>(__t));
334 __t >>= numeric_limits<unsigned long long>::digits;
335 }
336 return __ret;
337 }
338}
339
340
341// integral log base 2
342template<class _Tp>
343_LIBCPP_INLINE_VISIBILITY constexpr
344unsigned __bit_log2(_Tp __t) noexcept
345{ return std::numeric_limits<_Tp>::digits - 1 - countl_zero(__t); }
346
347
348template <class _Tp>
349_LIBCPP_INLINE_VISIBILITY constexpr
350enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
351ispow2(_Tp __t) noexcept { return popcount(__t) == 1; }
352
353template <class _Tp>
354_LIBCPP_INLINE_VISIBILITY constexpr
355enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
356floor2(_Tp __t) noexcept { return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t); }
357
358template <class _Tp>
359_LIBCPP_INLINE_VISIBILITY constexpr
360enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
361ceil2(_Tp __t) noexcept
362{
363 if (__t < 2) return 1;
364 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
365
366#ifndef _LIBCPP_HAS_NO_BUILTIN_IS_CONSTANT_EVALUATED
367 if (!__builtin_is_constant_evaluated ())
368 _LIBCPP_DEBUG_ASSERT( __n != numeric_limits<_Tp>::digits, "Bad input to ceil2" );
369#endif
370
371 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
372 return _Tp{1} << __n;
373 else
374 {
375 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
376 const unsigned __retVal = 1u << (__n + __extra);
377 return (_Tp) (__retVal >> __extra);
378 }
379}
380
381template <class _Tp>
382_LIBCPP_INLINE_VISIBILITY constexpr
383enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
384log2p1(_Tp __t) noexcept
385{ return __t == 0 ? 0 : __bit_log2(__t) + 1; }
386
387#endif // _LIBCPP_STD_VER > 17
388
Marshall Clowbf32ec92018-08-17 16:07:48 +0000389_LIBCPP_END_NAMESPACE_STD
390
Marshall Clowd852d5d2019-07-01 23:00:32 +0000391_LIBCPP_POP_MACROS
392
Marshall Clowbf32ec92018-08-17 16:07:48 +0000393#endif // _LIBCPP_BIT