1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10
11 // template <class T>
12 // constexpr int rotr(T x, unsigned int s) noexcept;
13
14 // Remarks: This function shall not participate in overload resolution unless
15 // T is an unsigned integer type
16
17 #include <bit>
18 #include <cstdint>
19 #include <type_traits>
20 #include <cassert>
21
22 #include "test_macros.h"
23
24 class A{};
25 enum E1 : unsigned char { rEd };
26 enum class E2 : unsigned char { red };
27
28 template <typename T>
constexpr_test()29 constexpr bool constexpr_test()
30 {
31 const T max = std::numeric_limits<T>::max();
32
33 return std::rotr(T(128), 0) == T(128)
34 && std::rotr(T(128), 1) == T( 64)
35 && std::rotr(T(128), 2) == T( 32)
36 && std::rotr(T(128), 3) == T( 16)
37 && std::rotr(T(128), 4) == T( 8)
38 && std::rotr(T(128), 5) == T( 4)
39 && std::rotr(T(128), 6) == T( 2)
40 && std::rotr(T(128), 7) == T( 1)
41 && std::rotr(max, 0) == max
42 && std::rotr(max, 1) == max
43 && std::rotr(max, 2) == max
44 && std::rotr(max, 3) == max
45 && std::rotr(max, 4) == max
46 && std::rotr(max, 5) == max
47 && std::rotr(max, 6) == max
48 && std::rotr(max, 7) == max
49 ;
50 }
51
52
53 template <typename T>
runtime_test()54 void runtime_test()
55 {
56 ASSERT_SAME_TYPE(T, decltype(std::rotr(T(0), 0)));
57 ASSERT_NOEXCEPT( std::rotr(T(0), 0));
58 const T max = std::numeric_limits<T>::max();
59 const T val = std::numeric_limits<T>::max() - 1;
60
61 const T uppers [] = {
62 max, // not used
63 max - max, // 000 .. 0
64 max - (max >> 1), // 800 .. 0
65 max - (max >> 2), // C00 .. 0
66 max - (max >> 3), // E00 .. 0
67 max - (max >> 4), // F00 .. 0
68 max - (max >> 5), // F80 .. 0
69 max - (max >> 6), // FC0 .. 0
70 max - (max >> 7), // FE0 .. 0
71 };
72
73 assert( std::rotr(val, 0) == val);
74 assert( std::rotr(val, 1) == T((val >> 1) + uppers[1]));
75 assert( std::rotr(val, 2) == T((val >> 2) + uppers[2]));
76 assert( std::rotr(val, 3) == T((val >> 3) + uppers[3]));
77 assert( std::rotr(val, 4) == T((val >> 4) + uppers[4]));
78 assert( std::rotr(val, 5) == T((val >> 5) + uppers[5]));
79 assert( std::rotr(val, 6) == T((val >> 6) + uppers[6]));
80 assert( std::rotr(val, 7) == T((val >> 7) + uppers[7]));
81 }
82
main(int,char **)83 int main(int, char**)
84 {
85
86 {
87 auto lambda = [](auto x) -> decltype(std::rotr(x, 1U)) {};
88 using L = decltype(lambda);
89
90 static_assert( std::is_invocable_v<L, unsigned char>, "");
91 static_assert( std::is_invocable_v<L, unsigned int>, "");
92 static_assert( std::is_invocable_v<L, unsigned long>, "");
93 static_assert( std::is_invocable_v<L, unsigned long long>, "");
94
95 static_assert( std::is_invocable_v<L, uint8_t>, "");
96 static_assert( std::is_invocable_v<L, uint16_t>, "");
97 static_assert( std::is_invocable_v<L, uint32_t>, "");
98 static_assert( std::is_invocable_v<L, uint64_t>, "");
99 static_assert( std::is_invocable_v<L, size_t>, "");
100
101 static_assert( std::is_invocable_v<L, uintmax_t>, "");
102 static_assert( std::is_invocable_v<L, uintptr_t>, "");
103
104
105 static_assert(!std::is_invocable_v<L, int>, "");
106 static_assert(!std::is_invocable_v<L, signed int>, "");
107 static_assert(!std::is_invocable_v<L, long>, "");
108 static_assert(!std::is_invocable_v<L, long long>, "");
109
110 static_assert(!std::is_invocable_v<L, int8_t>, "");
111 static_assert(!std::is_invocable_v<L, int16_t>, "");
112 static_assert(!std::is_invocable_v<L, int32_t>, "");
113 static_assert(!std::is_invocable_v<L, int64_t>, "");
114 static_assert(!std::is_invocable_v<L, ptrdiff_t>, "");
115
116 static_assert(!std::is_invocable_v<L, bool>, "");
117 static_assert(!std::is_invocable_v<L, signed char>, "");
118 static_assert(!std::is_invocable_v<L, char16_t>, "");
119 static_assert(!std::is_invocable_v<L, char32_t>, "");
120
121 #ifndef _LIBCPP_HAS_NO_INT128
122 static_assert( std::is_invocable_v<L, __uint128_t>, "");
123 static_assert(!std::is_invocable_v<L, __int128_t>, "");
124 #endif
125
126 static_assert(!std::is_invocable_v<L, A, unsigned>, "");
127 static_assert(!std::is_invocable_v<L, E1, unsigned>, "");
128 static_assert(!std::is_invocable_v<L, E2, unsigned>, "");
129 }
130
131 static_assert(constexpr_test<unsigned char>(), "");
132 static_assert(constexpr_test<unsigned short>(), "");
133 static_assert(constexpr_test<unsigned>(), "");
134 static_assert(constexpr_test<unsigned long>(), "");
135 static_assert(constexpr_test<unsigned long long>(), "");
136
137 static_assert(constexpr_test<uint8_t>(), "");
138 static_assert(constexpr_test<uint16_t>(), "");
139 static_assert(constexpr_test<uint32_t>(), "");
140 static_assert(constexpr_test<uint64_t>(), "");
141 static_assert(constexpr_test<size_t>(), "");
142 static_assert(constexpr_test<uintmax_t>(), "");
143 static_assert(constexpr_test<uintptr_t>(), "");
144
145 #ifndef _LIBCPP_HAS_NO_INT128
146 static_assert(constexpr_test<__uint128_t>(), "");
147 #endif
148
149
150 runtime_test<unsigned char>();
151 runtime_test<unsigned>();
152 runtime_test<unsigned short>();
153 runtime_test<unsigned long>();
154 runtime_test<unsigned long long>();
155
156 runtime_test<uint8_t>();
157 runtime_test<uint16_t>();
158 runtime_test<uint32_t>();
159 runtime_test<uint64_t>();
160 runtime_test<size_t>();
161 runtime_test<uintmax_t>();
162 runtime_test<uintptr_t>();
163
164
165 #ifndef _LIBCPP_HAS_NO_INT128
166 runtime_test<__uint128_t>();
167
168 {
169 __uint128_t val = 168; // 0xA8 (aka 10101000)
170
171 assert( std::rotr(val, 128) == 168);
172 val <<= 32;
173 assert( std::rotr(val, 32) == 168);
174 val <<= 2;
175 assert( std::rotr(val, 33) == 336);
176 val <<= 3;
177 assert( std::rotr(val, 38) == 84);
178 assert( std::rotr(val, 166) == 84);
179 }
180 #endif
181
182 return 0;
183 }
184