1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//
#ifndef _LIBCPP_BIT
#define _LIBCPP_BIT
/*
bit synopsis
namespace std {
// [bit.cast], bit_cast
template<class To, class From>
constexpr To bit_cast(const From& from) noexcept; // C++20
// [bit.byteswap], byteswap
template<class T>
constexpr T byteswap(T value) noexcept; // C++23
// [bit.pow.two], integral powers of 2
template <class T>
constexpr bool has_single_bit(T x) noexcept; // C++20
template <class T>
constexpr T bit_ceil(T x); // C++20
template <class T>
constexpr T bit_floor(T x) noexcept; // C++20
template <class T>
constexpr int bit_width(T x) noexcept; // C++20
// [bit.rotate], rotating
template<class T>
constexpr T rotl(T x, unsigned int s) noexcept; // C++20
template<class T>
constexpr T rotr(T x, unsigned int s) noexcept; // C++20
// [bit.count], counting
template<class T>
constexpr int countl_zero(T x) noexcept; // C++20
template<class T>
constexpr int countl_one(T x) noexcept; // C++20
template<class T>
constexpr int countr_zero(T x) noexcept; // C++20
template<class T>
constexpr int countr_one(T x) noexcept; // C++20
template<class T>
constexpr int popcount(T x) noexcept; // C++20
// [bit.endian], endian
enum class endian {
little = see below, // C++20
big = see below, // C++20
native = see below // C++20
};
} // namespace std
*/
#include <__assert>
#include <__bit/bit_cast.h>
#include <__bit/byteswap.h>
#include <__bits> // __libcpp_clz
#include <__config>
#include <limits>
#include <type_traits>
#include <version>
#if defined(__IBMCPP__)
#include "__support/ibm/support.h"
#endif
#if defined(_LIBCPP_COMPILER_MSVC)
#include <intrin.h>
#endif
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
const unsigned int __dig = numeric_limits<_Tp>::digits;
if ((__cnt % __dig) == 0)
return __t;
return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
const unsigned int __dig = numeric_limits<_Tp>::digits;
if ((__cnt % __dig) == 0)
return __t;
return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
int __countr_zero(_Tp __t) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
if (__t == 0)
return numeric_limits<_Tp>::digits;
if (sizeof(_Tp) <= sizeof(unsigned int))
return __libcpp_ctz(static_cast<unsigned int>(__t));
else if (sizeof(_Tp) <= sizeof(unsigned long))
return __libcpp_ctz(static_cast<unsigned long>(__t));
else if (sizeof(_Tp) <= sizeof(unsigned long long))
return __libcpp_ctz(static_cast<unsigned long long>(__t));
else
{
int __ret = 0;
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
while (static_cast<unsigned long long>(__t) == 0uLL)
{
__ret += __ulldigits;
__t >>= __ulldigits;
}
return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t));
}
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
int __countl_zero(_Tp __t) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
if (__t == 0)
return numeric_limits<_Tp>::digits;
if (sizeof(_Tp) <= sizeof(unsigned int))
return __libcpp_clz(static_cast<unsigned int>(__t))
- (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
else if (sizeof(_Tp) <= sizeof(unsigned long))
return __libcpp_clz(static_cast<unsigned long>(__t))
- (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
else if (sizeof(_Tp) <= sizeof(unsigned long long))
return __libcpp_clz(static_cast<unsigned long long>(__t))
- (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
else
{
int __ret = 0;
int __iter = 0;
const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
while (true) {
__t = __rotr(__t, __ulldigits);
if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
break;
__ret += __iter;
}
return __ret + __iter;
}
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
int __countl_one(_Tp __t) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
return __t != numeric_limits<_Tp>::max()
? __countl_zero(static_cast<_Tp>(~__t))
: numeric_limits<_Tp>::digits;
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
int __countr_one(_Tp __t) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
return __t != numeric_limits<_Tp>::max()
? __countr_zero(static_cast<_Tp>(~__t))
: numeric_limits<_Tp>::digits;
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
int __popcount(_Tp __t) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
if (sizeof(_Tp) <= sizeof(unsigned int))
return __libcpp_popcount(static_cast<unsigned int>(__t));
else if (sizeof(_Tp) <= sizeof(unsigned long))
return __libcpp_popcount(static_cast<unsigned long>(__t));
else if (sizeof(_Tp) <= sizeof(unsigned long long))
return __libcpp_popcount(static_cast<unsigned long long>(__t));
else
{
int __ret = 0;
while (__t != 0)
{
__ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
__t >>= numeric_limits<unsigned long long>::digits;
}
return __ret;
}
}
// integral log base 2
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
unsigned __bit_log2(_Tp __t) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
bool __has_single_bit(_Tp __t) _NOEXCEPT
{
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
return __t != 0 && (((__t & (__t - 1)) == 0));
}
#if _LIBCPP_STD_VER > 17
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
rotl(_Tp __t, unsigned int __cnt) noexcept
{
return __rotl(__t, __cnt);
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
rotr(_Tp __t, unsigned int __cnt) noexcept
{
return __rotr(__t, __cnt);
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
countl_zero(_Tp __t) noexcept
{
return __countl_zero(__t);
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
countl_one(_Tp __t) noexcept
{
return __countl_one(__t);
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
countr_zero(_Tp __t) noexcept
{
return __countr_zero(__t);
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
countr_one(_Tp __t) noexcept
{
return __countr_one(__t);
}
template<class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
popcount(_Tp __t) noexcept
{
return __popcount(__t);
}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool>
has_single_bit(_Tp __t) noexcept
{
return __has_single_bit(__t);
}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
bit_floor(_Tp __t) noexcept
{
return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
bit_ceil(_Tp __t) noexcept
{
if (__t < 2) return 1;
const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
_LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
if constexpr (sizeof(_Tp) >= sizeof(unsigned))
return _Tp{1} << __n;
else
{
const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
const unsigned __retVal = 1u << (__n + __extra);
return (_Tp) (__retVal >> __extra);
}
}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
bit_width(_Tp __t) noexcept
{
return __t == 0 ? 0 : __bit_log2(__t) + 1;
}
enum class endian
{
little = 0xDEAD,
big = 0xFACE,
#if defined(_LIBCPP_LITTLE_ENDIAN)
native = little
#elif defined(_LIBCPP_BIG_ENDIAN)
native = big
#else
native = 0xCAFE
#endif
};
#endif // _LIBCPP_STD_VER > 17
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // _LIBCPP_BIT
|