Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 9 | // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 10 | |
| 11 | // template <class T> |
| 12 | // constexpr int rotl(T x, unsigned int s) noexcept; |
| 13 | |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 14 | // Remarks: This function shall not participate in overload resolution unless |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 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> |
| 29 | constexpr bool constexpr_test() |
| 30 | { |
| 31 | const T max = std::numeric_limits<T>::max(); |
| 32 | return std::rotl(T(1), 0) == T( 1) |
| 33 | && std::rotl(T(1), 1) == T( 2) |
| 34 | && std::rotl(T(1), 2) == T( 4) |
| 35 | && std::rotl(T(1), 3) == T( 8) |
| 36 | && std::rotl(T(1), 4) == T( 16) |
| 37 | && std::rotl(T(1), 5) == T( 32) |
| 38 | && std::rotl(T(1), 6) == T( 64) |
| 39 | && std::rotl(T(1), 7) == T(128) |
| 40 | && std::rotl(max, 0) == max |
| 41 | && std::rotl(max, 1) == max |
| 42 | && std::rotl(max, 2) == max |
| 43 | && std::rotl(max, 3) == max |
| 44 | && std::rotl(max, 4) == max |
| 45 | && std::rotl(max, 5) == max |
| 46 | && std::rotl(max, 6) == max |
| 47 | && std::rotl(max, 7) == max |
| 48 | ; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | template <typename T> |
| 53 | void runtime_test() |
| 54 | { |
| 55 | ASSERT_SAME_TYPE(T, decltype(std::rotl(T(0), 0))); |
| 56 | ASSERT_NOEXCEPT( std::rotl(T(0), 0)); |
| 57 | const T val = std::numeric_limits<T>::max() - 1; |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 58 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 59 | assert( std::rotl(val, 0) == val); |
| 60 | assert( std::rotl(val, 1) == T((val << 1) + 1)); |
| 61 | assert( std::rotl(val, 2) == T((val << 2) + 3)); |
| 62 | assert( std::rotl(val, 3) == T((val << 3) + 7)); |
| 63 | assert( std::rotl(val, 4) == T((val << 4) + 15)); |
| 64 | assert( std::rotl(val, 5) == T((val << 5) + 31)); |
| 65 | assert( std::rotl(val, 6) == T((val << 6) + 63)); |
| 66 | assert( std::rotl(val, 7) == T((val << 7) + 127)); |
| 67 | } |
| 68 | |
| 69 | int main() |
| 70 | { |
| 71 | |
| 72 | { |
| 73 | auto lambda = [](auto x) -> decltype(std::rotl(x, 1U)) {}; |
| 74 | using L = decltype(lambda); |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 75 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 76 | static_assert( std::is_invocable_v<L, unsigned char>, ""); |
| 77 | static_assert( std::is_invocable_v<L, unsigned int>, ""); |
| 78 | static_assert( std::is_invocable_v<L, unsigned long>, ""); |
| 79 | static_assert( std::is_invocable_v<L, unsigned long long>, ""); |
| 80 | |
| 81 | static_assert( std::is_invocable_v<L, uint8_t>, ""); |
| 82 | static_assert( std::is_invocable_v<L, uint16_t>, ""); |
| 83 | static_assert( std::is_invocable_v<L, uint32_t>, ""); |
| 84 | static_assert( std::is_invocable_v<L, uint64_t>, ""); |
| 85 | static_assert( std::is_invocable_v<L, size_t>, ""); |
| 86 | |
| 87 | static_assert( std::is_invocable_v<L, uintmax_t>, ""); |
| 88 | static_assert( std::is_invocable_v<L, uintptr_t>, ""); |
| 89 | |
| 90 | |
| 91 | static_assert(!std::is_invocable_v<L, int>, ""); |
| 92 | static_assert(!std::is_invocable_v<L, signed int>, ""); |
| 93 | static_assert(!std::is_invocable_v<L, long>, ""); |
| 94 | static_assert(!std::is_invocable_v<L, long long>, ""); |
| 95 | |
| 96 | static_assert(!std::is_invocable_v<L, int8_t>, ""); |
| 97 | static_assert(!std::is_invocable_v<L, int16_t>, ""); |
| 98 | static_assert(!std::is_invocable_v<L, int32_t>, ""); |
| 99 | static_assert(!std::is_invocable_v<L, int64_t>, ""); |
| 100 | static_assert(!std::is_invocable_v<L, ptrdiff_t>, ""); |
| 101 | |
| 102 | static_assert(!std::is_invocable_v<L, bool>, ""); |
| 103 | static_assert(!std::is_invocable_v<L, signed char>, ""); |
| 104 | static_assert(!std::is_invocable_v<L, char16_t>, ""); |
| 105 | static_assert(!std::is_invocable_v<L, char32_t>, ""); |
| 106 | |
| 107 | #ifndef _LIBCPP_HAS_NO_INT128 |
| 108 | static_assert( std::is_invocable_v<L, __uint128_t>, ""); |
| 109 | static_assert(!std::is_invocable_v<L, __int128_t>, ""); |
| 110 | #endif |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 111 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 112 | static_assert(!std::is_invocable_v<L, A>, ""); |
| 113 | static_assert(!std::is_invocable_v<L, E1>, ""); |
| 114 | static_assert(!std::is_invocable_v<L, E2>, ""); |
| 115 | } |
Louis Dionne | 6b77ebd | 2019-10-23 10:40:15 -0700 | [diff] [blame] | 116 | |
Marshall Clow | a5c3485 | 2019-07-01 23:00:32 +0000 | [diff] [blame] | 117 | static_assert(constexpr_test<unsigned char>(), ""); |
| 118 | static_assert(constexpr_test<unsigned short>(), ""); |
| 119 | static_assert(constexpr_test<unsigned>(), ""); |
| 120 | static_assert(constexpr_test<unsigned long>(), ""); |
| 121 | static_assert(constexpr_test<unsigned long long>(), ""); |
| 122 | |
| 123 | static_assert(constexpr_test<uint8_t>(), ""); |
| 124 | static_assert(constexpr_test<uint16_t>(), ""); |
| 125 | static_assert(constexpr_test<uint32_t>(), ""); |
| 126 | static_assert(constexpr_test<uint64_t>(), ""); |
| 127 | static_assert(constexpr_test<size_t>(), ""); |
| 128 | static_assert(constexpr_test<uintmax_t>(), ""); |
| 129 | static_assert(constexpr_test<uintptr_t>(), ""); |
| 130 | |
| 131 | #ifndef _LIBCPP_HAS_NO_INT128 |
| 132 | static_assert(constexpr_test<__uint128_t>(), ""); |
| 133 | #endif |
| 134 | |
| 135 | |
| 136 | runtime_test<unsigned char>(); |
| 137 | runtime_test<unsigned short>(); |
| 138 | runtime_test<unsigned>(); |
| 139 | runtime_test<unsigned long>(); |
| 140 | runtime_test<unsigned long long>(); |
| 141 | |
| 142 | runtime_test<uint8_t>(); |
| 143 | runtime_test<uint16_t>(); |
| 144 | runtime_test<uint32_t>(); |
| 145 | runtime_test<uint64_t>(); |
| 146 | runtime_test<size_t>(); |
| 147 | runtime_test<uintmax_t>(); |
| 148 | runtime_test<uintptr_t>(); |
| 149 | |
| 150 | #ifndef _LIBCPP_HAS_NO_INT128 |
| 151 | runtime_test<__uint128_t>(); |
| 152 | |
| 153 | { |
| 154 | __uint128_t val = 168; // 0xA8 (aka 10101000) |
| 155 | |
| 156 | assert( std::rotl(val, 128) == 168); |
| 157 | val <<= 32; |
| 158 | assert( std::rotl(val, 96) == 168); |
| 159 | val <<= 2; |
| 160 | assert( std::rotl(val, 95) == 336); |
| 161 | val <<= 3; |
| 162 | assert( std::rotl(val, 90) == 84); |
| 163 | assert( std::rotl(val, 218) == 84); |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | } |