| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 | |
| 11 | // template <class T> |
| 12 | // constexpr T bit_ceil(T x) noexcept; |
| 13 | |
| 14 | // Constraints: T is an unsigned integer type |
| 15 | // Returns: The minimal value y such that has_single_bit(y) is true and y >= x; |
| 16 | // if y is not representable as a value of type T, the result is an unspecified value. |
| 17 | |
| 18 | #include <bit> |
| 19 | #include <cassert> |
| 20 | #include <cstddef> |
| 21 | #include <cstdint> |
| 22 | #include <type_traits> |
| 23 | |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | struct A {}; |
| 27 | enum E1 : unsigned char { rEd }; |
| 28 | enum class E2 : unsigned char { red }; |
| 29 | |
| 30 | template <class T> |
| 31 | constexpr bool test() |
| 32 | { |
| 33 | ASSERT_SAME_TYPE(decltype(std::bit_ceil(T())), T); |
| 34 | LIBCPP_ASSERT_NOEXCEPT(std::bit_ceil(T())); |
| 35 | |
| 36 | assert(std::bit_ceil(T(0)) == T(1)); |
| 37 | assert(std::bit_ceil(T(1)) == T(1)); |
| 38 | assert(std::bit_ceil(T(2)) == T(2)); |
| 39 | assert(std::bit_ceil(T(3)) == T(4)); |
| 40 | assert(std::bit_ceil(T(4)) == T(4)); |
| 41 | assert(std::bit_ceil(T(5)) == T(8)); |
| 42 | assert(std::bit_ceil(T(6)) == T(8)); |
| 43 | assert(std::bit_ceil(T(7)) == T(8)); |
| 44 | assert(std::bit_ceil(T(8)) == T(8)); |
| 45 | assert(std::bit_ceil(T(9)) == T(16)); |
| 46 | assert(std::bit_ceil(T(60)) == T(64)); |
| 47 | assert(std::bit_ceil(T(61)) == T(64)); |
| 48 | assert(std::bit_ceil(T(62)) == T(64)); |
| 49 | assert(std::bit_ceil(T(63)) == T(64)); |
| 50 | assert(std::bit_ceil(T(64)) == T(64)); |
| 51 | assert(std::bit_ceil(T(65)) == T(128)); |
| 52 | assert(std::bit_ceil(T(66)) == T(128)); |
| 53 | assert(std::bit_ceil(T(67)) == T(128)); |
| 54 | assert(std::bit_ceil(T(68)) == T(128)); |
| 55 | assert(std::bit_ceil(T(69)) == T(128)); |
| 56 | |
| 57 | #ifndef TEST_HAS_NO_INT128 |
| 58 | if constexpr (std::is_same_v<T, __uint128_t>) { |
| 59 | T val = 168; |
| 60 | T ceil = 256; |
| 61 | |
| 62 | assert(std::bit_ceil(val) == ceil); |
| 63 | assert(std::bit_ceil(val << 32) == (ceil << 32)); |
| 64 | assert(std::bit_ceil((val << 64) | 0x1) == (ceil << 64)); |
| 65 | assert(std::bit_ceil((val << 72) | 0x1) == (ceil << 72)); |
| 66 | assert(std::bit_ceil((val << 100) | 0x1) == (ceil << 100)); |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | int main(int, char**) |
| 74 | { |
| 75 | { |
| 76 | auto lambda = [](auto x) -> decltype(std::bit_ceil(x)) {}; |
| 77 | using L = decltype(lambda); |
| 78 | |
| 79 | static_assert(!std::is_invocable_v<L, signed char>); |
| 80 | static_assert(!std::is_invocable_v<L, short>); |
| 81 | static_assert(!std::is_invocable_v<L, int>); |
| 82 | static_assert(!std::is_invocable_v<L, long>); |
| 83 | static_assert(!std::is_invocable_v<L, long long>); |
| 84 | #ifndef TEST_HAS_NO_INT128 |
| 85 | static_assert(!std::is_invocable_v<L, __int128_t>); |
| 86 | #endif |
| 87 | |
| 88 | static_assert(!std::is_invocable_v<L, std::int8_t>); |
| 89 | static_assert(!std::is_invocable_v<L, std::int16_t>); |
| 90 | static_assert(!std::is_invocable_v<L, std::int32_t>); |
| 91 | static_assert(!std::is_invocable_v<L, std::int64_t>); |
| 92 | static_assert(!std::is_invocable_v<L, std::intmax_t>); |
| 93 | static_assert(!std::is_invocable_v<L, std::intptr_t>); |
| 94 | static_assert(!std::is_invocable_v<L, std::ptrdiff_t>); |
| 95 | |
| 96 | static_assert(!std::is_invocable_v<L, bool>); |
| 97 | static_assert(!std::is_invocable_v<L, char>); |
| 98 | static_assert(!std::is_invocable_v<L, wchar_t>); |
| 99 | #ifndef TEST_HAS_NO_CHAR8_T |
| 100 | static_assert(!std::is_invocable_v<L, char8_t>); |
| 101 | #endif |
| 102 | static_assert(!std::is_invocable_v<L, char16_t>); |
| 103 | static_assert(!std::is_invocable_v<L, char32_t>); |
| 104 | |
| 105 | static_assert(!std::is_invocable_v<L, A>); |
| 106 | static_assert(!std::is_invocable_v<L, A*>); |
| 107 | static_assert(!std::is_invocable_v<L, E1>); |
| 108 | static_assert(!std::is_invocable_v<L, E2>); |
| 109 | } |
| 110 | |
| 111 | static_assert(test<unsigned char>()); |
| 112 | static_assert(test<unsigned short>()); |
| 113 | static_assert(test<unsigned int>()); |
| 114 | static_assert(test<unsigned long>()); |
| 115 | static_assert(test<unsigned long long>()); |
| 116 | #ifndef TEST_HAS_NO_INT128 |
| 117 | static_assert(test<__uint128_t>()); |
| 118 | #endif |
| 119 | static_assert(test<std::uint8_t>()); |
| 120 | static_assert(test<std::uint16_t>()); |
| 121 | static_assert(test<std::uint32_t>()); |
| 122 | static_assert(test<std::uint64_t>()); |
| 123 | static_assert(test<std::uintmax_t>()); |
| 124 | static_assert(test<std::uintptr_t>()); |
| 125 | static_assert(test<std::size_t>()); |
| 126 | |
| 127 | test<unsigned char>(); |
| 128 | test<unsigned short>(); |
| 129 | test<unsigned int>(); |
| 130 | test<unsigned long>(); |
| 131 | test<unsigned long long>(); |
| 132 | #ifndef TEST_HAS_NO_INT128 |
| 133 | test<__uint128_t>(); |
| 134 | #endif |
| 135 | test<std::uint8_t>(); |
| 136 | test<std::uint16_t>(); |
| 137 | test<std::uint32_t>(); |
| 138 | test<std::uint64_t>(); |
| 139 | test<std::uintmax_t>(); |
| 140 | test<std::uintptr_t>(); |
| 141 | test<std::size_t>(); |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |