1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // constexpr int countr_zero(T x) noexcept;
14 // Constraints: T is an unsigned integer type
15 // Returns: The number of consecutive 0 bits, starting from the most significant bit.
16 // [ Note: Returns N if x == 0. ]
22 #include <type_traits>
24 #include "test_macros.h"
27 enum E1
: unsigned char { rEd
};
28 enum class E2
: unsigned char { red
};
33 ASSERT_SAME_TYPE(decltype(std::countr_zero(T())), int);
34 ASSERT_NOEXCEPT(std::countr_zero(T()));
35 T max
= std::numeric_limits
<T
>::max();
37 assert(std::countr_zero(T(0)) == std::numeric_limits
<T
>::digits
);
38 assert(std::countr_zero(T(1)) == 0);
39 assert(std::countr_zero(T(2)) == 1);
40 assert(std::countr_zero(T(3)) == 0);
41 assert(std::countr_zero(T(4)) == 2);
42 assert(std::countr_zero(T(5)) == 0);
43 assert(std::countr_zero(T(6)) == 1);
44 assert(std::countr_zero(T(7)) == 0);
45 assert(std::countr_zero(T(8)) == 3);
46 assert(std::countr_zero(T(9)) == 0);
47 assert(std::countr_zero(T(126)) == 1);
48 assert(std::countr_zero(T(127)) == 0);
49 assert(std::countr_zero(T(128)) == 7);
50 assert(std::countr_zero(T(129)) == 0);
51 assert(std::countr_zero(T(130)) == 1);
52 assert(std::countr_zero(max
) == 0);
54 #ifndef TEST_HAS_NO_INT128
55 if constexpr (std::is_same_v
<T
, __uint128_t
>) {
57 assert(std::countr_zero(val
-1) == 0);
58 assert(std::countr_zero(val
) == 39);
59 assert(std::countr_zero(val
+1) == 0);
61 assert(std::countr_zero(val
-1) == 0);
62 assert(std::countr_zero(val
) == 99);
63 assert(std::countr_zero(val
+1) == 0);
73 auto lambda
= [](auto x
) -> decltype(std::countr_zero(x
)) {};
74 using L
= decltype(lambda
);
76 static_assert(!std::is_invocable_v
<L
, signed char>);
77 static_assert(!std::is_invocable_v
<L
, short>);
78 static_assert(!std::is_invocable_v
<L
, int>);
79 static_assert(!std::is_invocable_v
<L
, long>);
80 static_assert(!std::is_invocable_v
<L
, long long>);
81 #ifndef TEST_HAS_NO_INT128
82 static_assert(!std::is_invocable_v
<L
, __int128_t
>);
85 static_assert(!std::is_invocable_v
<L
, std::int8_t>);
86 static_assert(!std::is_invocable_v
<L
, std::int16_t>);
87 static_assert(!std::is_invocable_v
<L
, std::int32_t>);
88 static_assert(!std::is_invocable_v
<L
, std::int64_t>);
89 static_assert(!std::is_invocable_v
<L
, std::intmax_t>);
90 static_assert(!std::is_invocable_v
<L
, std::intptr_t>);
91 static_assert(!std::is_invocable_v
<L
, std::ptrdiff_t>);
93 static_assert(!std::is_invocable_v
<L
, bool>);
94 static_assert(!std::is_invocable_v
<L
, char>);
95 static_assert(!std::is_invocable_v
<L
, wchar_t>);
96 #ifndef TEST_HAS_NO_CHAR8_T
97 static_assert(!std::is_invocable_v
<L
, char8_t
>);
99 static_assert(!std::is_invocable_v
<L
, char16_t
>);
100 static_assert(!std::is_invocable_v
<L
, char32_t
>);
102 static_assert(!std::is_invocable_v
<L
, A
>);
103 static_assert(!std::is_invocable_v
<L
, A
*>);
104 static_assert(!std::is_invocable_v
<L
, E1
>);
105 static_assert(!std::is_invocable_v
<L
, E2
>);
108 static_assert(test
<unsigned char>());
109 static_assert(test
<unsigned short>());
110 static_assert(test
<unsigned int>());
111 static_assert(test
<unsigned long>());
112 static_assert(test
<unsigned long long>());
113 #ifndef TEST_HAS_NO_INT128
114 static_assert(test
<__uint128_t
>());
116 static_assert(test
<std::uint8_t>());
117 static_assert(test
<std::uint16_t>());
118 static_assert(test
<std::uint32_t>());
119 static_assert(test
<std::uint64_t>());
120 static_assert(test
<std::uintmax_t>());
121 static_assert(test
<std::uintptr_t>());
122 static_assert(test
<std::size_t>());
124 test
<unsigned char>();
125 test
<unsigned short>();
126 test
<unsigned int>();
127 test
<unsigned long>();
128 test
<unsigned long long>();
129 #ifndef TEST_HAS_NO_INT128
132 test
<std::uint8_t>();
133 test
<std::uint16_t>();
134 test
<std::uint32_t>();
135 test
<std::uint64_t>();
136 test
<std::uintmax_t>();
137 test
<std::uintptr_t>();