1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 // constexpr T bit_ceil(T x) noexcept;
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.
21 #include <type_traits>
23 #include "test_macros.h"
26 enum E1
: unsigned char { rEd
};
27 enum class E2
: unsigned char { red
};
32 ASSERT_SAME_TYPE(decltype(std::bit_ceil(T())), T
);
33 LIBCPP_ASSERT_NOEXCEPT(std::bit_ceil(T()));
35 assert(std::bit_ceil(T(0)) == T(1));
36 assert(std::bit_ceil(T(1)) == T(1));
37 assert(std::bit_ceil(T(2)) == T(2));
38 assert(std::bit_ceil(T(3)) == T(4));
39 assert(std::bit_ceil(T(4)) == T(4));
40 assert(std::bit_ceil(T(5)) == T(8));
41 assert(std::bit_ceil(T(6)) == T(8));
42 assert(std::bit_ceil(T(7)) == T(8));
43 assert(std::bit_ceil(T(8)) == T(8));
44 assert(std::bit_ceil(T(9)) == T(16));
45 assert(std::bit_ceil(T(60)) == T(64));
46 assert(std::bit_ceil(T(61)) == T(64));
47 assert(std::bit_ceil(T(62)) == T(64));
48 assert(std::bit_ceil(T(63)) == T(64));
49 assert(std::bit_ceil(T(64)) == T(64));
50 assert(std::bit_ceil(T(65)) == T(128));
51 assert(std::bit_ceil(T(66)) == T(128));
52 assert(std::bit_ceil(T(67)) == T(128));
53 assert(std::bit_ceil(T(68)) == T(128));
54 assert(std::bit_ceil(T(69)) == T(128));
56 #ifndef TEST_HAS_NO_INT128
57 if constexpr (std::is_same_v
<T
, __uint128_t
>) {
61 assert(std::bit_ceil(val
) == ceil
);
62 assert(std::bit_ceil(val
<< 32) == (ceil
<< 32));
63 assert(std::bit_ceil((val
<< 64) | 0x1) == (ceil
<< 64));
64 assert(std::bit_ceil((val
<< 72) | 0x1) == (ceil
<< 72));
65 assert(std::bit_ceil((val
<< 100) | 0x1) == (ceil
<< 100));
75 auto lambda
= [](auto x
) -> decltype(std::bit_ceil(x
)) {};
76 using L
= decltype(lambda
);
78 static_assert(!std::is_invocable_v
<L
, signed char>);
79 static_assert(!std::is_invocable_v
<L
, short>);
80 static_assert(!std::is_invocable_v
<L
, int>);
81 static_assert(!std::is_invocable_v
<L
, long>);
82 static_assert(!std::is_invocable_v
<L
, long long>);
83 #ifndef TEST_HAS_NO_INT128
84 static_assert(!std::is_invocable_v
<L
, __int128_t
>);
87 static_assert(!std::is_invocable_v
<L
, std::int8_t>);
88 static_assert(!std::is_invocable_v
<L
, std::int16_t>);
89 static_assert(!std::is_invocable_v
<L
, std::int32_t>);
90 static_assert(!std::is_invocable_v
<L
, std::int64_t>);
91 static_assert(!std::is_invocable_v
<L
, std::intmax_t>);
92 static_assert(!std::is_invocable_v
<L
, std::intptr_t>);
93 static_assert(!std::is_invocable_v
<L
, std::ptrdiff_t>);
95 static_assert(!std::is_invocable_v
<L
, bool>);
96 static_assert(!std::is_invocable_v
<L
, char>);
97 static_assert(!std::is_invocable_v
<L
, wchar_t>);
98 #ifndef TEST_HAS_NO_CHAR8_T
99 static_assert(!std::is_invocable_v
<L
, char8_t
>);
101 static_assert(!std::is_invocable_v
<L
, char16_t
>);
102 static_assert(!std::is_invocable_v
<L
, char32_t
>);
104 static_assert(!std::is_invocable_v
<L
, A
>);
105 static_assert(!std::is_invocable_v
<L
, A
*>);
106 static_assert(!std::is_invocable_v
<L
, E1
>);
107 static_assert(!std::is_invocable_v
<L
, E2
>);
110 static_assert(test
<unsigned char>());
111 static_assert(test
<unsigned short>());
112 static_assert(test
<unsigned int>());
113 static_assert(test
<unsigned long>());
114 static_assert(test
<unsigned long long>());
115 #ifndef TEST_HAS_NO_INT128
116 static_assert(test
<__uint128_t
>());
118 static_assert(test
<std::uint8_t>());
119 static_assert(test
<std::uint16_t>());
120 static_assert(test
<std::uint32_t>());
121 static_assert(test
<std::uint64_t>());
122 static_assert(test
<std::uintmax_t>());
123 static_assert(test
<std::uintptr_t>());
124 static_assert(test
<std::size_t>());
126 test
<unsigned char>();
127 test
<unsigned short>();
128 test
<unsigned int>();
129 test
<unsigned long>();
130 test
<unsigned long long>();
131 #ifndef TEST_HAS_NO_INT128
134 test
<std::uint8_t>();
135 test
<std::uint16_t>();
136 test
<std::uint32_t>();
137 test
<std::uint64_t>();
138 test
<std::uintmax_t>();
139 test
<std::uintptr_t>();