xref: /freebsd/contrib/llvm-project/libcxx/include/bit (revision e40139ff33b48b56a24c808b166b04b8ee6f5b21)
10b57cec5SDimitry Andric// -*- C++ -*-
20b57cec5SDimitry Andric//===------------------------------ bit ----------------------------------===//
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 {
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric  template <class T>
190b57cec5SDimitry Andric    constexpr bool ispow2(T x) noexcept; // C++20
200b57cec5SDimitry Andric  template <class T>
210b57cec5SDimitry Andric    constexpr T ceil2(T x);              // C++20
220b57cec5SDimitry Andric  template <class T>
230b57cec5SDimitry Andric    constexpr T floor2(T x) noexcept;    // C++20
240b57cec5SDimitry Andric  template <class T>
250b57cec5SDimitry Andric    constexpr T log2p1(T x) noexcept;    // C++20
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric  // 23.20.2, rotating
280b57cec5SDimitry Andric  template<class T>
290b57cec5SDimitry Andric    constexpr T rotl(T x, unsigned int s) noexcept; // C++20
300b57cec5SDimitry Andric  template<class T>
310b57cec5SDimitry Andric    constexpr T rotr(T x, unsigned int s) noexcept; // C++20
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric  // 23.20.3, counting
340b57cec5SDimitry Andric  template<class T>
350b57cec5SDimitry Andric    constexpr int countl_zero(T x) noexcept;  // C++20
360b57cec5SDimitry Andric  template<class T>
370b57cec5SDimitry Andric    constexpr int countl_one(T x) noexcept;   // C++20
380b57cec5SDimitry Andric  template<class T>
390b57cec5SDimitry Andric    constexpr int countr_zero(T x) noexcept;  // C++20
400b57cec5SDimitry Andric  template<class T>
410b57cec5SDimitry Andric    constexpr int countr_one(T x) noexcept;   // C++20
420b57cec5SDimitry Andric  template<class T>
430b57cec5SDimitry Andric    constexpr int popcount(T x) noexcept;     // C++20
440b57cec5SDimitry Andric
45*e40139ffSDimitry Andric  // 20.15.9, endian
46*e40139ffSDimitry Andric  enum class endian {
47*e40139ffSDimitry Andric    little = see below,        // C++20
48*e40139ffSDimitry Andric    big = see below,           // C++20
49*e40139ffSDimitry Andric    native = see below         // C++20
50*e40139ffSDimitry Andric};
51*e40139ffSDimitry Andric
520b57cec5SDimitry Andric} // namespace std
530b57cec5SDimitry Andric
540b57cec5SDimitry Andric*/
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric#include <__config>
570b57cec5SDimitry Andric#include <limits>
580b57cec5SDimitry Andric#include <type_traits>
590b57cec5SDimitry Andric#include <version>
600b57cec5SDimitry Andric#include <__debug>
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric#if defined(__IBMCPP__)
630b57cec5SDimitry Andric#include "support/ibm/support.h"
640b57cec5SDimitry Andric#endif
650b57cec5SDimitry Andric#if defined(_LIBCPP_COMPILER_MSVC)
660b57cec5SDimitry Andric#include <intrin.h>
670b57cec5SDimitry Andric#endif
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
700b57cec5SDimitry Andric#pragma GCC system_header
710b57cec5SDimitry Andric#endif
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
740b57cec5SDimitry Andric#include <__undef_macros>
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric#ifndef _LIBCPP_COMPILER_MSVC
790b57cec5SDimitry Andric
800b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
810b57cec5SDimitry Andricint __libcpp_ctz(unsigned __x)           _NOEXCEPT { return __builtin_ctz(__x); }
820b57cec5SDimitry Andric
830b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
840b57cec5SDimitry Andricint __libcpp_ctz(unsigned long __x)      _NOEXCEPT { return __builtin_ctzl(__x); }
850b57cec5SDimitry Andric
860b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
870b57cec5SDimitry Andricint __libcpp_ctz(unsigned long long __x) _NOEXCEPT { return __builtin_ctzll(__x); }
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric
900b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
910b57cec5SDimitry Andricint __libcpp_clz(unsigned __x)           _NOEXCEPT { return __builtin_clz(__x); }
920b57cec5SDimitry Andric
930b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
940b57cec5SDimitry Andricint __libcpp_clz(unsigned long __x)      _NOEXCEPT { return __builtin_clzl(__x); }
950b57cec5SDimitry Andric
960b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
970b57cec5SDimitry Andricint __libcpp_clz(unsigned long long __x) _NOEXCEPT { return __builtin_clzll(__x); }
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1010b57cec5SDimitry Andricint __libcpp_popcount(unsigned __x)           _NOEXCEPT { return __builtin_popcount(__x); }
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1040b57cec5SDimitry Andricint __libcpp_popcount(unsigned long __x)      _NOEXCEPT { return __builtin_popcountl(__x); }
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
1070b57cec5SDimitry Andricint __libcpp_popcount(unsigned long long __x) _NOEXCEPT { return __builtin_popcountll(__x); }
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric#else  // _LIBCPP_COMPILER_MSVC
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric// Precondition:  __x != 0
1120b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1130b57cec5SDimitry Andricint __libcpp_ctz(unsigned __x) {
1140b57cec5SDimitry Andric  static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
1150b57cec5SDimitry Andric  static_assert(sizeof(unsigned long) == 4, "");
1160b57cec5SDimitry Andric  unsigned long __where;
1170b57cec5SDimitry Andric  if (_BitScanForward(&__where, __x))
1180b57cec5SDimitry Andric    return static_cast<int>(__where);
1190b57cec5SDimitry Andric  return 32;
1200b57cec5SDimitry Andric}
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1230b57cec5SDimitry Andricint __libcpp_ctz(unsigned long __x) {
1240b57cec5SDimitry Andric    static_assert(sizeof(unsigned long) == sizeof(unsigned), "");
1250b57cec5SDimitry Andric    return __ctz(static_cast<unsigned>(__x));
1260b57cec5SDimitry Andric}
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1290b57cec5SDimitry Andricint __libcpp_ctz(unsigned long long __x) {
1300b57cec5SDimitry Andric    unsigned long __where;
1310b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_BITSCAN64)
1320b57cec5SDimitry Andric    (defined(_M_AMD64) || defined(__x86_64__))
1330b57cec5SDimitry Andric  if (_BitScanForward64(&__where, __x))
1340b57cec5SDimitry Andric    return static_cast<int>(__where);
1350b57cec5SDimitry Andric#else
1360b57cec5SDimitry Andric  // Win32 doesn't have _BitScanForward64 so emulate it with two 32 bit calls.
1370b57cec5SDimitry Andric  if (_BitScanForward(&__where, static_cast<unsigned long>(__x)))
1380b57cec5SDimitry Andric    return static_cast<int>(__where);
1390b57cec5SDimitry Andric  if (_BitScanForward(&__where, static_cast<unsigned long>(__x >> 32)))
1400b57cec5SDimitry Andric    return static_cast<int>(__where + 32);
1410b57cec5SDimitry Andric#endif
1420b57cec5SDimitry Andric  return 64;
1430b57cec5SDimitry Andric}
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric// Precondition:  __x != 0
1460b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1470b57cec5SDimitry Andricint __libcpp_clz(unsigned __x) {
1480b57cec5SDimitry Andric  static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
1490b57cec5SDimitry Andric  static_assert(sizeof(unsigned long) == 4, "");
1500b57cec5SDimitry Andric  unsigned long __where;
1510b57cec5SDimitry Andric  if (_BitScanReverse(&__where, __x))
1520b57cec5SDimitry Andric    return static_cast<int>(31 - __where);
1530b57cec5SDimitry Andric  return 32; // Undefined Behavior.
1540b57cec5SDimitry Andric}
1550b57cec5SDimitry Andric
1560b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1570b57cec5SDimitry Andricint __libcpp_clz(unsigned long __x) {
1580b57cec5SDimitry Andric    static_assert(sizeof(unsigned) == sizeof(unsigned long), "");
1590b57cec5SDimitry Andric    return __libcpp_clz(static_cast<unsigned>(__x));
1600b57cec5SDimitry Andric}
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY
1630b57cec5SDimitry Andricint __libcpp_clz(unsigned long long __x) {
1640b57cec5SDimitry Andric  unsigned long __where;
1650b57cec5SDimitry Andric#if defined(_LIBCPP_HAS_BITSCAN64)
1660b57cec5SDimitry Andric  if (_BitScanReverse64(&__where, __x))
1670b57cec5SDimitry Andric    return static_cast<int>(63 - __where);
1680b57cec5SDimitry Andric#else
1690b57cec5SDimitry Andric  // Win32 doesn't have _BitScanReverse64 so emulate it with two 32 bit calls.
1700b57cec5SDimitry Andric  if (_BitScanReverse(&__where, static_cast<unsigned long>(__x >> 32)))
1710b57cec5SDimitry Andric    return static_cast<int>(63 - (__where + 32));
1720b57cec5SDimitry Andric  if (_BitScanReverse(&__where, static_cast<unsigned long>(__x)))
1730b57cec5SDimitry Andric    return static_cast<int>(63 - __where);
1740b57cec5SDimitry Andric#endif
1750b57cec5SDimitry Andric  return 64; // Undefined Behavior.
1760b57cec5SDimitry Andric}
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned __x) {
1790b57cec5SDimitry Andric  static_assert(sizeof(unsigned) == 4, "");
1800b57cec5SDimitry Andric  return __popcnt(__x);
1810b57cec5SDimitry Andric}
1820b57cec5SDimitry Andric
1830b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long __x) {
1840b57cec5SDimitry Andric  static_assert(sizeof(unsigned long) == 4, "");
1850b57cec5SDimitry Andric  return __popcnt(__x);
1860b57cec5SDimitry Andric}
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andricinline _LIBCPP_INLINE_VISIBILITY int __libcpp_popcount(unsigned long long __x) {
1890b57cec5SDimitry Andric  static_assert(sizeof(unsigned long long) == 8, "");
1900b57cec5SDimitry Andric  return __popcnt64(__x);
1910b57cec5SDimitry Andric}
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric#endif // _LIBCPP_COMPILER_MSVC
1940b57cec5SDimitry Andric
1950b57cec5SDimitry Andrictemplate <class _Tp>
1960b57cec5SDimitry Andricusing __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
1970b57cec5SDimitry Andric         is_integral<_Tp>::value &&
1980b57cec5SDimitry Andric         is_unsigned<_Tp>::value &&
1990b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, bool>::value &&
2000b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value &&
2010b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value &&
2020b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value &&
2030b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value
2040b57cec5SDimitry Andric    >;
2050b57cec5SDimitry Andric
2060b57cec5SDimitry Andric
2070b57cec5SDimitry Andrictemplate<class _Tp>
2080b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2090b57cec5SDimitry Andric_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
2100b57cec5SDimitry Andric{
2110b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
2120b57cec5SDimitry Andric    const unsigned int __dig = numeric_limits<_Tp>::digits;
2130b57cec5SDimitry Andric    if ((__cnt % __dig) == 0)
2140b57cec5SDimitry Andric        return __t;
2150b57cec5SDimitry Andric    return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
2160b57cec5SDimitry Andric}
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric
2190b57cec5SDimitry Andrictemplate<class _Tp>
2200b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2210b57cec5SDimitry Andric_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
2220b57cec5SDimitry Andric{
2230b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
2240b57cec5SDimitry Andric    const unsigned int __dig = numeric_limits<_Tp>::digits;
2250b57cec5SDimitry Andric    if ((__cnt % __dig) == 0)
2260b57cec5SDimitry Andric        return __t;
2270b57cec5SDimitry Andric    return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
2280b57cec5SDimitry Andric}
2290b57cec5SDimitry Andric
2300b57cec5SDimitry Andric
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andrictemplate<class _Tp>
2330b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2340b57cec5SDimitry Andricint __countr_zero(_Tp __t) _NOEXCEPT
2350b57cec5SDimitry Andric{
2360b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
2370b57cec5SDimitry Andric    if (__t == 0)
2380b57cec5SDimitry Andric        return numeric_limits<_Tp>::digits;
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric    if      (sizeof(_Tp) <= sizeof(unsigned int))
2410b57cec5SDimitry Andric        return __libcpp_ctz(static_cast<unsigned int>(__t));
2420b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long))
2430b57cec5SDimitry Andric        return __libcpp_ctz(static_cast<unsigned long>(__t));
2440b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long long))
2450b57cec5SDimitry Andric        return __libcpp_ctz(static_cast<unsigned long long>(__t));
2460b57cec5SDimitry Andric    else
2470b57cec5SDimitry Andric    {
2480b57cec5SDimitry Andric        int __ret = 0;
2490b57cec5SDimitry Andric        int __iter = 0;
2500b57cec5SDimitry Andric        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
2510b57cec5SDimitry Andric        while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
2520b57cec5SDimitry Andric        {
2530b57cec5SDimitry Andric            __ret += __iter;
2540b57cec5SDimitry Andric            __t >>= __ulldigits;
2550b57cec5SDimitry Andric        }
2560b57cec5SDimitry Andric        return __ret + __iter;
2570b57cec5SDimitry Andric    }
2580b57cec5SDimitry Andric}
2590b57cec5SDimitry Andric
2600b57cec5SDimitry Andrictemplate<class _Tp>
2610b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2620b57cec5SDimitry Andricint __countl_zero(_Tp __t) _NOEXCEPT
2630b57cec5SDimitry Andric{
2640b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned");
2650b57cec5SDimitry Andric    if (__t == 0)
2660b57cec5SDimitry Andric        return numeric_limits<_Tp>::digits;
2670b57cec5SDimitry Andric
2680b57cec5SDimitry Andric    if      (sizeof(_Tp) <= sizeof(unsigned int))
2690b57cec5SDimitry Andric        return __libcpp_clz(static_cast<unsigned int>(__t))
2700b57cec5SDimitry Andric              - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
2710b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long))
2720b57cec5SDimitry Andric        return __libcpp_clz(static_cast<unsigned long>(__t))
2730b57cec5SDimitry Andric              - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
2740b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long long))
2750b57cec5SDimitry Andric        return __libcpp_clz(static_cast<unsigned long long>(__t))
2760b57cec5SDimitry Andric              - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
2770b57cec5SDimitry Andric    else
2780b57cec5SDimitry Andric    {
2790b57cec5SDimitry Andric        int __ret = 0;
2800b57cec5SDimitry Andric        int __iter = 0;
2810b57cec5SDimitry Andric        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
2820b57cec5SDimitry Andric        while (true) {
2830b57cec5SDimitry Andric            __t = __rotr(__t, __ulldigits);
2840b57cec5SDimitry Andric            if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
2850b57cec5SDimitry Andric                break;
2860b57cec5SDimitry Andric            __ret += __iter;
2870b57cec5SDimitry Andric            }
2880b57cec5SDimitry Andric        return __ret + __iter;
2890b57cec5SDimitry Andric    }
2900b57cec5SDimitry Andric}
2910b57cec5SDimitry Andric
2920b57cec5SDimitry Andrictemplate<class _Tp>
2930b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2940b57cec5SDimitry Andricint __countl_one(_Tp __t) _NOEXCEPT
2950b57cec5SDimitry Andric{
2960b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
2970b57cec5SDimitry Andric    return __t != numeric_limits<_Tp>::max()
2980b57cec5SDimitry Andric        ? __countl_zero(static_cast<_Tp>(~__t))
2990b57cec5SDimitry Andric        : numeric_limits<_Tp>::digits;
3000b57cec5SDimitry Andric}
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric
3030b57cec5SDimitry Andrictemplate<class _Tp>
3040b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
3050b57cec5SDimitry Andricint __countr_one(_Tp __t) _NOEXCEPT
3060b57cec5SDimitry Andric{
3070b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned");
3080b57cec5SDimitry Andric    return __t != numeric_limits<_Tp>::max()
3090b57cec5SDimitry Andric        ? __countr_zero(static_cast<_Tp>(~__t))
3100b57cec5SDimitry Andric        : numeric_limits<_Tp>::digits;
3110b57cec5SDimitry Andric}
3120b57cec5SDimitry Andric
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andrictemplate<class _Tp>
3150b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
3160b57cec5SDimitry Andricint
3170b57cec5SDimitry Andric__popcount(_Tp __t) _NOEXCEPT
3180b57cec5SDimitry Andric{
3190b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned");
3200b57cec5SDimitry Andric    if      (sizeof(_Tp) <= sizeof(unsigned int))
3210b57cec5SDimitry Andric        return __libcpp_popcount(static_cast<unsigned int>(__t));
3220b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long))
3230b57cec5SDimitry Andric        return __libcpp_popcount(static_cast<unsigned long>(__t));
3240b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long long))
3250b57cec5SDimitry Andric        return __libcpp_popcount(static_cast<unsigned long long>(__t));
3260b57cec5SDimitry Andric    else
3270b57cec5SDimitry Andric    {
3280b57cec5SDimitry Andric        int __ret = 0;
3290b57cec5SDimitry Andric        while (__t != 0)
3300b57cec5SDimitry Andric        {
3310b57cec5SDimitry Andric            __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
3320b57cec5SDimitry Andric            __t >>= numeric_limits<unsigned long long>::digits;
3330b57cec5SDimitry Andric        }
3340b57cec5SDimitry Andric        return __ret;
3350b57cec5SDimitry Andric    }
3360b57cec5SDimitry Andric}
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric
3390b57cec5SDimitry Andric// integral log base 2
3400b57cec5SDimitry Andrictemplate<class _Tp>
3410b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
3420b57cec5SDimitry Andricunsigned __bit_log2(_Tp __t) _NOEXCEPT
3430b57cec5SDimitry Andric{
3440b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned");
3450b57cec5SDimitry Andric    return std::numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
3460b57cec5SDimitry Andric}
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andrictemplate <class _Tp>
3490b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
3500b57cec5SDimitry Andricbool __ispow2(_Tp __t) _NOEXCEPT
3510b57cec5SDimitry Andric{
3520b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__ispow2 requires unsigned");
3530b57cec5SDimitry Andric	return __t != 0 && (((__t & (__t - 1)) == 0));
3540b57cec5SDimitry Andric}
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric
3570b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andrictemplate<class _Tp>
3600b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3610b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
3620b57cec5SDimitry Andricrotl(_Tp __t, unsigned int __cnt) noexcept
3630b57cec5SDimitry Andric{
3640b57cec5SDimitry Andric    return __rotl(__t, __cnt);
3650b57cec5SDimitry Andric}
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric// rotr
3690b57cec5SDimitry Andrictemplate<class _Tp>
3700b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3710b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
3720b57cec5SDimitry Andricrotr(_Tp __t, unsigned int __cnt) noexcept
3730b57cec5SDimitry Andric{
3740b57cec5SDimitry Andric    return __rotr(__t, __cnt);
3750b57cec5SDimitry Andric}
3760b57cec5SDimitry Andric
3770b57cec5SDimitry Andric
3780b57cec5SDimitry Andrictemplate<class _Tp>
3790b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3800b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
3810b57cec5SDimitry Andriccountl_zero(_Tp __t) noexcept
3820b57cec5SDimitry Andric{
3830b57cec5SDimitry Andric    return __countl_zero(__t);
3840b57cec5SDimitry Andric}
3850b57cec5SDimitry Andric
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andrictemplate<class _Tp>
3880b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3890b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
3900b57cec5SDimitry Andriccountl_one(_Tp __t) noexcept
3910b57cec5SDimitry Andric{
3920b57cec5SDimitry Andric    return __countl_one(__t);
3930b57cec5SDimitry Andric}
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andric
3960b57cec5SDimitry Andrictemplate<class _Tp>
3970b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3980b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
3990b57cec5SDimitry Andriccountr_zero(_Tp __t) noexcept
4000b57cec5SDimitry Andric{
4010b57cec5SDimitry Andric	return __countr_zero(__t);
4020b57cec5SDimitry Andric}
4030b57cec5SDimitry Andric
4040b57cec5SDimitry Andric
4050b57cec5SDimitry Andrictemplate<class _Tp>
4060b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
4070b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
4080b57cec5SDimitry Andriccountr_one(_Tp __t) noexcept
4090b57cec5SDimitry Andric{
4100b57cec5SDimitry Andric    return __countr_one(__t);
4110b57cec5SDimitry Andric}
4120b57cec5SDimitry Andric
4130b57cec5SDimitry Andric
4140b57cec5SDimitry Andrictemplate<class _Tp>
4150b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
4160b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
4170b57cec5SDimitry Andricpopcount(_Tp __t) noexcept
4180b57cec5SDimitry Andric{
4190b57cec5SDimitry Andric    return __popcount(__t);
4200b57cec5SDimitry Andric}
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric
4230b57cec5SDimitry Andrictemplate <class _Tp>
4240b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
4250b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
4260b57cec5SDimitry Andricispow2(_Tp __t) noexcept
4270b57cec5SDimitry Andric{
4280b57cec5SDimitry Andric    return __ispow2(__t);
4290b57cec5SDimitry Andric}
4300b57cec5SDimitry Andric
4310b57cec5SDimitry Andrictemplate <class _Tp>
4320b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
4330b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
4340b57cec5SDimitry Andricfloor2(_Tp __t) noexcept
4350b57cec5SDimitry Andric{
4360b57cec5SDimitry Andric    return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
4370b57cec5SDimitry Andric}
4380b57cec5SDimitry Andric
4390b57cec5SDimitry Andrictemplate <class _Tp>
4400b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
4410b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
4420b57cec5SDimitry Andricceil2(_Tp __t) noexcept
4430b57cec5SDimitry Andric{
4440b57cec5SDimitry Andric    if (__t < 2) return 1;
4450b57cec5SDimitry Andric    const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
4460b57cec5SDimitry Andric    _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to ceil2");
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric    if constexpr (sizeof(_Tp) >= sizeof(unsigned))
4490b57cec5SDimitry Andric        return _Tp{1} << __n;
4500b57cec5SDimitry Andric    else
4510b57cec5SDimitry Andric    {
4520b57cec5SDimitry Andric        const unsigned __extra = numeric_limits<unsigned>::digits  - numeric_limits<_Tp>::digits;
4530b57cec5SDimitry Andric        const unsigned __retVal = 1u << (__n + __extra);
4540b57cec5SDimitry Andric        return (_Tp) (__retVal >> __extra);
4550b57cec5SDimitry Andric    }
4560b57cec5SDimitry Andric}
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andrictemplate <class _Tp>
4590b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
4600b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
4610b57cec5SDimitry Andriclog2p1(_Tp __t) noexcept
4620b57cec5SDimitry Andric{
4630b57cec5SDimitry Andric    return __t == 0 ? 0 : __bit_log2(__t) + 1;
4640b57cec5SDimitry Andric}
4650b57cec5SDimitry Andric
466*e40139ffSDimitry Andric
467*e40139ffSDimitry Andricenum class endian
468*e40139ffSDimitry Andric{
469*e40139ffSDimitry Andric    little = 0xDEAD,
470*e40139ffSDimitry Andric    big    = 0xFACE,
471*e40139ffSDimitry Andric#if defined(_LIBCPP_LITTLE_ENDIAN)
472*e40139ffSDimitry Andric    native = little
473*e40139ffSDimitry Andric#elif defined(_LIBCPP_BIG_ENDIAN)
474*e40139ffSDimitry Andric    native = big
475*e40139ffSDimitry Andric#else
476*e40139ffSDimitry Andric    native = 0xCAFE
477*e40139ffSDimitry Andric#endif
478*e40139ffSDimitry Andric};
479*e40139ffSDimitry Andric
4800b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 17
4810b57cec5SDimitry Andric
4820b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
4830b57cec5SDimitry Andric
4840b57cec5SDimitry Andric_LIBCPP_POP_MACROS
4850b57cec5SDimitry Andric
4860b57cec5SDimitry Andric#endif // _LIBCPP_BIT
487