xref: /freebsd/contrib/llvm-project/libcxx/include/bit (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
1// -*- C++ -*-
2//===------------------------------ bit ----------------------------------===//
3//
4// 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
7//
8//===---------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_BIT
11#define _LIBCPP_BIT
12
13/*
14    bit synopsis
15
16namespace std {
17
18  // [bit.pow.two], integral powers of 2
19  template <class T>
20    constexpr bool has_single_bit(T x) noexcept; // C++20
21  template <class T>
22    constexpr T bit_ceil(T x);                   // C++20
23  template <class T>
24    constexpr T bit_floor(T x) noexcept;         // C++20
25  template <class T>
26    constexpr T bit_width(T x) noexcept;         // C++20
27
28  // [bit.rotate], rotating
29  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
34  // [bit.count], counting
35  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
46  // [bit.endian], endian
47  enum class endian {
48    little = see below,        // C++20
49    big = see below,           // C++20
50    native = see below         // C++20
51};
52
53} // namespace std
54
55*/
56
57#include <__config>
58#include <__bits>
59#include <limits>
60#include <type_traits>
61#include <version>
62#include <__debug>
63
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
75_LIBCPP_PUSH_MACROS
76#include <__undef_macros>
77
78_LIBCPP_BEGIN_NAMESPACE_STD
79
80
81template <class _Tp>
82using __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
83         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
90    >;
91
92
93template<class _Tp>
94_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
95_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
96{
97    static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
98    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
105template<class _Tp>
106_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
107_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
108{
109    static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
110    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
117
118template<class _Tp>
119_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
120int __countr_zero(_Tp __t) _NOEXCEPT
121{
122    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
123    if (__t == 0)
124        return numeric_limits<_Tp>::digits;
125
126    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));
132    else
133    {
134        int __ret = 0;
135        int __iter = 0;
136        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
137        while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
138        {
139            __ret += __iter;
140            __t >>= __ulldigits;
141        }
142        return __ret + __iter;
143    }
144}
145
146template<class _Tp>
147_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
148int __countl_zero(_Tp __t) _NOEXCEPT
149{
150    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");
183    return __t != numeric_limits<_Tp>::max()
184        ? __countl_zero(static_cast<_Tp>(~__t))
185        : numeric_limits<_Tp>::digits;
186}
187
188
189template<class _Tp>
190_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
191int __countr_one(_Tp __t) _NOEXCEPT
192{
193    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));
212    else
213    {
214        int __ret = 0;
215        while (__t != 0)
216        {
217            __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
218            __t >>= numeric_limits<unsigned long long>::digits;
219        }
220        return __ret;
221    }
222}
223
224
225// integral log base 2
226template<class _Tp>
227_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");
231    return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
232}
233
234template <class _Tp>
235_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
236bool __has_single_bit(_Tp __t) _NOEXCEPT
237{
238    static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned");
239    return __t != 0 && (((__t & (__t - 1)) == 0));
240}
241
242
243#if _LIBCPP_STD_VER > 17
244
245template<class _Tp>
246_LIBCPP_INLINE_VISIBILITY constexpr
247enable_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{
287    return __countr_zero(__t);
288}
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}
307
308
309template <class _Tp>
310_LIBCPP_INLINE_VISIBILITY constexpr
311enable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
312has_single_bit(_Tp __t) noexcept
313{
314    return __has_single_bit(__t);
315}
316
317template <class _Tp>
318_LIBCPP_INLINE_VISIBILITY constexpr
319enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
320bit_floor(_Tp __t) noexcept
321{
322    return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
323}
324
325template <class _Tp>
326_LIBCPP_INLINE_VISIBILITY constexpr
327enable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
328bit_ceil(_Tp __t) noexcept
329{
330    if (__t < 2) return 1;
331    const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
332    _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
333
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>
347bit_width(_Tp __t) noexcept
348{
349    return __t == 0 ? 0 : __bit_log2(__t) + 1;
350}
351
352enum 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
365#endif // _LIBCPP_STD_VER > 17
366
367_LIBCPP_END_NAMESPACE_STD
368
369_LIBCPP_POP_MACROS
370
371#endif // _LIBCPP_BIT
372