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 countl_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::countl_zero(T())), int);
34 ASSERT_NOEXCEPT(std::countl_zero(T()));
35 T max
= std::numeric_limits
<T
>::max();
36 int dig
= std::numeric_limits
<T
>::digits
;
38 assert(std::countl_zero(T(0)) == dig
);
39 assert(std::countl_zero(T(1)) == dig
- 1);
40 assert(std::countl_zero(T(2)) == dig
- 2);
41 assert(std::countl_zero(T(3)) == dig
- 2);
42 assert(std::countl_zero(T(4)) == dig
- 3);
43 assert(std::countl_zero(T(5)) == dig
- 3);
44 assert(std::countl_zero(T(6)) == dig
- 3);
45 assert(std::countl_zero(T(7)) == dig
- 3);
46 assert(std::countl_zero(T(8)) == dig
- 4);
47 assert(std::countl_zero(T(9)) == dig
- 4);
48 assert(std::countl_zero(T(127)) == dig
- 7);
49 assert(std::countl_zero(T(128)) == dig
- 8);
50 assert(std::countl_zero(max
) == 0);
52 #ifndef TEST_HAS_NO_INT128
53 if constexpr (std::is_same_v
<T
, __uint128_t
>) {
55 assert(std::countl_zero(val
-1) == 89);
56 assert(std::countl_zero(val
) == 88);
57 assert(std::countl_zero(val
+1) == 88);
59 assert(std::countl_zero(val
-1) == 29);
60 assert(std::countl_zero(val
) == 28);
61 assert(std::countl_zero(val
+1) == 28);
71 auto lambda
= [](auto x
) -> decltype(std::countl_zero(x
)) {};
72 using L
= decltype(lambda
);
74 static_assert(!std::is_invocable_v
<L
, signed char>);
75 static_assert(!std::is_invocable_v
<L
, short>);
76 static_assert(!std::is_invocable_v
<L
, int>);
77 static_assert(!std::is_invocable_v
<L
, long>);
78 static_assert(!std::is_invocable_v
<L
, long long>);
79 #ifndef TEST_HAS_NO_INT128
80 static_assert(!std::is_invocable_v
<L
, __int128_t
>);
83 static_assert(!std::is_invocable_v
<L
, std::int8_t>);
84 static_assert(!std::is_invocable_v
<L
, std::int16_t>);
85 static_assert(!std::is_invocable_v
<L
, std::int32_t>);
86 static_assert(!std::is_invocable_v
<L
, std::int64_t>);
87 static_assert(!std::is_invocable_v
<L
, std::intmax_t>);
88 static_assert(!std::is_invocable_v
<L
, std::intptr_t>);
89 static_assert(!std::is_invocable_v
<L
, std::ptrdiff_t>);
91 static_assert(!std::is_invocable_v
<L
, bool>);
92 static_assert(!std::is_invocable_v
<L
, char>);
93 static_assert(!std::is_invocable_v
<L
, wchar_t>);
94 #ifndef TEST_HAS_NO_CHAR8_T
95 static_assert(!std::is_invocable_v
<L
, char8_t
>);
97 static_assert(!std::is_invocable_v
<L
, char16_t
>);
98 static_assert(!std::is_invocable_v
<L
, char32_t
>);
100 static_assert(!std::is_invocable_v
<L
, A
>);
101 static_assert(!std::is_invocable_v
<L
, A
*>);
102 static_assert(!std::is_invocable_v
<L
, E1
>);
103 static_assert(!std::is_invocable_v
<L
, E2
>);
106 static_assert(test
<unsigned char>());
107 static_assert(test
<unsigned short>());
108 static_assert(test
<unsigned int>());
109 static_assert(test
<unsigned long>());
110 static_assert(test
<unsigned long long>());
111 #ifndef TEST_HAS_NO_INT128
112 static_assert(test
<__uint128_t
>());
114 static_assert(test
<std::uint8_t>());
115 static_assert(test
<std::uint16_t>());
116 static_assert(test
<std::uint32_t>());
117 static_assert(test
<std::uint64_t>());
118 static_assert(test
<std::uintmax_t>());
119 static_assert(test
<std::uintptr_t>());
120 static_assert(test
<std::size_t>());
122 test
<unsigned char>();
123 test
<unsigned short>();
124 test
<unsigned int>();
125 test
<unsigned long>();
126 test
<unsigned long long>();
127 #ifndef TEST_HAS_NO_INT128
130 test
<std::uint8_t>();
131 test
<std::uint16_t>();
132 test
<std::uint32_t>();
133 test
<std::uint64_t>();
134 test
<std::uintmax_t>();
135 test
<std::uintptr_t>();