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