]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/bit_ceil.cc
1 // Copyright (C) 2018-2023 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-options "-std=gnu++2a" }
19 // { dg-do compile { target c++2a } }
25 constexpr T max
= std::numeric_limits
<T
>::max();
26 // Largest representable power of two (i.e. has most significant bit set)
28 constexpr T maxpow2
= T(1) << (std::numeric_limits
<T
>::digits
- 1);
30 // Detect whether std::bit_ceil(N) is a constant expression.
31 template<auto N
, typename
= void>
33 : std::false_type
{ };
36 struct bit_ceil_valid
<N
, std::void_t
<char[(std::bit_ceil(N
), 1)]>>
39 template<typename UInt
>
42 -> decltype(std::bit_ceil(x
))
44 static_assert( noexcept(std::bit_ceil(x
)) );
46 static_assert( std::bit_ceil(UInt(0)) == 1 );
47 static_assert( std::bit_ceil(UInt(1)) == 1 );
48 static_assert( std::bit_ceil(UInt(2)) == 2 );
49 static_assert( std::bit_ceil(UInt(3)) == 4 );
50 static_assert( std::bit_ceil(UInt(4)) == 4 );
51 static_assert( std::bit_ceil(UInt(0x11)) == 0x20 );
52 static_assert( std::bit_ceil(UInt(0x20)) == 0x20 );
54 if constexpr (std::numeric_limits
<UInt
>::digits
> 8)
56 static_assert( std::bit_ceil(UInt(0x201)) == 0x400 );
57 static_assert( std::bit_ceil(UInt(0x8ff)) == 0x1000 );
58 static_assert( std::bit_ceil(UInt(0x1000)) == 0x1000 );
61 if constexpr (std::numeric_limits
<UInt
>::digits
> 32)
63 static_assert( std::bit_ceil(UInt(0xabcdef)) == 0x1000000 );
64 static_assert( std::bit_ceil(UInt(0x1000000)) == 0x1000000 );
65 static_assert( std::bit_ceil(UInt(0x1000001)) == 0x2000000 );
68 if constexpr (std::numeric_limits
<UInt
>::digits
> 64)
70 static_assert( std::bit_ceil(UInt(1) << 64) == (UInt(1) << 64) );
71 static_assert( std::bit_ceil(UInt(3) << 64) == (UInt(4) << 64) );
74 constexpr UInt msb
= maxpow2
<UInt
>;
75 static_assert( bit_ceil_valid
<msb
>() );
76 static_assert( std::bit_ceil( msb
) == msb
);
77 static_assert( std::bit_ceil( UInt(msb
- 1) ) == msb
);
78 static_assert( std::bit_ceil( UInt(msb
- 2) ) == msb
);
79 static_assert( std::bit_ceil( UInt(msb
- 3) ) == msb
);
81 // P1355R2: not a constant expression if the result is not representable
82 static_assert( !bit_ceil_valid
<UInt(msb
+ 1)>() );
83 static_assert( !bit_ceil_valid
<max
<UInt
>>() );
84 static_assert( !bit_ceil_valid
<UInt(max
<UInt
> - 1)>() );
85 static_assert( !bit_ceil_valid
<UInt(max
<UInt
> - 2)>() );
90 static_assert( test( (unsigned char)0 ) );
91 static_assert( test( (unsigned short)0 ) );
92 static_assert( test( (unsigned int)0 ) );
93 static_assert( test( (unsigned long)0 ) );
94 static_assert( test( (unsigned long long)0 ) );
96 // std::bit_ceil(T) shall not participate in overload resolution
97 // unless T is an unsigned integer type.
98 struct X
{ constexpr bool did_not_match() { return true; } };
99 constexpr X
test(...) { return X
{}; }
100 static_assert( test( (bool)0 ).did_not_match() );
101 static_assert( test( (char)0 ).did_not_match() );
102 static_assert( test( (int)0 ).did_not_match() );
103 static_assert( test( (char16_t
)0 ).did_not_match() );
104 static_assert( test( (float)0 ).did_not_match() );
105 static_assert( test( (void*)0 ).did_not_match() );
106 static_assert( test( X
{} ).did_not_match() );
107 enum E
: unsigned { e
};
108 static_assert( test( e
).did_not_match() );
110 #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
111 static_assert( test( (unsigned __int128
)0 ) );
112 static_assert( test( (__int128
)0 ).did_not_match() );
114 #if defined(__GLIBCXX_TYPE_INT_N_0)
115 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0
)0 ) );
116 static_assert( test( (__GLIBCXX_TYPE_INT_N_0
)0 ).did_not_match() );
118 #if defined(__GLIBCXX_TYPE_INT_N_1)
119 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1
)0 ) );
120 static_assert( test( (__GLIBCXX_TYPE_INT_N_1
)0 ).did_not_match() );
122 #if defined(__GLIBCXX_TYPE_INT_N_2)
123 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2
)0 ) );
124 static_assert( test( (__GLIBCXX_TYPE_INT_N_2
)0 ).did_not_match() );
126 #if defined(__GLIBCXX_TYPE_INT_N_3)
127 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3
)0 ) );
128 static_assert( test( (__GLIBCXX_TYPE_INT_N_3
)0 ).did_not_match() );
132 static_assert( test( (std::byte
)0 ).did_not_match() );