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// UNSUPPORTED: c++03, c++11, c++14, c++17
10
11// template <class T>
12// constexpr int popcount(T x) noexcept;
13
14// Constraints: T is an unsigned integer type
15// Returns: The number of bits set to one in the value of x.
16
17#include <bit>
18#include <cassert>
19#include <cstddef>
20#include <cstdint>
21#include <limits>
22#include <type_traits>
23
24#include "test_macros.h"
25
26struct A {};
27enum E1 : unsigned char { rEd };
28enum class E2 : unsigned char { red };
29
30template <class T>
31constexpr bool test()
32{
33 ASSERT_SAME_TYPE(decltype(std::popcount(T())), int);
34 ASSERT_NOEXCEPT(std::popcount(T()));
35 T max = std::numeric_limits<T>::max();
36
37 assert(std::popcount(T(0)) == 0);
38 assert(std::popcount(T(1)) == 1);
39 assert(std::popcount(T(2)) == 1);
40 assert(std::popcount(T(3)) == 2);
41 assert(std::popcount(T(4)) == 1);
42 assert(std::popcount(T(5)) == 2);
43 assert(std::popcount(T(6)) == 2);
44 assert(std::popcount(T(7)) == 3);
45 assert(std::popcount(T(8)) == 1);
46 assert(std::popcount(T(9)) == 2);
47 assert(std::popcount(T(121)) == 5);
48 assert(std::popcount(T(127)) == 7);
49 assert(std::popcount(T(128)) == 1);
50 assert(std::popcount(T(130)) == 2);
51 assert(std::popcount(T(max >> 1)) == std::numeric_limits<T>::digits - 1);
52 assert(std::popcount(T(max - 1)) == std::numeric_limits<T>::digits - 1);
53 assert(std::popcount(max) == std::numeric_limits<T>::digits);
54
55#ifndef TEST_HAS_NO_INT128
56 if constexpr (std::is_same_v<T, __uint128_t>) {
57 T val = 128;
58 assert(std::popcount(val-1) == 7);
59 assert(std::popcount(val) == 1);
60 assert(std::popcount(val+1) == 2);
61 val <<= 32;
62 assert(std::popcount(val-1) == 39);
63 assert(std::popcount(val) == 1);
64 assert(std::popcount(val+1) == 2);
65 val <<= 60;
66 assert(std::popcount(val-1) == 99);
67 assert(std::popcount(val) == 1);
68 assert(std::popcount(val+1) == 2);
69
70 T x = T(1) << 63;
71 T y = T(1) << 64;
72 assert(std::popcount(x) == 1);
73 assert(std::popcount(y) == 1);
74 assert(std::popcount(x+y) == 2);
75 }
76#endif
77
78 return true;
79}
80
81int main(int, char**)
82{
83 {
84 auto lambda = [](auto x) -> decltype(std::popcount(x)) {};
85 using L = decltype(lambda);
86
87 static_assert(!std::is_invocable_v<L, signed char>);
88 static_assert(!std::is_invocable_v<L, short>);
89 static_assert(!std::is_invocable_v<L, int>);
90 static_assert(!std::is_invocable_v<L, long>);
91 static_assert(!std::is_invocable_v<L, long long>);
92#ifndef TEST_HAS_NO_INT128
93 static_assert(!std::is_invocable_v<L, __int128_t>);
94#endif
95
96 static_assert(!std::is_invocable_v<L, std::int8_t>);
97 static_assert(!std::is_invocable_v<L, std::int16_t>);
98 static_assert(!std::is_invocable_v<L, std::int32_t>);
99 static_assert(!std::is_invocable_v<L, std::int64_t>);
100 static_assert(!std::is_invocable_v<L, std::intmax_t>);
101 static_assert(!std::is_invocable_v<L, std::intptr_t>);
102 static_assert(!std::is_invocable_v<L, std::ptrdiff_t>);
103
104 static_assert(!std::is_invocable_v<L, bool>);
105 static_assert(!std::is_invocable_v<L, char>);
106 static_assert(!std::is_invocable_v<L, wchar_t>);
107#ifndef TEST_HAS_NO_CHAR8_T
108 static_assert(!std::is_invocable_v<L, char8_t>);
109#endif
110 static_assert(!std::is_invocable_v<L, char16_t>);
111 static_assert(!std::is_invocable_v<L, char32_t>);
112
113 static_assert(!std::is_invocable_v<L, A>);
114 static_assert(!std::is_invocable_v<L, A*>);
115 static_assert(!std::is_invocable_v<L, E1>);
116 static_assert(!std::is_invocable_v<L, E2>);
117 }
118
119 static_assert(test<unsigned char>());
120 static_assert(test<unsigned short>());
121 static_assert(test<unsigned int>());
122 static_assert(test<unsigned long>());
123 static_assert(test<unsigned long long>());
124#ifndef TEST_HAS_NO_INT128
125 static_assert(test<__uint128_t>());
126#endif
127 static_assert(test<std::uint8_t>());
128 static_assert(test<std::uint16_t>());
129 static_assert(test<std::uint32_t>());
130 static_assert(test<std::uint64_t>());
131 static_assert(test<std::uintmax_t>());
132 static_assert(test<std::uintptr_t>());
133 static_assert(test<std::size_t>());
134
135 test<unsigned char>();
136 test<unsigned short>();
137 test<unsigned int>();
138 test<unsigned long>();
139 test<unsigned long long>();
140#ifndef TEST_HAS_NO_INT128
141 test<__uint128_t>();
142#endif
143 test<std::uint8_t>();
144 test<std::uint16_t>();
145 test<std::uint32_t>();
146 test<std::uint64_t>();
147 test<std::uintmax_t>();
148 test<std::uintptr_t>();
149 test<std::size_t>();
150
151 return 0;
152}
153

source code of libcxx/test/std/numerics/bit/bitops.count/popcount.pass.cpp