xref: /freebsd/contrib/llvm-project/libcxx/include/__bit/bit_ceil.h (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___BIT_BIT_CEIL_H
10 #define _LIBCPP___BIT_BIT_CEIL_H
11 
12 #include <__assert>
13 #include <__bit/countl.h>
14 #include <__concepts/arithmetic.h>
15 #include <__config>
16 #include <limits>
17 
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 #  pragma GCC system_header
20 #endif
21 
22 _LIBCPP_BEGIN_NAMESPACE_STD
23 
24 #if _LIBCPP_STD_VER >= 20
25 
26 template <__libcpp_unsigned_integer _Tp>
27 _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept {
28   if (__t < 2)
29     return 1;
30   const unsigned __n = numeric_limits<_Tp>::digits - std::countl_zero((_Tp)(__t - 1u));
31   _LIBCPP_ASSERT_UNCATEGORIZED(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil");
32 
33   if constexpr (sizeof(_Tp) >= sizeof(unsigned))
34     return _Tp{1} << __n;
35   else {
36     const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits;
37     const unsigned __ret_val = 1u << (__n + __extra);
38     return (_Tp)(__ret_val >> __extra);
39   }
40 }
41 
42 #endif // _LIBCPP_STD_VER >= 20
43 
44 _LIBCPP_END_NAMESPACE_STD
45 
46 #endif // _LIBCPP___BIT_BIT_CEIL_H
47