blob: eab67977a19907b093f066b15ad6291dacc1cfed [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 {
Louis Dionne1462d4d2020-05-28 14:28:38 -040017 // [bit.cast], bit_cast
18 template<class To, class From>
19 constexpr To bit_cast(const From& from) noexcept; // C++20
Marshall Clowbf32ec92018-08-17 16:07:48 +000020
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040021 // [bit.pow.two], integral powers of 2
Marshall Clowd852d5d2019-07-01 23:00:32 +000022 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010023 constexpr bool has_single_bit(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000024 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010025 constexpr T bit_ceil(T x); // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000026 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010027 constexpr T bit_floor(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000028 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010029 constexpr T bit_width(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000030
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040031 // [bit.rotate], rotating
Marshall Clowd852d5d2019-07-01 23:00:32 +000032 template<class T>
33 constexpr T rotl(T x, unsigned int s) noexcept; // C++20
34 template<class T>
35 constexpr T rotr(T x, unsigned int s) noexcept; // C++20
36
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040037 // [bit.count], counting
Marshall Clowd852d5d2019-07-01 23:00:32 +000038 template<class T>
39 constexpr int countl_zero(T x) noexcept; // C++20
40 template<class T>
41 constexpr int countl_one(T x) noexcept; // C++20
42 template<class T>
43 constexpr int countr_zero(T x) noexcept; // C++20
44 template<class T>
45 constexpr int countr_one(T x) noexcept; // C++20
46 template<class T>
47 constexpr int popcount(T x) noexcept; // C++20
48
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040049 // [bit.endian], endian
Marshall Clowe8a18f32019-07-23 04:20:19 +000050 enum class endian {
51 little = see below, // C++20
52 big = see below, // C++20
53 native = see below // C++20
54};
55
Marshall Clowbf32ec92018-08-17 16:07:48 +000056} // namespace std
57
58*/
59
Louis Dionne1462d4d2020-05-28 14:28:38 -040060#include <__bit/bit_cast.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040061#include <__bits> // __libcpp_clz
Louis Dionne1462d4d2020-05-28 14:28:38 -040062#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -040063#include <__debug>
Marshall Clowd852d5d2019-07-01 23:00:32 +000064#include <limits>
65#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000066#include <version>
Marshall Clowbf32ec92018-08-17 16:07:48 +000067
68#if defined(__IBMCPP__)
Louis Dionne90a04492021-02-02 16:58:38 -050069#include "__support/ibm/support.h"
Marshall Clowbf32ec92018-08-17 16:07:48 +000070#endif
71#if defined(_LIBCPP_COMPILER_MSVC)
72#include <intrin.h>
73#endif
74
75#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
76#pragma GCC system_header
77#endif
78
Marshall Clowd852d5d2019-07-01 23:00:32 +000079_LIBCPP_PUSH_MACROS
80#include <__undef_macros>
81
Marshall Clowbf32ec92018-08-17 16:07:48 +000082_LIBCPP_BEGIN_NAMESPACE_STD
83
Marshall Clowd852d5d2019-07-01 23:00:32 +000084template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +000085_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
86_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +000087{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -040088 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +000089 const unsigned int __dig = numeric_limits<_Tp>::digits;
90 if ((__cnt % __dig) == 0)
91 return __t;
92 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
93}
94
Marshall Clowd852d5d2019-07-01 23:00:32 +000095template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +000096_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
97_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +000098{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -040099 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000100 const unsigned int __dig = numeric_limits<_Tp>::digits;
101 if ((__cnt % __dig) == 0)
102 return __t;
103 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
104}
105
Marshall Clowd852d5d2019-07-01 23:00:32 +0000106template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000107_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
108int __countr_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000109{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400110 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000111 if (__t == 0)
112 return numeric_limits<_Tp>::digits;
113
Marshall Clow370375e2019-07-12 01:01:55 +0000114 if (sizeof(_Tp) <= sizeof(unsigned int))
115 return __libcpp_ctz(static_cast<unsigned int>(__t));
116 else if (sizeof(_Tp) <= sizeof(unsigned long))
117 return __libcpp_ctz(static_cast<unsigned long>(__t));
118 else if (sizeof(_Tp) <= sizeof(unsigned long long))
119 return __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000120 else
121 {
122 int __ret = 0;
Marshall Clowd852d5d2019-07-01 23:00:32 +0000123 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400124 while (static_cast<unsigned long long>(__t) == 0uLL)
Marshall Clowd852d5d2019-07-01 23:00:32 +0000125 {
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400126 __ret += __ulldigits;
Marshall Clowd852d5d2019-07-01 23:00:32 +0000127 __t >>= __ulldigits;
128 }
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400129 return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000130 }
131}
132
Marshall Clowd852d5d2019-07-01 23:00:32 +0000133template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000134_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
135int __countl_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000136{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400137 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000138 if (__t == 0)
139 return numeric_limits<_Tp>::digits;
140
141 if (sizeof(_Tp) <= sizeof(unsigned int))
142 return __libcpp_clz(static_cast<unsigned int>(__t))
143 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
144 else if (sizeof(_Tp) <= sizeof(unsigned long))
145 return __libcpp_clz(static_cast<unsigned long>(__t))
146 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
147 else if (sizeof(_Tp) <= sizeof(unsigned long long))
148 return __libcpp_clz(static_cast<unsigned long long>(__t))
149 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
150 else
151 {
152 int __ret = 0;
153 int __iter = 0;
154 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
155 while (true) {
156 __t = __rotr(__t, __ulldigits);
157 if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
158 break;
159 __ret += __iter;
160 }
161 return __ret + __iter;
162 }
163}
164
165template<class _Tp>
166_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
167int __countl_one(_Tp __t) _NOEXCEPT
168{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400169 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000170 return __t != numeric_limits<_Tp>::max()
Marshall Clow370375e2019-07-12 01:01:55 +0000171 ? __countl_zero(static_cast<_Tp>(~__t))
Marshall Clowd852d5d2019-07-01 23:00:32 +0000172 : numeric_limits<_Tp>::digits;
173}
174
Marshall Clowd852d5d2019-07-01 23:00:32 +0000175template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000176_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
177int __countr_one(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000178{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400179 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000180 return __t != numeric_limits<_Tp>::max()
181 ? __countr_zero(static_cast<_Tp>(~__t))
182 : numeric_limits<_Tp>::digits;
183}
184
Marshall Clow370375e2019-07-12 01:01:55 +0000185template<class _Tp>
186_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400187int __popcount(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000188{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400189 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000190 if (sizeof(_Tp) <= sizeof(unsigned int))
191 return __libcpp_popcount(static_cast<unsigned int>(__t));
192 else if (sizeof(_Tp) <= sizeof(unsigned long))
193 return __libcpp_popcount(static_cast<unsigned long>(__t));
194 else if (sizeof(_Tp) <= sizeof(unsigned long long))
195 return __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000196 else
197 {
198 int __ret = 0;
199 while (__t != 0)
200 {
Marshall Clow370375e2019-07-12 01:01:55 +0000201 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000202 __t >>= numeric_limits<unsigned long long>::digits;
203 }
204 return __ret;
205 }
206}
207
Marshall Clowd852d5d2019-07-01 23:00:32 +0000208// integral log base 2
209template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000210_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
211unsigned __bit_log2(_Tp __t) _NOEXCEPT
212{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400213 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500214 return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000215}
216
217template <class _Tp>
218_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Mark de Wever896e0742020-11-24 14:50:49 +0100219bool __has_single_bit(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000220{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400221 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
Louis Dionnea1e3b7d2020-05-28 14:28:09 -0400222 return __t != 0 && (((__t & (__t - 1)) == 0));
Marshall Clow370375e2019-07-12 01:01:55 +0000223}
224
Marshall Clow370375e2019-07-12 01:01:55 +0000225#if _LIBCPP_STD_VER > 17
226
227template<class _Tp>
Marshall Clowd852d5d2019-07-01 23:00:32 +0000228_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400229enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000230rotl(_Tp __t, unsigned int __cnt) noexcept
231{
232 return __rotl(__t, __cnt);
233}
234
Marshall Clow370375e2019-07-12 01:01:55 +0000235template<class _Tp>
236_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400237enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000238rotr(_Tp __t, unsigned int __cnt) noexcept
239{
240 return __rotr(__t, __cnt);
241}
242
Marshall Clow370375e2019-07-12 01:01:55 +0000243template<class _Tp>
244_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400245enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000246countl_zero(_Tp __t) noexcept
247{
248 return __countl_zero(__t);
249}
250
Marshall Clow370375e2019-07-12 01:01:55 +0000251template<class _Tp>
252_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400253enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000254countl_one(_Tp __t) noexcept
255{
256 return __countl_one(__t);
257}
258
Marshall Clow370375e2019-07-12 01:01:55 +0000259template<class _Tp>
260_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400261enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000262countr_zero(_Tp __t) noexcept
263{
Mark de Wever896e0742020-11-24 14:50:49 +0100264 return __countr_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000265}
266
Marshall Clow370375e2019-07-12 01:01:55 +0000267template<class _Tp>
268_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400269enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000270countr_one(_Tp __t) noexcept
271{
272 return __countr_one(__t);
273}
274
Marshall Clow370375e2019-07-12 01:01:55 +0000275template<class _Tp>
276_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400277enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000278popcount(_Tp __t) noexcept
279{
280 return __popcount(__t);
281}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000282
Marshall Clowd852d5d2019-07-01 23:00:32 +0000283template <class _Tp>
284_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400285enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool>
Mark de Wever896e0742020-11-24 14:50:49 +0100286has_single_bit(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000287{
Mark de Wever896e0742020-11-24 14:50:49 +0100288 return __has_single_bit(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000289}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000290
291template <class _Tp>
292_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400293enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100294bit_floor(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000295{
296 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
297}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000298
299template <class _Tp>
300_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400301enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100302bit_ceil(_Tp __t) noexcept
Marshall Clowd852d5d2019-07-01 23:00:32 +0000303{
304 if (__t < 2) return 1;
305 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
Mark de Wever896e0742020-11-24 14:50:49 +0100306 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000307
308 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
309 return _Tp{1} << __n;
310 else
311 {
312 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
313 const unsigned __retVal = 1u << (__n + __extra);
314 return (_Tp) (__retVal >> __extra);
315 }
316}
317
318template <class _Tp>
319_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400320enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100321bit_width(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000322{
323 return __t == 0 ? 0 : __bit_log2(__t) + 1;
324}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000325
Marshall Clowe8a18f32019-07-23 04:20:19 +0000326enum class endian
327{
328 little = 0xDEAD,
329 big = 0xFACE,
330#if defined(_LIBCPP_LITTLE_ENDIAN)
331 native = little
332#elif defined(_LIBCPP_BIG_ENDIAN)
333 native = big
334#else
335 native = 0xCAFE
336#endif
337};
338
Marshall Clowd852d5d2019-07-01 23:00:32 +0000339#endif // _LIBCPP_STD_VER > 17
340
Marshall Clowbf32ec92018-08-17 16:07:48 +0000341_LIBCPP_END_NAMESPACE_STD
342
Marshall Clowd852d5d2019-07-01 23:00:32 +0000343_LIBCPP_POP_MACROS
344
Marshall Clowbf32ec92018-08-17 16:07:48 +0000345#endif // _LIBCPP_BIT