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