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 countr_one(T x) noexcept;
14 // Returns: The number of consecutive 1 bits, starting from the least significant bit.
15 // [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
17 // Remarks: This function shall not participate in overload resolution unless
18 // T is an unsigned integer type
22 #include <type_traits>
25 #include "test_macros.h"
28 enum E1
: unsigned char { rEd
};
29 enum class E2
: unsigned char { red
};
32 constexpr bool constexpr_test()
34 const int dig
= std::numeric_limits
<T
>::digits
;
35 return std::countr_one(T(0)) == 0
36 && std::countr_one(T(1)) == 1
37 && std::countr_one(T(2)) == 0
38 && std::countr_one(T(3)) == 2
39 && std::countr_one(T(4)) == 0
40 && std::countr_one(T(5)) == 1
41 && std::countr_one(T(6)) == 0
42 && std::countr_one(T(7)) == 3
43 && std::countr_one(T(8)) == 0
44 && std::countr_one(T(9)) == 1
45 && std::countr_one(std::numeric_limits
<T
>::max()) == dig
53 ASSERT_SAME_TYPE(int, decltype(std::countr_one(T(0))));
54 ASSERT_NOEXCEPT( std::countr_one(T(0)));
56 assert( std::countr_one(T(121)) == 1);
57 assert( std::countr_one(T(122)) == 0);
58 assert( std::countr_one(T(123)) == 2);
59 assert( std::countr_one(T(124)) == 0);
60 assert( std::countr_one(T(125)) == 1);
61 assert( std::countr_one(T(126)) == 0);
62 assert( std::countr_one(T(127)) == 7);
63 assert( std::countr_one(T(128)) == 0);
64 assert( std::countr_one(T(129)) == 1);
65 assert( std::countr_one(T(130)) == 0);
72 auto lambda
= [](auto x
) -> decltype(std::countr_one(x
)) {};
73 using L
= decltype(lambda
);
75 static_assert( std::is_invocable_v
<L
, unsigned char>, "");
76 static_assert( std::is_invocable_v
<L
, unsigned int>, "");
77 static_assert( std::is_invocable_v
<L
, unsigned long>, "");
78 static_assert( std::is_invocable_v
<L
, unsigned long long>, "");
80 static_assert( std::is_invocable_v
<L
, uint8_t>, "");
81 static_assert( std::is_invocable_v
<L
, uint16_t>, "");
82 static_assert( std::is_invocable_v
<L
, uint32_t>, "");
83 static_assert( std::is_invocable_v
<L
, uint64_t>, "");
84 static_assert( std::is_invocable_v
<L
, size_t>, "");
86 static_assert( std::is_invocable_v
<L
, uintmax_t>, "");
87 static_assert( std::is_invocable_v
<L
, uintptr_t>, "");
90 static_assert(!std::is_invocable_v
<L
, int>, "");
91 static_assert(!std::is_invocable_v
<L
, signed int>, "");
92 static_assert(!std::is_invocable_v
<L
, long>, "");
93 static_assert(!std::is_invocable_v
<L
, long long>, "");
95 static_assert(!std::is_invocable_v
<L
, int8_t>, "");
96 static_assert(!std::is_invocable_v
<L
, int16_t>, "");
97 static_assert(!std::is_invocable_v
<L
, int32_t>, "");
98 static_assert(!std::is_invocable_v
<L
, int64_t>, "");
99 static_assert(!std::is_invocable_v
<L
, ptrdiff_t>, "");
101 static_assert(!std::is_invocable_v
<L
, bool>, "");
102 static_assert(!std::is_invocable_v
<L
, signed char>, "");
103 static_assert(!std::is_invocable_v
<L
, char16_t
>, "");
104 static_assert(!std::is_invocable_v
<L
, char32_t
>, "");
106 #ifndef _LIBCPP_HAS_NO_INT128
107 static_assert( std::is_invocable_v
<L
, __uint128_t
>, "");
108 static_assert(!std::is_invocable_v
<L
, __int128_t
>, "");
111 static_assert(!std::is_invocable_v
<L
, A
>, "");
112 static_assert(!std::is_invocable_v
<L
, E1
>, "");
113 static_assert(!std::is_invocable_v
<L
, E2
>, "");
116 static_assert(constexpr_test
<unsigned char>(), "");
117 static_assert(constexpr_test
<unsigned short>(), "");
118 static_assert(constexpr_test
<unsigned>(), "");
119 static_assert(constexpr_test
<unsigned long>(), "");
120 static_assert(constexpr_test
<unsigned long long>(), "");
122 static_assert(constexpr_test
<uint8_t>(), "");
123 static_assert(constexpr_test
<uint16_t>(), "");
124 static_assert(constexpr_test
<uint32_t>(), "");
125 static_assert(constexpr_test
<uint64_t>(), "");
126 static_assert(constexpr_test
<size_t>(), "");
127 static_assert(constexpr_test
<uintmax_t>(), "");
128 static_assert(constexpr_test
<uintptr_t>(), "");
130 #ifndef _LIBCPP_HAS_NO_INT128
131 static_assert(constexpr_test
<__uint128_t
>(), "");
135 runtime_test
<unsigned char>();
136 runtime_test
<unsigned>();
137 runtime_test
<unsigned short>();
138 runtime_test
<unsigned long>();
139 runtime_test
<unsigned long long>();
141 runtime_test
<uint8_t>();
142 runtime_test
<uint16_t>();
143 runtime_test
<uint32_t>();
144 runtime_test
<uint64_t>();
145 runtime_test
<size_t>();
146 runtime_test
<uintmax_t>();
147 runtime_test
<uintptr_t>();
149 #ifndef _LIBCPP_HAS_NO_INT128
150 runtime_test
<__uint128_t
>();
153 __uint128_t val
= 128;
156 assert( std::countr_one(val
-1) == 39);
157 assert( std::countr_one(val
) == 0);
158 assert( std::countr_one(val
+1) == 1);
160 assert( std::countr_one(val
-1) == 41);
161 assert( std::countr_one(val
) == 0);
162 assert( std::countr_one(val
+1) == 1);
164 assert( std::countr_one(val
-1) == 44);
165 assert( std::countr_one(val
) == 0);
166 assert( std::countr_one(val
+1) == 1);