]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/bit_ceil.cc
libstdc++: Remove dg-options "-std=gnu++20" from remaining tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.pow.two / bit_ceil.cc
CommitLineData
83ffe9cd 1// Copyright (C) 2018-2023 Free Software Foundation, Inc.
f3e91052
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
8ccae163 18// { dg-do compile { target c++20 } }
f3e91052
JW
19
20#include <bit>
c03b53da 21#include <limits>
f3e91052 22
281ab2fb
JW
23template<typename T>
24 constexpr T max = std::numeric_limits<T>::max();
25// Largest representable power of two (i.e. has most significant bit set)
26template<typename T>
27 constexpr T maxpow2 = T(1) << (std::numeric_limits<T>::digits - 1);
28
9866abe3 29// Detect whether std::bit_ceil(N) is a constant expression.
281ab2fb 30template<auto N, typename = void>
9866abe3 31 struct bit_ceil_valid
281ab2fb
JW
32 : std::false_type { };
33
34template<auto N>
9866abe3 35 struct bit_ceil_valid<N, std::void_t<char[(std::bit_ceil(N), 1)]>>
281ab2fb
JW
36 : std::true_type { };
37
f3e91052
JW
38template<typename UInt>
39constexpr auto
40test(UInt x)
9866abe3 41-> decltype(std::bit_ceil(x))
f3e91052 42{
9866abe3 43 static_assert( noexcept(std::bit_ceil(x)) );
f3e91052 44
9866abe3
JW
45 static_assert( std::bit_ceil(UInt(0)) == 1 );
46 static_assert( std::bit_ceil(UInt(1)) == 1 );
47 static_assert( std::bit_ceil(UInt(2)) == 2 );
48 static_assert( std::bit_ceil(UInt(3)) == 4 );
49 static_assert( std::bit_ceil(UInt(4)) == 4 );
50 static_assert( std::bit_ceil(UInt(0x11)) == 0x20 );
51 static_assert( std::bit_ceil(UInt(0x20)) == 0x20 );
f3e91052
JW
52
53 if constexpr (std::numeric_limits<UInt>::digits > 8)
54 {
9866abe3
JW
55 static_assert( std::bit_ceil(UInt(0x201)) == 0x400 );
56 static_assert( std::bit_ceil(UInt(0x8ff)) == 0x1000 );
57 static_assert( std::bit_ceil(UInt(0x1000)) == 0x1000 );
f3e91052
JW
58 }
59
60 if constexpr (std::numeric_limits<UInt>::digits > 32)
61 {
9866abe3
JW
62 static_assert( std::bit_ceil(UInt(0xabcdef)) == 0x1000000 );
63 static_assert( std::bit_ceil(UInt(0x1000000)) == 0x1000000 );
64 static_assert( std::bit_ceil(UInt(0x1000001)) == 0x2000000 );
f3e91052
JW
65 }
66
67 if constexpr (std::numeric_limits<UInt>::digits > 64)
68 {
9866abe3
JW
69 static_assert( std::bit_ceil(UInt(1) << 64) == (UInt(1) << 64) );
70 static_assert( std::bit_ceil(UInt(3) << 64) == (UInt(4) << 64) );
f3e91052
JW
71 }
72
281ab2fb 73 constexpr UInt msb = maxpow2<UInt>;
9866abe3
JW
74 static_assert( bit_ceil_valid<msb>() );
75 static_assert( std::bit_ceil( msb ) == msb );
76 static_assert( std::bit_ceil( UInt(msb - 1) ) == msb );
77 static_assert( std::bit_ceil( UInt(msb - 2) ) == msb );
78 static_assert( std::bit_ceil( UInt(msb - 3) ) == msb );
281ab2fb
JW
79
80 // P1355R2: not a constant expression if the result is not representable
9866abe3
JW
81 static_assert( !bit_ceil_valid<UInt(msb + 1)>() );
82 static_assert( !bit_ceil_valid<max<UInt>>() );
83 static_assert( !bit_ceil_valid<UInt(max<UInt> - 1)>() );
84 static_assert( !bit_ceil_valid<UInt(max<UInt> - 2)>() );
2fb17d2d 85
f3e91052
JW
86 return true;
87}
88
89static_assert( test( (unsigned char)0 ) );
90static_assert( test( (unsigned short)0 ) );
91static_assert( test( (unsigned int)0 ) );
92static_assert( test( (unsigned long)0 ) );
93static_assert( test( (unsigned long long)0 ) );
94
9866abe3 95// std::bit_ceil(T) shall not participate in overload resolution
f3e91052
JW
96// unless T is an unsigned integer type.
97struct X { constexpr bool did_not_match() { return true; } };
98constexpr X test(...) { return X{}; }
99static_assert( test( (bool)0 ).did_not_match() );
100static_assert( test( (char)0 ).did_not_match() );
101static_assert( test( (int)0 ).did_not_match() );
102static_assert( test( (char16_t)0 ).did_not_match() );
103static_assert( test( (float)0 ).did_not_match() );
104static_assert( test( (void*)0 ).did_not_match() );
105static_assert( test( X{} ).did_not_match() );
106enum E : unsigned { e };
107static_assert( test( e ).did_not_match() );
108
29a9de9b 109#if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
f3e91052
JW
110static_assert( test( (unsigned __int128)0 ) );
111static_assert( test( (__int128)0 ).did_not_match() );
112#endif
113#if defined(__GLIBCXX_TYPE_INT_N_0)
114static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
115static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
116#endif
117#if defined(__GLIBCXX_TYPE_INT_N_1)
118static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
119static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
120#endif
121#if defined(__GLIBCXX_TYPE_INT_N_2)
122static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
123static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
124#endif
47f79054
JW
125#if defined(__GLIBCXX_TYPE_INT_N_3)
126static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) );
127static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() );
128#endif
129
130#include <cstddef>
131static_assert( test( (std::byte)0 ).did_not_match() );