Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / numerics / bit / bitops.count / countr_one.pass.cpp
blob8e8ef1d535a54ae9d93538eaa9b0b6238bb731f4
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 countr_one(T x) noexcept;
14 // Constraints: T is an unsigned integer type
15 // Returns: The number of consecutive 1 bits, starting from the least significant bit.
16 // [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
18 #include <bit>
19 #include <cassert>
20 #include <cstdint>
21 #include <limits>
22 #include <type_traits>
24 #include "test_macros.h"
26 struct A {};
27 enum E1 : unsigned char { rEd };
28 enum class E2 : unsigned char { red };
30 template <class T>
31 constexpr bool test()
33 ASSERT_SAME_TYPE(decltype(std::countr_one(T())), int);
34 ASSERT_NOEXCEPT(std::countr_one(T()));
35 T max = std::numeric_limits<T>::max();
37 assert(std::countr_one(T(0)) == 0);
38 assert(std::countr_one(T(1)) == 1);
39 assert(std::countr_one(T(2)) == 0);
40 assert(std::countr_one(T(3)) == 2);
41 assert(std::countr_one(T(4)) == 0);
42 assert(std::countr_one(T(5)) == 1);
43 assert(std::countr_one(T(6)) == 0);
44 assert(std::countr_one(T(7)) == 3);
45 assert(std::countr_one(T(8)) == 0);
46 assert(std::countr_one(T(9)) == 1);
47 assert(std::countr_one(T(126)) == 0);
48 assert(std::countr_one(T(127)) == 7);
49 assert(std::countr_one(T(128)) == 0);
50 assert(std::countr_one(T(max - 1)) == 0);
51 assert(std::countr_one(max) == std::numeric_limits<T>::digits);
53 #ifndef TEST_HAS_NO_INT128
54 if constexpr (std::is_same_v<T, __uint128_t>) {
55 T val = 128;
56 assert(std::countr_one(val-1) == 7);
57 assert(std::countr_one(val) == 0);
58 assert(std::countr_one(val+1) == 1);
59 val <<= 32;
60 assert(std::countr_one(val-1) == 39);
61 assert(std::countr_one(val) == 0);
62 assert(std::countr_one(val+1) == 1);
63 val <<= 60;
64 assert(std::countr_one(val-1) == 99);
65 assert(std::countr_one(val) == 0);
66 assert(std::countr_one(val+1) == 1);
68 #endif
70 return true;
73 int main(int, char**)
76 auto lambda = [](auto x) -> decltype(std::countr_one(x)) {};
77 using L = decltype(lambda);
79 static_assert(!std::is_invocable_v<L, signed char>);
80 static_assert(!std::is_invocable_v<L, short>);
81 static_assert(!std::is_invocable_v<L, int>);
82 static_assert(!std::is_invocable_v<L, long>);
83 static_assert(!std::is_invocable_v<L, long long>);
84 #ifndef TEST_HAS_NO_INT128
85 static_assert(!std::is_invocable_v<L, __int128_t>);
86 #endif
88 static_assert(!std::is_invocable_v<L, std::int8_t>);
89 static_assert(!std::is_invocable_v<L, std::int16_t>);
90 static_assert(!std::is_invocable_v<L, std::int32_t>);
91 static_assert(!std::is_invocable_v<L, std::int64_t>);
92 static_assert(!std::is_invocable_v<L, std::intmax_t>);
93 static_assert(!std::is_invocable_v<L, std::intptr_t>);
94 static_assert(!std::is_invocable_v<L, std::ptrdiff_t>);
96 static_assert(!std::is_invocable_v<L, bool>);
97 static_assert(!std::is_invocable_v<L, char>);
98 static_assert(!std::is_invocable_v<L, wchar_t>);
99 #ifndef TEST_HAS_NO_CHAR8_T
100 static_assert(!std::is_invocable_v<L, char8_t>);
101 #endif
102 static_assert(!std::is_invocable_v<L, char16_t>);
103 static_assert(!std::is_invocable_v<L, char32_t>);
105 static_assert(!std::is_invocable_v<L, A>);
106 static_assert(!std::is_invocable_v<L, A*>);
107 static_assert(!std::is_invocable_v<L, E1>);
108 static_assert(!std::is_invocable_v<L, E2>);
111 static_assert(test<unsigned char>());
112 static_assert(test<unsigned short>());
113 static_assert(test<unsigned int>());
114 static_assert(test<unsigned long>());
115 static_assert(test<unsigned long long>());
116 #ifndef TEST_HAS_NO_INT128
117 static_assert(test<__uint128_t>());
118 #endif
119 static_assert(test<std::uint8_t>());
120 static_assert(test<std::uint16_t>());
121 static_assert(test<std::uint32_t>());
122 static_assert(test<std::uint64_t>());
123 static_assert(test<std::uintmax_t>());
124 static_assert(test<std::uintptr_t>());
125 static_assert(test<std::size_t>());
127 test<unsigned char>();
128 test<unsigned short>();
129 test<unsigned int>();
130 test<unsigned long>();
131 test<unsigned long long>();
132 #ifndef TEST_HAS_NO_INT128
133 test<__uint128_t>();
134 #endif
135 test<std::uint8_t>();
136 test<std::uint16_t>();
137 test<std::uint32_t>();
138 test<std::uint64_t>();
139 test<std::uintmax_t>();
140 test<std::uintptr_t>();
141 test<std::size_t>();
143 return 0;