| 1 | // Copyright (C) 2018-2025 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-do compile { target c++20 } } |
| 19 | |
| 20 | #include <bit> |
| 21 | #include <limits> |
| 22 | |
| 23 | template<typename T> |
| 24 | constexpr T max = std::numeric_limits<T>::max(); |
| 25 | // Largest representable power of two (i.e. has most significant bit set) |
| 26 | template<typename T> |
| 27 | constexpr T maxpow2 = T(1) << (std::numeric_limits<T>::digits - 1); |
| 28 | |
| 29 | // Detect whether std::bit_ceil(N) is a constant expression. |
| 30 | template<auto N, typename = void> |
| 31 | struct bit_ceil_valid |
| 32 | : std::false_type { }; |
| 33 | |
| 34 | template<auto N> |
| 35 | struct bit_ceil_valid<N, std::void_t<char[(std::bit_ceil(N), 1)]>> |
| 36 | : std::true_type { }; |
| 37 | |
| 38 | template<typename UInt> |
| 39 | constexpr auto |
| 40 | test(UInt x) |
| 41 | -> decltype(std::bit_ceil(x)) |
| 42 | { |
| 43 | static_assert( noexcept(std::bit_ceil(x)) ); |
| 44 | |
| 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 ); |
| 52 | |
| 53 | if constexpr (std::numeric_limits<UInt>::digits > 8) |
| 54 | { |
| 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 ); |
| 58 | } |
| 59 | |
| 60 | if constexpr (std::numeric_limits<UInt>::digits > 32) |
| 61 | { |
| 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 ); |
| 65 | } |
| 66 | |
| 67 | if constexpr (std::numeric_limits<UInt>::digits > 64) |
| 68 | { |
| 69 | static_assert( std::bit_ceil(UInt(1) << 64) == (UInt(1) << 64) ); |
| 70 | static_assert( std::bit_ceil(UInt(3) << 64) == (UInt(4) << 64) ); |
| 71 | } |
| 72 | |
| 73 | constexpr UInt msb = maxpow2<UInt>; |
| 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 ); |
| 79 | |
| 80 | // P1355R2: not a constant expression if the result is not representable |
| 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)>() ); |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | static_assert( test( (unsigned char)0 ) ); |
| 90 | static_assert( test( (unsigned short)0 ) ); |
| 91 | static_assert( test( (unsigned int)0 ) ); |
| 92 | static_assert( test( (unsigned long)0 ) ); |
| 93 | static_assert( test( (unsigned long long)0 ) ); |
| 94 | |
| 95 | // std::bit_ceil(T) shall not participate in overload resolution |
| 96 | // unless T is an unsigned integer type. |
| 97 | struct X { constexpr bool did_not_match() { return true; } }; |
| 98 | constexpr X test(...) { return X{}; } |
| 99 | static_assert( test( (bool)0 ).did_not_match() ); |
| 100 | static_assert( test( (char)0 ).did_not_match() ); |
| 101 | static_assert( test( (int)0 ).did_not_match() ); |
| 102 | static_assert( test( (char16_t)0 ).did_not_match() ); |
| 103 | static_assert( test( (float)0 ).did_not_match() ); |
| 104 | static_assert( test( (void*)0 ).did_not_match() ); |
| 105 | static_assert( test( X{} ).did_not_match() ); |
| 106 | enum E : unsigned { e }; |
| 107 | static_assert( test( e ).did_not_match() ); |
| 108 | |
| 109 | #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__ |
| 110 | static_assert( test( (unsigned __int128)0 ) ); |
| 111 | static_assert( test( (__int128)0 ).did_not_match() ); |
| 112 | #endif |
| 113 | #if defined(__GLIBCXX_TYPE_INT_N_0) |
| 114 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) ); |
| 115 | static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() ); |
| 116 | #endif |
| 117 | #if defined(__GLIBCXX_TYPE_INT_N_1) |
| 118 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) ); |
| 119 | static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() ); |
| 120 | #endif |
| 121 | #if defined(__GLIBCXX_TYPE_INT_N_2) |
| 122 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) ); |
| 123 | static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() ); |
| 124 | #endif |
| 125 | #if defined(__GLIBCXX_TYPE_INT_N_3) |
| 126 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) ); |
| 127 | static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() ); |
| 128 | #endif |
| 129 | |
| 130 | #include <cstddef> |
| 131 | static_assert( test( (std::byte)0 ).did_not_match() ); |