xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/log2.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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>
134824e7fdSDimitry Andric #include <cstddef>
144824e7fdSDimitry Andric #include <type_traits>
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>
264824e7fdSDimitry Andric struct __log2_imp<unsigned long long, _Xp, _Rp>
274824e7fdSDimitry Andric {
284824e7fdSDimitry Andric     static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
294824e7fdSDimitry Andric                                            : __log2_imp<unsigned long long, _Xp, _Rp - 1>::value;
304824e7fdSDimitry Andric };
314824e7fdSDimitry Andric 
324824e7fdSDimitry Andric template <unsigned long long _Xp>
334824e7fdSDimitry Andric struct __log2_imp<unsigned long long, _Xp, 0>
344824e7fdSDimitry Andric {
354824e7fdSDimitry Andric     static const size_t value = 0;
364824e7fdSDimitry Andric };
374824e7fdSDimitry Andric 
384824e7fdSDimitry Andric template <size_t _Rp>
394824e7fdSDimitry Andric struct __log2_imp<unsigned long long, 0, _Rp>
404824e7fdSDimitry Andric {
414824e7fdSDimitry Andric     static const size_t value = _Rp + 1;
424824e7fdSDimitry Andric };
434824e7fdSDimitry Andric 
444824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128
454824e7fdSDimitry Andric 
464824e7fdSDimitry Andric template <__uint128_t _Xp, size_t _Rp>
474824e7fdSDimitry Andric struct __log2_imp<__uint128_t, _Xp, _Rp>
484824e7fdSDimitry Andric {
494824e7fdSDimitry Andric     static const size_t value = (_Xp >> 64)
504824e7fdSDimitry Andric         ? (64 + __log2_imp<unsigned long long, (_Xp >> 64), 63>::value)
514824e7fdSDimitry Andric         : __log2_imp<unsigned long long, _Xp, 63>::value;
524824e7fdSDimitry Andric };
534824e7fdSDimitry Andric 
544824e7fdSDimitry Andric #endif // _LIBCPP_HAS_NO_INT128
554824e7fdSDimitry Andric 
564824e7fdSDimitry Andric template <class _UIntType, _UIntType _Xp>
574824e7fdSDimitry Andric struct __log2
584824e7fdSDimitry Andric {
594824e7fdSDimitry Andric     static const size_t value = __log2_imp<
604824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128
61*bdd1243dSDimitry Andric         __conditional_t<sizeof(_UIntType) <= sizeof(unsigned long long), unsigned long long, __uint128_t>,
624824e7fdSDimitry Andric #else
634824e7fdSDimitry Andric         unsigned long long,
644824e7fdSDimitry Andric #endif // _LIBCPP_HAS_NO_INT128
65*bdd1243dSDimitry Andric         _Xp,
66*bdd1243dSDimitry Andric         sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
674824e7fdSDimitry Andric };
684824e7fdSDimitry Andric 
694824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
704824e7fdSDimitry Andric 
714824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_LOG2_H
72