File indexing completed on 2026-05-03 08:14:00
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LIBCPP___RANDOM_LOG2_H
0010 #define _LIBCPP___RANDOM_LOG2_H
0011
0012 #include <__config>
0013 #include <__cstddef/size_t.h>
0014 #include <__type_traits/conditional.h>
0015
0016 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0017 # pragma GCC system_header
0018 #endif
0019
0020 _LIBCPP_BEGIN_NAMESPACE_STD
0021
0022 template <class _UIntType, _UIntType _Xp, size_t _Rp>
0023 struct __log2_imp;
0024
0025 template <unsigned long long _Xp, size_t _Rp>
0026 struct __log2_imp<unsigned long long, _Xp, _Rp> {
0027 static const size_t value =
0028 _Xp & ((unsigned long long)(1) << _Rp) ? _Rp : __log2_imp<unsigned long long, _Xp, _Rp - 1>::value;
0029 };
0030
0031 template <unsigned long long _Xp>
0032 struct __log2_imp<unsigned long long, _Xp, 0> {
0033 static const size_t value = 0;
0034 };
0035
0036 template <size_t _Rp>
0037 struct __log2_imp<unsigned long long, 0, _Rp> {
0038 static const size_t value = _Rp + 1;
0039 };
0040
0041 #if _LIBCPP_HAS_INT128
0042
0043 template <__uint128_t _Xp, size_t _Rp>
0044 struct __log2_imp<__uint128_t, _Xp, _Rp> {
0045 static const size_t value =
0046 (_Xp >> 64) ? (64 + __log2_imp<unsigned long long, (_Xp >> 64), 63>::value)
0047 : __log2_imp<unsigned long long, _Xp, 63>::value;
0048 };
0049
0050 #endif
0051
0052 template <class _UIntType, _UIntType _Xp>
0053 struct __log2 {
0054 static const size_t value = __log2_imp<
0055 #if _LIBCPP_HAS_INT128
0056 __conditional_t<sizeof(_UIntType) <= sizeof(unsigned long long), unsigned long long, __uint128_t>,
0057 #else
0058 unsigned long long,
0059 #endif
0060 _Xp,
0061 sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
0062 };
0063
0064 _LIBCPP_END_NAMESPACE_STD
0065
0066 #endif