| 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 UInt> |
| 24 |
constexpr auto |
| 25 |
test(UInt x) |
| 26 |
-> decltype(std::has_single_bit(x)) |
| 27 |
{ |
| 28 |
static_assert( noexcept(std::has_single_bit(x)) ); |
| 29 |
|
| 30 |
static_assert( ! std::has_single_bit( (UInt)0 ) ); |
| 31 |
static_assert( ! std::has_single_bit( (UInt)-1 ) ); |
| 32 |
static_assert( ! std::has_single_bit( (UInt)3 ) ); |
| 33 |
static_assert( ! std::has_single_bit( (UInt)0x0f ) ); |
| 34 |
static_assert( ! std::has_single_bit( (UInt)0xff ) ); |
| 35 |
static_assert( ! std::has_single_bit( (UInt)0x0a ) ); |
| 36 |
static_assert( ! std::has_single_bit( (UInt)0xa0 ) ); |
| 37 |
|
| 38 |
constexpr UInt one = 1; |
| 39 |
static_assert( std::has_single_bit( (UInt)(one << 0) ) ); |
| 40 |
|
| 41 |
static_assert( std::has_single_bit( (UInt)(one << 1) ) ); |
| 42 |
static_assert( std::has_single_bit( (UInt)(one << 2) ) ); |
| 43 |
static_assert( std::has_single_bit( (UInt)(one << 3) ) ); |
| 44 |
static_assert( std::has_single_bit( (UInt)(one << 4) ) ); |
| 45 |
static_assert( std::has_single_bit( (UInt)(one << 5) ) ); |
| 46 |
static_assert( std::has_single_bit( (UInt)(one << 6) ) ); |
| 47 |
static_assert( std::has_single_bit( (UInt)(one << 7) ) ); |
| 48 |
|
| 49 |
if constexpr (std::numeric_limits<UInt>::digits > 8) |
| 50 |
{ |
| 51 |
static_assert( std::has_single_bit( (UInt)(one << 8) ) ); |
| 52 |
static_assert( std::has_single_bit( (UInt)(one << 9) ) ); |
| 53 |
static_assert( std::has_single_bit( (UInt)(one << 10) ) ); |
| 54 |
static_assert( std::has_single_bit( (UInt)(one << 11) ) ); |
| 55 |
static_assert( std::has_single_bit( (UInt)(one << 12) ) ); |
| 56 |
static_assert( std::has_single_bit( (UInt)(one << 13) ) ); |
| 57 |
static_assert( std::has_single_bit( (UInt)(one << 14) ) ); |
| 58 |
static_assert( std::has_single_bit( (UInt)(one << 15) ) ); |
| 59 |
|
| 60 |
static_assert( ! std::has_single_bit( (UInt)0xf000 ) ); |
| 61 |
static_assert( ! std::has_single_bit( (UInt)0xff00 ) ); |
| 62 |
static_assert( ! std::has_single_bit( (UInt)0xf0f0 ) ); |
| 63 |
static_assert( ! std::has_single_bit( (UInt)0xf00f ) ); |
| 64 |
static_assert( ! std::has_single_bit( (UInt)0x0f0f ) ); |
| 65 |
static_assert( ! std::has_single_bit( (UInt)0x00ff ) ); |
| 66 |
} |
| 67 |
|
| 68 |
if constexpr (std::numeric_limits<UInt>::digits > 16) |
| 69 |
{ |
| 70 |
static_assert( std::has_single_bit( (UInt)(one << 16) ) ); |
| 71 |
static_assert( std::has_single_bit( (UInt)(one << 17) ) ); |
| 72 |
static_assert( ! std::has_single_bit( (UInt)((one << 16) + 1) ) ); |
| 73 |
static_assert( ! std::has_single_bit( (UInt)((one << 16) + 0x10) ) ); |
| 74 |
} |
| 75 |
|
| 76 |
// msp340 target has 20-bit __GLIBCXX_TYPE_INT_N_0 type |
| 77 |
if constexpr (std::numeric_limits<UInt>::digits > 20) |
| 78 |
{ |
| 79 |
static_assert( std::has_single_bit( (UInt)(one << 20) ) ); |
| 80 |
static_assert( std::has_single_bit( (UInt)(one << 21) ) ); |
| 81 |
static_assert( std::has_single_bit( (UInt)(one << 24) ) ); |
| 82 |
static_assert( std::has_single_bit( (UInt)(one << 28) ) ); |
| 83 |
static_assert( std::has_single_bit( (UInt)(one << 31) ) ); |
| 84 |
} |
| 85 |
|
| 86 |
if constexpr (std::numeric_limits<UInt>::digits > 32) |
| 87 |
{ |
| 88 |
static_assert( std::has_single_bit( (UInt)(one << 32) ) ); |
| 89 |
static_assert( std::has_single_bit( (UInt)(one << 33) ) ); |
| 90 |
static_assert( std::has_single_bit( (UInt)(one << 41) ) ); |
| 91 |
|
| 92 |
static_assert( ! std::has_single_bit( (UInt)((one << 32) + 1) ) ); |
| 93 |
static_assert( ! std::has_single_bit( (UInt)((one << 32) + (one << 31)) ) ); |
| 94 |
static_assert( ! std::has_single_bit( (UInt)((one << 33) + 1) ) ); |
| 95 |
static_assert( ! std::has_single_bit( (UInt)((one << 33) + (one << 32)) ) ); |
| 96 |
} |
| 97 |
|
| 98 |
if constexpr (std::numeric_limits<UInt>::digits == 64) |
| 99 |
{ |
| 100 |
static_assert( std::has_single_bit( (UInt)(one << 63) ) ); |
| 101 |
|
| 102 |
static_assert( ! std::has_single_bit( (UInt)((one << 63) + 1) ) ); |
| 103 |
static_assert( ! std::has_single_bit( (UInt)((one << 63) + (one << 8)) ) ); |
| 104 |
static_assert( ! std::has_single_bit( (UInt)((one << 63) + (one << 32)) ) ); |
| 105 |
} |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
static_assert( test( (unsigned char)0 ) ); |
| 110 |
static_assert( test( (unsigned short)0 ) ); |
| 111 |
static_assert( test( (unsigned int)0 ) ); |
| 112 |
static_assert( test( (unsigned long)0 ) ); |
| 113 |
static_assert( test( (unsigned long long)0 ) ); |
| 114 |
|
| 115 |
// std::has_single_bit(T) shall not participate in overload resolution |
| 116 |
// unless T is an unsigned integer type. |
| 117 |
struct X { constexpr bool did_not_match() { return true; } }; |
| 118 |
constexpr X test(...) { return X{}; } |
| 119 |
static_assert( test( (bool)0 ).did_not_match() ); |
| 120 |
static_assert( test( (char)0 ).did_not_match() ); |
| 121 |
static_assert( test( (int)0 ).did_not_match() ); |
| 122 |
static_assert( test( (char16_t)0 ).did_not_match() ); |
| 123 |
static_assert( test( (float)0 ).did_not_match() ); |
| 124 |
static_assert( test( (void*)0 ).did_not_match() ); |
| 125 |
static_assert( test( X{} ).did_not_match() ); |
| 126 |
enum E : unsigned { e }; |
| 127 |
static_assert( test( e ).did_not_match() ); |
| 128 |
|
| 129 |
#if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__ |
| 130 |
static_assert( test( (unsigned __int128)0 ) ); |
| 131 |
static_assert( test( (__int128)0 ).did_not_match() ); |
| 132 |
#endif |
| 133 |
#if defined(__GLIBCXX_TYPE_INT_N_0) |
| 134 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) ); |
| 135 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() ); |
| 136 |
#endif |
| 137 |
#if defined(__GLIBCXX_TYPE_INT_N_1) |
| 138 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) ); |
| 139 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() ); |
| 140 |
#endif |
| 141 |
#if defined(__GLIBCXX_TYPE_INT_N_2) |
| 142 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) ); |
| 143 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() ); |
| 144 |
#endif |
| 145 |
#if defined(__GLIBCXX_TYPE_INT_N_3) |
| 146 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) ); |
| 147 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() ); |
| 148 |
#endif |
| 149 |
|
| 150 |
#include <cstddef> |
| 151 |
static_assert( test( (std::byte)0 ).did_not_match() ); |