]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/bit/bitops.count/popcount.cc
1 // Copyright (C) 2018-2019 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do compile { target c++2a } }
23 template<typename UInt
>
26 -> decltype(std::popcount(x
))
28 static_assert( noexcept(std::popcount(x
)) );
30 constexpr unsigned digits
= std::numeric_limits
<UInt
>::digits
;
32 static_assert( std::popcount((UInt
)0) == 0 );
33 static_assert( std::popcount((UInt
)-1) == digits
);
34 static_assert( std::popcount((UInt
)-2) == digits
- 1 );
35 static_assert( std::popcount((UInt
)127) == 7 );
37 static_assert( std::popcount((UInt
)1) == 1 );
38 static_assert( std::popcount((UInt
)2) == 1 );
39 static_assert( std::popcount((UInt
)0x70) == 3 );
41 if constexpr (std::numeric_limits
<UInt
>::digits
> 8)
43 static_assert( std::popcount((UInt
)(0x101)) == 2 );
44 static_assert( std::popcount((UInt
)(0xfff)) == 12 );
47 if constexpr (std::numeric_limits
<UInt
>::digits
> 64)
49 static_assert( std::popcount((UInt
)0xffffffffffffffff) == 64 );
50 static_assert( std::popcount(0x5555555555555555 | ((UInt
)1 << 64)) == 33 );
51 static_assert( std::popcount(0x5555555555555555 | ((UInt
)3 << 64)) == 34 );
57 static_assert( test( (unsigned char)0 ) );
58 static_assert( test( (unsigned short)0 ) );
59 static_assert( test( (unsigned int)0 ) );
60 static_assert( test( (unsigned long)0 ) );
61 static_assert( test( (unsigned long long)0 ) );
63 // std::popcount(T) shall not participate in overload resolution
64 // unless T is an unsigned integer type.
65 struct X
{ constexpr bool did_not_match() { return true; } };
66 constexpr X
test(...) { return X
{}; }
67 static_assert( test( (bool)0 ).did_not_match() );
68 static_assert( test( (char)0 ).did_not_match() );
69 static_assert( test( (int)0 ).did_not_match() );
70 static_assert( test( (char16_t
)0 ).did_not_match() );
71 static_assert( test( (float)0 ).did_not_match() );
72 static_assert( test( (void*)0 ).did_not_match() );
73 static_assert( test( X
{} ).did_not_match() );
74 enum E
: unsigned { e
};
75 static_assert( test( e
).did_not_match() );
77 #ifndef __STRICT_ANSI__
79 static_assert( std::popcount(std::byte
{0x00}) == 0 );
80 static_assert( std::popcount(std::byte
{0x01}) == 1 );
81 static_assert( std::popcount(std::byte
{0x02}) == 1 );
82 static_assert( std::popcount(std::byte
{0x03}) == 2 );
83 static_assert( std::popcount(std::byte
{0x30}) == 2 );
84 static_assert( std::popcount(std::byte
{0x40}) == 1 );
85 static_assert( std::popcount(std::byte
{0x41}) == 2 );
86 static_assert( std::popcount(std::byte
{0xff}) == 8 );
88 static_assert( test( (std::byte
)0 ).did_not_match() );
91 #if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
92 static_assert( test( (unsigned __int128
)0 ) );
93 static_assert( test( (__int128
)0 ).did_not_match() );
95 #if defined(__GLIBCXX_TYPE_INT_N_0)
96 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0
)0 ) );
97 static_assert( test( (__GLIBCXX_TYPE_INT_N_0
)0 ).did_not_match() );
99 #if defined(__GLIBCXX_TYPE_INT_N_1)
100 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1
)0 ) );
101 static_assert( test( (__GLIBCXX_TYPE_INT_N_1
)0 ).did_not_match() );
103 #if defined(__GLIBCXX_TYPE_INT_N_2)
104 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2
)0 ) );
105 static_assert( test( (__GLIBCXX_TYPE_INT_N_2
)0 ).did_not_match() );