| 1 |
// Copyright (C) 2019-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-options "-D_GLIBCXX_ASSERTIONS" } |
| 19 |
// { dg-do run { target c++20 } } |
| 20 |
// { dg-xfail-run-if "__glibcxx_assert in bit_ceil should fail" { *-*-* } } |
| 21 |
|
| 22 |
#include <bit> |
| 23 |
#include <limits> |
| 24 |
|
| 25 |
// P1355R2: not a constant expression if the result is not representable |
| 26 |
|
| 27 |
template<auto N, typename = void> |
| 28 |
struct bit_ceil_valid |
| 29 |
: std::false_type { }; |
| 30 |
|
| 31 |
template<auto N> |
| 32 |
struct bit_ceil_valid<N, std::void_t<char[(std::bit_ceil(N), 1)]>> |
| 33 |
: std::true_type { }; |
| 34 |
|
| 35 |
template<typename T> |
| 36 |
constexpr T max = std::numeric_limits<T>::max(); |
| 37 |
template<typename T> |
| 38 |
constexpr T maxpow2 = T(1) << (std::numeric_limits<T>::digits - 1); |
| 39 |
|
| 40 |
static_assert( bit_ceil_valid<maxpow2<unsigned char>>() ); |
| 41 |
static_assert( !bit_ceil_valid<maxpow2<unsigned char> + (unsigned char)1>() ); |
| 42 |
|
| 43 |
static_assert( !bit_ceil_valid<max<unsigned char>>() ); |
| 44 |
static_assert( !bit_ceil_valid<max<unsigned char> - (unsigned char)1>() ); |
| 45 |
|
| 46 |
static_assert( bit_ceil_valid<maxpow2<unsigned short>>() ); |
| 47 |
static_assert( !bit_ceil_valid<maxpow2<unsigned short> + (unsigned short)1>() ); |
| 48 |
static_assert( !bit_ceil_valid<max<unsigned short>>() ); |
| 49 |
static_assert( !bit_ceil_valid<max<unsigned short> - (unsigned short)1>() ); |
| 50 |
|
| 51 |
static_assert( bit_ceil_valid<maxpow2<unsigned int>>() ); |
| 52 |
static_assert( !bit_ceil_valid<maxpow2<unsigned int> + 1u>() ); |
| 53 |
static_assert( !bit_ceil_valid<max<unsigned int>>() ); |
| 54 |
static_assert( !bit_ceil_valid<max<unsigned int> - 1u>() ); |
| 55 |
|
| 56 |
static_assert( bit_ceil_valid<maxpow2<unsigned long>>() ); |
| 57 |
static_assert( !bit_ceil_valid<maxpow2<unsigned long> + 1ul>() ); |
| 58 |
static_assert( !bit_ceil_valid<max<unsigned long>>() ); |
| 59 |
static_assert( !bit_ceil_valid<max<unsigned long> - 1ul>() ); |
| 60 |
|
| 61 |
static_assert( bit_ceil_valid<maxpow2<unsigned long long>>() ); |
| 62 |
static_assert( !bit_ceil_valid<maxpow2<unsigned long long> + 1ull>() ); |
| 63 |
static_assert( !bit_ceil_valid<max<unsigned long long>>() ); |
| 64 |
static_assert( !bit_ceil_valid<max<unsigned long long> - 1ull>() ); |
| 65 |
|
| 66 |
void |
| 67 |
test01() |
| 68 |
{ |
| 69 |
std::bit_ceil( maxpow2<unsigned> + 1u ); // should fail __glibcxx_assert |
| 70 |
} |
| 71 |
|
| 72 |
int main() |
| 73 |
{ |
| 74 |
test01(); |
| 75 |
} |