]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/bit/bit.pow.two/ceil2.cc
Add new helper traits for signed/unsigned integer types
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / bit / bit.pow.two / ceil2.cc
1 // Copyright (C) 2018-2019 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
23 template<typename UInt>
24 constexpr auto
25 test(UInt x)
26 -> decltype(std::ceil2(x))
27 {
28 static_assert( noexcept(std::ceil2(x)) );
29
30 static_assert( std::ceil2(UInt(0)) == 1 );
31 static_assert( std::ceil2(UInt(1)) == 1 );
32 static_assert( std::ceil2(UInt(2)) == 2 );
33 static_assert( std::ceil2(UInt(3)) == 4 );
34 static_assert( std::ceil2(UInt(4)) == 4 );
35 static_assert( std::ceil2(UInt(0x11)) == 0x20 );
36 static_assert( std::ceil2(UInt(0x20)) == 0x20 );
37
38 if constexpr (std::numeric_limits<UInt>::digits > 8)
39 {
40 static_assert( std::ceil2(UInt(0x201)) == 0x400 );
41 static_assert( std::ceil2(UInt(0x8ff)) == 0x1000 );
42 static_assert( std::ceil2(UInt(0x1000)) == 0x1000 );
43 }
44
45 if constexpr (std::numeric_limits<UInt>::digits > 32)
46 {
47 static_assert( std::ceil2(UInt(0xabcdef)) == 0x1000000 );
48 static_assert( std::ceil2(UInt(0x1000000)) == 0x1000000 );
49 static_assert( std::ceil2(UInt(0x1000001)) == 0x2000000 );
50 }
51
52 if constexpr (std::numeric_limits<UInt>::digits > 64)
53 {
54 static_assert( std::ceil2(UInt(1) << 64) == (UInt(1) << 64) );
55 static_assert( std::ceil2(UInt(3) << 64) == (UInt(4) << 64) );
56 }
57
58 constexpr UInt msb = UInt(1) << (std::numeric_limits<UInt>::digits - 1);
59 static_assert( std::ceil2( msb ) == msb );
60 // Larger values cannot be represented so the return value is unspecified,
61 // but must still be valid in constant expressions, i.e. not undefined.
62 static_assert( std::ceil2( UInt(msb + 1) ) != 77 );
63 static_assert( std::ceil2( UInt(msb + 2) ) != 77 );
64 static_assert( std::ceil2( UInt(msb + 77) ) != 77 );
65
66 return true;
67 }
68
69 static_assert( test( (unsigned char)0 ) );
70 static_assert( test( (unsigned short)0 ) );
71 static_assert( test( (unsigned int)0 ) );
72 static_assert( test( (unsigned long)0 ) );
73 static_assert( test( (unsigned long long)0 ) );
74
75 // std::ceil2(T) shall not participate in overload resolution
76 // unless T is an unsigned integer type.
77 struct X { constexpr bool did_not_match() { return true; } };
78 constexpr X test(...) { return X{}; }
79 static_assert( test( (bool)0 ).did_not_match() );
80 static_assert( test( (char)0 ).did_not_match() );
81 static_assert( test( (int)0 ).did_not_match() );
82 static_assert( test( (char16_t)0 ).did_not_match() );
83 static_assert( test( (float)0 ).did_not_match() );
84 static_assert( test( (void*)0 ).did_not_match() );
85 static_assert( test( X{} ).did_not_match() );
86 enum E : unsigned { e };
87 static_assert( test( e ).did_not_match() );
88
89 #if !defined(__STRICT_ANSI__) && defined _GLIBCXX_USE_INT128
90 static_assert( test( (unsigned __int128)0 ) );
91 static_assert( test( (__int128)0 ).did_not_match() );
92 #endif
93 #if defined(__GLIBCXX_TYPE_INT_N_0)
94 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) );
95 static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() );
96 #endif
97 #if defined(__GLIBCXX_TYPE_INT_N_1)
98 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) );
99 static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() );
100 #endif
101 #if defined(__GLIBCXX_TYPE_INT_N_2)
102 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) );
103 static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() );
104 #endif
105 #if defined(__GLIBCXX_TYPE_INT_N_3)
106 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) );
107 static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() );
108 #endif
109
110 #include <cstddef>
111 static_assert( test( (std::byte)0 ).did_not_match() );