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