P0556R3 Integral power-of-2 operations, P0553R2 Bit operations
[gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.pow.two / ispow2.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-options "-std=gnu++2a" }
19 // { dg-do compile { target c++2a } }
20
21 #include <bit>
22
23 template<typename UInt>
24 constexpr auto
25 test(UInt x)
26 -> decltype(std::ispow2(x))
27 {
28 static_assert( noexcept(std::ispow2(x)) );
29
30 static_assert( ! std::ispow2( (UInt)0 ) );
31 static_assert( ! std::ispow2( (UInt)-1 ) );
32 static_assert( ! std::ispow2( (UInt)3 ) );
33 static_assert( ! std::ispow2( (UInt)0x0f ) );
34 static_assert( ! std::ispow2( (UInt)0xff ) );
35 static_assert( ! std::ispow2( (UInt)0x0a ) );
36 static_assert( ! std::ispow2( (UInt)0xa0 ) );
37
38 constexpr UInt one = 1;
39 static_assert( std::ispow2( (UInt)(one << 0) ) );
40
41 static_assert( std::ispow2( (UInt)(one << 1) ) );
42 static_assert( std::ispow2( (UInt)(one << 2) ) );
43 static_assert( std::ispow2( (UInt)(one << 3) ) );
44 static_assert( std::ispow2( (UInt)(one << 4) ) );
45 static_assert( std::ispow2( (UInt)(one << 5) ) );
46 static_assert( std::ispow2( (UInt)(one << 6) ) );
47 static_assert( std::ispow2( (UInt)(one << 7) ) );
48
49 if constexpr (std::numeric_limits<UInt>::digits > 8)
50 {
51 static_assert( std::ispow2( (UInt)(one << 8) ) );
52 static_assert( std::ispow2( (UInt)(one << 9) ) );
53 static_assert( std::ispow2( (UInt)(one << 10) ) );
54 static_assert( std::ispow2( (UInt)(one << 11) ) );
55 static_assert( std::ispow2( (UInt)(one << 12) ) );
56 static_assert( std::ispow2( (UInt)(one << 13) ) );
57 static_assert( std::ispow2( (UInt)(one << 14) ) );
58 static_assert( std::ispow2( (UInt)(one << 15) ) );
59
60 static_assert( ! std::ispow2( (UInt)0xf000 ) );
61 static_assert( ! std::ispow2( (UInt)0xff00 ) );
62 static_assert( ! std::ispow2( (UInt)0xf0f0 ) );
63 static_assert( ! std::ispow2( (UInt)0xf00f ) );
64 static_assert( ! std::ispow2( (UInt)0x0f0f ) );
65 static_assert( ! std::ispow2( (UInt)0x00ff ) );
66 }
67
68 if constexpr (std::numeric_limits<UInt>::digits > 16)
69 {
70 static_assert( std::ispow2( (UInt)(one << 16) ) );
71 static_assert( std::ispow2( (UInt)(one << 17) ) );
72 static_assert( ! std::ispow2( (UInt)((one << 16) + 1) ) );
73 static_assert( ! std::ispow2( (UInt)((one << 16) + 0x10) ) );
74 }
75
76 // msp340 target has 20-bit __GLIBCXX_TYPE_INT_N_0 type
77 if constexpr (std::numeric_limits<UInt>::digits > 20)
78 {
79 static_assert( std::ispow2( (UInt)(one << 20) ) );
80 static_assert( std::ispow2( (UInt)(one << 21) ) );
81 static_assert( std::ispow2( (UInt)(one << 24) ) );
82 static_assert( std::ispow2( (UInt)(one << 28) ) );
83 static_assert( std::ispow2( (UInt)(one << 31) ) );
84 }
85
86 if constexpr (std::numeric_limits<UInt>::digits > 32)
87 {
88 static_assert( std::ispow2( (UInt)(one << 32) ) );
89 static_assert( std::ispow2( (UInt)(one << 33) ) );
90 static_assert( std::ispow2( (UInt)(one << 41) ) );
91
92 static_assert( ! std::ispow2( (UInt)((one << 32) + 1) ) );
93 static_assert( ! std::ispow2( (UInt)((one << 32) + (one << 31)) ) );
94 static_assert( ! std::ispow2( (UInt)((one << 33) + 1) ) );
95 static_assert( ! std::ispow2( (UInt)((one << 33) + (one << 32)) ) );
96 }
97
98 if constexpr (std::numeric_limits<UInt>::digits == 64)
99 {
100 static_assert( std::ispow2( (UInt)(one << 63) ) );
101
102 static_assert( ! std::ispow2( (UInt)((one << 63) + 1) ) );
103 static_assert( ! std::ispow2( (UInt)((one << 63) + (one << 8)) ) );
104 static_assert( ! std::ispow2( (UInt)((one << 63) + (one << 32)) ) );
105 }
106 return true;
107 }
108
109 static_assert( test( (unsigned char)0 ) );
110 static_assert( test( (unsigned short)0 ) );
111 static_assert( test( (unsigned int)0 ) );
112 static_assert( test( (unsigned long)0 ) );
113 static_assert( test( (unsigned long long)0 ) );
114
115 // std::ispow2(T) shall not participate in overload resolution
116 // unless T is an unsigned integer type.
117 struct X { constexpr bool did_not_match() { return true; } };
118 constexpr X test(...) { return X{}; }
119 static_assert( test( (bool)0 ).did_not_match() );
120 static_assert( test( (char)0 ).did_not_match() );
121 static_assert( test( (int)0 ).did_not_match() );
122 static_assert( test( (char16_t)0 ).did_not_match() );
123 static_assert( test( (float)0 ).did_not_match() );
124 static_assert( test( (void*)0 ).did_not_match() );
125 static_assert( test( X{} ).did_not_match() );
126 enum E : unsigned { e };
127 static_assert( test( e ).did_not_match() );
128
129 #ifndef __STRICT_ANSI__
130 #include <cstddef>
131 static_assert( std::ispow2(std::byte{0}) == false );
132 static_assert( std::ispow2(std::byte{1}) == true );
133 static_assert( std::ispow2(std::byte{2}) == true );
134 static_assert( std::ispow2(std::byte{3}) == false );
135 static_assert( std::ispow2(std::byte{100}) == false );
136 static_assert( std::ispow2(std::byte{128}) == true );
137 static_assert( std::ispow2(std::byte{255}) == false );
138 #else
139 static_assert( test( (std::byte)0 ).did_not_match() );
140 #endif
141
142 #if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
143 static_assert( test( (unsigned __int128)0 ) );
144 static_assert( test( (__int128)0 ).did_not_match() );
145 #endif
146 #if defined(__GLIBCXX_TYPE_INT_N_0)
147 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
148 static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
149 #endif
150 #if defined(__GLIBCXX_TYPE_INT_N_1)
151 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
152 static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
153 #endif
154 #if defined(__GLIBCXX_TYPE_INT_N_2)
155 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
156 static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
157 #endif