1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
12 // constexpr int popcount(T x) noexcept;
14 // Returns: The number of bits set to one in the value of x.
16 // Remarks: This function shall not participate in overload resolution unless
17 // T is an unsigned integer type
21 #include <type_traits>
24 #include "test_macros.h"
27 enum E1
: unsigned char { rEd
};
28 enum class E2
: unsigned char { red
};
31 constexpr bool constexpr_test()
33 return std::popcount(T(0)) == 0
34 && std::popcount(T(1)) == 1
35 && std::popcount(T(2)) == 1
36 && std::popcount(T(3)) == 2
37 && std::popcount(T(4)) == 1
38 && std::popcount(T(5)) == 2
39 && std::popcount(T(6)) == 2
40 && std::popcount(T(7)) == 3
41 && std::popcount(T(8)) == 1
42 && std::popcount(T(9)) == 2
43 && std::popcount(std::numeric_limits
<T
>::max()) == std::numeric_limits
<T
>::digits
51 ASSERT_SAME_TYPE(int, decltype(std::popcount(T(0))));
52 ASSERT_NOEXCEPT( std::popcount(T(0)));
54 assert( std::popcount(T(121)) == 5);
55 assert( std::popcount(T(122)) == 5);
56 assert( std::popcount(T(123)) == 6);
57 assert( std::popcount(T(124)) == 5);
58 assert( std::popcount(T(125)) == 6);
59 assert( std::popcount(T(126)) == 6);
60 assert( std::popcount(T(127)) == 7);
61 assert( std::popcount(T(128)) == 1);
62 assert( std::popcount(T(129)) == 2);
63 assert( std::popcount(T(130)) == 2);
70 auto lambda
= [](auto x
) -> decltype(std::popcount(x
)) {};
71 using L
= decltype(lambda
);
73 static_assert( std::is_invocable_v
<L
, unsigned char>, "");
74 static_assert( std::is_invocable_v
<L
, unsigned int>, "");
75 static_assert( std::is_invocable_v
<L
, unsigned long>, "");
76 static_assert( std::is_invocable_v
<L
, unsigned long long>, "");
78 static_assert( std::is_invocable_v
<L
, uint8_t>, "");
79 static_assert( std::is_invocable_v
<L
, uint16_t>, "");
80 static_assert( std::is_invocable_v
<L
, uint32_t>, "");
81 static_assert( std::is_invocable_v
<L
, uint64_t>, "");
82 static_assert( std::is_invocable_v
<L
, size_t>, "");
84 static_assert( std::is_invocable_v
<L
, uintmax_t>, "");
85 static_assert( std::is_invocable_v
<L
, uintptr_t>, "");
88 static_assert(!std::is_invocable_v
<L
, int>, "");
89 static_assert(!std::is_invocable_v
<L
, signed int>, "");
90 static_assert(!std::is_invocable_v
<L
, long>, "");
91 static_assert(!std::is_invocable_v
<L
, long long>, "");
93 static_assert(!std::is_invocable_v
<L
, int8_t>, "");
94 static_assert(!std::is_invocable_v
<L
, int16_t>, "");
95 static_assert(!std::is_invocable_v
<L
, int32_t>, "");
96 static_assert(!std::is_invocable_v
<L
, int64_t>, "");
97 static_assert(!std::is_invocable_v
<L
, ptrdiff_t>, "");
99 static_assert(!std::is_invocable_v
<L
, bool>, "");
100 static_assert(!std::is_invocable_v
<L
, signed char>, "");
101 static_assert(!std::is_invocable_v
<L
, char16_t
>, "");
102 static_assert(!std::is_invocable_v
<L
, char32_t
>, "");
104 #ifndef _LIBCPP_HAS_NO_INT128
105 static_assert( std::is_invocable_v
<L
, __uint128_t
>, "");
106 static_assert(!std::is_invocable_v
<L
, __int128_t
>, "");
109 static_assert(!std::is_invocable_v
<L
, A
>, "");
110 static_assert(!std::is_invocable_v
<L
, E1
>, "");
111 static_assert(!std::is_invocable_v
<L
, E2
>, "");
114 static_assert(constexpr_test
<unsigned char>(), "");
115 static_assert(constexpr_test
<unsigned short>(), "");
116 static_assert(constexpr_test
<unsigned>(), "");
117 static_assert(constexpr_test
<unsigned long>(), "");
118 static_assert(constexpr_test
<unsigned long long>(), "");
120 static_assert(constexpr_test
<uint8_t>(), "");
121 static_assert(constexpr_test
<uint16_t>(), "");
122 static_assert(constexpr_test
<uint32_t>(), "");
123 static_assert(constexpr_test
<uint64_t>(), "");
124 static_assert(constexpr_test
<size_t>(), "");
125 static_assert(constexpr_test
<uintmax_t>(), "");
126 static_assert(constexpr_test
<uintptr_t>(), "");
128 #ifndef _LIBCPP_HAS_NO_INT128
129 static_assert(constexpr_test
<__uint128_t
>(), "");
133 runtime_test
<unsigned char>();
134 runtime_test
<unsigned>();
135 runtime_test
<unsigned short>();
136 runtime_test
<unsigned long>();
137 runtime_test
<unsigned long long>();
139 runtime_test
<uint8_t>();
140 runtime_test
<uint16_t>();
141 runtime_test
<uint32_t>();
142 runtime_test
<uint64_t>();
143 runtime_test
<size_t>();
144 runtime_test
<uintmax_t>();
145 runtime_test
<uintptr_t>();
147 #ifndef _LIBCPP_HAS_NO_INT128
148 runtime_test
<__uint128_t
>();
151 __uint128_t val
= 128;
154 assert( std::popcount(val
-1) == 39);
155 assert( std::popcount(val
) == 1);
156 assert( std::popcount(val
+1) == 2);
158 assert( std::popcount(val
-1) == 41);
159 assert( std::popcount(val
) == 1);
160 assert( std::popcount(val
+1) == 2);
162 assert( std::popcount(val
-1) == 44);
163 assert( std::popcount(val
) == 1);
164 assert( std::popcount(val
+1) == 2);