]>
Commit | Line | Data |
---|---|---|
a945c346 | 1 | // Copyright (C) 2018-2024 Free Software Foundation, Inc. |
f3e91052 JW |
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 | ||
bb2dd761 | 18 | // { dg-do compile { target c++20 } } |
f3e91052 JW |
19 | |
20 | #include <bit> | |
c03b53da | 21 | #include <limits> |
f3e91052 JW |
22 | |
23 | template<typename UInt> | |
24 | constexpr auto | |
25 | test(UInt x) | |
9866abe3 | 26 | -> decltype(std::bit_width(x)) |
f3e91052 | 27 | { |
9866abe3 | 28 | static_assert( noexcept(std::bit_width(x)) ); |
f3e91052 | 29 | |
9866abe3 JW |
30 | static_assert( std::bit_width(UInt(0)) == 0 ); |
31 | static_assert( std::bit_width(UInt(1)) == 1 ); | |
32 | static_assert( std::bit_width(UInt(2)) == 2 ); | |
33 | static_assert( std::bit_width(UInt(3)) == 2 ); | |
34 | static_assert( std::bit_width(UInt(4)) == 3 ); | |
35 | static_assert( std::bit_width(UInt(0x11)) == 5 ); | |
36 | static_assert( std::bit_width(UInt(0x20)) == 6 ); | |
f3e91052 JW |
37 | |
38 | if constexpr (std::numeric_limits<UInt>::digits > 8) | |
39 | { | |
9866abe3 JW |
40 | static_assert( std::bit_width(UInt(0x201)) == 10 ); |
41 | static_assert( std::bit_width(UInt(0x8ff)) == 12 ); | |
42 | static_assert( std::bit_width(UInt(0x1000)) == 13 ); | |
f3e91052 JW |
43 | } |
44 | ||
45 | if constexpr (std::numeric_limits<UInt>::digits > 32) | |
46 | { | |
9866abe3 JW |
47 | static_assert( std::bit_width(UInt(0xabcdef)) == 24 ); |
48 | static_assert( std::bit_width(UInt(0x1000000)) == 25 ); | |
49 | static_assert( std::bit_width(UInt(0x1000001)) == 25 ); | |
f3e91052 JW |
50 | } |
51 | ||
52 | if constexpr (std::numeric_limits<UInt>::digits > 64) | |
53 | { | |
9866abe3 JW |
54 | static_assert( std::bit_width(UInt(1) << 64) == 65 ); |
55 | static_assert( std::bit_width(UInt(3) << 64) == 66 ); | |
f3e91052 JW |
56 | } |
57 | ||
58 | return true; | |
59 | } | |
60 | ||
61 | static_assert( test( (unsigned char)0 ) ); | |
62 | static_assert( test( (unsigned short)0 ) ); | |
63 | static_assert( test( (unsigned int)0 ) ); | |
64 | static_assert( test( (unsigned long)0 ) ); | |
65 | static_assert( test( (unsigned long long)0 ) ); | |
66 | ||
9866abe3 | 67 | // std::bit_width(T) shall not participate in overload resolution |
f3e91052 JW |
68 | // unless T is an unsigned integer type. |
69 | struct X { constexpr bool did_not_match() { return true; } }; | |
70 | constexpr X test(...) { return X{}; } | |
71 | static_assert( test( (bool)0 ).did_not_match() ); | |
72 | static_assert( test( (char)0 ).did_not_match() ); | |
73 | static_assert( test( (int)0 ).did_not_match() ); | |
74 | static_assert( test( (char16_t)0 ).did_not_match() ); | |
75 | static_assert( test( (float)0 ).did_not_match() ); | |
76 | static_assert( test( (void*)0 ).did_not_match() ); | |
77 | static_assert( test( X{} ).did_not_match() ); | |
78 | enum E : unsigned { e }; | |
79 | static_assert( test( e ).did_not_match() ); | |
80 | ||
29a9de9b | 81 | #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__ |
f3e91052 JW |
82 | static_assert( test( (unsigned __int128)0 ) ); |
83 | static_assert( test( (__int128)0 ).did_not_match() ); | |
84 | #endif | |
85 | #if defined(__GLIBCXX_TYPE_INT_N_0) | |
86 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) ); | |
87 | static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() ); | |
88 | #endif | |
89 | #if defined(__GLIBCXX_TYPE_INT_N_1) | |
90 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) ); | |
91 | static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() ); | |
92 | #endif | |
93 | #if defined(__GLIBCXX_TYPE_INT_N_2) | |
94 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) ); | |
95 | static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() ); | |
96 | #endif | |
29a9de9b JW |
97 | #if defined(__GLIBCXX_TYPE_INT_N_3) |
98 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) ); | |
99 | static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() ); | |
100 | #endif | |
47f79054 JW |
101 | |
102 | #include <cstddef> | |
103 | static_assert( test( (std::byte)0 ).did_not_match() ); |