]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/bit/bit.count/countr_one.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.count / countr_one.cc
1 // Copyright (C) 2018-2022 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-options "-std=gnu++2a" }
19 // { dg-do compile { target c++2a } }
20
21 #include <bit>
22 #include <limits>
23
24 template<typename UInt>
25 constexpr auto
26 test(UInt x)
27 -> decltype(std::countr_one(x))
28 {
29 static_assert( noexcept(std::countr_one(x)) );
30
31 constexpr unsigned digits = std::numeric_limits<UInt>::digits;
32
33 static_assert( std::countr_one((UInt)0) == 0 );
34 static_assert( std::countr_one((UInt)-1) == digits );
35 static_assert( std::countr_one((UInt)127) == 7 );
36
37 static_assert( std::countr_one((UInt)1) == 1 );
38 static_assert( std::countr_one((UInt)3) == 2 );
39 static_assert( std::countr_one((UInt)0x1f) == 5 );
40 static_assert( std::countr_one((UInt)((UInt)-1 ^ 0x10)) == 4 );
41
42 if constexpr (std::numeric_limits<UInt>::digits > 8)
43 {
44 static_assert( std::countr_one((UInt)((UInt)-1 ^ 0x100)) == 8 );
45 static_assert( std::countr_one((UInt)((UInt)-1 ^ 0x101)) == 0 );
46 }
47
48 if constexpr (std::numeric_limits<UInt>::digits > 64)
49 {
50 static_assert( std::countr_one((UInt)-1 ^ ((UInt)1 << 70)) == 70 );
51 }
52
53 return true;
54 }
55
56 static_assert( test( (unsigned char)0 ) );
57 static_assert( test( (unsigned short)0 ) );
58 static_assert( test( (unsigned int)0 ) );
59 static_assert( test( (unsigned long)0 ) );
60 static_assert( test( (unsigned long long)0 ) );
61
62 // std::countr_one(T) shall not participate in overload resolution
63 // unless T is an unsigned integer type.
64 struct X { constexpr bool did_not_match() { return true; } };
65 constexpr X test(...) { return X{}; }
66 static_assert( test( (bool)0 ).did_not_match() );
67 static_assert( test( (char)0 ).did_not_match() );
68 static_assert( test( (int)0 ).did_not_match() );
69 static_assert( test( (char16_t)0 ).did_not_match() );
70 static_assert( test( (float)0 ).did_not_match() );
71 static_assert( test( (void*)0 ).did_not_match() );
72 static_assert( test( X{} ).did_not_match() );
73 enum E : unsigned { e };
74 static_assert( test( e ).did_not_match() );
75
76 #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
77 static_assert( test( (unsigned __int128)0 ) );
78 static_assert( test( (__int128)0 ).did_not_match() );
79 #endif
80 #if defined(__GLIBCXX_TYPE_INT_N_0)
81 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
82 static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
83 #endif
84 #if defined(__GLIBCXX_TYPE_INT_N_1)
85 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
86 static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
87 #endif
88 #if defined(__GLIBCXX_TYPE_INT_N_2)
89 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
90 static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
91 #endif
92 #if defined(__GLIBCXX_TYPE_INT_N_3)
93 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) );
94 static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() );
95 #endif
96
97 #include <cstddef>
98 static_assert( test( (std::byte)0 ).did_not_match() );