xref: /freebsd/contrib/llvm-project/libcxx/include/bit (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric// -*- C++ -*-
2349cc55cSDimitry Andric//===----------------------------------------------------------------------===//
30b57cec5SDimitry Andric//
40b57cec5SDimitry Andric// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
50b57cec5SDimitry Andric// See https://llvm.org/LICENSE.txt for license information.
60b57cec5SDimitry Andric// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
70b57cec5SDimitry Andric//
80b57cec5SDimitry Andric//===---------------------------------------------------------------------===//
90b57cec5SDimitry Andric
100b57cec5SDimitry Andric#ifndef _LIBCPP_BIT
110b57cec5SDimitry Andric#define _LIBCPP_BIT
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric/*
140b57cec5SDimitry Andric    bit synopsis
150b57cec5SDimitry Andric
160b57cec5SDimitry Andricnamespace std {
17349cc55cSDimitry Andric  // [bit.cast], bit_cast
18349cc55cSDimitry Andric  template<class To, class From>
19349cc55cSDimitry Andric    constexpr To bit_cast(const From& from) noexcept; // C++20
200b57cec5SDimitry Andric
214824e7fdSDimitry Andric  // [bit.byteswap], byteswap
224824e7fdSDimitry Andric  template<class T>
234824e7fdSDimitry Andric    constexpr T byteswap(T value) noexcept;      // C++23
244824e7fdSDimitry Andric
255ffd83dbSDimitry Andric  // [bit.pow.two], integral powers of 2
260b57cec5SDimitry Andric  template <class T>
27e8d8bef9SDimitry Andric    constexpr bool has_single_bit(T x) noexcept; // C++20
280b57cec5SDimitry Andric  template <class T>
29e8d8bef9SDimitry Andric    constexpr T bit_ceil(T x);                   // C++20
300b57cec5SDimitry Andric  template <class T>
31e8d8bef9SDimitry Andric    constexpr T bit_floor(T x) noexcept;         // C++20
320b57cec5SDimitry Andric  template <class T>
33*81ad6265SDimitry Andric    constexpr int bit_width(T x) noexcept;       // C++20
340b57cec5SDimitry Andric
355ffd83dbSDimitry Andric  // [bit.rotate], rotating
360b57cec5SDimitry Andric  template<class T>
370b57cec5SDimitry Andric    constexpr T rotl(T x, unsigned int s) noexcept; // C++20
380b57cec5SDimitry Andric  template<class T>
390b57cec5SDimitry Andric    constexpr T rotr(T x, unsigned int s) noexcept; // C++20
400b57cec5SDimitry Andric
415ffd83dbSDimitry Andric  // [bit.count], counting
420b57cec5SDimitry Andric  template<class T>
430b57cec5SDimitry Andric    constexpr int countl_zero(T x) noexcept;  // C++20
440b57cec5SDimitry Andric  template<class T>
450b57cec5SDimitry Andric    constexpr int countl_one(T x) noexcept;   // C++20
460b57cec5SDimitry Andric  template<class T>
470b57cec5SDimitry Andric    constexpr int countr_zero(T x) noexcept;  // C++20
480b57cec5SDimitry Andric  template<class T>
490b57cec5SDimitry Andric    constexpr int countr_one(T x) noexcept;   // C++20
500b57cec5SDimitry Andric  template<class T>
510b57cec5SDimitry Andric    constexpr int popcount(T x) noexcept;     // C++20
520b57cec5SDimitry Andric
535ffd83dbSDimitry Andric  // [bit.endian], endian
54e40139ffSDimitry Andric  enum class endian {
55e40139ffSDimitry Andric    little = see below,        // C++20
56e40139ffSDimitry Andric    big = see below,           // C++20
57e40139ffSDimitry Andric    native = see below         // C++20
58e40139ffSDimitry Andric  };
59e40139ffSDimitry Andric
600b57cec5SDimitry Andric} // namespace std
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric*/
630b57cec5SDimitry Andric
64*81ad6265SDimitry Andric#include <__assert> // all public C++ headers provide the assertion handler
65349cc55cSDimitry Andric#include <__bit/bit_cast.h>
664824e7fdSDimitry Andric#include <__bit/byteswap.h>
67fe6060f1SDimitry Andric#include <__bits> // __libcpp_clz
68*81ad6265SDimitry Andric#include <__concepts/arithmetic.h>
69349cc55cSDimitry Andric#include <__config>
700b57cec5SDimitry Andric#include <limits>
710b57cec5SDimitry Andric#include <type_traits>
720b57cec5SDimitry Andric#include <version>
730b57cec5SDimitry Andric
74*81ad6265SDimitry Andric#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
75*81ad6265SDimitry Andric#  include <iosfwd>
76*81ad6265SDimitry Andric#endif
77*81ad6265SDimitry Andric
780b57cec5SDimitry Andric#if defined(__IBMCPP__)
79d409305fSDimitry Andric#  include "__support/ibm/support.h"
800b57cec5SDimitry Andric#endif
810b57cec5SDimitry Andric#if defined(_LIBCPP_COMPILER_MSVC)
820b57cec5SDimitry Andric#  include <intrin.h>
830b57cec5SDimitry Andric#endif
840b57cec5SDimitry Andric
850b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
860b57cec5SDimitry Andric#  pragma GCC system_header
870b57cec5SDimitry Andric#endif
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
900b57cec5SDimitry Andric#include <__undef_macros>
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
930b57cec5SDimitry Andric
940b57cec5SDimitry Andrictemplate<class _Tp>
95*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
960b57cec5SDimitry Andric_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
970b57cec5SDimitry Andric{
98fe6060f1SDimitry Andric    static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__rotr requires an unsigned integer type");
990b57cec5SDimitry Andric    const unsigned int __dig = numeric_limits<_Tp>::digits;
1000b57cec5SDimitry Andric    if ((__cnt % __dig) == 0)
1010b57cec5SDimitry Andric        return __t;
1020b57cec5SDimitry Andric    return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
1030b57cec5SDimitry Andric}
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andrictemplate<class _Tp>
106*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX11
1070b57cec5SDimitry Andricint __countl_zero(_Tp __t) _NOEXCEPT
1080b57cec5SDimitry Andric{
109fe6060f1SDimitry Andric    static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
1100b57cec5SDimitry Andric    if (__t == 0)
1110b57cec5SDimitry Andric        return numeric_limits<_Tp>::digits;
1120b57cec5SDimitry Andric
1130b57cec5SDimitry Andric    if      (sizeof(_Tp) <= sizeof(unsigned int))
114*81ad6265SDimitry Andric        return std::__libcpp_clz(static_cast<unsigned int>(__t))
1150b57cec5SDimitry Andric              - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
1160b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long))
117*81ad6265SDimitry Andric        return std::__libcpp_clz(static_cast<unsigned long>(__t))
1180b57cec5SDimitry Andric              - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
1190b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long long))
120*81ad6265SDimitry Andric        return std::__libcpp_clz(static_cast<unsigned long long>(__t))
1210b57cec5SDimitry Andric              - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
1220b57cec5SDimitry Andric    else
1230b57cec5SDimitry Andric    {
1240b57cec5SDimitry Andric        int __ret = 0;
1250b57cec5SDimitry Andric        int __iter = 0;
1260b57cec5SDimitry Andric        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
1270b57cec5SDimitry Andric        while (true) {
128*81ad6265SDimitry Andric            __t = std::__rotr(__t, __ulldigits);
129*81ad6265SDimitry Andric            if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
1300b57cec5SDimitry Andric                break;
1310b57cec5SDimitry Andric            __ret += __iter;
1320b57cec5SDimitry Andric            }
1330b57cec5SDimitry Andric        return __ret + __iter;
1340b57cec5SDimitry Andric    }
1350b57cec5SDimitry Andric}
1360b57cec5SDimitry Andric
137*81ad6265SDimitry Andric#if _LIBCPP_STD_VER > 17
138*81ad6265SDimitry Andric
139*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
140*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotl(_Tp __t, unsigned int __cnt) noexcept {
141*81ad6265SDimitry Andric  const unsigned int __dig = numeric_limits<_Tp>::digits;
142*81ad6265SDimitry Andric  if ((__cnt % __dig) == 0)
143*81ad6265SDimitry Andric    return __t;
144*81ad6265SDimitry Andric  return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
1450b57cec5SDimitry Andric}
1460b57cec5SDimitry Andric
147*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
148*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Tp rotr(_Tp __t, unsigned int __cnt) noexcept {
149*81ad6265SDimitry Andric  return std::__rotr(__t, __cnt);
1500b57cec5SDimitry Andric}
1510b57cec5SDimitry Andric
152*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
153*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {
154*81ad6265SDimitry Andric  return std::__countl_zero(__t);
155*81ad6265SDimitry Andric}
156*81ad6265SDimitry Andric
157*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
158*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {
159*81ad6265SDimitry Andric  return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
160*81ad6265SDimitry Andric}
161*81ad6265SDimitry Andric
162*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
163*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int countr_zero(_Tp __t) noexcept {
164*81ad6265SDimitry Andric  if (__t == 0)
165*81ad6265SDimitry Andric    return numeric_limits<_Tp>::digits;
166*81ad6265SDimitry Andric
1670b57cec5SDimitry Andric  if (sizeof(_Tp) <= sizeof(unsigned int))
168*81ad6265SDimitry Andric    return std::__libcpp_ctz(static_cast<unsigned int>(__t));
1690b57cec5SDimitry Andric  else if (sizeof(_Tp) <= sizeof(unsigned long))
170*81ad6265SDimitry Andric    return std::__libcpp_ctz(static_cast<unsigned long>(__t));
1710b57cec5SDimitry Andric  else if (sizeof(_Tp) <= sizeof(unsigned long long))
172*81ad6265SDimitry Andric    return std::__libcpp_ctz(static_cast<unsigned long long>(__t));
173*81ad6265SDimitry Andric  else {
1740b57cec5SDimitry Andric    int __ret = 0;
175*81ad6265SDimitry Andric    const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
176*81ad6265SDimitry Andric    while (static_cast<unsigned long long>(__t) == 0uLL) {
177*81ad6265SDimitry Andric      __ret += __ulldigits;
178*81ad6265SDimitry Andric      __t >>= __ulldigits;
179*81ad6265SDimitry Andric    }
180*81ad6265SDimitry Andric    return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
181*81ad6265SDimitry Andric  }
182*81ad6265SDimitry Andric}
183*81ad6265SDimitry Andric
184*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
185*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int countr_one(_Tp __t) noexcept {
186*81ad6265SDimitry Andric  return __t != numeric_limits<_Tp>::max() ? std::countr_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;
187*81ad6265SDimitry Andric}
188*81ad6265SDimitry Andric
189*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
190*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
191*81ad6265SDimitry Andric  if (sizeof(_Tp) <= sizeof(unsigned int))
192*81ad6265SDimitry Andric    return std::__libcpp_popcount(static_cast<unsigned int>(__t));
193*81ad6265SDimitry Andric  else if (sizeof(_Tp) <= sizeof(unsigned long))
194*81ad6265SDimitry Andric    return std::__libcpp_popcount(static_cast<unsigned long>(__t));
195*81ad6265SDimitry Andric  else if (sizeof(_Tp) <= sizeof(unsigned long long))
196*81ad6265SDimitry Andric    return std::__libcpp_popcount(static_cast<unsigned long long>(__t));
197*81ad6265SDimitry Andric  else {
198*81ad6265SDimitry Andric    int __ret = 0;
199*81ad6265SDimitry Andric    while (__t != 0) {
200*81ad6265SDimitry Andric      __ret += std::__libcpp_popcount(static_cast<unsigned long long>(__t));
2010b57cec5SDimitry Andric      __t >>= numeric_limits<unsigned long long>::digits;
2020b57cec5SDimitry Andric    }
2030b57cec5SDimitry Andric    return __ret;
2040b57cec5SDimitry Andric  }
2050b57cec5SDimitry Andric}
2060b57cec5SDimitry Andric
207*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
208*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr bool has_single_bit(_Tp __t) noexcept {
2090b57cec5SDimitry Andric  return __t != 0 && (((__t & (__t - 1)) == 0));
2100b57cec5SDimitry Andric}
2110b57cec5SDimitry Andric
212*81ad6265SDimitry Andric// integral log base 2
213*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
214*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Tp __bit_log2(_Tp __t) noexcept {
215*81ad6265SDimitry Andric  return numeric_limits<_Tp>::digits - 1 - std::countl_zero(__t);
2160b57cec5SDimitry Andric}
2170b57cec5SDimitry Andric
218*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
219*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_floor(_Tp __t) noexcept {
220*81ad6265SDimitry Andric  return __t == 0 ? 0 : _Tp{1} << std::__bit_log2(__t);
2210b57cec5SDimitry Andric}
2220b57cec5SDimitry Andric
223*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
224*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept {
225*81ad6265SDimitry Andric  if (__t < 2)
226*81ad6265SDimitry Andric    return 1;
227*81ad6265SDimitry Andric  const unsigned __n = numeric_limits<_Tp>::digits - std::countl_zero((_Tp)(__t - 1u));
2280eae32dcSDimitry Andric  _LIBCPP_ASSERT(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric  if constexpr (sizeof(_Tp) >= sizeof(unsigned))
2310b57cec5SDimitry Andric    return _Tp{1} << __n;
232*81ad6265SDimitry Andric  else {
2330b57cec5SDimitry Andric    const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
2340b57cec5SDimitry Andric    const unsigned __retVal = 1u << (__n + __extra);
2350b57cec5SDimitry Andric    return (_Tp)(__retVal >> __extra);
2360b57cec5SDimitry Andric  }
2370b57cec5SDimitry Andric}
2380b57cec5SDimitry Andric
239*81ad6265SDimitry Andrictemplate <__libcpp_unsigned_integer _Tp>
240*81ad6265SDimitry Andric_LIBCPP_HIDE_FROM_ABI constexpr int bit_width(_Tp __t) noexcept {
241*81ad6265SDimitry Andric  return __t == 0 ? 0 : std::__bit_log2(__t) + 1;
2420b57cec5SDimitry Andric}
2430b57cec5SDimitry Andric
244*81ad6265SDimitry Andricenum class endian {
245e40139ffSDimitry Andric  little = 0xDEAD,
246e40139ffSDimitry Andric  big = 0xFACE,
247e40139ffSDimitry Andric#  if defined(_LIBCPP_LITTLE_ENDIAN)
248e40139ffSDimitry Andric  native = little
249e40139ffSDimitry Andric#  elif defined(_LIBCPP_BIG_ENDIAN)
250e40139ffSDimitry Andric  native = big
251e40139ffSDimitry Andric#  else
252e40139ffSDimitry Andric  native = 0xCAFE
253e40139ffSDimitry Andric#  endif
254e40139ffSDimitry Andric};
255e40139ffSDimitry Andric
2560b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 17
2570b57cec5SDimitry Andric
2580b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andric_LIBCPP_POP_MACROS
2610b57cec5SDimitry Andric
2620b57cec5SDimitry Andric#endif // _LIBCPP_BIT
263