]>
Commit | Line | Data |
---|---|---|
7adcbafe | 1 | // Copyright (C) 2018-2022 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 | ||
f3e91052 JW |
18 | // { dg-options "-std=gnu++2a" } |
19 | // { dg-do compile { target c++2a } } | |
20 | ||
21 | #include <bit> | |
c03b53da | 22 | #include <limits> |
f3e91052 | 23 | |
f35da524 JW |
24 | template<typename UInt> |
25 | constexpr bool | |
26 | test_negative_shifts() | |
27 | { | |
28 | constexpr unsigned digits = std::numeric_limits<UInt>::digits; | |
29 | ||
30 | UInt xarr[] = { (UInt)-1, 0, 1, 3, 6, 7, 0x10, 0x11, 0x22, 0x44, 0x80 }; | |
31 | int sarr[] = { 1, 4, 5, digits - 1, digits }; | |
32 | for (UInt x : xarr) | |
33 | for (int s : sarr) | |
34 | if (std::rotl(x, -s) != std::rotr(x, s)) | |
35 | return false; | |
36 | return true; | |
37 | } | |
38 | ||
f3e91052 JW |
39 | template<typename UInt> |
40 | constexpr auto | |
41 | test(UInt x) | |
f35da524 | 42 | -> decltype(std::rotl(x, 0)) |
f3e91052 | 43 | { |
f35da524 | 44 | static_assert( noexcept(std::rotl(x, 0)) ); |
f3e91052 JW |
45 | |
46 | constexpr unsigned digits = std::numeric_limits<UInt>::digits; | |
47 | ||
48 | static_assert( std::rotl((UInt)0, 0) == 0 ); | |
49 | static_assert( std::rotl((UInt)0, 1) == 0 ); | |
50 | static_assert( std::rotl((UInt)0, 4) == 0 ); | |
51 | static_assert( std::rotl((UInt)0, 8) == 0 ); | |
52 | static_assert( std::rotl((UInt)-1, 0) == (UInt)-1 ); | |
53 | static_assert( std::rotl((UInt)-1, 1) == (UInt)-1 ); | |
54 | static_assert( std::rotl((UInt)-1, 4) == (UInt)-1 ); | |
55 | static_assert( std::rotl((UInt)-1, 8) == (UInt)-1 ); | |
56 | ||
57 | static_assert( std::rotl((UInt)1, 0) == (UInt)1 << 0 ); | |
58 | static_assert( std::rotl((UInt)1, 1) == (UInt)1 << 1 ); | |
59 | static_assert( std::rotl((UInt)1, 4) == (UInt)1 << 4 ); | |
60 | static_assert( std::rotl((UInt)1, digits) == (UInt)1 ); | |
61 | static_assert( std::rotl((UInt)7, digits) == (UInt)7 ); | |
62 | static_assert( std::rotl((UInt)6, digits - 1) == (UInt)3 ); | |
63 | static_assert( std::rotl((UInt)3, 6) == (UInt)3 << 6 ); | |
64 | ||
65 | static_assert( std::rotl((UInt)0b0110'1100, 1) == 0b1101'1000 ); | |
66 | static_assert( std::rotl((UInt)0b0110'1100, digits - 1) == 0b0011'0110 ); | |
67 | ||
68 | static_assert( std::rotl((UInt)0x01, 0 ) == 0x01 ); | |
69 | static_assert( std::rotl((UInt)0x10, 0 ) == 0x10 ); | |
70 | static_assert( std::rotl((UInt)0x10, 1 ) == 0x20 ); | |
71 | static_assert( std::rotl((UInt)0x10, 2 ) == 0x40 ); | |
72 | static_assert( std::rotl((UInt)0x10, 3 ) == 0x80 ); | |
73 | static_assert( std::rotl((UInt)0x11, 1 ) == 0x22 ); | |
74 | static_assert( std::rotl((UInt)0x11, 2 ) == 0x44 ); | |
75 | ||
76 | if constexpr (std::numeric_limits<UInt>::digits > 8) | |
77 | { | |
78 | static_assert( std::rotl((UInt)0b0011'0111, 3) == 0b1'1011'1000 ); | |
79 | static_assert( std::rotl((UInt)0b1010'0101, 4) == 0b1010'0101'0000 ); | |
80 | } | |
81 | ||
f35da524 JW |
82 | static_assert( test_negative_shifts<UInt>() ); |
83 | ||
f3e91052 JW |
84 | return true; |
85 | } | |
86 | ||
87 | static_assert( test( (unsigned char)0 ) ); | |
88 | static_assert( test( (unsigned short)0 ) ); | |
89 | static_assert( test( (unsigned int)0 ) ); | |
90 | static_assert( test( (unsigned long)0 ) ); | |
91 | static_assert( test( (unsigned long long)0 ) ); | |
92 | ||
93 | // std::rotl(T) shall not participate in overload resolution | |
94 | // unless T is an unsigned integer type. | |
95 | struct X { constexpr bool did_not_match() { return true; } }; | |
96 | constexpr X test(...) { return X{}; } | |
97 | static_assert( test( (bool)0 ).did_not_match() ); | |
98 | static_assert( test( (char)0 ).did_not_match() ); | |
99 | static_assert( test( (int)0 ).did_not_match() ); | |
100 | static_assert( test( (char16_t)0 ).did_not_match() ); | |
101 | static_assert( test( (float)0 ).did_not_match() ); | |
102 | static_assert( test( (void*)0 ).did_not_match() ); | |
103 | static_assert( test( X{} ).did_not_match() ); | |
104 | enum E : unsigned { e }; | |
105 | static_assert( test( e ).did_not_match() ); | |
106 | ||
29a9de9b | 107 | #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__ |
f3e91052 JW |
108 | static_assert( test( (unsigned __int128)0 ) ); |
109 | static_assert( test( (__int128)0 ).did_not_match() ); | |
110 | #endif | |
111 | #if defined(__GLIBCXX_TYPE_INT_N_0) | |
112 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) ); | |
113 | static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() ); | |
114 | #endif | |
115 | #if defined(__GLIBCXX_TYPE_INT_N_1) | |
116 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) ); | |
117 | static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() ); | |
118 | #endif | |
119 | #if defined(__GLIBCXX_TYPE_INT_N_2) | |
120 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) ); | |
121 | static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() ); | |
122 | #endif | |
29a9de9b JW |
123 | #if defined(__GLIBCXX_TYPE_INT_N_3) |
124 | static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) ); | |
125 | static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() ); | |
126 | #endif | |
47f79054 JW |
127 | |
128 | #include <cstddef> | |
129 | static_assert( test( (std::byte)0 ).did_not_match() ); |