comparison libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/has_single_bit.cc @ 152:2b5abeee2509

update gcc11
author anatofuz
date Mon, 25 May 2020 07:50:57 +0900
parents
children
comparison
equal deleted inserted replaced
145:1830386684a0 152:2b5abeee2509
1 // Copyright (C) 2018-2020 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::has_single_bit(x))
28 {
29 static_assert( noexcept(std::has_single_bit(x)) );
30
31 static_assert( ! std::has_single_bit( (UInt)0 ) );
32 static_assert( ! std::has_single_bit( (UInt)-1 ) );
33 static_assert( ! std::has_single_bit( (UInt)3 ) );
34 static_assert( ! std::has_single_bit( (UInt)0x0f ) );
35 static_assert( ! std::has_single_bit( (UInt)0xff ) );
36 static_assert( ! std::has_single_bit( (UInt)0x0a ) );
37 static_assert( ! std::has_single_bit( (UInt)0xa0 ) );
38
39 constexpr UInt one = 1;
40 static_assert( std::has_single_bit( (UInt)(one << 0) ) );
41
42 static_assert( std::has_single_bit( (UInt)(one << 1) ) );
43 static_assert( std::has_single_bit( (UInt)(one << 2) ) );
44 static_assert( std::has_single_bit( (UInt)(one << 3) ) );
45 static_assert( std::has_single_bit( (UInt)(one << 4) ) );
46 static_assert( std::has_single_bit( (UInt)(one << 5) ) );
47 static_assert( std::has_single_bit( (UInt)(one << 6) ) );
48 static_assert( std::has_single_bit( (UInt)(one << 7) ) );
49
50 if constexpr (std::numeric_limits<UInt>::digits > 8)
51 {
52 static_assert( std::has_single_bit( (UInt)(one << 8) ) );
53 static_assert( std::has_single_bit( (UInt)(one << 9) ) );
54 static_assert( std::has_single_bit( (UInt)(one << 10) ) );
55 static_assert( std::has_single_bit( (UInt)(one << 11) ) );
56 static_assert( std::has_single_bit( (UInt)(one << 12) ) );
57 static_assert( std::has_single_bit( (UInt)(one << 13) ) );
58 static_assert( std::has_single_bit( (UInt)(one << 14) ) );
59 static_assert( std::has_single_bit( (UInt)(one << 15) ) );
60
61 static_assert( ! std::has_single_bit( (UInt)0xf000 ) );
62 static_assert( ! std::has_single_bit( (UInt)0xff00 ) );
63 static_assert( ! std::has_single_bit( (UInt)0xf0f0 ) );
64 static_assert( ! std::has_single_bit( (UInt)0xf00f ) );
65 static_assert( ! std::has_single_bit( (UInt)0x0f0f ) );
66 static_assert( ! std::has_single_bit( (UInt)0x00ff ) );
67 }
68
69 if constexpr (std::numeric_limits<UInt>::digits > 16)
70 {
71 static_assert( std::has_single_bit( (UInt)(one << 16) ) );
72 static_assert( std::has_single_bit( (UInt)(one << 17) ) );
73 static_assert( ! std::has_single_bit( (UInt)((one << 16) + 1) ) );
74 static_assert( ! std::has_single_bit( (UInt)((one << 16) + 0x10) ) );
75 }
76
77 // msp340 target has 20-bit __GLIBCXX_TYPE_INT_N_0 type
78 if constexpr (std::numeric_limits<UInt>::digits > 20)
79 {
80 static_assert( std::has_single_bit( (UInt)(one << 20) ) );
81 static_assert( std::has_single_bit( (UInt)(one << 21) ) );
82 static_assert( std::has_single_bit( (UInt)(one << 24) ) );
83 static_assert( std::has_single_bit( (UInt)(one << 28) ) );
84 static_assert( std::has_single_bit( (UInt)(one << 31) ) );
85 }
86
87 if constexpr (std::numeric_limits<UInt>::digits > 32)
88 {
89 static_assert( std::has_single_bit( (UInt)(one << 32) ) );
90 static_assert( std::has_single_bit( (UInt)(one << 33) ) );
91 static_assert( std::has_single_bit( (UInt)(one << 41) ) );
92
93 static_assert( ! std::has_single_bit( (UInt)((one << 32) + 1) ) );
94 static_assert( ! std::has_single_bit( (UInt)((one << 32) + (one << 31)) ) );
95 static_assert( ! std::has_single_bit( (UInt)((one << 33) + 1) ) );
96 static_assert( ! std::has_single_bit( (UInt)((one << 33) + (one << 32)) ) );
97 }
98
99 if constexpr (std::numeric_limits<UInt>::digits == 64)
100 {
101 static_assert( std::has_single_bit( (UInt)(one << 63) ) );
102
103 static_assert( ! std::has_single_bit( (UInt)((one << 63) + 1) ) );
104 static_assert( ! std::has_single_bit( (UInt)((one << 63) + (one << 8)) ) );
105 static_assert( ! std::has_single_bit( (UInt)((one << 63) + (one << 32)) ) );
106 }
107 return true;
108 }
109
110 static_assert( test( (unsigned char)0 ) );
111 static_assert( test( (unsigned short)0 ) );
112 static_assert( test( (unsigned int)0 ) );
113 static_assert( test( (unsigned long)0 ) );
114 static_assert( test( (unsigned long long)0 ) );
115
116 // std::has_single_bit(T) shall not participate in overload resolution
117 // unless T is an unsigned integer type.
118 struct X { constexpr bool did_not_match() { return true; } };
119 constexpr X test(...) { return X{}; }
120 static_assert( test( (bool)0 ).did_not_match() );
121 static_assert( test( (char)0 ).did_not_match() );
122 static_assert( test( (int)0 ).did_not_match() );
123 static_assert( test( (char16_t)0 ).did_not_match() );
124 static_assert( test( (float)0 ).did_not_match() );
125 static_assert( test( (void*)0 ).did_not_match() );
126 static_assert( test( X{} ).did_not_match() );
127 enum E : unsigned { e };
128 static_assert( test( e ).did_not_match() );
129
130 #if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
131 static_assert( test( (unsigned __int128)0 ) );
132 static_assert( test( (__int128)0 ).did_not_match() );
133 #endif
134 #if defined(__GLIBCXX_TYPE_INT_N_0)
135 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
136 static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
137 #endif
138 #if defined(__GLIBCXX_TYPE_INT_N_1)
139 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
140 static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
141 #endif
142 #if defined(__GLIBCXX_TYPE_INT_N_2)
143 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
144 static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
145 #endif
146
147 #include <cstddef>
148 static_assert( test( (std::byte)0 ).did_not_match() );