Debian Codegraph @

libstdc++-v3/testsuite/26_numerics/bit/bit.count/countl_zero.cc

raw
1 // Copyright (C) 2018-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do compile { target c++20 } }
19
20 #include <bit>
21 #include <limits>
22
23 template<typename UInt>
24 constexpr auto
25 test(UInt x)
26 -> decltype(std::countl_zero(x))
27 {
28 static_assert( noexcept(std::countl_zero(x)) );
29
30 constexpr unsigned digits = std::numeric_limits<UInt>::digits;
31
32 static_assert( std::countl_zero((UInt)0) == digits );
33 static_assert( std::countl_zero((UInt)-1) == 0 );
34
35 static_assert( std::countl_zero((UInt)1) == digits - 1 );
36 static_assert( std::countl_zero((UInt)3) == digits - 2 );
37 static_assert( std::countl_zero((UInt)0b0101'1010) == digits - 7 );
38
39 if constexpr (std::numeric_limits<UInt>::digits > 8)
40 {
41 static_assert( std::countl_zero((UInt)(1u << 8)) == digits - 9 );
42 static_assert( std::countl_zero((UInt)(3u << 9)) == digits - 11 );
43 }
44
45 if constexpr (std::numeric_limits<UInt>::digits > 64)
46 {
47 static_assert( std::countl_zero((UInt)3 << 70) == digits - 72 );
48 }
49
50 return true;
51 }
52
53 static_assert( test( (unsigned char)0 ) );
54 static_assert( test( (unsigned short)0 ) );
55 static_assert( test( (unsigned int)0 ) );
56 static_assert( test( (unsigned long)0 ) );
57 static_assert( test( (unsigned long long)0 ) );
58
59 // std::countl_zero(T) shall not participate in overload resolution
60 // unless T is an unsigned integer type.
61 struct X { constexpr bool did_not_match() { return true; } };
62 constexpr X test(...) { return X{}; }
63 static_assert( test( (bool)0 ).did_not_match() );
64 static_assert( test( (char)0 ).did_not_match() );
65 static_assert( test( (int)0 ).did_not_match() );
66 static_assert( test( (char16_t)0 ).did_not_match() );
67 static_assert( test( (float)0 ).did_not_match() );
68 static_assert( test( (void*)0 ).did_not_match() );
69 static_assert( test( X{} ).did_not_match() );
70 enum E : unsigned { e };
71 static_assert( test( e ).did_not_match() );
72
73 #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
74 static_assert( test( (unsigned __int128)0 ) );
75 static_assert( test( (__int128)0 ).did_not_match() );
76 #endif
77 #if defined(__GLIBCXX_TYPE_INT_N_0)
78 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
79 static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
80 #endif
81 #if defined(__GLIBCXX_TYPE_INT_N_1)
82 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
83 static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
84 #endif
85 #if defined(__GLIBCXX_TYPE_INT_N_2)
86 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
87 static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
88 #endif
89 #if defined(__GLIBCXX_TYPE_INT_N_3)
90 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) );
91 static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() );
92 #endif
93
94 #include <cstddef>
95 static_assert( test( (std::byte)0 ).did_not_match() );