]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/has_single_bit.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.pow.two / has_single_bit.cc
CommitLineData
83ffe9cd 1// Copyright (C) 2018-2023 Free Software Foundation, Inc.
9866abe3
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
18// { dg-options "-std=gnu++2a" }
19// { dg-do compile { target c++2a } }
20
21#include <bit>
22#include <limits>
23
24template<typename UInt>
25constexpr auto
26test(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
110static_assert( test( (unsigned char)0 ) );
111static_assert( test( (unsigned short)0 ) );
112static_assert( test( (unsigned int)0 ) );
113static_assert( test( (unsigned long)0 ) );
114static_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.
118struct X { constexpr bool did_not_match() { return true; } };
119constexpr X test(...) { return X{}; }
120static_assert( test( (bool)0 ).did_not_match() );
121static_assert( test( (char)0 ).did_not_match() );
122static_assert( test( (int)0 ).did_not_match() );
123static_assert( test( (char16_t)0 ).did_not_match() );
124static_assert( test( (float)0 ).did_not_match() );
125static_assert( test( (void*)0 ).did_not_match() );
126static_assert( test( X{} ).did_not_match() );
127enum E : unsigned { e };
128static_assert( test( e ).did_not_match() );
129
29a9de9b 130#if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
9866abe3
JW
131static_assert( test( (unsigned __int128)0 ) );
132static_assert( test( (__int128)0 ).did_not_match() );
133#endif
134#if defined(__GLIBCXX_TYPE_INT_N_0)
135static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
136static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
137#endif
138#if defined(__GLIBCXX_TYPE_INT_N_1)
139static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
140static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
141#endif
142#if defined(__GLIBCXX_TYPE_INT_N_2)
143static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
144static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
145#endif
29a9de9b
JW
146#if defined(__GLIBCXX_TYPE_INT_N_3)
147static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) );
148static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() );
149#endif
9866abe3
JW
150
151#include <cstddef>
152static_assert( test( (std::byte)0 ).did_not_match() );