]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/bit_ceil.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.pow.two / bit_ceil.cc
1 // Copyright (C) 2018-2023 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 T>
25 constexpr T max = std::numeric_limits<T>::max();
26 // Largest representable power of two (i.e. has most significant bit set)
27 template<typename T>
28 constexpr T maxpow2 = T(1) << (std::numeric_limits<T>::digits - 1);
29
30 // Detect whether std::bit_ceil(N) is a constant expression.
31 template<auto N, typename = void>
32 struct bit_ceil_valid
33 : std::false_type { };
34
35 template<auto N>
36 struct bit_ceil_valid<N, std::void_t<char[(std::bit_ceil(N), 1)]>>
37 : std::true_type { };
38
39 template<typename UInt>
40 constexpr auto
41 test(UInt x)
42 -> decltype(std::bit_ceil(x))
43 {
44 static_assert( noexcept(std::bit_ceil(x)) );
45
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 );
53
54 if constexpr (std::numeric_limits<UInt>::digits > 8)
55 {
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 );
59 }
60
61 if constexpr (std::numeric_limits<UInt>::digits > 32)
62 {
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 );
66 }
67
68 if constexpr (std::numeric_limits<UInt>::digits > 64)
69 {
70 static_assert( std::bit_ceil(UInt(1) << 64) == (UInt(1) << 64) );
71 static_assert( std::bit_ceil(UInt(3) << 64) == (UInt(4) << 64) );
72 }
73
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 );
80
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)>() );
86
87 return true;
88 }
89
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 ) );
95
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() );
109
110 #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
111 static_assert( test( (unsigned __int128)0 ) );
112 static_assert( test( (__int128)0 ).did_not_match() );
113 #endif
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() );
117 #endif
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() );
121 #endif
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() );
125 #endif
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() );
129 #endif
130
131 #include <cstddef>
132 static_assert( test( (std::byte)0 ).did_not_match() );