| 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 bool |
| 25 |
test_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 |
|
| 38 |
template<typename UInt> |
| 39 |
constexpr auto |
| 40 |
test(UInt x) |
| 41 |
-> decltype(std::rotr(x, 0)) |
| 42 |
{ |
| 43 |
static_assert( noexcept(std::rotr(x, 0)) ); |
| 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 |
|
| 83 |
static_assert( test_negative_shifts<UInt>() ); |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
static_assert( test( (unsigned char)0 ) ); |
| 89 |
static_assert( test( (unsigned short)0 ) ); |
| 90 |
static_assert( test( (unsigned int)0 ) ); |
| 91 |
static_assert( test( (unsigned long)0 ) ); |
| 92 |
static_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. |
| 96 |
struct X { constexpr bool did_not_match() { return true; } }; |
| 97 |
constexpr X test(...) { return X{}; } |
| 98 |
static_assert( test( (bool)0 ).did_not_match() ); |
| 99 |
static_assert( test( (char)0 ).did_not_match() ); |
| 100 |
static_assert( test( (int)0 ).did_not_match() ); |
| 101 |
static_assert( test( (char16_t)0 ).did_not_match() ); |
| 102 |
static_assert( test( (float)0 ).did_not_match() ); |
| 103 |
static_assert( test( (void*)0 ).did_not_match() ); |
| 104 |
static_assert( test( X{} ).did_not_match() ); |
| 105 |
enum E : unsigned { e }; |
| 106 |
static_assert( test( e ).did_not_match() ); |
| 107 |
|
| 108 |
#if !defined(__STRICT_ANSI__) && defined __SIZEOF_INT128__ |
| 109 |
static_assert( test( (unsigned __int128)0 ) ); |
| 110 |
static_assert( test( (__int128)0 ).did_not_match() ); |
| 111 |
#endif |
| 112 |
#if defined(__GLIBCXX_TYPE_INT_N_0) |
| 113 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_0)0 ) ); |
| 114 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_0)0 ).did_not_match() ); |
| 115 |
#endif |
| 116 |
#if defined(__GLIBCXX_TYPE_INT_N_1) |
| 117 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_1)0 ) ); |
| 118 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_1)0 ).did_not_match() ); |
| 119 |
#endif |
| 120 |
#if defined(__GLIBCXX_TYPE_INT_N_2) |
| 121 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_2)0 ) ); |
| 122 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_2)0 ).did_not_match() ); |
| 123 |
#endif |
| 124 |
#if defined(__GLIBCXX_TYPE_INT_N_3) |
| 125 |
static_assert( test( (unsigned __GLIBCXX_TYPE_INT_N_3)0 ) ); |
| 126 |
static_assert( test( (__GLIBCXX_TYPE_INT_N_3)0 ).did_not_match() ); |
| 127 |
#endif |
| 128 |
|
| 129 |
#include <cstddef> |
| 130 |
static_assert( test( (std::byte)0 ).did_not_match() ); |