Path: blob/main/contrib/llvm-project/libcxx/include/__bit/countl.h
35260 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78// TODO: __builtin_clzg is available since Clang 19 and GCC 14. When support for older versions is dropped, we can9// refactor this code to exclusively use __builtin_clzg.1011#ifndef _LIBCPP___BIT_COUNTL_H12#define _LIBCPP___BIT_COUNTL_H1314#include <__bit/rotate.h>15#include <__concepts/arithmetic.h>16#include <__config>17#include <__type_traits/is_unsigned_integer.h>18#include <limits>1920#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif2324_LIBCPP_PUSH_MACROS25#include <__undef_macros>2627_LIBCPP_BEGIN_NAMESPACE_STD2829_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned __x) _NOEXCEPT {30return __builtin_clz(__x);31}3233_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned long __x) _NOEXCEPT {34return __builtin_clzl(__x);35}3637_LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(unsigned long long __x) _NOEXCEPT {38return __builtin_clzll(__x);39}4041#ifndef _LIBCPP_HAS_NO_INT12842inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(__uint128_t __x) _NOEXCEPT {43# if __has_builtin(__builtin_clzg)44return __builtin_clzg(__x);45# else46// The function is written in this form due to C++ constexpr limitations.47// The algorithm:48// - Test whether any bit in the high 64-bits is set49// - No bits set:50// - The high 64-bits contain 64 leading zeros,51// - Add the result of the low 64-bits.52// - Any bits set:53// - The number of leading zeros of the input is the number of leading54// zeros in the high 64-bits.55return ((__x >> 64) == 0) ? (64 + __builtin_clzll(static_cast<unsigned long long>(__x)))56: __builtin_clzll(static_cast<unsigned long long>(__x >> 64));57# endif58}59#endif // _LIBCPP_HAS_NO_INT1286061template <class _Tp>62_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _NOEXCEPT {63static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");64#if __has_builtin(__builtin_clzg)65return __builtin_clzg(__t, numeric_limits<_Tp>::digits);66#else // __has_builtin(__builtin_clzg)67if (__t == 0)68return numeric_limits<_Tp>::digits;6970if (sizeof(_Tp) <= sizeof(unsigned int))71return std::__libcpp_clz(static_cast<unsigned int>(__t)) -72(numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);73else if (sizeof(_Tp) <= sizeof(unsigned long))74return std::__libcpp_clz(static_cast<unsigned long>(__t)) -75(numeric_limits<unsigned long>::digits - numeric_limits<_Tp>::digits);76else if (sizeof(_Tp) <= sizeof(unsigned long long))77return std::__libcpp_clz(static_cast<unsigned long long>(__t)) -78(numeric_limits<unsigned long long>::digits - numeric_limits<_Tp>::digits);79else {80int __ret = 0;81int __iter = 0;82const unsigned int __ulldigits = numeric_limits<unsigned long long>::digits;83while (true) {84__t = std::__rotl(__t, __ulldigits);85if ((__iter = std::__countl_zero(static_cast<unsigned long long>(__t))) != __ulldigits)86break;87__ret += __iter;88}89return __ret + __iter;90}91#endif // __has_builtin(__builtin_clzg)92}9394#if _LIBCPP_STD_VER >= 209596template <__libcpp_unsigned_integer _Tp>97[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countl_zero(_Tp __t) noexcept {98return std::__countl_zero(__t);99}100101template <__libcpp_unsigned_integer _Tp>102[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int countl_one(_Tp __t) noexcept {103return __t != numeric_limits<_Tp>::max() ? std::countl_zero(static_cast<_Tp>(~__t)) : numeric_limits<_Tp>::digits;104}105106#endif // _LIBCPP_STD_VER >= 20107108_LIBCPP_END_NAMESPACE_STD109110_LIBCPP_POP_MACROS111112#endif // _LIBCPP___BIT_COUNTL_H113114115