1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef _LIBCPP___BIT_BIT_CEIL_H
10 #define _LIBCPP___BIT_BIT_CEIL_H
12 #include <__cxx03/__assert>
13 #include <__cxx03/__bit/countl.h>
14 #include <__cxx03/__concepts/arithmetic.h>
15 #include <__cxx03/__config>
16 #include <__cxx03/limits>
18 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
19 # pragma GCC system_header
22 _LIBCPP_BEGIN_NAMESPACE_STD
24 #if _LIBCPP_STD_VER >= 17
27 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
__bit_ceil(_Tp __t
) noexcept
{
30 const unsigned __n
= numeric_limits
<_Tp
>::digits
- std::__countl_zero((_Tp
)(__t
- 1u));
31 _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__n
!= numeric_limits
<_Tp
>::digits
, "Bad input to bit_ceil");
33 if constexpr (sizeof(_Tp
) >= sizeof(unsigned))
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
);
42 # if _LIBCPP_STD_VER >= 20
44 template <__libcpp_unsigned_integer _Tp
>
45 [[nodiscard
]] _LIBCPP_HIDE_FROM_ABI
constexpr _Tp
bit_ceil(_Tp __t
) noexcept
{
46 return std::__bit_ceil(__t
);
49 # endif // _LIBCPP_STD_VER >= 20
50 #endif // _LIBCPP_STD_VER >= 17
52 _LIBCPP_END_NAMESPACE_STD
54 #endif // _LIBCPP___BIT_BIT_CEIL_H