blob: 7708106c8a71270441f1f5dbbab545f5637b8c89 [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>
Arthur O'Dwyer597cac42021-05-12 23:04:03 -040059#include <__debug>
Marshall Clowd852d5d2019-07-01 23:00:32 +000060#include <limits>
61#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000062#include <version>
Marshall Clowbf32ec92018-08-17 16:07:48 +000063
64#if defined(__IBMCPP__)
Louis Dionne90a04492021-02-02 16:58:38 -050065#include "__support/ibm/support.h"
Marshall Clowbf32ec92018-08-17 16:07:48 +000066#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 Clowd852d5d2019-07-01 23:00:32 +000080template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +000081_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
82_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +000083{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -040084 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotl requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +000085 const unsigned int __dig = numeric_limits<_Tp>::digits;
86 if ((__cnt % __dig) == 0)
87 return __t;
88 return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
89}
90
Marshall Clowd852d5d2019-07-01 23:00:32 +000091template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +000092_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
93_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +000094{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -040095 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +000096 const unsigned int __dig = numeric_limits<_Tp>::digits;
97 if ((__cnt % __dig) == 0)
98 return __t;
99 return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
100}
101
Marshall Clowd852d5d2019-07-01 23:00:32 +0000102template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000103_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
104int __countr_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000105{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400106 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_zero requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000107 if (__t == 0)
108 return numeric_limits<_Tp>::digits;
109
Marshall Clow370375e2019-07-12 01:01:55 +0000110 if (sizeof(_Tp) <= sizeof(unsigned int))
111 return __libcpp_ctz(static_cast<unsigned int>(__t));
112 else if (sizeof(_Tp) <= sizeof(unsigned long))
113 return __libcpp_ctz(static_cast<unsigned long>(__t));
114 else if (sizeof(_Tp) <= sizeof(unsigned long long))
115 return __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000116 else
117 {
118 int __ret = 0;
Marshall Clowd852d5d2019-07-01 23:00:32 +0000119 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400120 while (static_cast<unsigned long long>(__t) == 0uLL)
Marshall Clowd852d5d2019-07-01 23:00:32 +0000121 {
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400122 __ret += __ulldigits;
Marshall Clowd852d5d2019-07-01 23:00:32 +0000123 __t >>= __ulldigits;
124 }
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400125 return __ret + __libcpp_ctz(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000126 }
127}
128
Marshall Clowd852d5d2019-07-01 23:00:32 +0000129template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000130_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
131int __countl_zero(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000132{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400133 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000134 if (__t == 0)
135 return numeric_limits<_Tp>::digits;
136
137 if (sizeof(_Tp) <= sizeof(unsigned int))
138 return __libcpp_clz(static_cast<unsigned int>(__t))
139 - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
140 else if (sizeof(_Tp) <= sizeof(unsigned long))
141 return __libcpp_clz(static_cast<unsigned long>(__t))
142 - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
143 else if (sizeof(_Tp) <= sizeof(unsigned long long))
144 return __libcpp_clz(static_cast<unsigned long long>(__t))
145 - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
146 else
147 {
148 int __ret = 0;
149 int __iter = 0;
150 const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
151 while (true) {
152 __t = __rotr(__t, __ulldigits);
153 if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
154 break;
155 __ret += __iter;
156 }
157 return __ret + __iter;
158 }
159}
160
161template<class _Tp>
162_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
163int __countl_one(_Tp __t) _NOEXCEPT
164{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400165 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_one requires an unsigned integer type");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000166 return __t != numeric_limits<_Tp>::max()
Marshall Clow370375e2019-07-12 01:01:55 +0000167 ? __countl_zero(static_cast<_Tp>(~__t))
Marshall Clowd852d5d2019-07-01 23:00:32 +0000168 : numeric_limits<_Tp>::digits;
169}
170
Marshall Clowd852d5d2019-07-01 23:00:32 +0000171template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000172_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
173int __countr_one(_Tp __t) _NOEXCEPT
Marshall Clowd852d5d2019-07-01 23:00:32 +0000174{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400175 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countr_one requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000176 return __t != numeric_limits<_Tp>::max()
177 ? __countr_zero(static_cast<_Tp>(~__t))
178 : numeric_limits<_Tp>::digits;
179}
180
Marshall Clow370375e2019-07-12 01:01:55 +0000181template<class _Tp>
182_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400183int __popcount(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000184{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400185 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__popcount requires an unsigned integer type");
Marshall Clow370375e2019-07-12 01:01:55 +0000186 if (sizeof(_Tp) <= sizeof(unsigned int))
187 return __libcpp_popcount(static_cast<unsigned int>(__t));
188 else if (sizeof(_Tp) <= sizeof(unsigned long))
189 return __libcpp_popcount(static_cast<unsigned long>(__t));
190 else if (sizeof(_Tp) <= sizeof(unsigned long long))
191 return __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000192 else
193 {
194 int __ret = 0;
195 while (__t != 0)
196 {
Marshall Clow370375e2019-07-12 01:01:55 +0000197 __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
Marshall Clowd852d5d2019-07-01 23:00:32 +0000198 __t >>= numeric_limits<unsigned long long>::digits;
199 }
200 return __ret;
201 }
202}
203
Marshall Clowd852d5d2019-07-01 23:00:32 +0000204// integral log base 2
205template<class _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000206_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
207unsigned __bit_log2(_Tp __t) _NOEXCEPT
208{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400209 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__bit_log2 requires an unsigned integer type");
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500210 return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000211}
212
213template <class _Tp>
214_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Mark de Wever896e0742020-11-24 14:50:49 +0100215bool __has_single_bit(_Tp __t) _NOEXCEPT
Marshall Clow370375e2019-07-12 01:01:55 +0000216{
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400217 static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__has_single_bit requires an unsigned integer type");
Louis Dionnea1e3b7d2020-05-28 14:28:09 -0400218 return __t != 0 && (((__t & (__t - 1)) == 0));
Marshall Clow370375e2019-07-12 01:01:55 +0000219}
220
Marshall Clow370375e2019-07-12 01:01:55 +0000221#if _LIBCPP_STD_VER > 17
222
223template<class _Tp>
Marshall Clowd852d5d2019-07-01 23:00:32 +0000224_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400225_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000226rotl(_Tp __t, unsigned int __cnt) noexcept
227{
228 return __rotl(__t, __cnt);
229}
230
Marshall Clow370375e2019-07-12 01:01:55 +0000231template<class _Tp>
232_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400233_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Marshall Clow370375e2019-07-12 01:01:55 +0000234rotr(_Tp __t, unsigned int __cnt) noexcept
235{
236 return __rotr(__t, __cnt);
237}
238
Marshall Clow370375e2019-07-12 01:01:55 +0000239template<class _Tp>
240_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400241_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000242countl_zero(_Tp __t) noexcept
243{
244 return __countl_zero(__t);
245}
246
Marshall Clow370375e2019-07-12 01:01:55 +0000247template<class _Tp>
248_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400249_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000250countl_one(_Tp __t) noexcept
251{
252 return __countl_one(__t);
253}
254
Marshall Clow370375e2019-07-12 01:01:55 +0000255template<class _Tp>
256_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400257_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000258countr_zero(_Tp __t) noexcept
259{
Mark de Wever896e0742020-11-24 14:50:49 +0100260 return __countr_zero(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000261}
262
Marshall Clow370375e2019-07-12 01:01:55 +0000263template<class _Tp>
264_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400265_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000266countr_one(_Tp __t) noexcept
267{
268 return __countr_one(__t);
269}
270
Marshall Clow370375e2019-07-12 01:01:55 +0000271template<class _Tp>
272_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400273_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, int>
Marshall Clow370375e2019-07-12 01:01:55 +0000274popcount(_Tp __t) noexcept
275{
276 return __popcount(__t);
277}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000278
Marshall Clowd852d5d2019-07-01 23:00:32 +0000279template <class _Tp>
280_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400281_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, bool>
Mark de Wever896e0742020-11-24 14:50:49 +0100282has_single_bit(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000283{
Mark de Wever896e0742020-11-24 14:50:49 +0100284 return __has_single_bit(__t);
Marshall Clow370375e2019-07-12 01:01:55 +0000285}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000286
287template <class _Tp>
288_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400289_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100290bit_floor(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000291{
292 return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
293}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000294
295template <class _Tp>
296_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400297_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100298bit_ceil(_Tp __t) noexcept
Marshall Clowd852d5d2019-07-01 23:00:32 +0000299{
300 if (__t < 2) return 1;
301 const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
Mark de Wever896e0742020-11-24 14:50:49 +0100302 _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
Marshall Clowd852d5d2019-07-01 23:00:32 +0000303
304 if constexpr (sizeof(_Tp) >= sizeof(unsigned))
305 return _Tp{1} << __n;
306 else
307 {
308 const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
309 const unsigned __retVal = 1u << (__n + __extra);
310 return (_Tp) (__retVal >> __extra);
311 }
312}
313
314template <class _Tp>
315_LIBCPP_INLINE_VISIBILITY constexpr
Arthur O'Dwyer515615e2021-05-12 13:09:26 -0400316_EnableIf<__libcpp_is_unsigned_integer<_Tp>::value, _Tp>
Mark de Wever896e0742020-11-24 14:50:49 +0100317bit_width(_Tp __t) noexcept
Marshall Clow370375e2019-07-12 01:01:55 +0000318{
319 return __t == 0 ? 0 : __bit_log2(__t) + 1;
320}
Marshall Clowd852d5d2019-07-01 23:00:32 +0000321
Marshall Clowe8a18f32019-07-23 04:20:19 +0000322enum class endian
323{
324 little = 0xDEAD,
325 big = 0xFACE,
326#if defined(_LIBCPP_LITTLE_ENDIAN)
327 native = little
328#elif defined(_LIBCPP_BIG_ENDIAN)
329 native = big
330#else
331 native = 0xCAFE
332#endif
333};
334
Marshall Clowd852d5d2019-07-01 23:00:32 +0000335#endif // _LIBCPP_STD_VER > 17
336
Marshall Clowbf32ec92018-08-17 16:07:48 +0000337_LIBCPP_END_NAMESPACE_STD
338
Marshall Clowd852d5d2019-07-01 23:00:32 +0000339_LIBCPP_POP_MACROS
340
Marshall Clowbf32ec92018-08-17 16:07:48 +0000341#endif // _LIBCPP_BIT