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