xref: /freebsd-src/contrib/llvm-project/libcxx/include/__random/log2.h (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
1*4824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
2*4824e7fdSDimitry Andric //
3*4824e7fdSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*4824e7fdSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*4824e7fdSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*4824e7fdSDimitry Andric //
7*4824e7fdSDimitry Andric //===----------------------------------------------------------------------===//
8*4824e7fdSDimitry Andric 
9*4824e7fdSDimitry Andric #ifndef _LIBCPP___RANDOM_LOG2_H
10*4824e7fdSDimitry Andric #define _LIBCPP___RANDOM_LOG2_H
11*4824e7fdSDimitry Andric 
12*4824e7fdSDimitry Andric #include <__config>
13*4824e7fdSDimitry Andric #include <cstddef>
14*4824e7fdSDimitry Andric #include <type_traits>
15*4824e7fdSDimitry Andric 
16*4824e7fdSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17*4824e7fdSDimitry Andric #pragma GCC system_header
18*4824e7fdSDimitry Andric #endif
19*4824e7fdSDimitry Andric 
20*4824e7fdSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
21*4824e7fdSDimitry Andric 
22*4824e7fdSDimitry Andric template <class _UIntType, _UIntType _Xp, size_t _Rp>
23*4824e7fdSDimitry Andric struct __log2_imp;
24*4824e7fdSDimitry Andric 
25*4824e7fdSDimitry Andric template <unsigned long long _Xp, size_t _Rp>
26*4824e7fdSDimitry Andric struct __log2_imp<unsigned long long, _Xp, _Rp>
27*4824e7fdSDimitry Andric {
28*4824e7fdSDimitry Andric     static const size_t value = _Xp & ((unsigned long long)(1) << _Rp) ? _Rp
29*4824e7fdSDimitry Andric                                            : __log2_imp<unsigned long long, _Xp, _Rp - 1>::value;
30*4824e7fdSDimitry Andric };
31*4824e7fdSDimitry Andric 
32*4824e7fdSDimitry Andric template <unsigned long long _Xp>
33*4824e7fdSDimitry Andric struct __log2_imp<unsigned long long, _Xp, 0>
34*4824e7fdSDimitry Andric {
35*4824e7fdSDimitry Andric     static const size_t value = 0;
36*4824e7fdSDimitry Andric };
37*4824e7fdSDimitry Andric 
38*4824e7fdSDimitry Andric template <size_t _Rp>
39*4824e7fdSDimitry Andric struct __log2_imp<unsigned long long, 0, _Rp>
40*4824e7fdSDimitry Andric {
41*4824e7fdSDimitry Andric     static const size_t value = _Rp + 1;
42*4824e7fdSDimitry Andric };
43*4824e7fdSDimitry Andric 
44*4824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128
45*4824e7fdSDimitry Andric 
46*4824e7fdSDimitry Andric template <__uint128_t _Xp, size_t _Rp>
47*4824e7fdSDimitry Andric struct __log2_imp<__uint128_t, _Xp, _Rp>
48*4824e7fdSDimitry Andric {
49*4824e7fdSDimitry Andric     static const size_t value = (_Xp >> 64)
50*4824e7fdSDimitry Andric         ? (64 + __log2_imp<unsigned long long, (_Xp >> 64), 63>::value)
51*4824e7fdSDimitry Andric         : __log2_imp<unsigned long long, _Xp, 63>::value;
52*4824e7fdSDimitry Andric };
53*4824e7fdSDimitry Andric 
54*4824e7fdSDimitry Andric #endif // _LIBCPP_HAS_NO_INT128
55*4824e7fdSDimitry Andric 
56*4824e7fdSDimitry Andric template <class _UIntType, _UIntType _Xp>
57*4824e7fdSDimitry Andric struct __log2
58*4824e7fdSDimitry Andric {
59*4824e7fdSDimitry Andric     static const size_t value = __log2_imp<
60*4824e7fdSDimitry Andric #ifndef _LIBCPP_HAS_NO_INT128
61*4824e7fdSDimitry Andric         typename conditional<
62*4824e7fdSDimitry Andric                 sizeof(_UIntType) <= sizeof(unsigned long long),
63*4824e7fdSDimitry Andric                     unsigned long long,
64*4824e7fdSDimitry Andric                     __uint128_t
65*4824e7fdSDimitry Andric             >::type,
66*4824e7fdSDimitry Andric #else
67*4824e7fdSDimitry Andric         unsigned long long,
68*4824e7fdSDimitry Andric #endif // _LIBCPP_HAS_NO_INT128
69*4824e7fdSDimitry Andric         _Xp, sizeof(_UIntType) * __CHAR_BIT__ - 1>::value;
70*4824e7fdSDimitry Andric };
71*4824e7fdSDimitry Andric 
72*4824e7fdSDimitry Andric _LIBCPP_END_NAMESPACE_STD
73*4824e7fdSDimitry Andric 
74*4824e7fdSDimitry Andric #endif // _LIBCPP___RANDOM_LOG2_H
75