1bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 2bdd1243dSDimitry Andric // 3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bdd1243dSDimitry Andric // 7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 8bdd1243dSDimitry Andric 9bdd1243dSDimitry Andric #ifndef _LIBCPP___BIT_BIT_CEIL_H 10bdd1243dSDimitry Andric #define _LIBCPP___BIT_BIT_CEIL_H 11bdd1243dSDimitry Andric 12bdd1243dSDimitry Andric #include <__assert> 13bdd1243dSDimitry Andric #include <__bit/countl.h> 14bdd1243dSDimitry Andric #include <__concepts/arithmetic.h> 15bdd1243dSDimitry Andric #include <__config> 16bdd1243dSDimitry Andric #include <limits> 17bdd1243dSDimitry Andric 18bdd1243dSDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 19bdd1243dSDimitry Andric # pragma GCC system_header 20bdd1243dSDimitry Andric #endif 21bdd1243dSDimitry Andric 22bdd1243dSDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD 23bdd1243dSDimitry Andric 245f757f3fSDimitry Andric #if _LIBCPP_STD_VER >= 17 25bdd1243dSDimitry Andric 265f757f3fSDimitry Andric template <class _Tp> 27*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp __bit_ceil(_Tp __t) noexcept { 28bdd1243dSDimitry Andric if (__t < 2) 29bdd1243dSDimitry Andric return 1; 305f757f3fSDimitry Andric const unsigned __n = numeric_limits<_Tp>::digits - std::__countl_zero((_Tp)(__t - 1u)); 317a6dacacSDimitry Andric _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__n != numeric_limits<_Tp>::digits, "Bad input to bit_ceil"); 32bdd1243dSDimitry Andric 33bdd1243dSDimitry Andric if constexpr (sizeof(_Tp) >= sizeof(unsigned)) 34bdd1243dSDimitry Andric return _Tp{1} << __n; 35bdd1243dSDimitry Andric else { 36bdd1243dSDimitry Andric const unsigned __extra = numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits; 3706c3fb27SDimitry Andric const unsigned __ret_val = 1u << (__n + __extra); 3806c3fb27SDimitry Andric return (_Tp)(__ret_val >> __extra); 39bdd1243dSDimitry Andric } 40bdd1243dSDimitry Andric } 41bdd1243dSDimitry Andric 425f757f3fSDimitry Andric # if _LIBCPP_STD_VER >= 20 435f757f3fSDimitry Andric 445f757f3fSDimitry Andric template <__libcpp_unsigned_integer _Tp> 45*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Tp bit_ceil(_Tp __t) noexcept { 465f757f3fSDimitry Andric return std::__bit_ceil(__t); 475f757f3fSDimitry Andric } 485f757f3fSDimitry Andric 49bdd1243dSDimitry Andric # endif // _LIBCPP_STD_VER >= 20 505f757f3fSDimitry Andric #endif // _LIBCPP_STD_VER >= 17 51bdd1243dSDimitry Andric 52bdd1243dSDimitry Andric _LIBCPP_END_NAMESPACE_STD 53bdd1243dSDimitry Andric 54bdd1243dSDimitry Andric #endif // _LIBCPP___BIT_BIT_CEIL_H 55