blob: fe360179c5ca04824f1390b1fb415240f12caee6 [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>
Thorsten Schütt90821092021-01-18 13:21:00 +010058#include <__bits>
Marshall Clowd852d5d2019-07-01 23:00:32 +000059#include <limits>
60#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000061#include <version>
Marshall Clowd852d5d2019-07-01 23:00:32 +000062#include <__debug>
Marshall Clowbf32ec92018-08-17 16:07:48 +000063
64#if defined(__IBMCPP__)
65#include "support/ibm/support.h"
66#endif
67#if defined(_LIBCPP_COMPILER_MSVC)
68#include <intrin.h>
69#endif
70
71#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
72#pragma GCC system_header
73#endif
74
Marshall Clowd852d5d2019-07-01 23:00:32 +000075_LIBCPP_PUSH_MACROS
76#include <__undef_macros>
77
Marshall Clowbf32ec92018-08-17 16:07:48 +000078_LIBCPP_BEGIN_NAMESPACE_STD
79
Marshall Clowea687e22018-08-17 17:27:25 +000080
Marshall Clowd852d5d2019-07-01 23:00:32 +000081template <class _Tp>
82using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
Marshall Clow370375e2019-07-12 01:01:55 +000083 is_integral<_Tp>::value &&
84 is_unsigned<_Tp>::value &&
85 _IsNotSame<typename remove_cv<_Tp>::type, bool>::value &&
86 _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value &&
87 _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value &&
88 _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value &&
89 _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value
Marshall Clowd852d5d2019-07-01 23:00:32 +000090 >;
91
92
Marshall Clowd852d5d2019-07-01 23:00:32 +000093template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +000094_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
95_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +000096{
Marshall Clow370375e2019-07-12 01:01:55 +000097 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +000098 const unsigned int __dig = numeric_limits<_Tp>::digits;
99 if ((__cnt % __dig) == 0)
100 return __t;
101 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
102}
103
104
Marshall Clowd852d5d2019-07-01 23:00:32 +0000105template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000106_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
107_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000108{
Marshall Clow370375e2019-07-12 01:01:55 +0000109 static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000110 const unsigned int __dig = numeric_limits<_Tp>::digits;
111 if ((__cnt % __dig) == 0)
112 return __t;
113 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
114}
115
116
Marshall Clow370375e2019-07-12 01:01:55 +0000117
Marshall Clowd852d5d2019-07-01 23:00:32 +0000118template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000119_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
120int __countr_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000121{
Marshall Clow370375e2019-07-12 01:01:55 +0000122 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000123 if (__t == 0)
124 return numeric_limits<_Tp>::digits;
125
Marshall Clow370375e2019-07-12 01:01:55 +0000126 if (sizeof(_Tp) <= sizeof(unsigned int))
127 return __libcpp_ctz(static_cast<unsigned int>(__t));
128 else if (sizeof(_Tp) <= sizeof(unsigned long))
129 return __libcpp_ctz(static_cast<unsigned long>(__t));
130 else if (sizeof(_Tp) <= sizeof(unsigned long long))
131 return __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000132 else
133 {
134 int __ret = 0;
135 int __iter = 0;
136 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
Marshall Clow370375e2019-07-12 01:01:55 +0000137 while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
Marshall Clowd852d5d2019-07-01 23:00:32 +0000138 {
139 __ret += __iter;
140 __t >>= __ulldigits;
141 }
142 return __ret + __iter;
143 }
144}
145
Marshall Clowd852d5d2019-07-01 23:00:32 +0000146template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000147_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
148int __countl_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000149{
Marshall Clow370375e2019-07-12 01:01:55 +0000150 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned");
151 if (__t == 0)
152 return numeric_limits<_Tp>::digits;
153
154 if (sizeof(_Tp) <= sizeof(unsigned int))
155 return __libcpp_clz(static_cast<unsigned int>(__t))
156 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
157 else if (sizeof(_Tp) <= sizeof(unsigned long))
158 return __libcpp_clz(static_cast<unsigned long>(__t))
159 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
160 else if (sizeof(_Tp) <= sizeof(unsigned long long))
161 return __libcpp_clz(static_cast<unsigned long long>(__t))
162 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
163 else
164 {
165 int __ret = 0;
166 int __iter = 0;
167 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
168 while (true) {
169 __t = __rotr(__t, __ulldigits);
170 if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
171 break;
172 __ret += __iter;
173 }
174 return __ret + __iter;
175 }
176}
177
178template<class _Tp>
179_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
180int __countl_one(_Tp __t) _NOEXCEPT
181{
182 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000183 return __t != numeric_limits<_Tp>::max()
Marshall Clow370375e2019-07-12 01:01:55 +0000184 ? __countl_zero(static_cast<_Tp>(~__t))
Marshall Clowd852d5d2019-07-01 23:00:32 +0000185 : numeric_limits<_Tp>::digits;
186}
187
188
Marshall Clowd852d5d2019-07-01 23:00:32 +0000189template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000190_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
191int __countr_one(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000192{
Marshall Clow370375e2019-07-12 01:01:55 +0000193 static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned");
194 return __t != numeric_limits<_Tp>::max()
195 ? __countr_zero(static_cast<_Tp>(~__t))
196 : numeric_limits<_Tp>::digits;
197}
198
199
200template<class _Tp>
201_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
202int
203__popcount(_Tp __t) _NOEXCEPT
204{
205 static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned");
206 if (sizeof(_Tp) <= sizeof(unsigned int))
207 return __libcpp_popcount(static_cast<unsigned int>(__t));
208 else if (sizeof(_Tp) <= sizeof(unsigned long))
209 return __libcpp_popcount(static_cast<unsigned long>(__t));
210 else if (sizeof(_Tp) <= sizeof(unsigned long long))
211 return __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000212 else
213 {
214 int __ret = 0;
215 while (__t != 0)
216 {
Marshall Clow370375e2019-07-12 01:01:55 +0000217 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000218 __t >>= numeric_limits<unsigned long long>::digits;
219 }
220 return __ret;
221 }
222}
223
224
225// integral log base 2
226template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000227_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
228unsigned __bit_log2(_Tp __t) _NOEXCEPT
229{
230 static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned");
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500231 return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000232}
233
234template <class _Tp>
235_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Mark de Wever896e0742020-11-24 14:50:49 +0100236bool __has_single_bit(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000237{
Mark de Wever896e0742020-11-24 14:50:49 +0100238 static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned");
Louis Dionnea1e3b7d2020-05-28 14:28:09 -0400239 return __t != 0 && (((__t & (__t - 1)) == 0));
Marshall Clow370375e2019-07-12 01:01:55 +0000240}
241
242
243#if _LIBCPP_STD_VER > 17
244
245template<class _Tp>
Marshall Clowd852d5d2019-07-01 23:00:32 +0000246_LIBCPP_INLINE_VISIBILITY constexpr
Marshall Clow370375e2019-07-12 01:01:55 +0000247enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
248rotl(_Tp __t, unsigned int __cnt) noexcept
249{
250 return __rotl(__t, __cnt);
251}
252
253
254// rotr
255template<class _Tp>
256_LIBCPP_INLINE_VISIBILITY constexpr
257enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
258rotr(_Tp __t, unsigned int __cnt) noexcept
259{
260 return __rotr(__t, __cnt);
261}
262
263
264template<class _Tp>
265_LIBCPP_INLINE_VISIBILITY constexpr
266enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
267countl_zero(_Tp __t) noexcept
268{
269 return __countl_zero(__t);
270}
271
272
273template<class _Tp>
274_LIBCPP_INLINE_VISIBILITY constexpr
275enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
276countl_one(_Tp __t) noexcept
277{
278 return __countl_one(__t);
279}
280
281
282template<class _Tp>
283_LIBCPP_INLINE_VISIBILITY constexpr
284enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
285countr_zero(_Tp __t) noexcept
286{
Mark de Wever896e0742020-11-24 14:50:49 +0100287 return __countr_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000288}
289
290
291template<class _Tp>
292_LIBCPP_INLINE_VISIBILITY constexpr
293enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
294countr_one(_Tp __t) noexcept
295{
296 return __countr_one(__t);
297}
298
299
300template<class _Tp>
301_LIBCPP_INLINE_VISIBILITY constexpr
302enable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
303popcount(_Tp __t) noexcept
304{
305 return __popcount(__t);
306}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000307
308
309template <class _Tp>
310_LIBCPP_INLINE_VISIBILITY constexpr
311enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
Mark de Wever896e0742020-11-24 14:50:49 +0100312has_single_bit(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000313{
Mark de Wever896e0742020-11-24 14:50:49 +0100314 return __has_single_bit(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000315}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000316
317template <class _Tp>
318_LIBCPP_INLINE_VISIBILITY constexpr
319enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100320bit_floor(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000321{
322 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
323}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000324
325template <class _Tp>
326_LIBCPP_INLINE_VISIBILITY constexpr
327enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100328bit_ceil(_Tp __t) noexcept
Marshall Clowd852d5d2019-07-01 23:00:32 +0000329{
330 if (__t < 2) return 1;
331 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
Mark de Wever896e0742020-11-24 14:50:49 +0100332 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000333
334 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
335 return _Tp{1} << __n;
336 else
337 {
338 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
339 const unsigned __retVal = 1u << (__n + __extra);
340 return (_Tp) (__retVal >> __extra);
341 }
342}
343
344template <class _Tp>
345_LIBCPP_INLINE_VISIBILITY constexpr
346enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100347bit_width(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000348{
349 return __t == 0 ? 0 : __bit_log2(__t) + 1;
350}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000351
Marshall Clowe8a18f32019-07-23 04:20:19 +0000352enum class endian
353{
354 little = 0xDEAD,
355 big = 0xFACE,
356#if defined(_LIBCPP_LITTLE_ENDIAN)
357 native = little
358#elif defined(_LIBCPP_BIG_ENDIAN)
359 native = big
360#else
361 native = 0xCAFE
362#endif
363};
364
Marshall Clowd852d5d2019-07-01 23:00:32 +0000365#endif // _LIBCPP_STD_VER > 17
366
Marshall Clowbf32ec92018-08-17 16:07:48 +0000367_LIBCPP_END_NAMESPACE_STD
368
Marshall Clowd852d5d2019-07-01 23:00:32 +0000369_LIBCPP_POP_MACROS
370
Marshall Clowbf32ec92018-08-17 16:07:48 +0000371#endif // _LIBCPP_BIT