| 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 <cstdint> |
| 21 |
#include <type_traits> |
| 22 |
|
| 23 |
#include "test_macros.h" |
| 24 |
|
| 25 |
struct A {}; |
| 26 |
enum E1 : unsigned char { rEd }; |
| 27 |
enum class E2 : unsigned char { red }; |
| 28 |
|
| 29 |
template <class T> |
| 30 |
constexpr bool test() |
| 31 |
{ |
| 32 |
ASSERT_SAME_TYPE(decltype(std::bit_ceil(T())), T); |
| 33 |
LIBCPP_ASSERT_NOEXCEPT(std::bit_ceil(T())); |
| 34 |
|
| 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)); |
| 55 |
|
| 56 |
#ifndef TEST_HAS_NO_INT128 |
| 57 |
if constexpr (std::is_same_v<T, __uint128_t>) { |
| 58 |
T val = 168; |
| 59 |
T ceil = 256; |
| 60 |
|
| 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)); |
| 66 |
} |
| 67 |
#endif |
| 68 |
|
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
int main(int, char**) |
| 73 |
{ |
| 74 |
{ |
| 75 |
auto lambda = [](auto x) -> decltype(std::bit_ceil(x)) {}; |
| 76 |
using L = decltype(lambda); |
| 77 |
|
| 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>); |
| 85 |
#endif |
| 86 |
|
| 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>); |
| 94 |
|
| 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>); |
| 100 |
#endif |
| 101 |
static_assert(!std::is_invocable_v<L, char16_t>); |
| 102 |
static_assert(!std::is_invocable_v<L, char32_t>); |
| 103 |
|
| 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>); |
| 108 |
} |
| 109 |
|
| 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>()); |
| 117 |
#endif |
| 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>()); |
| 125 |
|
| 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 |
| 132 |
test<__uint128_t>(); |
| 133 |
#endif |
| 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>(); |
| 140 |
test<std::size_t>(); |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |