14824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 24824e7fdSDimitry Andric // 34824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 44824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 54824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 64824e7fdSDimitry Andric // 74824e7fdSDimitry Andric //===----------------------------------------------------------------------===// 84824e7fdSDimitry Andric 94824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_LOG2_H 104824e7fdSDimitry Andric #define _LIBCPP___RANDOM_LOG2_H 114824e7fdSDimitry Andric 124824e7fdSDimitry Andric #include <__config> 1306c3fb27SDimitry Andric #include <__type_traits/conditional.h> 144824e7fdSDimitry Andric #include <cstddef> 154824e7fdSDimitry Andric 164824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 174824e7fdSDimitry Andric # pragma GCC system_header 184824e7fdSDimitry Andric #endif 194824e7fdSDimitry Andric 204824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 214824e7fdSDimitry Andric 224824e7fdSDimitry Andric template <class _UIntType, _UIntType _Xp, size_t _Rp> 234824e7fdSDimitry Andric struct __log2_imp; 244824e7fdSDimitry Andric 254824e7fdSDimitry Andric template <unsigned long long _Xp, size_t _Rp> 26*cb14a3feSDimitry Andric struct __log2_imp<unsigned long long, _Xp, _Rp> { 27*cb14a3feSDimitry Andric static const size_t value = 28*cb14a3feSDimitry Andric _Xp & ((unsigned long long)(1) << _Rp) ? _Rp : __log2_imp<unsigned long long, _Xp, _Rp - 1>::value; 294824e7fdSDimitry Andric }; 304824e7fdSDimitry Andric 314824e7fdSDimitry Andric template <unsigned long long _Xp> 32*cb14a3feSDimitry Andric struct __log2_imp<unsigned long long, _Xp, 0> { 334824e7fdSDimitry Andric static const size_t value = 0; 344824e7fdSDimitry Andric }; 354824e7fdSDimitry Andric 364824e7fdSDimitry Andric template <size_t _Rp> 37*cb14a3feSDimitry Andric struct __log2_imp<unsigned long long, 0, _Rp> { 384824e7fdSDimitry Andric static const size_t value = _Rp + 1; 394824e7fdSDimitry Andric }; 404824e7fdSDimitry Andric 414824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128 424824e7fdSDimitry Andric 434824e7fdSDimitry Andric template <__uint128_t _Xp, size_t _Rp> 44*cb14a3feSDimitry Andric struct __log2_imp<__uint128_t, _Xp, _Rp> { 45*cb14a3feSDimitry Andric static const size_t value = 46*cb14a3feSDimitry Andric (_Xp >> 64) ? (64 + __log2_imp<unsigned long long, (_Xp >> 64), 63>::value) 474824e7fdSDimitry Andric : __log2_imp<unsigned long long, _Xp, 63>::value; 484824e7fdSDimitry Andric }; 494824e7fdSDimitry Andric 504824e7fdSDimitry Andric #endif // _LIBCPP_HAS_NO_INT128 514824e7fdSDimitry Andric 524824e7fdSDimitry Andric template <class _UIntType, _UIntType _Xp> 53*cb14a3feSDimitry Andric struct __log2 { 544824e7fdSDimitry Andric static const size_t value = __log2_imp< 554824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128 56bdd1243dSDimitry Andric __conditional_t<sizeof(_UIntType) <= sizeof(unsigned long long), unsigned long long, __uint128_t>, 574824e7fdSDimitry Andric #else 584824e7fdSDimitry Andric unsigned long long, 594824e7fdSDimitry Andric #endif // _LIBCPP_HAS_NO_INT128 60bdd1243dSDimitry Andric _Xp, 61bdd1243dSDimitry Andric sizeof(_UIntType) * __CHAR_BIT__ - 1>::value; 624824e7fdSDimitry Andric }; 634824e7fdSDimitry Andric 644824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD 654824e7fdSDimitry Andric 664824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_LOG2_H 67