xref: /llvm-project/libcxx/test/std/numerics/bit/bitops.count/countr_zero.pass.cpp (revision d6832a611a7c4ec36f08b1cfe9af850dad32da2e)
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // template <class T>
12 //   constexpr int countr_zero(T x) noexcept;
13 
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. ]
17 
18 #include <bit>
19 #include <cassert>
20 #include <cstddef>
21 #include <cstdint>
22 #include <limits>
23 #include <type_traits>
24 
25 #include "test_macros.h"
26 
27 struct A {};
28 enum       E1 : unsigned char { rEd };
29 enum class E2 : unsigned char { red };
30 
31 template <class T>
32 constexpr bool test()
33 {
34     ASSERT_SAME_TYPE(decltype(std::countr_zero(T())), int);
35     ASSERT_NOEXCEPT(std::countr_zero(T()));
36     T max = std::numeric_limits<T>::max();
37 
38     assert(std::countr_zero(T(0)) == std::numeric_limits<T>::digits);
39     assert(std::countr_zero(T(1)) == 0);
40     assert(std::countr_zero(T(2)) == 1);
41     assert(std::countr_zero(T(3)) == 0);
42     assert(std::countr_zero(T(4)) == 2);
43     assert(std::countr_zero(T(5)) == 0);
44     assert(std::countr_zero(T(6)) == 1);
45     assert(std::countr_zero(T(7)) == 0);
46     assert(std::countr_zero(T(8)) == 3);
47     assert(std::countr_zero(T(9)) == 0);
48     assert(std::countr_zero(T(126)) == 1);
49     assert(std::countr_zero(T(127)) == 0);
50     assert(std::countr_zero(T(128)) == 7);
51     assert(std::countr_zero(T(129)) == 0);
52     assert(std::countr_zero(T(130)) == 1);
53     assert(std::countr_zero(max) == 0);
54 
55 #ifndef TEST_HAS_NO_INT128
56     if constexpr (std::is_same_v<T, __uint128_t>) {
57         T val = T(128) << 32;
58         assert(std::countr_zero(val-1) ==  0);
59         assert(std::countr_zero(val)   == 39);
60         assert(std::countr_zero(val+1) ==  0);
61         val <<= 60;
62         assert(std::countr_zero(val-1) ==  0);
63         assert(std::countr_zero(val)   == 99);
64         assert(std::countr_zero(val+1) ==  0);
65     }
66 #endif
67 
68     return true;
69 }
70 
71 int main(int, char**)
72 {
73     {
74     auto lambda = [](auto x) -> decltype(std::countr_zero(x)) {};
75     using L = decltype(lambda);
76 
77     static_assert(!std::is_invocable_v<L, signed char>);
78     static_assert(!std::is_invocable_v<L, short>);
79     static_assert(!std::is_invocable_v<L, int>);
80     static_assert(!std::is_invocable_v<L, long>);
81     static_assert(!std::is_invocable_v<L, long long>);
82 #ifndef TEST_HAS_NO_INT128
83     static_assert(!std::is_invocable_v<L, __int128_t>);
84 #endif
85 
86     static_assert(!std::is_invocable_v<L, std::int8_t>);
87     static_assert(!std::is_invocable_v<L, std::int16_t>);
88     static_assert(!std::is_invocable_v<L, std::int32_t>);
89     static_assert(!std::is_invocable_v<L, std::int64_t>);
90     static_assert(!std::is_invocable_v<L, std::intmax_t>);
91     static_assert(!std::is_invocable_v<L, std::intptr_t>);
92     static_assert(!std::is_invocable_v<L, std::ptrdiff_t>);
93 
94     static_assert(!std::is_invocable_v<L, bool>);
95     static_assert(!std::is_invocable_v<L, char>);
96     static_assert(!std::is_invocable_v<L, wchar_t>);
97 #ifndef TEST_HAS_NO_CHAR8_T
98     static_assert(!std::is_invocable_v<L, char8_t>);
99 #endif
100     static_assert(!std::is_invocable_v<L, char16_t>);
101     static_assert(!std::is_invocable_v<L, char32_t>);
102 
103     static_assert(!std::is_invocable_v<L, A>);
104     static_assert(!std::is_invocable_v<L, A*>);
105     static_assert(!std::is_invocable_v<L, E1>);
106     static_assert(!std::is_invocable_v<L, E2>);
107     }
108 
109     static_assert(test<unsigned char>());
110     static_assert(test<unsigned short>());
111     static_assert(test<unsigned int>());
112     static_assert(test<unsigned long>());
113     static_assert(test<unsigned long long>());
114 #ifndef TEST_HAS_NO_INT128
115     static_assert(test<__uint128_t>());
116 #endif
117     static_assert(test<std::uint8_t>());
118     static_assert(test<std::uint16_t>());
119     static_assert(test<std::uint32_t>());
120     static_assert(test<std::uint64_t>());
121     static_assert(test<std::uintmax_t>());
122     static_assert(test<std::uintptr_t>());
123     static_assert(test<std::size_t>());
124 
125     test<unsigned char>();
126     test<unsigned short>();
127     test<unsigned int>();
128     test<unsigned long>();
129     test<unsigned long long>();
130 #ifndef TEST_HAS_NO_INT128
131     test<__uint128_t>();
132 #endif
133     test<std::uint8_t>();
134     test<std::uint16_t>();
135     test<std::uint32_t>();
136     test<std::uint64_t>();
137     test<std::uintmax_t>();
138     test<std::uintptr_t>();
139     test<std::size_t>();
140 
141     return 0;
142 }
143