blob: 0aab83e7a6ebe8e1d38c79cdb2619c159bf41924 [file] [log] [blame]
Marshall Clowbf32ec92018-08-17 16:07:48 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Marshall Clowbf32ec92018-08-17 16:07:48 +00003//
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 {
Nikolas Klauserb52cfd92021-11-22 00:22:55 +010017 // [bit.cast], bit_cast
18 template<class To, class From>
19 constexpr To bit_cast(const From& from) noexcept; // C++20
20
21 // [bit.byteswap], byteswap
22 template<class T>
23 constexpr T byteswap(T value) noexcept; // C++23
Marshall Clowbf32ec92018-08-17 16:07:48 +000024
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040025 // [bit.pow.two], integral powers of 2
Marshall Clowd852d5d2019-07-01 23:00:32 +000026 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010027 constexpr bool has_single_bit(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_ceil(T x); // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000030 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010031 constexpr T bit_floor(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000032 template <class T>
Mark de Wever896e0742020-11-24 14:50:49 +010033 constexpr T bit_width(T x) noexcept; // C++20
Marshall Clowd852d5d2019-07-01 23:00:32 +000034
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040035 // [bit.rotate], rotating
Marshall Clowd852d5d2019-07-01 23:00:32 +000036 template<class T>
37 constexpr T rotl(T x, unsigned int s) noexcept; // C++20
38 template<class T>
39 constexpr T rotr(T x, unsigned int s) noexcept; // C++20
40
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040041 // [bit.count], counting
Marshall Clowd852d5d2019-07-01 23:00:32 +000042 template<class T>
43 constexpr int countl_zero(T x) noexcept; // C++20
44 template<class T>
45 constexpr int countl_one(T x) noexcept; // C++20
46 template<class T>
47 constexpr int countr_zero(T x) noexcept; // C++20
48 template<class T>
49 constexpr int countr_one(T x) noexcept; // C++20
50 template<class T>
51 constexpr int popcount(T x) noexcept; // C++20
52
Louis Dionnea1e3b7d2020-05-28 14:28:09 -040053 // [bit.endian], endian
Marshall Clowe8a18f32019-07-23 04:20:19 +000054 enum class endian {
55 little = see below, // C++20
56 big = see below, // C++20
57 native = see below // C++20
Nikolas Klauserb52cfd92021-11-22 00:22:55 +010058 };
Marshall Clowe8a18f32019-07-23 04:20:19 +000059
Marshall Clowbf32ec92018-08-17 16:07:48 +000060} // namespace std
61
62*/
63
Louis Dionne1462d4d2020-05-28 14:28:38 -040064#include <__bit/bit_cast.h>
Nikolas Klauserb52cfd92021-11-22 00:22:55 +010065#include <__bit/byteswap.h>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040066#include <__bits> // __libcpp_clz
Louis Dionne1462d4d2020-05-28 14:28:38 -040067#include <__config>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -040068#include <__debug>
Marshall Clowd852d5d2019-07-01 23:00:32 +000069#include <limits>
70#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000071#include <version>
Marshall Clowbf32ec92018-08-17 16:07:48 +000072
73#if defined(__IBMCPP__)
Louis Dionne90a04492021-02-02 16:58:38 -050074#include "__support/ibm/support.h"
Marshall Clowbf32ec92018-08-17 16:07:48 +000075#endif
76#if defined(_LIBCPP_COMPILER_MSVC)
77#include <intrin.h>
78#endif
79
80#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
81#pragma GCC system_header
82#endif
83
Marshall Clowd852d5d2019-07-01 23:00:32 +000084_LIBCPP_PUSH_MACROS
85#include <__undef_macros>
86
Marshall Clowbf32ec92018-08-17 16:07:48 +000087_LIBCPP_BEGIN_NAMESPACE_STD
88
Marshall Clowd852d5d2019-07-01 23:00:32 +000089template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +000090_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
91_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +000092{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -040093 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +000094 const unsigned int __dig = numeric_limits<_Tp>::digits;
95 if ((__cnt % __dig) == 0)
96 return __t;
97 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
98}
99
Marshall Clowd852d5d2019-07-01 23:00:32 +0000100template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000101_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
102_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000103{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400104 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000105 const unsigned int __dig = numeric_limits<_Tp>::digits;
106 if ((__cnt % __dig) == 0)
107 return __t;
108 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
109}
110
Marshall Clowd852d5d2019-07-01 23:00:32 +0000111template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000112_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
113int __countr_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000114{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400115 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000116 if (__t == 0)
117 return numeric_limits<_Tp>::digits;
118
Marshall Clow370375e2019-07-12 01:01:55 +0000119 if (sizeof(_Tp) <= sizeof(unsigned int))
120 return __libcpp_ctz(static_cast<unsigned int>(__t));
121 else if (sizeof(_Tp) <= sizeof(unsigned long))
122 return __libcpp_ctz(static_cast<unsigned long>(__t));
123 else if (sizeof(_Tp) <= sizeof(unsigned long long))
124 return __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000125 else
126 {
127 int __ret = 0;
Marshall Clowd852d5d2019-07-01 23:00:32 +0000128 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400129 while (static_cast<unsigned long long>(__t) == 0uLL)
Marshall Clowd852d5d2019-07-01 23:00:32 +0000130 {
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400131 __ret += __ulldigits;
Marshall Clowd852d5d2019-07-01 23:00:32 +0000132 __t >>= __ulldigits;
133 }
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400134 return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000135 }
136}
137
Marshall Clowd852d5d2019-07-01 23:00:32 +0000138template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000139_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
140int __countl_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000141{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400142 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000143 if (__t == 0)
144 return numeric_limits<_Tp>::digits;
145
146 if (sizeof(_Tp) <= sizeof(unsigned int))
147 return __libcpp_clz(static_cast<unsigned int>(__t))
148 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
149 else if (sizeof(_Tp) <= sizeof(unsigned long))
150 return __libcpp_clz(static_cast<unsigned long>(__t))
151 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
152 else if (sizeof(_Tp) <= sizeof(unsigned long long))
153 return __libcpp_clz(static_cast<unsigned long long>(__t))
154 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
155 else
156 {
157 int __ret = 0;
158 int __iter = 0;
159 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
160 while (true) {
161 __t = __rotr(__t, __ulldigits);
162 if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
163 break;
164 __ret += __iter;
165 }
166 return __ret + __iter;
167 }
168}
169
170template<class _Tp>
171_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
172int __countl_one(_Tp __t) _NOEXCEPT
173{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400174 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000175 return __t != numeric_limits<_Tp>::max()
Marshall Clow370375e2019-07-12 01:01:55 +0000176 ? __countl_zero(static_cast<_Tp>(~__t))
Marshall Clowd852d5d2019-07-01 23:00:32 +0000177 : numeric_limits<_Tp>::digits;
178}
179
Marshall Clowd852d5d2019-07-01 23:00:32 +0000180template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000181_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
182int __countr_one(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000183{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400184 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000185 return __t != numeric_limits<_Tp>::max()
186 ? __countr_zero(static_cast<_Tp>(~__t))
187 : numeric_limits<_Tp>::digits;
188}
189
Marshall Clow370375e2019-07-12 01:01:55 +0000190template<class _Tp>
191_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400192int __popcount(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000193{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400194 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000195 if (sizeof(_Tp) <= sizeof(unsigned int))
196 return __libcpp_popcount(static_cast<unsigned int>(__t));
197 else if (sizeof(_Tp) <= sizeof(unsigned long))
198 return __libcpp_popcount(static_cast<unsigned long>(__t));
199 else if (sizeof(_Tp) <= sizeof(unsigned long long))
200 return __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000201 else
202 {
203 int __ret = 0;
204 while (__t != 0)
205 {
Marshall Clow370375e2019-07-12 01:01:55 +0000206 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000207 __t >>= numeric_limits<unsigned long long>::digits;
208 }
209 return __ret;
210 }
211}
212
Marshall Clowd852d5d2019-07-01 23:00:32 +0000213// integral log base 2
214template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000215_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
216unsigned __bit_log2(_Tp __t) _NOEXCEPT
217{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400218 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500219 return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000220}
221
222template <class _Tp>
223_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Mark de Wever896e0742020-11-24 14:50:49 +0100224bool __has_single_bit(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000225{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400226 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
Louis Dionnea1e3b7d2020-05-28 14:28:09 -0400227 return __t != 0 && (((__t & (__t - 1)) == 0));
Marshall Clow370375e2019-07-12 01:01:55 +0000228}
229
Marshall Clow370375e2019-07-12 01:01:55 +0000230#if _LIBCPP_STD_VER > 17
231
232template<class _Tp>
Marshall Clowd852d5d2019-07-01 23:00:32 +0000233_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400234enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000235rotl(_Tp __t, unsigned int __cnt) noexcept
236{
237 return __rotl(__t, __cnt);
238}
239
Marshall Clow370375e2019-07-12 01:01:55 +0000240template<class _Tp>
241_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400242enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000243rotr(_Tp __t, unsigned int __cnt) noexcept
244{
245 return __rotr(__t, __cnt);
246}
247
Marshall Clow370375e2019-07-12 01:01:55 +0000248template<class _Tp>
249_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400250enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000251countl_zero(_Tp __t) noexcept
252{
253 return __countl_zero(__t);
254}
255
Marshall Clow370375e2019-07-12 01:01:55 +0000256template<class _Tp>
257_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400258enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000259countl_one(_Tp __t) noexcept
260{
261 return __countl_one(__t);
262}
263
Marshall Clow370375e2019-07-12 01:01:55 +0000264template<class _Tp>
265_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400266enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000267countr_zero(_Tp __t) noexcept
268{
Mark de Wever896e0742020-11-24 14:50:49 +0100269 return __countr_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000270}
271
Marshall Clow370375e2019-07-12 01:01:55 +0000272template<class _Tp>
273_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400274enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000275countr_one(_Tp __t) noexcept
276{
277 return __countr_one(__t);
278}
279
Marshall Clow370375e2019-07-12 01:01:55 +0000280template<class _Tp>
281_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400282enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000283popcount(_Tp __t) noexcept
284{
285 return __popcount(__t);
286}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000287
Marshall Clowd852d5d2019-07-01 23:00:32 +0000288template <class _Tp>
289_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400290enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, bool>
Mark de Wever896e0742020-11-24 14:50:49 +0100291has_single_bit(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000292{
Mark de Wever896e0742020-11-24 14:50:49 +0100293 return __has_single_bit(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000294}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000295
296template <class _Tp>
297_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400298enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100299bit_floor(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000300{
301 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
302}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000303
304template <class _Tp>
305_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400306enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100307bit_ceil(_Tp __t) noexcept
Marshall Clowd852d5d2019-07-01 23:00:32 +0000308{
309 if (__t < 2) return 1;
310 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
Mark de Wever896e0742020-11-24 14:50:49 +0100311 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000312
313 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
314 return _Tp{1} << __n;
315 else
316 {
317 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
318 const unsigned __retVal = 1u << (__n + __extra);
319 return (_Tp) (__retVal >> __extra);
320 }
321}
322
323template <class _Tp>
324_LIBCPP_INLINE_VISIBILITY constexpr
Louis Dionne25547162021-08-17 12:26:09 -0400325enable_if_t<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100326bit_width(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000327{
328 return __t == 0 ? 0 : __bit_log2(__t) + 1;
329}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000330
Marshall Clowe8a18f32019-07-23 04:20:19 +0000331enum class endian
332{
333 little = 0xDEAD,
334 big = 0xFACE,
335#if defined(_LIBCPP_LITTLE_ENDIAN)
336 native = little
337#elif defined(_LIBCPP_BIG_ENDIAN)
338 native = big
339#else
340 native = 0xCAFE
341#endif
342};
343
Marshall Clowd852d5d2019-07-01 23:00:32 +0000344#endif // _LIBCPP_STD_VER > 17
345
Marshall Clowbf32ec92018-08-17 16:07:48 +0000346_LIBCPP_END_NAMESPACE_STD
347
Marshall Clowd852d5d2019-07-01 23:00:32 +0000348_LIBCPP_POP_MACROS
349
Marshall Clowbf32ec92018-08-17 16:07:48 +0000350#endif // _LIBCPP_BIT