xref: /freebsd/contrib/llvm-project/libcxx/include/bit (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
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
185ffd83dbSDimitry Andric  // [bit.pow.two], integral powers of 2
190b57cec5SDimitry Andric  template <class T>
20*e8d8bef9SDimitry Andric    constexpr bool has_single_bit(T x) noexcept; // C++20
210b57cec5SDimitry Andric  template <class T>
22*e8d8bef9SDimitry Andric    constexpr T bit_ceil(T x);                   // C++20
230b57cec5SDimitry Andric  template <class T>
24*e8d8bef9SDimitry Andric    constexpr T bit_floor(T x) noexcept;         // C++20
250b57cec5SDimitry Andric  template <class T>
26*e8d8bef9SDimitry Andric    constexpr T bit_width(T x) noexcept;         // C++20
270b57cec5SDimitry Andric
285ffd83dbSDimitry Andric  // [bit.rotate], rotating
290b57cec5SDimitry Andric  template<class T>
300b57cec5SDimitry Andric    constexpr T rotl(T x, unsigned int s) noexcept; // C++20
310b57cec5SDimitry Andric  template<class T>
320b57cec5SDimitry Andric    constexpr T rotr(T x, unsigned int s) noexcept; // C++20
330b57cec5SDimitry Andric
345ffd83dbSDimitry Andric  // [bit.count], counting
350b57cec5SDimitry Andric  template<class T>
360b57cec5SDimitry Andric    constexpr int countl_zero(T x) noexcept;  // C++20
370b57cec5SDimitry Andric  template<class T>
380b57cec5SDimitry Andric    constexpr int countl_one(T x) noexcept;   // C++20
390b57cec5SDimitry Andric  template<class T>
400b57cec5SDimitry Andric    constexpr int countr_zero(T x) noexcept;  // C++20
410b57cec5SDimitry Andric  template<class T>
420b57cec5SDimitry Andric    constexpr int countr_one(T x) noexcept;   // C++20
430b57cec5SDimitry Andric  template<class T>
440b57cec5SDimitry Andric    constexpr int popcount(T x) noexcept;     // C++20
450b57cec5SDimitry Andric
465ffd83dbSDimitry Andric  // [bit.endian], endian
47e40139ffSDimitry Andric  enum class endian {
48e40139ffSDimitry Andric    little = see below,        // C++20
49e40139ffSDimitry Andric    big = see below,           // C++20
50e40139ffSDimitry Andric    native = see below         // C++20
51e40139ffSDimitry Andric};
52e40139ffSDimitry Andric
530b57cec5SDimitry Andric} // namespace std
540b57cec5SDimitry Andric
550b57cec5SDimitry Andric*/
560b57cec5SDimitry Andric
570b57cec5SDimitry Andric#include <__config>
58*e8d8bef9SDimitry Andric#include <__bits>
590b57cec5SDimitry Andric#include <limits>
600b57cec5SDimitry Andric#include <type_traits>
610b57cec5SDimitry Andric#include <version>
620b57cec5SDimitry Andric#include <__debug>
630b57cec5SDimitry Andric
640b57cec5SDimitry Andric#if defined(__IBMCPP__)
650b57cec5SDimitry Andric#include "support/ibm/support.h"
660b57cec5SDimitry Andric#endif
670b57cec5SDimitry Andric#if defined(_LIBCPP_COMPILER_MSVC)
680b57cec5SDimitry Andric#include <intrin.h>
690b57cec5SDimitry Andric#endif
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
720b57cec5SDimitry Andric#pragma GCC system_header
730b57cec5SDimitry Andric#endif
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric_LIBCPP_PUSH_MACROS
760b57cec5SDimitry Andric#include <__undef_macros>
770b57cec5SDimitry Andric
780b57cec5SDimitry Andric_LIBCPP_BEGIN_NAMESPACE_STD
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric
810b57cec5SDimitry Andrictemplate <class _Tp>
820b57cec5SDimitry Andricusing __bitop_unsigned_integer _LIBCPP_NODEBUG_TYPE = integral_constant<bool,
830b57cec5SDimitry Andric         is_integral<_Tp>::value &&
840b57cec5SDimitry Andric         is_unsigned<_Tp>::value &&
850b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, bool>::value &&
860b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, signed char>::value &&
870b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, wchar_t>::value &&
880b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, char16_t>::value &&
890b57cec5SDimitry Andric        _IsNotSame<typename remove_cv<_Tp>::type, char32_t>::value
900b57cec5SDimitry Andric    >;
910b57cec5SDimitry Andric
920b57cec5SDimitry Andric
930b57cec5SDimitry Andrictemplate<class _Tp>
940b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
950b57cec5SDimitry Andric_Tp __rotl(_Tp __t, unsigned int __cnt) _NOEXCEPT
960b57cec5SDimitry Andric{
970b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotl requires unsigned");
980b57cec5SDimitry Andric    const unsigned int __dig = numeric_limits<_Tp>::digits;
990b57cec5SDimitry Andric    if ((__cnt % __dig) == 0)
1000b57cec5SDimitry Andric        return __t;
1010b57cec5SDimitry Andric    return (__t << (__cnt % __dig)) | (__t >> (__dig - (__cnt % __dig)));
1020b57cec5SDimitry Andric}
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andrictemplate<class _Tp>
1060b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1070b57cec5SDimitry Andric_Tp __rotr(_Tp __t, unsigned int __cnt) _NOEXCEPT
1080b57cec5SDimitry Andric{
1090b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__rotr requires unsigned");
1100b57cec5SDimitry Andric    const unsigned int __dig = numeric_limits<_Tp>::digits;
1110b57cec5SDimitry Andric    if ((__cnt % __dig) == 0)
1120b57cec5SDimitry Andric        return __t;
1130b57cec5SDimitry Andric    return (__t >> (__cnt % __dig)) | (__t << (__dig - (__cnt % __dig)));
1140b57cec5SDimitry Andric}
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric
1170b57cec5SDimitry Andric
1180b57cec5SDimitry Andrictemplate<class _Tp>
1190b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1200b57cec5SDimitry Andricint __countr_zero(_Tp __t) _NOEXCEPT
1210b57cec5SDimitry Andric{
1220b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_zero requires unsigned");
1230b57cec5SDimitry Andric    if (__t == 0)
1240b57cec5SDimitry Andric        return numeric_limits<_Tp>::digits;
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric    if      (sizeof(_Tp) <= sizeof(unsigned int))
1270b57cec5SDimitry Andric        return __libcpp_ctz(static_cast<unsigned int>(__t));
1280b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long))
1290b57cec5SDimitry Andric        return __libcpp_ctz(static_cast<unsigned long>(__t));
1300b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long long))
1310b57cec5SDimitry Andric        return __libcpp_ctz(static_cast<unsigned long long>(__t));
1320b57cec5SDimitry Andric    else
1330b57cec5SDimitry Andric    {
1340b57cec5SDimitry Andric        int __ret = 0;
1350b57cec5SDimitry Andric        int __iter = 0;
1360b57cec5SDimitry Andric        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
1370b57cec5SDimitry Andric        while ((__iter = __libcpp_ctz(static_cast<unsigned long long>(__t))) == __ulldigits)
1380b57cec5SDimitry Andric        {
1390b57cec5SDimitry Andric            __ret += __iter;
1400b57cec5SDimitry Andric            __t >>= __ulldigits;
1410b57cec5SDimitry Andric        }
1420b57cec5SDimitry Andric        return __ret + __iter;
1430b57cec5SDimitry Andric    }
1440b57cec5SDimitry Andric}
1450b57cec5SDimitry Andric
1460b57cec5SDimitry Andrictemplate<class _Tp>
1470b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1480b57cec5SDimitry Andricint __countl_zero(_Tp __t) _NOEXCEPT
1490b57cec5SDimitry Andric{
1500b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_zero requires unsigned");
1510b57cec5SDimitry Andric    if (__t == 0)
1520b57cec5SDimitry Andric        return numeric_limits<_Tp>::digits;
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric    if      (sizeof(_Tp) <= sizeof(unsigned int))
1550b57cec5SDimitry Andric        return __libcpp_clz(static_cast<unsigned int>(__t))
1560b57cec5SDimitry Andric              - (numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
1570b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long))
1580b57cec5SDimitry Andric        return __libcpp_clz(static_cast<unsigned long>(__t))
1590b57cec5SDimitry Andric              - (numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);
1600b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long long))
1610b57cec5SDimitry Andric        return __libcpp_clz(static_cast<unsigned long long>(__t))
1620b57cec5SDimitry Andric              - (numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);
1630b57cec5SDimitry Andric    else
1640b57cec5SDimitry Andric    {
1650b57cec5SDimitry Andric        int __ret = 0;
1660b57cec5SDimitry Andric        int __iter = 0;
1670b57cec5SDimitry Andric        const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;
1680b57cec5SDimitry Andric        while (true) {
1690b57cec5SDimitry Andric            __t = __rotr(__t, __ulldigits);
1700b57cec5SDimitry Andric            if ((__iter = __countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)
1710b57cec5SDimitry Andric                break;
1720b57cec5SDimitry Andric            __ret += __iter;
1730b57cec5SDimitry Andric            }
1740b57cec5SDimitry Andric        return __ret + __iter;
1750b57cec5SDimitry Andric    }
1760b57cec5SDimitry Andric}
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andrictemplate<class _Tp>
1790b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1800b57cec5SDimitry Andricint __countl_one(_Tp __t) _NOEXCEPT
1810b57cec5SDimitry Andric{
1820b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countl_one requires unsigned");
1830b57cec5SDimitry Andric    return __t != numeric_limits<_Tp>::max()
1840b57cec5SDimitry Andric        ? __countl_zero(static_cast<_Tp>(~__t))
1850b57cec5SDimitry Andric        : numeric_limits<_Tp>::digits;
1860b57cec5SDimitry Andric}
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric
1890b57cec5SDimitry Andrictemplate<class _Tp>
1900b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1910b57cec5SDimitry Andricint __countr_one(_Tp __t) _NOEXCEPT
1920b57cec5SDimitry Andric{
1930b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__countr_one requires unsigned");
1940b57cec5SDimitry Andric    return __t != numeric_limits<_Tp>::max()
1950b57cec5SDimitry Andric        ? __countr_zero(static_cast<_Tp>(~__t))
1960b57cec5SDimitry Andric        : numeric_limits<_Tp>::digits;
1970b57cec5SDimitry Andric}
1980b57cec5SDimitry Andric
1990b57cec5SDimitry Andric
2000b57cec5SDimitry Andrictemplate<class _Tp>
2010b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2020b57cec5SDimitry Andricint
2030b57cec5SDimitry Andric__popcount(_Tp __t) _NOEXCEPT
2040b57cec5SDimitry Andric{
2050b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__libcpp_popcount requires unsigned");
2060b57cec5SDimitry Andric    if      (sizeof(_Tp) <= sizeof(unsigned int))
2070b57cec5SDimitry Andric        return __libcpp_popcount(static_cast<unsigned int>(__t));
2080b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long))
2090b57cec5SDimitry Andric        return __libcpp_popcount(static_cast<unsigned long>(__t));
2100b57cec5SDimitry Andric    else if (sizeof(_Tp) <= sizeof(unsigned long long))
2110b57cec5SDimitry Andric        return __libcpp_popcount(static_cast<unsigned long long>(__t));
2120b57cec5SDimitry Andric    else
2130b57cec5SDimitry Andric    {
2140b57cec5SDimitry Andric        int __ret = 0;
2150b57cec5SDimitry Andric        while (__t != 0)
2160b57cec5SDimitry Andric        {
2170b57cec5SDimitry Andric            __ret += __libcpp_popcount(static_cast<unsigned long long>(__t));
2180b57cec5SDimitry Andric            __t >>= numeric_limits<unsigned long long>::digits;
2190b57cec5SDimitry Andric        }
2200b57cec5SDimitry Andric        return __ret;
2210b57cec5SDimitry Andric    }
2220b57cec5SDimitry Andric}
2230b57cec5SDimitry Andric
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric// integral log base 2
2260b57cec5SDimitry Andrictemplate<class _Tp>
2270b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
2280b57cec5SDimitry Andricunsigned __bit_log2(_Tp __t) _NOEXCEPT
2290b57cec5SDimitry Andric{
2300b57cec5SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__bit_log2 requires unsigned");
231*e8d8bef9SDimitry Andric    return numeric_limits<_Tp>::digits - 1 - __countl_zero(__t);
2320b57cec5SDimitry Andric}
2330b57cec5SDimitry Andric
2340b57cec5SDimitry Andrictemplate <class _Tp>
2350b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
236*e8d8bef9SDimitry Andricbool __has_single_bit(_Tp __t) _NOEXCEPT
2370b57cec5SDimitry Andric{
238*e8d8bef9SDimitry Andric    static_assert(__bitop_unsigned_integer<_Tp>::value, "__has_single_bit requires unsigned");
2390b57cec5SDimitry Andric    return __t != 0 && (((__t & (__t - 1)) == 0));
2400b57cec5SDimitry Andric}
2410b57cec5SDimitry Andric
2420b57cec5SDimitry Andric
2430b57cec5SDimitry Andric#if _LIBCPP_STD_VER > 17
2440b57cec5SDimitry Andric
2450b57cec5SDimitry Andrictemplate<class _Tp>
2460b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
2470b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
2480b57cec5SDimitry Andricrotl(_Tp __t, unsigned int __cnt) noexcept
2490b57cec5SDimitry Andric{
2500b57cec5SDimitry Andric    return __rotl(__t, __cnt);
2510b57cec5SDimitry Andric}
2520b57cec5SDimitry Andric
2530b57cec5SDimitry Andric
2540b57cec5SDimitry Andric// rotr
2550b57cec5SDimitry Andrictemplate<class _Tp>
2560b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
2570b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
2580b57cec5SDimitry Andricrotr(_Tp __t, unsigned int __cnt) noexcept
2590b57cec5SDimitry Andric{
2600b57cec5SDimitry Andric    return __rotr(__t, __cnt);
2610b57cec5SDimitry Andric}
2620b57cec5SDimitry Andric
2630b57cec5SDimitry Andric
2640b57cec5SDimitry Andrictemplate<class _Tp>
2650b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
2660b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
2670b57cec5SDimitry Andriccountl_zero(_Tp __t) noexcept
2680b57cec5SDimitry Andric{
2690b57cec5SDimitry Andric    return __countl_zero(__t);
2700b57cec5SDimitry Andric}
2710b57cec5SDimitry Andric
2720b57cec5SDimitry Andric
2730b57cec5SDimitry Andrictemplate<class _Tp>
2740b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
2750b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
2760b57cec5SDimitry Andriccountl_one(_Tp __t) noexcept
2770b57cec5SDimitry Andric{
2780b57cec5SDimitry Andric    return __countl_one(__t);
2790b57cec5SDimitry Andric}
2800b57cec5SDimitry Andric
2810b57cec5SDimitry Andric
2820b57cec5SDimitry Andrictemplate<class _Tp>
2830b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
2840b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
2850b57cec5SDimitry Andriccountr_zero(_Tp __t) noexcept
2860b57cec5SDimitry Andric{
2870b57cec5SDimitry Andric    return __countr_zero(__t);
2880b57cec5SDimitry Andric}
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andric
2910b57cec5SDimitry Andrictemplate<class _Tp>
2920b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
2930b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
2940b57cec5SDimitry Andriccountr_one(_Tp __t) noexcept
2950b57cec5SDimitry Andric{
2960b57cec5SDimitry Andric    return __countr_one(__t);
2970b57cec5SDimitry Andric}
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andric
3000b57cec5SDimitry Andrictemplate<class _Tp>
3010b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3020b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, int>
3030b57cec5SDimitry Andricpopcount(_Tp __t) noexcept
3040b57cec5SDimitry Andric{
3050b57cec5SDimitry Andric    return __popcount(__t);
3060b57cec5SDimitry Andric}
3070b57cec5SDimitry Andric
3080b57cec5SDimitry Andric
3090b57cec5SDimitry Andrictemplate <class _Tp>
3100b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3110b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, bool>
312*e8d8bef9SDimitry Andrichas_single_bit(_Tp __t) noexcept
3130b57cec5SDimitry Andric{
314*e8d8bef9SDimitry Andric    return __has_single_bit(__t);
3150b57cec5SDimitry Andric}
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andrictemplate <class _Tp>
3180b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3190b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
320*e8d8bef9SDimitry Andricbit_floor(_Tp __t) noexcept
3210b57cec5SDimitry Andric{
3220b57cec5SDimitry Andric    return __t == 0 ? 0 : _Tp{1} << __bit_log2(__t);
3230b57cec5SDimitry Andric}
3240b57cec5SDimitry Andric
3250b57cec5SDimitry Andrictemplate <class _Tp>
3260b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3270b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
328*e8d8bef9SDimitry Andricbit_ceil(_Tp __t) noexcept
3290b57cec5SDimitry Andric{
3300b57cec5SDimitry Andric    if (__t < 2) return 1;
3310b57cec5SDimitry Andric    const unsigned __n = numeric_limits<_Tp>::digits - countl_zero((_Tp)(__t - 1u));
332*e8d8bef9SDimitry Andric    _LIBCPP_DEBUG_ASSERT(__libcpp_is_constant_evaluated() || __n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric    if constexpr (sizeof(_Tp) >= sizeof(unsigned))
3350b57cec5SDimitry Andric        return _Tp{1} << __n;
3360b57cec5SDimitry Andric    else
3370b57cec5SDimitry Andric    {
3380b57cec5SDimitry Andric        const unsigned __extra = numeric_limits<unsigned>::digits  - numeric_limits<_Tp>::digits;
3390b57cec5SDimitry Andric        const unsigned __retVal = 1u << (__n + __extra);
3400b57cec5SDimitry Andric        return (_Tp) (__retVal >> __extra);
3410b57cec5SDimitry Andric    }
3420b57cec5SDimitry Andric}
3430b57cec5SDimitry Andric
3440b57cec5SDimitry Andrictemplate <class _Tp>
3450b57cec5SDimitry Andric_LIBCPP_INLINE_VISIBILITY constexpr
3460b57cec5SDimitry Andricenable_if_t<__bitop_unsigned_integer<_Tp>::value, _Tp>
347*e8d8bef9SDimitry Andricbit_width(_Tp __t) noexcept
3480b57cec5SDimitry Andric{
3490b57cec5SDimitry Andric    return __t == 0 ? 0 : __bit_log2(__t) + 1;
3500b57cec5SDimitry Andric}
3510b57cec5SDimitry Andric
352e40139ffSDimitry Andricenum class endian
353e40139ffSDimitry Andric{
354e40139ffSDimitry Andric    little = 0xDEAD,
355e40139ffSDimitry Andric    big    = 0xFACE,
356e40139ffSDimitry Andric#if defined(_LIBCPP_LITTLE_ENDIAN)
357e40139ffSDimitry Andric    native = little
358e40139ffSDimitry Andric#elif defined(_LIBCPP_BIG_ENDIAN)
359e40139ffSDimitry Andric    native = big
360e40139ffSDimitry Andric#else
361e40139ffSDimitry Andric    native = 0xCAFE
362e40139ffSDimitry Andric#endif
363e40139ffSDimitry Andric};
364e40139ffSDimitry Andric
3650b57cec5SDimitry Andric#endif // _LIBCPP_STD_VER > 17
3660b57cec5SDimitry Andric
3670b57cec5SDimitry Andric_LIBCPP_END_NAMESPACE_STD
3680b57cec5SDimitry Andric
3690b57cec5SDimitry Andric_LIBCPP_POP_MACROS
3700b57cec5SDimitry Andric
3710b57cec5SDimitry Andric#endif // _LIBCPP_BIT
372