]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/bit/bit.rotate/rotl.cc
1 // Copyright (C) 2018-2024 Free Software Foundation, Inc.
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)
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.
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/>.
18 // { dg-do compile { target c++20 } }
23 template<typename UInt
>
25 test_negative_shifts()
27 constexpr unsigned digits
= std::numeric_limits
<UInt
>::digits
;
29 UInt xarr
[] = { (UInt
)-1, 0, 1, 3, 6, 7, 0x10, 0x11, 0x22, 0x44, 0x80 };
30 int sarr
[] = { 1, 4, 5, digits
- 1, digits
};
33 if (std::rotl(x
, -s
) != std::rotr(x
, s
))
38 template<typename UInt
>
41 -> decltype(std::rotl(x
, 0))
43 static_assert( noexcept(std::rotl(x
, 0)) );
45 constexpr unsigned digits
= std::numeric_limits
<UInt
>::digits
;
47 static_assert( std::rotl((UInt
)0, 0) == 0 );
48 static_assert( std::rotl((UInt
)0, 1) == 0 );
49 static_assert( std::rotl((UInt
)0, 4) == 0 );
50 static_assert( std::rotl((UInt
)0, 8) == 0 );
51 static_assert( std::rotl((UInt
)-1, 0) == (UInt
)-1 );
52 static_assert( std::rotl((UInt
)-1, 1) == (UInt
)-1 );
53 static_assert( std::rotl((UInt
)-1, 4) == (UInt
)-1 );
54 static_assert( std::rotl((UInt
)-1, 8) == (UInt
)-1 );
56 static_assert( std::rotl((UInt
)1, 0) == (UInt
)1 << 0 );
57 static_assert( std::rotl((UInt
)1, 1) == (UInt
)1 << 1 );
58 static_assert( std::rotl((UInt
)1, 4) == (UInt
)1 << 4 );
59 static_assert( std::rotl((UInt
)1, digits
) == (UInt
)1 );
60 static_assert( std::rotl((UInt
)7, digits
) == (UInt
)7 );
61 static_assert( std::rotl((UInt
)6, digits
- 1) == (UInt
)3 );
62 static_assert( std::rotl((UInt
)3, 6) == (UInt
)3 << 6 );
64 static_assert( std::rotl((UInt
)0b0110'1100, 1) == 0b1101'1000 );
65 static_assert( std::rotl((UInt
)0b0110'1100, digits
- 1) == 0b0011'0110 );
67 static_assert( std::rotl((UInt
)0x01, 0 ) == 0x01 );
68 static_assert( std::rotl((UInt
)0x10, 0 ) == 0x10 );
69 static_assert( std::rotl((UInt
)0x10, 1 ) == 0x20 );
70 static_assert( std::rotl((UInt
)0x10, 2 ) == 0x40 );
71 static_assert( std::rotl((UInt
)0x10, 3 ) == 0x80 );
72 static_assert( std::rotl((UInt
)0x11, 1 ) == 0x22 );
73 static_assert( std::rotl((UInt
)0x11, 2 ) == 0x44 );
75 if constexpr (std::numeric_limits
<UInt
>::digits
> 8)
77 static_assert( std::rotl((UInt
)0b0011'0111, 3) == 0b1'1011'1000 );
78 static_assert( std::rotl((UInt
)0b1010'0101, 4) == 0b1010'0101'0000 );
81 static_assert( test_negative_shifts
<UInt
>() );
86 static_assert( test( (unsigned char)0 ) );
87 static_assert( test( (unsigned short)0 ) );
88 static_assert( test( (unsigned int)0 ) );
89 static_assert( test( (unsigned long)0 ) );
90 static_assert( test( (unsigned long long)0 ) );
92 // std::rotl(T) shall not participate in overload resolution
93 // unless T is an unsigned integer type.
94 struct X
{ constexpr bool did_not_match() { return true; } };
95 constexpr X
test(...) { return X
{}; }
96 static_assert( test( (bool)0 ).did_not_match() );
97 static_assert( test( (char)0 ).did_not_match() );
98 static_assert( test( (int)0 ).did_not_match() );
99 static_assert( test( (char16_t
)0 ).did_not_match() );
100 static_assert( test( (float)0 ).did_not_match() );
101 static_assert( test( (void*)0 ).did_not_match() );
102 static_assert( test( X
{} ).did_not_match() );
103 enum E
: unsigned { e
};
104 static_assert( test( e
).did_not_match() );
106 #if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__
107 static_assert( test( (unsigned __int128
)0 ) );
108 static_assert( test( (__int128
)0 ).did_not_match() );
110 #if defined(__GLIBCXX_TYPE_INT_N_0)
111 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0
)0 ) );
112 static_assert( test( (__GLIBCXX_TYPE_INT_N_0
)0 ).did_not_match() );
114 #if defined(__GLIBCXX_TYPE_INT_N_1)
115 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1
)0 ) );
116 static_assert( test( (__GLIBCXX_TYPE_INT_N_1
)0 ).did_not_match() );
118 #if defined(__GLIBCXX_TYPE_INT_N_2)
119 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2
)0 ) );
120 static_assert( test( (__GLIBCXX_TYPE_INT_N_2
)0 ).did_not_match() );
122 #if defined(__GLIBCXX_TYPE_INT_N_3)
123 static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3
)0 ) );
124 static_assert( test( (__GLIBCXX_TYPE_INT_N_3
)0 ).did_not_match() );
128 static_assert( test( (std::byte
)0 ).did_not_match() );