| 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 <__config> |
| 15 | #include <__type_traits/integer_traits.h> |
| 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 >= 17 |
| 25 | |
| 26 | template <class _Tp> |
| 27 | [[nodiscard]] _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_ARGUMENT_WITHIN_DOMAIN(__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 | # if _LIBCPP_STD_VER >= 20 |
| 43 | |
| 44 | template <__unsigned_integer _Tp> |
| 45 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept { |
| 46 | return std::__bit_ceil(__t); |
| 47 | } |
| 48 | |
| 49 | # endif // _LIBCPP_STD_VER >= 20 |
| 50 | #endif // _LIBCPP_STD_VER >= 17 |
| 51 | |
| 52 | _LIBCPP_END_NAMESPACE_STD |
| 53 | |
| 54 | #endif // _LIBCPP___BIT_BIT_CEIL_H |
| 55 |