Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / numerics / bit / bitops.count / popcount.pass.cpp
blobb8759c440432eeab309b14b47abfad41c857876a
1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 // template <class T>
12 // constexpr int popcount(T x) noexcept;
14 // Constraints: T is an unsigned integer type
15 // Returns: The number of bits set to one in the value of x.
17 #include <bit>
18 #include <cassert>
19 #include <cstdint>
20 #include <limits>
21 #include <type_traits>
23 #include "test_macros.h"
25 struct A {};
26 enum E1 : unsigned char { rEd };
27 enum class E2 : unsigned char { red };
29 template <class T>
30 constexpr bool test()
32 ASSERT_SAME_TYPE(decltype(std::popcount(T())), int);
33 ASSERT_NOEXCEPT(std::popcount(T()));
34 T max = std::numeric_limits<T>::max();
36 assert(std::popcount(T(0)) == 0);
37 assert(std::popcount(T(1)) == 1);
38 assert(std::popcount(T(2)) == 1);
39 assert(std::popcount(T(3)) == 2);
40 assert(std::popcount(T(4)) == 1);
41 assert(std::popcount(T(5)) == 2);
42 assert(std::popcount(T(6)) == 2);
43 assert(std::popcount(T(7)) == 3);
44 assert(std::popcount(T(8)) == 1);
45 assert(std::popcount(T(9)) == 2);
46 assert(std::popcount(T(121)) == 5);
47 assert(std::popcount(T(127)) == 7);
48 assert(std::popcount(T(128)) == 1);
49 assert(std::popcount(T(130)) == 2);
50 assert(std::popcount(T(max >> 1)) == std::numeric_limits<T>::digits - 1);
51 assert(std::popcount(T(max - 1)) == std::numeric_limits<T>::digits - 1);
52 assert(std::popcount(max) == std::numeric_limits<T>::digits);
54 #ifndef TEST_HAS_NO_INT128
55 if constexpr (std::is_same_v<T, __uint128_t>) {
56 T val = 128;
57 assert(std::popcount(val-1) == 7);
58 assert(std::popcount(val) == 1);
59 assert(std::popcount(val+1) == 2);
60 val <<= 32;
61 assert(std::popcount(val-1) == 39);
62 assert(std::popcount(val) == 1);
63 assert(std::popcount(val+1) == 2);
64 val <<= 60;
65 assert(std::popcount(val-1) == 99);
66 assert(std::popcount(val) == 1);
67 assert(std::popcount(val+1) == 2);
69 T x = T(1) << 63;
70 T y = T(1) << 64;
71 assert(std::popcount(x) == 1);
72 assert(std::popcount(y) == 1);
73 assert(std::popcount(x+y) == 2);
75 #endif
77 return true;
80 int main(int, char**)
83 auto lambda = [](auto x) -> decltype(std::popcount(x)) {};
84 using L = decltype(lambda);
86 static_assert(!std::is_invocable_v<L, signed char>);
87 static_assert(!std::is_invocable_v<L, short>);
88 static_assert(!std::is_invocable_v<L, int>);
89 static_assert(!std::is_invocable_v<L, long>);
90 static_assert(!std::is_invocable_v<L, long long>);
91 #ifndef TEST_HAS_NO_INT128
92 static_assert(!std::is_invocable_v<L, __int128_t>);
93 #endif
95 static_assert(!std::is_invocable_v<L, std::int8_t>);
96 static_assert(!std::is_invocable_v<L, std::int16_t>);
97 static_assert(!std::is_invocable_v<L, std::int32_t>);
98 static_assert(!std::is_invocable_v<L, std::int64_t>);
99 static_assert(!std::is_invocable_v<L, std::intmax_t>);
100 static_assert(!std::is_invocable_v<L, std::intptr_t>);
101 static_assert(!std::is_invocable_v<L, std::ptrdiff_t>);
103 static_assert(!std::is_invocable_v<L, bool>);
104 static_assert(!std::is_invocable_v<L, char>);
105 static_assert(!std::is_invocable_v<L, wchar_t>);
106 #ifndef TEST_HAS_NO_CHAR8_T
107 static_assert(!std::is_invocable_v<L, char8_t>);
108 #endif
109 static_assert(!std::is_invocable_v<L, char16_t>);
110 static_assert(!std::is_invocable_v<L, char32_t>);
112 static_assert(!std::is_invocable_v<L, A>);
113 static_assert(!std::is_invocable_v<L, A*>);
114 static_assert(!std::is_invocable_v<L, E1>);
115 static_assert(!std::is_invocable_v<L, E2>);
118 static_assert(test<unsigned char>());
119 static_assert(test<unsigned short>());
120 static_assert(test<unsigned int>());
121 static_assert(test<unsigned long>());
122 static_assert(test<unsigned long long>());
123 #ifndef TEST_HAS_NO_INT128
124 static_assert(test<__uint128_t>());
125 #endif
126 static_assert(test<std::uint8_t>());
127 static_assert(test<std::uint16_t>());
128 static_assert(test<std::uint32_t>());
129 static_assert(test<std::uint64_t>());
130 static_assert(test<std::uintmax_t>());
131 static_assert(test<std::uintptr_t>());
132 static_assert(test<std::size_t>());
134 test<unsigned char>();
135 test<unsigned short>();
136 test<unsigned int>();
137 test<unsigned long>();
138 test<unsigned long long>();
139 #ifndef TEST_HAS_NO_INT128
140 test<__uint128_t>();
141 #endif
142 test<std::uint8_t>();
143 test<std::uint16_t>();
144 test<std::uint32_t>();
145 test<std::uint64_t>();
146 test<std::uintmax_t>();
147 test<std::uintptr_t>();
148 test<std::size_t>();
150 return 0;