xref: /llvm-project/libcxx/test/std/numerics/bit/bitops.count/countl_one.pass.cpp (revision d6832a611a7c4ec36f08b1cfe9af850dad32da2e)
1a5c3485aSMarshall Clow //===----------------------------------------------------------------------===//
2a5c3485aSMarshall Clow //
31ee41b41SNikolas Klauser // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41ee41b41SNikolas Klauser // See https://llvm.org/LICENSE.txt for license information.
51ee41b41SNikolas Klauser // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a5c3485aSMarshall Clow //
7a5c3485aSMarshall Clow //===----------------------------------------------------------------------===//
81ee41b41SNikolas Klauser 
931cbe0f2SLouis Dionne // UNSUPPORTED: c++03, c++11, c++14, c++17
10a5c3485aSMarshall Clow 
11a5c3485aSMarshall Clow // template <class T>
12a5c3485aSMarshall Clow //   constexpr int countl_one(T x) noexcept;
13a5c3485aSMarshall Clow 
14e130fbe2SArthur O'Dwyer // Constraints: T is an unsigned integer type
15a5c3485aSMarshall Clow // The number of consecutive 1 bits, starting from the most significant bit.
16a5c3485aSMarshall Clow //   [ Note: Returns N if x == std::numeric_limits<T>::max(). ]
17a5c3485aSMarshall Clow 
18a5c3485aSMarshall Clow #include <bit>
19e130fbe2SArthur O'Dwyer #include <cassert>
20*d6832a61SLouis Dionne #include <cstddef>
21a5c3485aSMarshall Clow #include <cstdint>
2286aac87fSNikolas Klauser #include <limits>
23a5c3485aSMarshall Clow #include <type_traits>
24a5c3485aSMarshall Clow 
25a5c3485aSMarshall Clow #include "test_macros.h"
26a5c3485aSMarshall Clow 
27e130fbe2SArthur O'Dwyer struct A {};
28a5c3485aSMarshall Clow enum       E1 : unsigned char { rEd };
29a5c3485aSMarshall Clow enum class E2 : unsigned char { red };
30a5c3485aSMarshall Clow 
31e130fbe2SArthur O'Dwyer template <class T>
32e130fbe2SArthur O'Dwyer constexpr bool test()
33a5c3485aSMarshall Clow {
34e130fbe2SArthur O'Dwyer     ASSERT_SAME_TYPE(decltype(std::countl_one(T())), int);
35e130fbe2SArthur O'Dwyer     ASSERT_NOEXCEPT(std::countl_one(T()));
36e130fbe2SArthur O'Dwyer     T max = std::numeric_limits<T>::max();
37e130fbe2SArthur O'Dwyer 
38e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(0)) == 0);
39e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(1)) == 0);
40e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(10)) == 0);
41e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(100)) == 0);
42e130fbe2SArthur O'Dwyer     assert(std::countl_one(max) == std::numeric_limits<T>::digits);
43e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 1)) == std::numeric_limits<T>::digits - 1);
44e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 2)) == std::numeric_limits<T>::digits - 2);
45e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 3)) == std::numeric_limits<T>::digits - 2);
46e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 4)) == std::numeric_limits<T>::digits - 3);
47e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 5)) == std::numeric_limits<T>::digits - 3);
48e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 6)) == std::numeric_limits<T>::digits - 3);
49e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 7)) == std::numeric_limits<T>::digits - 3);
50e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 8)) == std::numeric_limits<T>::digits - 4);
51e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 9)) == std::numeric_limits<T>::digits - 4);
52e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 126)) == std::numeric_limits<T>::digits - 7);
53e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 127)) == std::numeric_limits<T>::digits - 7);
54e130fbe2SArthur O'Dwyer     assert(std::countl_one(T(max - 128)) == std::numeric_limits<T>::digits - 8);
55e130fbe2SArthur O'Dwyer 
568f972cb0SMark de Wever #ifndef TEST_HAS_NO_INT128
57e130fbe2SArthur O'Dwyer     if constexpr (std::is_same_v<T, __uint128_t>) {
58e130fbe2SArthur O'Dwyer         T val = 128;
59e130fbe2SArthur O'Dwyer         assert(std::countl_one(~val) == 120);
60e130fbe2SArthur O'Dwyer         val <<= 32;
61e130fbe2SArthur O'Dwyer         assert(std::countl_one(~val) == 88);
62e130fbe2SArthur O'Dwyer         val <<= 60;
63e130fbe2SArthur O'Dwyer         assert(std::countl_one(~val) == 28);
64a5c3485aSMarshall Clow     }
65e130fbe2SArthur O'Dwyer #endif
66a5c3485aSMarshall Clow 
67e130fbe2SArthur O'Dwyer     return true;
68a5c3485aSMarshall Clow }
69a5c3485aSMarshall Clow 
70504bc07dSLouis Dionne int main(int, char**)
71a5c3485aSMarshall Clow {
72a5c3485aSMarshall Clow     {
73a5c3485aSMarshall Clow     auto lambda = [](auto x) -> decltype(std::countl_one(x)) {};
74a5c3485aSMarshall Clow     using L = decltype(lambda);
75a5c3485aSMarshall Clow 
76e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, signed char>);
77e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, short>);
78e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, int>);
79e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, long>);
80e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, long long>);
818f972cb0SMark de Wever #ifndef TEST_HAS_NO_INT128
82e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, __int128_t>);
83a5c3485aSMarshall Clow #endif
84a5c3485aSMarshall Clow 
85bd5d0feeSMark de Wever     static_assert(!std::is_invocable_v<L, std::int8_t>);
86bd5d0feeSMark de Wever     static_assert(!std::is_invocable_v<L, std::int16_t>);
87bd5d0feeSMark de Wever     static_assert(!std::is_invocable_v<L, std::int32_t>);
88bd5d0feeSMark de Wever     static_assert(!std::is_invocable_v<L, std::int64_t>);
8992fc93e1SMark de Wever     static_assert(!std::is_invocable_v<L, std::intmax_t>);
90d59a43feSMark de Wever     static_assert(!std::is_invocable_v<L, std::intptr_t>);
91d8681356SMark de Wever     static_assert(!std::is_invocable_v<L, std::ptrdiff_t>);
92e130fbe2SArthur O'Dwyer 
93e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, bool>);
94e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, char>);
95e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, wchar_t>);
969027887eSMark de Wever #ifndef TEST_HAS_NO_CHAR8_T
97e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, char8_t>);
98e130fbe2SArthur O'Dwyer #endif
99e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, char16_t>);
100e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, char32_t>);
101e130fbe2SArthur O'Dwyer 
102e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, A>);
103e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, A*>);
104e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, E1>);
105e130fbe2SArthur O'Dwyer     static_assert(!std::is_invocable_v<L, E2>);
106a5c3485aSMarshall Clow     }
107a5c3485aSMarshall Clow 
108e130fbe2SArthur O'Dwyer     static_assert(test<unsigned char>());
109e130fbe2SArthur O'Dwyer     static_assert(test<unsigned short>());
110e130fbe2SArthur O'Dwyer     static_assert(test<unsigned int>());
111e130fbe2SArthur O'Dwyer     static_assert(test<unsigned long>());
112e130fbe2SArthur O'Dwyer     static_assert(test<unsigned long long>());
1138f972cb0SMark de Wever #ifndef TEST_HAS_NO_INT128
114e130fbe2SArthur O'Dwyer     static_assert(test<__uint128_t>());
115a5c3485aSMarshall Clow #endif
116bd5d0feeSMark de Wever     static_assert(test<std::uint8_t>());
117bd5d0feeSMark de Wever     static_assert(test<std::uint16_t>());
118da79d6e1SMark de Wever     static_assert(test<std::uint32_t>());
119bd5d0feeSMark de Wever     static_assert(test<std::uint64_t>());
12092fc93e1SMark de Wever     static_assert(test<std::uintmax_t>());
121d59a43feSMark de Wever     static_assert(test<std::uintptr_t>());
122fb855eb9SMark de Wever     static_assert(test<std::size_t>());
123a5c3485aSMarshall Clow 
124e130fbe2SArthur O'Dwyer     test<unsigned char>();
125e130fbe2SArthur O'Dwyer     test<unsigned short>();
126e130fbe2SArthur O'Dwyer     test<unsigned int>();
127e130fbe2SArthur O'Dwyer     test<unsigned long>();
128e130fbe2SArthur O'Dwyer     test<unsigned long long>();
1298f972cb0SMark de Wever #ifndef TEST_HAS_NO_INT128
130e130fbe2SArthur O'Dwyer     test<__uint128_t>();
131a5c3485aSMarshall Clow #endif
132bd5d0feeSMark de Wever     test<std::uint8_t>();
133bd5d0feeSMark de Wever     test<std::uint16_t>();
134da79d6e1SMark de Wever     test<std::uint32_t>();
135bd5d0feeSMark de Wever     test<std::uint64_t>();
13692fc93e1SMark de Wever     test<std::uintmax_t>();
137d59a43feSMark de Wever     test<std::uintptr_t>();
138fb855eb9SMark de Wever     test<std::size_t>();
139a5c3485aSMarshall Clow 
140504bc07dSLouis Dionne     return 0;
141a5c3485aSMarshall Clow }
142