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
24template <class _Tp>
25[[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __popcount(_Tp __t) _NOEXCEPT {
26 return __builtin_popcountg(__t);
27}
28
29#if _LIBCPP_STD_VER >= 20
30
31template <__unsigned_integer _Tp>
32[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
33 return std::__popcount(__t);
34}
35
36#endif
37
38_LIBCPP_END_NAMESPACE_STD
39
40_LIBCPP_POP_MACROS
41
42#endif // _LIBCPP___BIT_POPCOUNT_H
43