| 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_POPCOUNT_H |
| 10 | #define _LIBCPP___BIT_POPCOUNT_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__type_traits/integer_traits.h> |
| 14 | |
| 15 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 16 | # pragma GCC system_header |
| 17 | #endif |
| 18 | |
| 19 | _LIBCPP_PUSH_MACROS |
| 20 | #include <__undef_macros> |
| 21 | |
| 22 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 23 | |
| 24 | template <class _Tp> |
| 25 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT { |
| 26 | static_assert(__is_unsigned_integer_v<_Tp>, "__popcount only works with unsigned types"); |
| 27 | return __builtin_popcountg(__t); |
| 28 | } |
| 29 | |
| 30 | #if _LIBCPP_STD_VER >= 20 |
| 31 | |
| 32 | template <__unsigned_integer _Tp> |
| 33 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept { |
| 34 | return std::__popcount(__t); |
| 35 | } |
| 36 | |
| 37 | #endif |
| 38 | |
| 39 | _LIBCPP_END_NAMESPACE_STD |
| 40 | |
| 41 | _LIBCPP_POP_MACROS |
| 42 | |
| 43 | #endif // _LIBCPP___BIT_POPCOUNT_H |
| 44 |